Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix(web): square the attachment preview and restore arrow-key navigation

bluedbird93a25a2e10b1
diff --git a/web/src/components/PreviewImageDialog.tsx b/web/src/components/PreviewImageDialog.tsx
index 55636630e..565f305f4 100644
--- a/web/src/components/PreviewImageDialog.tsx
+++ b/web/src/components/PreviewImageDialog.tsx
@@ -105,26 +105,6 @@ function PreviewImageDialog({ open, onOpenChange, imgUrls = [], items, initialIn
const zoomPercent = Math.round(zoomScale * 100);
const isZoomed = zoomScale > MIN_ZOOM;
- useEffect(() => {
- const handleKeyDown = (event: KeyboardEvent) => {
- if (!open) {
- return;
- }
-
- if (event.key === "ArrowLeft") {
- setCurrentIndex((prev) => Math.max(prev - 1, 0));
- return;
- }
-
- if (event.key === "ArrowRight") {
- setCurrentIndex((prev) => Math.min(prev + 1, itemCount - 1));
- }
- };
-
- document.addEventListener("keydown", handleKeyDown);
- return () => document.removeEventListener("keydown", handleKeyDown);
- }, [itemCount, open]);
-
useEffect(() => {
const drag = panDragRef.current;
if (drag && imageRef.current?.hasPointerCapture(drag.pointerId)) {
@@ -162,6 +142,19 @@ function PreviewImageDialog({ open, onOpenChange, imgUrls = [], items, initialIn
const handleNext = () => {
setCurrentIndex((prev) => Math.min(prev + 1, itemCount - 1));
};
+ // The dialog popup stops arrow keys from reaching the document, so navigation listens on the popup itself.
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
+ if (event.altKey || event.ctrlKey || event.metaKey) {
+ return;
+ }
+ if (event.key === "ArrowLeft") {
+ event.preventDefault();
+ handlePrevious();
+ } else if (event.key === "ArrowRight") {
+ event.preventDefault();
+ handleNext();
+ }
+ };
const updateZoom = (nextScale: number) => {
setZoomScale(clampZoom(nextScale));
@@ -334,7 +327,8 @@ function PreviewImageDialog({ open, onOpenChange, imgUrls = [], items, initialIn
>
<DialogContent
showCloseButton={false}
- className="!h-dvh !w-[100vw] !max-h-dvh !max-w-[100vw] overflow-hidden border-0 bg-black/92 p-0 shadow-none"
+ className="!h-dvh !w-[100vw] !max-h-dvh !max-w-[100vw] overflow-hidden rounded-none border-0 bg-black/92 p-0 shadow-none"
+ onKeyDown={handleKeyDown}
>
<VisuallyHidden>
<DialogTitle>{currentItem.filename || "Attachment preview"}</DialogTitle>
diff --git a/web/tests/preview-image-dialog.test.tsx b/web/tests/preview-image-dialog.test.tsx
index b5606e13a..d32dcdda7 100644
--- a/web/tests/preview-image-dialog.test.tsx
+++ b/web/tests/preview-image-dialog.test.tsx
@@ -316,8 +316,8 @@ describe("<PreviewImageDialog>", () => {
fireEvent.pointerMove(image, { pointerId: 1, clientX: 200, clientY: 300 });
expect(image).toHaveStyle({ transform: "translate3d(-100px, 0px, 0) scale(2)" });
- fireEvent.keyDown(document, { key: "ArrowRight" });
- fireEvent.keyDown(document, { key: "ArrowLeft" });
+ fireEvent.keyDown(screen.getByRole("dialog"), { key: "ArrowRight" });
+ fireEvent.keyDown(screen.getByRole("dialog"), { key: "ArrowLeft" });
const returned = screen.getByAltText("Preview image 1 of 2");
fireEvent.doubleClick(returned);
@@ -573,6 +573,30 @@ describe("<PreviewImageDialog>", () => {
expect(screen.getByRole("button", { name: /next item/i })).toBeInTheDocument();
});
+ it("switches items with the arrow keys while focus is inside the dialog", () => {
+ render(
+ <PreviewImageDialog
+ open
+ onOpenChange={vi.fn()}
+ items={[
+ { id: "image-1", kind: "image", sourceUrl: "/image-1.jpg", posterUrl: "/image-1.jpg", filename: "image-1.jpg" },
+ { id: "image-2", kind: "image", sourceUrl: "/image-2.jpg", posterUrl: "/image-2.jpg", filename: "image-2.jpg" },
+ ]}
+ />,
+ );
+
+ // The dialog traps focus, so key presses start from a control inside it and bubble through the popup.
+ const closeButton = screen.getByRole("button", { name: /close preview/i });
+ fireEvent.keyDown(closeButton, { key: "ArrowRight" });
+ expect(screen.getByAltText("Preview image 2 of 2")).toBeInTheDocument();
+
+ fireEvent.keyDown(closeButton, { key: "ArrowRight" });
+ expect(screen.getByAltText("Preview image 2 of 2")).toBeInTheDocument();
+
+ fireEvent.keyDown(closeButton, { key: "ArrowLeft" });
+ expect(screen.getByAltText("Preview image 1 of 2")).toBeInTheDocument();
+ });
+
it("shows saved media details on demand with grouped, lean facts", () => {
const attachment = buildAttachmentWithPhotoMetadata();
render(