diff --git a/frontend/src/core/components/layout/Workbench.tsx b/frontend/src/core/components/layout/Workbench.tsx index b0b8629cb9..d9d5e3b6cb 100644 --- a/frontend/src/core/components/layout/Workbench.tsx +++ b/frontend/src/core/components/layout/Workbench.tsx @@ -97,10 +97,71 @@ export default function Workbench() { } }; + const buildPageEditorFilter = useCallback((root: HTMLElement) => { + const rootRect = root.getBoundingClientRect(); + const margin = 100; + const isRectVisible = (rect: DOMRect) => + rect.bottom >= rootRect.top - margin && + rect.right >= rootRect.left - margin && + rect.top <= rootRect.bottom + margin && + rect.left <= rootRect.right + margin; + + const visiblePageIds = new Set(); + root.querySelectorAll('[data-page-id]').forEach((page) => { + const rect = page.getBoundingClientRect(); + if (isRectVisible(rect)) { + const id = page.getAttribute('data-page-id'); + if (id) { + visiblePageIds.add(id); + } + } + }); + + return (node: Node) => { + if (node === root) return true; + if (!(node instanceof Element)) return true; + + const pageElement = node.closest('[data-page-id]') as HTMLElement | null; + if (pageElement) { + const pageId = pageElement.getAttribute('data-page-id'); + const isVisible = pageId ? visiblePageIds.has(pageId) : true; + if (isVisible) { + return true; + } + return node === pageElement; + } + + return true; + }; + }, []); + // Capture screenshot helper for TopControls transitions const captureMainContentScreenshot = useCallback(async (): Promise => { - if (!mainContentRef.current) return null; - return captureElementScreenshot(mainContentRef.current); + const root = mainContentRef.current; + if (!root) return null; + + const filter = currentView === 'pageEditor' + ? buildPageEditorFilter(root) + : undefined; + + return captureElementScreenshot(root, { filter }); + }, [currentView, buildPageEditorFilter]); + + const capturePageEditorScreenshot = useCallback(async (): Promise => { + const root = mainContentRef.current; + if (!root) return null; + const rootRect = root.getBoundingClientRect(); + const filter = buildPageEditorFilter(root); + return captureElementScreenshot(root, { + filter, + width: Math.max(1, Math.round(rootRect.width)), + height: Math.max(1, Math.round(rootRect.height)), + }); + }, [buildPageEditorFilter]); + + const getMainContentRect = useCallback((): DOMRect | null => { + const root = mainContentRef.current; + return root ? root.getBoundingClientRect() : null; }, []); useEffect(() => { @@ -143,8 +204,9 @@ export default function Workbench() { // Get page editor transition handlers const { handleEntryTransition: handlePageEditorEntry, handleExitTransition: handlePageEditorExit } = usePageEditorTransition({ currentView, - captureScreenshot: captureMainContentScreenshot, + captureScreenshot: capturePageEditorScreenshot, activeFileId, + getScreenshotRect: getMainContentRect, }); // Wrapper for setCurrentView that adds transition when switching to/from viewer diff --git a/frontend/src/core/components/pageEditor/PageEditorSpreadTransition.tsx b/frontend/src/core/components/pageEditor/PageEditorSpreadTransition.tsx index c37c54f2cf..069e6af793 100644 --- a/frontend/src/core/components/pageEditor/PageEditorSpreadTransition.tsx +++ b/frontend/src/core/components/pageEditor/PageEditorSpreadTransition.tsx @@ -213,6 +213,7 @@ export const PageEditorSpreadTransition: React.FC = () => { const fileCardRects = pageEditorTransition?.fileCardRects ?? new Map(); const filePageCounts = pageEditorTransition?.filePageCounts ?? new Map(); const pageThumbnails = pageEditorTransition?.pageThumbnails ?? new Map(); + const screenshotRect = pageEditorTransition?.editorScreenshotRect ?? null; useEffect(() => { const currentUrl = pageEditorTransition?.editorScreenshotUrl ?? null; @@ -412,11 +413,11 @@ export const PageEditorSpreadTransition: React.FC = () => { {pageEditorTransition?.editorScreenshotUrl && (
; // Map file ID to first page thumbnail URL targetPageRects: Map | null; editorScreenshotUrl: string | null; + editorScreenshotRect: DOMRect | null; } // Navigation state diff --git a/frontend/src/core/hooks/usePageEditorTransition.ts b/frontend/src/core/hooks/usePageEditorTransition.ts index 149197dd3d..cf1f9b8abe 100644 --- a/frontend/src/core/hooks/usePageEditorTransition.ts +++ b/frontend/src/core/hooks/usePageEditorTransition.ts @@ -9,6 +9,7 @@ interface UsePageEditorTransitionParams { currentView: WorkbenchType; captureScreenshot: () => Promise; activeFileId?: FileId; + getScreenshotRect?: () => DOMRect | null; } interface PageEditorTransitionHandlers { @@ -24,6 +25,7 @@ export function usePageEditorTransition({ currentView, captureScreenshot, activeFileId, + getScreenshotRect, }: UsePageEditorTransitionParams): PageEditorTransitionHandlers { const { actions: navActions } = useNavigationActions(); const { selectors } = useFileState(); @@ -110,6 +112,7 @@ export function usePageEditorTransition({ // Optional: capture screenshot for static background let screenshot: string | null = null; + const screenshotRect = getScreenshotRect ? getScreenshotRect() : null; const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; if (!prefersReducedMotion) { @@ -141,9 +144,10 @@ export function usePageEditorTransition({ pageThumbnails, targetPageRects: null, // Will be set after PageEditor renders editorScreenshotUrl: screenshot, + editorScreenshotRect: screenshotRect, }); }, - [currentView, selectors, captureScreenshot, navActions, activeFileId, decodeImage] + [currentView, selectors, captureScreenshot, navActions, activeFileId, decodeImage, getScreenshotRect] ); /** @@ -197,6 +201,7 @@ export function usePageEditorTransition({ fileCardRects, // Will be filled after FileEditor renders filePageCounts, editorScreenshotUrl: null, + editorScreenshotRect: null, }); }, [currentView, navActions]); diff --git a/frontend/src/core/utils/screenshot.ts b/frontend/src/core/utils/screenshot.ts index 0afd370d34..03bc8bd00b 100644 --- a/frontend/src/core/utils/screenshot.ts +++ b/frontend/src/core/utils/screenshot.ts @@ -1,19 +1,37 @@ import { domToBlob } from 'modern-screenshot'; +interface ScreenshotOptions { + filter?: (node: Node) => boolean; + width?: number; + height?: number; + restoreScrollPosition?: boolean; +} + /** * Capture a screenshot of a DOM element as a data URL * * @param element - The DOM element to capture * @returns Promise resolving to an object URL of the screenshot, or null if capture fails */ -export async function captureElementScreenshot(element: HTMLElement): Promise { +export async function captureElementScreenshot( + element: HTMLElement, + options: ScreenshotOptions = {} +): Promise { try { + const { + filter, + width = window.innerWidth, + height = window.innerHeight, + restoreScrollPosition = true, + } = options; const blob = await domToBlob(element, { - width: window.innerWidth, - height: window.innerHeight, + width, + height, style: { transform: 'none', }, + filter, + features: { restoreScrollPosition }, }); return URL.createObjectURL(blob);