From acf93695d3a93add2e3b29313400fc9b3608f7fa Mon Sep 17 00:00:00 2001 From: Joshua Shoemaker Date: Tue, 13 Dec 2022 16:51:56 -0600 Subject: [PATCH] feat: render selected document --- core/Document/DocumentCollection.go | 13 ++++++ frontend/app/page.tsx | 5 ++- .../components/workspace/DocumentRenderer.tsx | 44 +++++++++++++++++++ frontend/components/workspace/Sidebar.tsx | 23 +++++++--- frontend/context/Project/provider.tsx | 6 +++ frontend/context/Project/types.ts | 3 ++ main.go | 37 +++++++++++++--- 7 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 frontend/components/workspace/DocumentRenderer.tsx diff --git a/core/Document/DocumentCollection.go b/core/Document/DocumentCollection.go index 58023f7..061894f 100644 --- a/core/Document/DocumentCollection.go +++ b/core/Document/DocumentCollection.go @@ -18,3 +18,16 @@ func GetDocumentCollection() *DocumentCollection { func (collection *DocumentCollection) AddDocument(document Entity) { collection.Documents = append(collection.Documents, document) } + +func (collection *DocumentCollection) GetDocumentById(id string) Entity { + var foundDocument Entity + + for _, d := range collection.Documents { + if d.Id == id { + foundDocument = d + break + } + } + + return foundDocument +} diff --git a/frontend/app/page.tsx b/frontend/app/page.tsx index c734811..18cf086 100644 --- a/frontend/app/page.tsx +++ b/frontend/app/page.tsx @@ -1,5 +1,6 @@ import 'server-only' import MainHead from '../components/MainHead' +import DocumentRenderer from '../components/workspace/DocumentRenderer' import Navigation from '../components/workspace/Navigation' export default function Home() { @@ -16,8 +17,8 @@ export default function Home() {
-
- {/* Add canvas */} +
+
diff --git a/frontend/components/workspace/DocumentRenderer.tsx b/frontend/components/workspace/DocumentRenderer.tsx new file mode 100644 index 0000000..6cb75e0 --- /dev/null +++ b/frontend/components/workspace/DocumentRenderer.tsx @@ -0,0 +1,44 @@ +'use client' + +import { useEffect, useRef } from "react" +import { useProject } from "../../context/Project/provider" + + +const loadImage = (path: string): Promise => { + return new Promise((resolve, reject) => { + const image = new Image() + image.src = path + image.onload = () => resolve(image) + image.onerror = (error) => reject(error) + }) +} + +const DocumentRenderer = () => { + const { getSelectedDocument } = useProject() + const canvasRef = useRef(null) + + const applyDocumentToCanvas = async (path: string) => { + const image = await loadImage(path) + + const canvas = canvasRef.current + if (!canvas) return + canvas.width = image.naturalWidth + canvas.height = image.naturalHeight + + const context = canvas.getContext('2d') + if (!context) return + context.drawImage(image, 10, 10, image.width, image.height) + } + + useEffect(() => { + const selectedDocument = getSelectedDocument() + const documentPath = selectedDocument?.path + if (documentPath) applyDocumentToCanvas(selectedDocument.path) + }, [getSelectedDocument]) + + return ( + + ) +} + +export default DocumentRenderer diff --git a/frontend/components/workspace/Sidebar.tsx b/frontend/components/workspace/Sidebar.tsx index ab0b041..a69af46 100644 --- a/frontend/components/workspace/Sidebar.tsx +++ b/frontend/components/workspace/Sidebar.tsx @@ -43,14 +43,20 @@ function classNames(...classes: any[]) { } function Sidebar() { - const [selectedItemId, setSelectedItemId] = useState('') const [selectedGroupId, setSelectedGroupId] = useState('') const [isAddNewDocumentInputShowing, setIsAddNewDocumentInputShowing] = useState(false) const [isAddNewGroupInputShowing, setIsAddNewGroupInputShowing] = useState(false) const addDocumentTextInput = useRef(null) const addGroupTextInput = useRef(null) - const { documents, groups, requestAddDocument, requestAddDocumentGroup } = useProject() + const { + documents, + groups, + requestAddDocument, + requestAddDocumentGroup, + selectedDocumentId, + setSelectedDocumentId + } = useProject() const navigation = getNavigationProps(documents, groups) @@ -75,7 +81,7 @@ function Sidebar() { } const onItemClickHandler = (itemId: string) => { - setSelectedItemId(itemId) + setSelectedDocumentId(itemId) setSelectedGroupId(getParentGroupIdFromItemId(itemId)) setIsAddNewDocumentInputShowing(false) setIsAddNewGroupInputShowing(false) @@ -96,12 +102,15 @@ function Sidebar() { const response = await requestAddDocument(groupId, documentName) if (!response.id) return - setSelectedItemId(response.id) + + setSelectedDocumentId(response.id) + + setSelectedGroupId(groupId) setIsAddNewDocumentInputShowing(false) } - const onConfirmAddGroupClickHandler = async(e: React.MouseEvent) => { + const onConfirmAddGroupClickHandler = async (e: React.MouseEvent) => { const groupName = addGroupTextInput.current?.value if (!groupName) return @@ -132,7 +141,7 @@ function Sidebar() { >