previewzoomoverlay

This commit is contained in:
2025-10-26 21:01:48 +01:00
parent 518b79bec6
commit 40e45f3a01
4 changed files with 521 additions and 498 deletions
+6 -381
View File
@@ -1,5 +1,4 @@
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import {
DownloadIcon,
EditIcon,
@@ -15,6 +14,7 @@ import { formatFileSize } from '../utils/format';
import { resolveDocumentAssetUrl, createAssetView } from '../asset_manager';
import { useAssetNavigator } from '../hooks/useAssetNavigator';
import { CORRESPONDENT_ROLES } from '../constants/correspondents';
import PreviewZoomOverlay from './PreviewZoomOverlay';
const MAX_PREVIEW_STACK_ITEMS = 15;
@@ -351,35 +351,6 @@ const DetailPanel = ({
const [ocrLoading, setOcrLoading] = useState(false);
const [ocrError, setOcrError] = useState(null);
const [zoomedPreview, setZoomedPreview] = useState(null);
const [zoomNative, setZoomNative] = useState(false);
const [zoomPan, setZoomPan] = useState({ x: 0, y: 0 });
const [isZoomDragging, setIsZoomDragging] = useState(false);
const zoomStageRef = useRef(null);
const zoomImageMetricsRef = useRef({ naturalWidth: 0, naturalHeight: 0 });
const zoomDragRef = useRef(null);
const zoomSkipClickRef = useRef(false);
const clampPan = useCallback(
(x, y) => {
const stage = zoomStageRef.current;
const { naturalWidth, naturalHeight } = zoomImageMetricsRef.current;
if (!stage || !naturalWidth || !naturalHeight) {
return { x: 0, y: 0 };
}
const stageRect = stage.getBoundingClientRect();
if (stageRect.width <= 0 || stageRect.height <= 0) {
return { x: 0, y: 0 };
}
const imageWidth = zoomNative ? naturalWidth : Math.min(naturalWidth, stageRect.width);
const imageHeight = zoomNative ? naturalHeight : Math.min(naturalHeight, stageRect.height);
const limitX = Math.max(0, (imageWidth - stageRect.width) / 2);
const limitY = Math.max(0, (imageHeight - stageRect.height) / 2);
const clampedX = Math.min(Math.max(x, -limitX), limitX);
const clampedY = Math.min(Math.max(y, -limitY), limitY);
return { x: clampedX, y: clampedY };
},
[zoomNative],
);
useEffect(() => {
if (!singleDoc) {
@@ -413,51 +384,6 @@ const DetailPanel = ({
setZoomedPreview(null);
}, [selectionKey]);
useEffect(() => {
if (!zoomedPreview) {
setZoomNative(false);
setZoomPan({ x: 0, y: 0 });
setIsZoomDragging(false);
zoomDragRef.current = null;
zoomSkipClickRef.current = false;
}
}, [zoomedPreview]);
useEffect(() => {
zoomSkipClickRef.current = false;
if (zoomNative) {
setZoomPan((current) => {
const clamped = clampPan(current.x, current.y);
if (clamped.x === current.x && clamped.y === current.y) {
return current;
}
return clamped;
});
} else {
setZoomPan({ x: 0, y: 0 });
setIsZoomDragging(false);
zoomDragRef.current = null;
}
}, [zoomNative, clampPan]);
useEffect(() => {
if (!zoomNative) {
return undefined;
}
const handleResize = () => {
setZoomPan((current) => {
const clamped = clampPan(current.x, current.y);
if (clamped.x === current.x && clamped.y === current.y) {
return current;
}
return clamped;
});
};
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
}, [zoomNative, clampPan]);
const startTitleEdit = useCallback(() => {
if (!singleDoc) return;
setTitleEditDocId(singleDoc.id);
@@ -826,22 +752,10 @@ const DetailPanel = ({
mode: config.mode,
docId: config.docId ?? null,
});
setZoomNative(false);
setZoomPan({ x: 0, y: 0 });
setIsZoomDragging(false);
zoomDragRef.current = null;
zoomSkipClickRef.current = false;
zoomImageMetricsRef.current = { naturalWidth: 0, naturalHeight: 0 };
}, []);
const closeZoomPreview = useCallback(() => {
setZoomedPreview(null);
setZoomNative(false);
setZoomPan({ x: 0, y: 0 });
setIsZoomDragging(false);
zoomDragRef.current = null;
zoomSkipClickRef.current = false;
zoomImageMetricsRef.current = { naturalWidth: 0, naturalHeight: 0 };
}, []);
const handleSingleZoom = useCallback(
@@ -863,160 +777,6 @@ const DetailPanel = ({
[openZoomPreview, stackTopDocId, topHasPreview],
);
const handleZoomImageLoad = useCallback(
(event) => {
zoomImageMetricsRef.current = {
naturalWidth: event.currentTarget.naturalWidth || 0,
naturalHeight: event.currentTarget.naturalHeight || 0,
};
setZoomPan((current) => {
const clamped = clampPan(current.x, current.y);
return { x: clamped.x, y: clamped.y };
});
},
[clampPan],
);
const handleZoomImageClick = useCallback(
(event) => {
event.stopPropagation();
if (zoomSkipClickRef.current) {
zoomSkipClickRef.current = false;
return;
}
if (!zoomNative) {
const stage = zoomStageRef.current;
const { naturalWidth, naturalHeight } = zoomImageMetricsRef.current;
if (stage && naturalWidth && naturalHeight) {
const stageRect = stage.getBoundingClientRect();
const imageRect = event.currentTarget.getBoundingClientRect();
const clickX = event.clientX - imageRect.left;
const clickY = event.clientY - imageRect.top;
const ratioX = imageRect.width ? clickX / imageRect.width : 0.5;
const ratioY = imageRect.height ? clickY / imageRect.height : 0.5;
const focusX = naturalWidth * ratioX;
const focusY = naturalHeight * ratioY;
const halfWidth = naturalWidth / 2;
const halfHeight = naturalHeight / 2;
const limitX = Math.max(0, (naturalWidth - stageRect.width) / 2);
const limitY = Math.max(0, (naturalHeight - stageRect.height) / 2);
let targetPanX = -(focusX - halfWidth);
let targetPanY = -(focusY - halfHeight);
targetPanX = Math.min(Math.max(targetPanX, -limitX), limitX);
targetPanY = Math.min(Math.max(targetPanY, -limitY), limitY);
setZoomPan({ x: targetPanX, y: targetPanY });
} else {
setZoomPan({ x: 0, y: 0 });
}
setZoomNative(true);
} else {
setZoomNative(false);
}
},
[zoomNative],
);
const handleZoomImagePointerDown = useCallback(
(event) => {
if (!zoomNative || event.button !== 0) {
return;
}
event.preventDefault();
zoomSkipClickRef.current = false;
zoomDragRef.current = {
pointerId: event.pointerId,
originX: zoomPan.x,
originY: zoomPan.y,
startX: event.clientX,
startY: event.clientY,
moved: false,
};
event.currentTarget.setPointerCapture(event.pointerId);
},
[zoomNative, zoomPan],
);
const handleZoomImagePointerMove = useCallback(
(event) => {
const drag = zoomDragRef.current;
if (!drag || drag.pointerId !== event.pointerId) {
return;
}
const deltaX = event.clientX - drag.startX;
const deltaY = event.clientY - drag.startY;
if (!drag.moved && (Math.abs(deltaX) > 2 || Math.abs(deltaY) > 2)) {
drag.moved = true;
setIsZoomDragging(true);
}
if (drag.moved) {
const clamped = clampPan(drag.originX + deltaX, drag.originY + deltaY);
setZoomPan((current) =>
current.x === clamped.x && current.y === clamped.y ? current : clamped,
);
}
},
[clampPan],
);
const endZoomDrag = useCallback(() => {
zoomDragRef.current = null;
setIsZoomDragging(false);
}, []);
const handleZoomImagePointerUp = useCallback(
(event) => {
const drag = zoomDragRef.current;
if (!drag || drag.pointerId !== event.pointerId) {
return;
}
event.currentTarget.releasePointerCapture(event.pointerId);
zoomSkipClickRef.current = Boolean(drag.moved);
endZoomDrag();
},
[endZoomDrag],
);
const handleZoomImagePointerCancel = useCallback(
(event) => {
const drag = zoomDragRef.current;
if (!drag || drag.pointerId !== event.pointerId) {
return;
}
event.currentTarget.releasePointerCapture(event.pointerId);
zoomSkipClickRef.current = true;
endZoomDrag();
},
[endZoomDrag],
);
const handleZoomStageWheel = useCallback(
(event) => {
if (!zoomNative) {
return;
}
const deltaX = Number.isFinite(event.deltaX) ? event.deltaX : 0;
const deltaY = Number.isFinite(event.deltaY) ? event.deltaY : 0;
if (!deltaX && !deltaY) {
return;
}
let updated = false;
setZoomPan((current) => {
const clamped = clampPan(current.x - deltaX, current.y - deltaY);
if (clamped.x === current.x && clamped.y === current.y) {
return current;
}
updated = true;
return clamped;
});
if (updated) {
event.preventDefault();
event.stopPropagation();
}
},
[zoomNative, clampPan],
);
const zoomDisplay = useMemo(() => {
if (!zoomedPreview) {
return null;
@@ -1089,50 +849,6 @@ const DetailPanel = ({
}
}, [zoomedPreview, zoomDisplay]);
const zoomDisplayUrl = zoomDisplay?.url;
useEffect(() => {
if (!zoomDisplayUrl) {
return;
}
setZoomPan((current) => {
const clamped = clampPan(current.x, current.y);
if (clamped.x === current.x && clamped.y === current.y) {
return current;
}
return clamped;
});
}, [zoomDisplayUrl, clampPan]);
useEffect(() => {
if (!zoomedPreview) {
return undefined;
}
const handleKeyDown = (event) => {
if (event.key === 'Escape') {
setZoomedPreview(null);
return;
}
if (event.key === 'ArrowLeft') {
event.preventDefault();
event.stopPropagation();
if (zoomDisplay?.canGoPrev) {
zoomDisplay.goPrev?.();
}
return;
}
if (event.key === 'ArrowRight') {
event.preventDefault();
event.stopPropagation();
if (zoomDisplay?.canGoNext) {
zoomDisplay.goNext?.();
}
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [zoomedPreview, zoomDisplay]);
useEffect(() => {
if (typeof ensureAssetUrl !== 'function') {
return;
@@ -1196,38 +912,6 @@ const DetailPanel = ({
stackPreviewNavigator.canGoNext,
]);
const zoomImageStyle = useMemo(() => {
if (!zoomNative) {
return {
cursor: 'zoom-in',
transform: 'none',
maxWidth: '95vw',
maxHeight: '95vh',
};
}
const { naturalWidth, naturalHeight } = zoomImageMetricsRef.current;
return {
cursor: isZoomDragging ? 'grabbing' : 'grab',
width: naturalWidth ? `${naturalWidth}px` : 'auto',
height: naturalHeight ? `${naturalHeight}px` : 'auto',
maxWidth: 'none',
maxHeight: 'none',
transform: `translate3d(${zoomPan.x}px, ${zoomPan.y}px, 0)`,
};
}, [zoomNative, zoomPan.x, zoomPan.y, isZoomDragging]);
const zoomStageClassName = useMemo(
() =>
[
'preview-zoom__stage',
zoomNative ? 'preview-zoom__stage--native' : '',
isZoomDragging ? 'preview-zoom__stage--dragging' : '',
]
.filter(Boolean)
.join(' '),
[zoomNative, isZoomDragging],
);
const renderSingle = () => {
if (!singleDoc) {
return <p className="meta">Select a document to view metadata, tags and actions.</p>;
@@ -1662,70 +1346,11 @@ const DetailPanel = ({
{selectedCount <= 1 ? renderSingle() : renderBulk()}
</div>
</aside>
{zoomDisplay?.url
? createPortal(
<div
className="preview-zoom-backdrop"
role="dialog"
aria-modal="true"
aria-label="Enlarged document preview"
onClick={closeZoomPreview}
>
<div
ref={zoomStageRef}
className={zoomStageClassName}
onWheel={handleZoomStageWheel}
>
<img
src={zoomDisplay.url}
alt={zoomDisplay.alt}
className="preview-zoom__image"
style={zoomImageStyle}
onLoad={handleZoomImageLoad}
onClick={handleZoomImageClick}
onPointerDown={handleZoomImagePointerDown}
onPointerMove={handleZoomImagePointerMove}
onPointerUp={handleZoomImagePointerUp}
onPointerCancel={handleZoomImagePointerCancel}
draggable={false}
/>
{zoomDisplay.canGoPrev || zoomDisplay.canGoNext ? (
<div className="preview-zoom__nav">
<button
type="button"
className="preview-zoom__nav-button"
onClick={(event) => {
event.stopPropagation();
if (zoomDisplay.canGoPrev) {
zoomDisplay.goPrev?.();
}
}}
aria-label="Previous preview"
disabled={!zoomDisplay.canGoPrev}
>
<ArrowLeftIcon />
</button>
<button
type="button"
className="preview-zoom__nav-button"
onClick={(event) => {
event.stopPropagation();
if (zoomDisplay.canGoNext) {
zoomDisplay.goNext?.();
}
}}
aria-label="Next preview"
disabled={!zoomDisplay.canGoNext}
>
<ArrowRightIcon />
</button>
</div>
) : null}
</div>
</div>,
document.body,
)
: null}
<PreviewZoomOverlay
open={Boolean(zoomDisplay?.url)}
display={zoomDisplay}
onClose={closeZoomPreview}
/>
</>
);
};