diff --git a/src/core/providers/canvas/canvas.model.ts b/src/core/providers/canvas/canvas.model.ts index b623b656..645736dc 100644 --- a/src/core/providers/canvas/canvas.model.ts +++ b/src/core/providers/canvas/canvas.model.ts @@ -101,6 +101,7 @@ export interface CanvasContextModel { setActivePage: (pageId: string) => void; deletePage: (pageIndex: number) => void; editPageTitle: (pageIndex: number, newName: string) => void; + activePageIndex: number; isThumbnailContextMenuVisible: boolean; setIsThumbnailContextMenuVisible: React.Dispatch< React.SetStateAction diff --git a/src/core/providers/canvas/canvas.provider.tsx b/src/core/providers/canvas/canvas.provider.tsx index 2d3ba3a4..fef9d809 100644 --- a/src/core/providers/canvas/canvas.provider.tsx +++ b/src/core/providers/canvas/canvas.provider.tsx @@ -307,6 +307,7 @@ export const CanvasProvider: React.FC = props => { setActivePage, deletePage, editPageTitle, + activePageIndex: document.activePageIndex, isThumbnailContextMenuVisible, setIsThumbnailContextMenuVisible, }} diff --git a/src/pods/thumb-pages/components/thumb-page.tsx b/src/pods/thumb-pages/components/thumb-page.tsx index eb853117..49496d52 100644 --- a/src/pods/thumb-pages/components/thumb-page.tsx +++ b/src/pods/thumb-pages/components/thumb-page.tsx @@ -13,13 +13,15 @@ import React from 'react'; interface Props { pageIndex: number; + isVisible: boolean; onSetActivePage: (pageId: string) => void; setPageTitleBeingEdited: (index: number) => void; } export const ThumbPage: React.FunctionComponent = props => { - const { pageIndex, onSetActivePage, setPageTitleBeingEdited } = props; - const { fullDocument } = useCanvasContext(); + const { pageIndex, onSetActivePage, setPageTitleBeingEdited, isVisible } = + props; + const { fullDocument, activePageIndex } = useCanvasContext(); const page = fullDocument.pages[pageIndex]; const shapes = page.shapes; const fakeShapeRefs = useRef({}); @@ -33,33 +35,41 @@ export const ThumbPage: React.FunctionComponent = props => { const divRef = useRef(null); const [key, setKey] = React.useState(0); - React.useEffect(() => { + const handleResizeAndForceRedraw = () => { const newCanvaSize = { width: divRef.current?.clientWidth || 1, height: divRef.current?.clientHeight || 1, }; - window.addEventListener('resize', () => { - setCanvasSize({ - width: divRef.current?.clientWidth || 1, - height: divRef.current?.clientHeight || 1, - }); - }); - setCanvasSize(newCanvaSize); setFinalScale(calculateScaleBasedOnBounds(shapes, newCanvaSize)); + setTimeout(() => { + setKey(key => key + 1); + }, 100); + }; + + React.useLayoutEffect(() => { + handleResizeAndForceRedraw(); + }, []); + + React.useEffect(() => { + if (!isVisible) return; + handleResizeAndForceRedraw(); + }, [isVisible]); + + React.useEffect(() => { + setTimeout(() => { + handleResizeAndForceRedraw(); + }, 100); + }, [shapes, activePageIndex]); - setKey(key => key + 1); + React.useEffect(() => { + window.addEventListener('resize', handleResizeAndForceRedraw); return () => { - window.removeEventListener('resize', () => { - setCanvasSize({ - width: divRef.current?.clientWidth || 1, - height: divRef.current?.clientHeight || 1, - }); - }); + window.removeEventListener('resize', handleResizeAndForceRedraw); }; - }, [divRef.current, shapes]); + }, [divRef.current]); const { showContextMenu, diff --git a/src/pods/thumb-pages/thumb-pages.pod.tsx b/src/pods/thumb-pages/thumb-pages.pod.tsx index 40c7cc5b..cf5a596f 100644 --- a/src/pods/thumb-pages/thumb-pages.pod.tsx +++ b/src/pods/thumb-pages/thumb-pages.pod.tsx @@ -4,7 +4,12 @@ import { useCanvasContext } from '@/core/providers'; import { PageTitleInlineEdit, ThumbPage } from './components'; import { PlusIcon } from '@/common/components/icons'; -export const ThumbPagesPod: React.FC = () => { +interface Props { + isVisible: boolean; +} + +export const ThumbPagesPod: React.FC = props => { + const { isVisible } = props; const { fullDocument, addNewPage, setActivePage, getActivePage } = useCanvasContext(); const [pageTitleBeingEdited, setPageTitleBeingEdited] = React.useState< @@ -34,6 +39,7 @@ export const ThumbPagesPod: React.FC = () => { pageIndex={index} onSetActivePage={handleSetActivePage} setPageTitleBeingEdited={setPageTitleBeingEdited} + isVisible={isVisible} /> {pageTitleBeingEdited === index ? ( { + const [isThumbPagesPodOpen, setIsThumbPagesPodOpen] = useState(false); + const thumbPagesPodRef = useRef(null); + + useEffect(() => { + const handleToggle = () => { + setIsThumbPagesPodOpen(thumbPagesPodRef.current?.open ?? false); + }; + + const detailsElement = thumbPagesPodRef.current; + if (detailsElement) { + detailsElement.addEventListener('toggle', handleToggle); + } + + // Cleanup event listener on component unmount + return () => { + if (detailsElement) { + detailsElement.removeEventListener('toggle', handleToggle); + } + }; + }, []); + + return { + thumbPagesPodRef, + isThumbPagesPodOpen, + }; +}; diff --git a/src/scenes/main.scene.tsx b/src/scenes/main.scene.tsx index 93d74453..c25929b3 100644 --- a/src/scenes/main.scene.tsx +++ b/src/scenes/main.scene.tsx @@ -13,15 +13,23 @@ import { import { PropertiesPod } from '@/pods/properties'; import { FooterPod } from '@/pods/footer/footer.pod'; import { ThumbPagesPod } from '@/pods/thumb-pages'; +import { useAccordionSectionVisibility } from './accordion-section-visibility.hook'; export const MainScene = () => { + const { isThumbPagesPodOpen, thumbPagesPodRef } = + useAccordionSectionVisibility(); + return (
-
+
Pages - +
Devices