stability
This commit is contained in:
@@ -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<string>();
|
||||
root.querySelectorAll<HTMLElement>('[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<string | null> => {
|
||||
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<string | null> => {
|
||||
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
|
||||
|
||||
@@ -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 && (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight,
|
||||
position: 'fixed',
|
||||
top: `${screenshotRect?.top ?? 0}px`,
|
||||
left: `${screenshotRect?.left ?? 0}px`,
|
||||
width: `${screenshotRect?.width ?? window.innerWidth}px`,
|
||||
height: `${screenshotRect?.height ?? window.innerHeight}px`,
|
||||
opacity: isGliding ? 0 : 1,
|
||||
transition: 'opacity 200ms ease-out',
|
||||
pointerEvents: 'none',
|
||||
|
||||
@@ -33,6 +33,7 @@ export interface PageEditorTransitionState {
|
||||
pageThumbnails: Map<string, string>; // Map file ID to first page thumbnail URL
|
||||
targetPageRects: Map<string, DOMRect> | null;
|
||||
editorScreenshotUrl: string | null;
|
||||
editorScreenshotRect: DOMRect | null;
|
||||
}
|
||||
|
||||
// Navigation state
|
||||
|
||||
@@ -9,6 +9,7 @@ interface UsePageEditorTransitionParams {
|
||||
currentView: WorkbenchType;
|
||||
captureScreenshot: () => Promise<string | null>;
|
||||
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]);
|
||||
|
||||
|
||||
@@ -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<string | null> {
|
||||
export async function captureElementScreenshot(
|
||||
element: HTMLElement,
|
||||
options: ScreenshotOptions = {}
|
||||
): Promise<string | null> {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user