This commit is contained in:
2025-11-16 21:08:07 +01:00
parent 4b9f74d5a8
commit 1f2c62241c
6 changed files with 82 additions and 142 deletions
+17
View File
@@ -39,6 +39,7 @@ export interface DeskDocument {
id?: Identifier | null; id?: Identifier | null;
title?: string; title?: string;
tags?: TagLike[] | null; tags?: TagLike[] | null;
previewEntry?: OverlaySource | null;
[key: string]: unknown; [key: string]: unknown;
} }
@@ -172,6 +173,7 @@ interface DesktopWorkspaceViewProps {
closeOverlay: () => void; closeOverlay: () => void;
overlayOriginRect: DOMRect | null; overlayOriginRect: DOMRect | null;
overlayOriginTransform: OverlayOriginTransform | null; overlayOriginTransform: OverlayOriginTransform | null;
overlayDocument: DeskDocument | null;
onEntryPointer?: DesktopWorkspaceProps['onEntryPointer']; onEntryPointer?: DesktopWorkspaceProps['onEntryPointer'];
onDocumentStackSelect?: DesktopWorkspaceProps['onDocumentStackSelect']; onDocumentStackSelect?: DesktopWorkspaceProps['onDocumentStackSelect'];
onPromoteSelection?: DesktopWorkspaceProps['onPromoteSelection']; onPromoteSelection?: DesktopWorkspaceProps['onPromoteSelection'];
@@ -677,6 +679,17 @@ const DesktopWorkspace: React.FC<DesktopWorkspaceProps> = ({
}; };
}, [overlaySource]); }, [overlaySource]);
const overlayDocument = useMemo<DeskDocument | null>(() => {
if (!overlayDocId) {
return null;
}
const baseDoc = documentLookup.get(String(overlayDocId)) || null;
if (baseDoc && overlayDisplay?.url) {
return { ...baseDoc, previewEntry: overlayDisplay };
}
return baseDoc;
}, [documentLookup, overlayDisplay, overlayDocId]);
const openOverlayForDoc = useCallback( const openOverlayForDoc = useCallback(
(docId: Identifier | null, originInfo: OverlayOriginHint | null = null) => { (docId: Identifier | null, originInfo: OverlayOriginHint | null = null) => {
if (!docId) { if (!docId) {
@@ -782,6 +795,7 @@ const DesktopWorkspace: React.FC<DesktopWorkspaceProps> = ({
closeOverlay, closeOverlay,
overlayOriginRect, overlayOriginRect,
overlayOriginTransform, overlayOriginTransform,
overlayDocument,
onEntryPointer, onEntryPointer,
onDocumentStackSelect, onDocumentStackSelect,
onPromoteSelection, onPromoteSelection,
@@ -838,6 +852,7 @@ const DesktopWorkspace: React.FC<DesktopWorkspaceProps> = ({
overlayDisplay, overlayDisplay,
overlayOriginRect, overlayOriginRect,
overlayOriginTransform, overlayOriginTransform,
overlayDocument,
documentLookup, documentLookup,
pendingRemovalTag, pendingRemovalTag,
pendingTagDocId, pendingTagDocId,
@@ -887,6 +902,7 @@ function DesktopWorkspaceView({
closeOverlay, closeOverlay,
overlayOriginRect, overlayOriginRect,
overlayOriginTransform, overlayOriginTransform,
overlayDocument,
onEntryPointer, onEntryPointer,
onDocumentStackSelect, onDocumentStackSelect,
onPromoteSelection, onPromoteSelection,
@@ -1108,6 +1124,7 @@ function DesktopWorkspaceView({
open={Boolean(overlayDisplay?.url)} open={Boolean(overlayDisplay?.url)}
display={overlayDisplay} display={overlayDisplay}
onClose={closeOverlay} onClose={closeOverlay}
document={overlayDocument}
originRect={overlayOriginRect} originRect={overlayOriginRect}
originTransform={overlayOriginTransform} originTransform={overlayOriginTransform}
/> />
+45 -80
View File
@@ -1,31 +1,38 @@
import React, { CSSProperties, useEffect, useMemo, useRef, useState } from 'react'; import React, { CSSProperties, useEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom'; import { createPortal } from 'react-dom';
import { ArrowLeftIcon, ArrowRightIcon } from '../ui/icons';
import { clamp } from '../utils/math'; import { clamp } from '../utils/math';
import PdfViewer from '../preview/PdfViewer'; import PdfViewer from '../preview/PdfViewer';
type PreviewNavigator = { type DocumentLike = {
url: string; id?: string | number;
alt?: string; title?: string;
contentType?: string | null; content_type?: string | null;
pageNumber?: number | null; [key: string]: unknown;
canGoPrev?: boolean;
canGoNext?: boolean;
goPrev?: () => void;
goNext?: () => void;
}; };
interface PreviewZoomOverlayProps { interface PreviewZoomOverlayProps {
open?: boolean; open?: boolean;
display?: PreviewNavigator | null;
onClose?: () => void; onClose?: () => void;
document?: DocumentLike | null;
} }
type NaturalSize = { width: number | null; height: number | null }; type NaturalSize = { width: number | null; height: number | null };
type FocusPoint = { xRatio: number; yRatio: number } | null; type FocusPoint = { xRatio: number; yRatio: number } | null;
type DisplayKind = 'image' | 'pdf'; type DisplayKind = 'image' | 'pdf';
const determineDisplayKind = (entry?: PreviewNavigator | null): DisplayKind => { type PreviewEntry = {
url: string;
alt?: string;
contentType?: string | null;
canGoPrev?: boolean;
canGoNext?: boolean;
goPrev?: () => void;
goNext?: () => void;
};
type DocumentLikeWithPreview = DocumentLike & { previewEntry?: PreviewEntry };
const determineDisplayKind = (entry?: PreviewEntry | null): DisplayKind => {
const type = entry?.contentType?.toLowerCase?.() || ''; const type = entry?.contentType?.toLowerCase?.() || '';
if (type.includes('pdf')) { if (type.includes('pdf')) {
return 'pdf'; return 'pdf';
@@ -44,15 +51,15 @@ const noop = () => {};
const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
open = false, open = false,
display = null,
onClose = noop, onClose = noop,
document: overlayDocument = null,
}) => { }) => {
const portalTarget = document.body; const portalTarget = document.body;
const [isNativeScale, setIsNativeScale] = useState(false); const [isNativeScale, setIsNativeScale] = useState(false);
const [naturalSize, setNaturalSize] = useState<NaturalSize>({ width: null, height: null }); const [naturalSize, setNaturalSize] = useState<NaturalSize>({ width: null, height: null });
const [renderBackdrop, setRenderBackdrop] = useState(false); const [renderBackdrop, setRenderBackdrop] = useState(false);
const [isBackdropVisible, setBackdropVisible] = useState(false); const [isBackdropVisible, setBackdropVisible] = useState(false);
const [displaySnapshot, setDisplaySnapshot] = useState<PreviewNavigator | null>(null); const [documentSnapshot, setDocumentSnapshot] = useState<DocumentLikeWithPreview | null>(null);
const scrollRef = useRef<HTMLDivElement | null>(null); const scrollRef = useRef<HTMLDivElement | null>(null);
const mediaRef = useRef<HTMLImageElement | null>(null); const mediaRef = useRef<HTMLImageElement | null>(null);
const focusRef = useRef<FocusPoint>(null); const focusRef = useRef<FocusPoint>(null);
@@ -60,15 +67,20 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
const visibilityTimerRef = useRef<number | null>(null); const visibilityTimerRef = useRef<number | null>(null);
const displayTimerRef = useRef<number | null>(null); const displayTimerRef = useRef<number | null>(null);
useEffect(() => { const currentDocument = overlayDocument as DocumentLikeWithPreview | null;
if (display?.url) {
setDisplaySnapshot(display);
}
}, [display]);
const activeDisplay = open && display?.url ? display : displaySnapshot; useEffect(() => {
const displayKind = useMemo(() => determineDisplayKind(activeDisplay), [activeDisplay]); if (currentDocument?.previewEntry?.url) {
setDocumentSnapshot(currentDocument);
}
}, [currentDocument]);
const activeDocument = open && currentDocument?.previewEntry?.url ? currentDocument : documentSnapshot;
const previewEntry = activeDocument?.previewEntry || null;
const displayKind = useMemo(() => determineDisplayKind(previewEntry), [previewEntry]);
const isPdfDisplay = displayKind === 'pdf'; const isPdfDisplay = displayKind === 'pdf';
const documentTitle = activeDocument?.title || undefined;
const effectiveAlt = previewEntry?.alt || documentTitle || 'Document preview';
useEffect(() => { useEffect(() => {
if (visibilityTimerRef.current) { if (visibilityTimerRef.current) {
@@ -80,7 +92,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
displayTimerRef.current = null; displayTimerRef.current = null;
} }
if (open && display?.url) { if (open && previewEntry?.url) {
setRenderBackdrop(true); setRenderBackdrop(true);
displayTimerRef.current = requestAnimationFrame(() => { displayTimerRef.current = requestAnimationFrame(() => {
displayTimerRef.current = requestAnimationFrame(() => { displayTimerRef.current = requestAnimationFrame(() => {
@@ -106,7 +118,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
visibilityTimerRef.current = null; visibilityTimerRef.current = null;
} }
}; };
}, [open, display?.url]); }, [open, previewEntry?.url]);
useEffect(() => () => { useEffect(() => () => {
if (visibilityTimerRef.current) { if (visibilityTimerRef.current) {
@@ -118,7 +130,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
}, []); }, []);
useEffect(() => { useEffect(() => {
}, [isPdfDisplay, open, activeDisplay?.url]); }, [isPdfDisplay, open, previewEntry?.url]);
useEffect(() => { useEffect(() => {
setIsNativeScale(false); setIsNativeScale(false);
@@ -129,7 +141,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
scrollEl.scrollLeft = 0; scrollEl.scrollLeft = 0;
scrollEl.scrollTop = 0; scrollEl.scrollTop = 0;
} }
}, [open, activeDisplay?.url, displayKind]); }, [open, previewEntry?.url, displayKind]);
useEffect(() => { useEffect(() => {
@@ -172,7 +184,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
}, [open]); }, [open]);
useEffect(() => { useEffect(() => {
if (!activeDisplay?.url) { if (!previewEntry?.url) {
return; return;
} }
@@ -182,10 +194,10 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
scrollEl.scrollTop = 0; scrollEl.scrollTop = 0;
} }
focusRef.current = null; focusRef.current = null;
}, [activeDisplay?.url]); }, [previewEntry?.url]);
useEffect(() => { useEffect(() => {
if (!renderBackdrop || !activeDisplay?.url) { if (!renderBackdrop || !previewEntry?.url) {
return undefined; return undefined;
} }
@@ -194,7 +206,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
}); });
return () => cancelAnimationFrame(frame); return () => cancelAnimationFrame(frame);
}, [renderBackdrop, activeDisplay?.url]); }, [renderBackdrop, previewEntry?.url]);
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => { const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
event.stopPropagation(); event.stopPropagation();
@@ -229,20 +241,6 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
return; return;
} }
if (key === 'ArrowLeft') {
if (activeDisplay?.canGoPrev && activeDisplay?.goPrev) {
event.preventDefault();
activeDisplay.goPrev();
}
return;
}
if (key === 'ArrowRight') {
if (activeDisplay?.canGoNext && activeDisplay?.goNext) {
event.preventDefault();
activeDisplay.goNext();
}
}
}; };
const toggleZoomAtPoint = (clientX: number, clientY: number) => { const toggleZoomAtPoint = (clientX: number, clientY: number) => {
@@ -273,7 +271,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
toggleZoomAtPoint(event.clientX ?? 0, event.clientY ?? 0); toggleZoomAtPoint(event.clientX ?? 0, event.clientY ?? 0);
}; };
const shouldRender = renderBackdrop && Boolean(activeDisplay?.url); const shouldRender = renderBackdrop && Boolean(previewEntry?.url);
const stageClassName = isPdfDisplay const stageClassName = isPdfDisplay
? 'preview-zoom__stage preview-zoom__stage--pdf' ? 'preview-zoom__stage preview-zoom__stage--pdf'
@@ -314,8 +312,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
return null; return null;
} }
const effectiveDisplay = activeDisplay; const effectiveDisplay = previewEntry;
const navVisible = Boolean(effectiveDisplay?.canGoPrev || effectiveDisplay?.canGoNext);
return createPortal( return createPortal(
( (
@@ -344,7 +341,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
<div className="preview-zoom__pdf"> <div className="preview-zoom__pdf">
<PdfViewer <PdfViewer
src={effectiveDisplay.url} src={effectiveDisplay.url}
title={effectiveDisplay.alt || 'Document preview'} title={effectiveAlt}
className="preview-zoom__pdf-viewer" className="preview-zoom__pdf-viewer"
viewportRef={scrollRef} viewportRef={scrollRef}
/> />
@@ -352,7 +349,7 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
) : ( ) : (
<img <img
src={effectiveDisplay.url} src={effectiveDisplay.url}
alt={effectiveDisplay.alt || 'Document preview'} alt={effectiveAlt}
className="preview-zoom__image" className="preview-zoom__image"
ref={(node) => { ref={(node) => {
mediaRef.current = node; mediaRef.current = node;
@@ -369,38 +366,6 @@ const PreviewZoomOverlay: React.FC<PreviewZoomOverlayProps> = ({
/> />
)} )}
</div> </div>
{navVisible ? (
<div className="preview-zoom__nav">
<button
type="button"
className="preview-zoom__nav-button"
onClick={(event) => {
event.stopPropagation();
if (effectiveDisplay?.canGoPrev && effectiveDisplay?.goPrev) {
effectiveDisplay.goPrev();
}
}}
aria-label="Previous preview"
disabled={!effectiveDisplay?.canGoPrev}
>
<ArrowLeftIcon />
</button>
<button
type="button"
className="preview-zoom__nav-button"
onClick={(event) => {
event.stopPropagation();
if (effectiveDisplay?.canGoNext && effectiveDisplay?.goNext) {
effectiveDisplay.goNext();
}
}}
aria-label="Next preview"
disabled={!effectiveDisplay?.canGoNext}
>
<ArrowRightIcon />
</button>
</div>
) : null}
</div> </div>
</div> </div>
), ),
@@ -162,6 +162,11 @@ const DocumentsPanel: React.FC<DocumentsPanelProps> = ({
const [previewZoomSource, setPreviewZoomSource] = useState<ZoomSource | null>(null); const [previewZoomSource, setPreviewZoomSource] = useState<ZoomSource | null>(null);
const zoomDisplay = previewZoomSource; const zoomDisplay = previewZoomSource;
const overlayDocument = useMemo(() => (
previewDoc && zoomDisplay?.url
? { ...previewDoc, previewEntry: zoomDisplay }
: previewDoc
), [previewDoc, zoomDisplay]);
useEffect(() => { useEffect(() => {
let cancelled = false; let cancelled = false;
@@ -762,8 +767,8 @@ const DocumentsPanel: React.FC<DocumentsPanelProps> = ({
</section> </section>
<PreviewZoomOverlay <PreviewZoomOverlay
open={Boolean(zoomDisplay?.url)} open={Boolean(zoomDisplay?.url)}
display={zoomDisplay}
onClose={closePreviewOverlay} onClose={closePreviewOverlay}
document={overlayDocument}
/> />
</> </>
); );
+14 -5
View File
@@ -39,6 +39,11 @@ interface DocumentLike {
version_number?: number; version_number?: number;
version?: { content_type?: string | null } | null; version?: { content_type?: string | null } | null;
} | null; } | null;
previewEntry?: {
url: string;
alt?: string;
contentType?: string | null;
} | null;
[key: string]: unknown; [key: string]: unknown;
} }
@@ -53,10 +58,8 @@ interface DocumentViewerPanelProps extends DocumentSummarySectionProps {
document: DocumentLike | null; document: DocumentLike | null;
previewEntry?: { previewEntry?: {
url?: string; url?: string;
canGoPrev?: boolean; contentType?: string | null;
canGoNext?: boolean; filename?: string | null;
goPrev?: () => void;
goNext?: () => void;
} | null; } | null;
hydrateDocument?: (doc: DocumentLike | null) => DocumentLike | null; hydrateDocument?: (doc: DocumentLike | null) => DocumentLike | null;
ensureAssetUrl?: (docId: string | number, asset: AssetLike, options?: { start?: number; limit?: number }) => Promise<unknown>; ensureAssetUrl?: (docId: string | number, asset: AssetLike, options?: { start?: number; limit?: number }) => Promise<unknown>;
@@ -490,10 +493,16 @@ const DocumentViewerPanel: React.FC<DocumentViewerPanelProps> = ({
document?.title || 'Document preview' document?.title || 'Document preview'
); );
const overlayDocument = useMemo(() => (
document && zoomDisplay?.url
? { ...document, previewEntry: zoomDisplay }
: document
), [document, zoomDisplay]);
const overlay = ( const overlay = (
<PreviewZoomOverlay <PreviewZoomOverlay
open={Boolean(zoomOverlayOpen && zoomDisplay)} open={Boolean(zoomOverlayOpen && zoomDisplay)}
display={zoomDisplay} document={overlayDocument}
onClose={handleZoomClose} onClose={handleZoomClose}
/> />
); );
-2
View File
@@ -90,7 +90,6 @@ const PdfViewer = ({ src, title, className, viewportRef }: PdfViewerProps): JSX.
pointerOffsetX: number; pointerOffsetX: number;
pointerOffsetY: number; pointerOffsetY: number;
} | null>(null); } | null>(null);
const focusRatioRef = useRef<{ x: number; y: number } | null>(null);
const ensureWasmUrl = useCallback(() => { const ensureWasmUrl = useCallback(() => {
if (!wasmUrlRef.current) { if (!wasmUrlRef.current) {
wasmUrlRef.current = resolvePdfWasmBaseUrl(); wasmUrlRef.current = resolvePdfWasmBaseUrl();
@@ -221,7 +220,6 @@ const PdfViewer = ({ src, title, className, viewportRef }: PdfViewerProps): JSX.
try { try {
loadingTask = getDocument({ loadingTask = getDocument({
url: src, url: src,
withCredentials: true,
wasmUrl, wasmUrl,
}); });
const pdf = await loadingTask.promise; const pdf = await loadingTask.promise;
@@ -74,60 +74,6 @@
align-items: flex-start; align-items: flex-start;
} }
.preview-zoom__nav {
position: absolute;
bottom: 1em;
left: 50%;
transform: translateX(-50%);
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.9rem;
opacity: 0;
transition: opacity 0.18s ease;
pointer-events: none;
}
.preview-zoom__stage:hover .preview-zoom__nav,
.preview-zoom__stage:focus-within .preview-zoom__nav {
opacity: 1;
pointer-events: auto;
}
.preview-zoom__nav-button {
width: 2.8rem;
height: 2.8rem;
border: none;
border-radius: 999px;
background: var(--preview-nav-bg);
color: var(--preview-nav-fg);
display: inline-flex;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 1;
transition: background 0.15s ease, transform 0.15s ease;
box-shadow: 0 12px 28px var(--shadow-strong);
}
.preview-zoom__nav-button:hover {
background: var(--preview-nav-bg-hover);
}
.preview-zoom__nav-button:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 3px;
}
.preview-zoom__nav-button svg {
width: 1.5rem;
height: 1.5rem;
}
.preview-zoom__nav-button[disabled] {
opacity: 0.35;
cursor: default;
}
.preview-zoom__pdf { .preview-zoom__pdf {
width: 100%; width: 100%;