Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/image viewer: fix some issues
Adorian Doranfecb42048610
diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsxindex 2ef4426e52..698e4f003c 100644--- a/apps/client/src/widgets/react/ImageViewer.tsx+++ b/apps/client/src/widgets/react/ImageViewer.tsx@@ -1,11 +1,12 @@import "./ImageViewer.css";import { Ref } from "preact";-import { type ReactZoomPanPinchRef,TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";+import { type ReactZoomPanPinchRef, TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";interface ImageViewerProps {src: string;imgClassName?: string;+ /** Alt text for the image; callers should pass a descriptive value such as the note title. */alt?: string;minScale?: number;maxScale?: number;diff --git a/apps/client/src/widgets/type_widgets/Attachment.css b/apps/client/src/widgets/type_widgets/Attachment.cssindex 09bc5101ba..ff1f8391bc 100644--- a/apps/client/src/widgets/type_widgets/Attachment.css+++ b/apps/client/src/widgets/type_widgets/Attachment.css@@ -109,6 +109,12 @@object-fit: contain;}+/* The interactive viewer manages its own fit/zoom, so let its image use the full viewport. */+.attachment-detail-wrapper.full-detail .attachment-content-wrapper.attachment-image-viewer img {+ max-width: 100%;+ object-fit: unset;+}+.attachment-detail-wrapper.scheduled-for-deletion .attachment-content-wrapper img {filter: contrast(10%);}diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex 1a05bb179e..91a0780640 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -7,6 +7,7 @@ import { useContext, useEffect, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context";import FAttachment from "../../entities/fattachment";import FNote from "../../entities/fnote";+import imageContextMenu from "../../menus/image_context_menu";import content_renderer from "../../services/content_renderer";import dialog from "../../services/dialog";import froca from "../../services/froca";@@ -26,6 +27,7 @@ import { FormDropdownDivider, FormListItem } from "../react/FormList";import HelpButton from "../react/HelpButton";import { useTriliumEvent } from "../react/hooks";import Icon from "../react/Icon";+import ImageViewer from "../react/ImageViewer";import NoItems from "../react/NoItems";import NoteLink from "../react/NoteLink";import { ParentComponent, refToJQuerySelector } from "../react/react_utils";@@ -139,15 +141,23 @@ export function AttachmentDetail({ note, viewScope }: TypeWidgetProps) {function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment, isFullDetail?: boolean }) {const contentWrapper = useRef<HTMLDivElement>(null);+ const imageViewerWrapper = useRef<HTMLDivElement>(null);const [ title, setTitle ] = useState(attachment.title);const [ textContent, setTextContent ] = useState<string | null>(null);const supportsOcr = attachment.role === "image" || attachment.role === "file";+ // Image attachments opened in full detail get the interactive zoom/pan viewer; everything+ // else is rendered imperatively via the content renderer.+ const isZoomableImage = !!isFullDetail && attachment.role === "image";+ const imageSrc = `api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}?${attachment.utcDateModified}`;+function refresh() {- content_renderer.getRenderedContent(attachment)- .then(({ $renderedContent }) => {- contentWrapper.current?.replaceChildren(...$renderedContent);- });+ if (!isZoomableImage) {+ content_renderer.getRenderedContent(attachment)+ .then(({ $renderedContent }) => {+ contentWrapper.current?.replaceChildren(...$renderedContent);+ });+ }if (attachment.role === "file") {attachment.getBlob().then(blob => setTextContent(blob?.content ?? null));@@ -156,17 +166,24 @@ function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment,setTitle(attachment.title);}- useEffect(refresh, [ attachment ]);+ useEffect(refresh, [ attachment, isZoomableImage ]);useTriliumEvent("entitiesReloaded", ({ loadResults }) => {if (loadResults.getAttachmentRows().find(attachment => attachment.attachmentId)) {refresh();}});+ // Electron right-click menu (copy image / reference) for the interactive image viewer.+ useEffect(() => {+ if (isZoomableImage) {+ return imageContextMenu.setupContextMenu(refToJQuerySelector(imageViewerWrapper));+ }+ }, [ isZoomableImage ]);+async function copyAttachmentLinkToClipboard() {if (attachment.role === "image") {- const $contentWrapper = refToJQuerySelector(contentWrapper);- image.copyImageReferenceToClipboard($contentWrapper);+ const $imageWrapper = refToJQuerySelector(isZoomableImage ? imageViewerWrapper : contentWrapper);+ image.copyImageReferenceToClipboard($imageWrapper.find("img").parent());} else if (attachment.role === "file") {const $link = await link.createLink(attachment.ownerId, {referenceLink: true,@@ -220,7 +237,13 @@ function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment,{attachment.utcDateScheduledForErasureSince && <DeletionAlert utcDateScheduledForErasureSince={attachment.utcDateScheduledForErasureSince} />}{textContent && <TextPreview content={textContent} />}- <div ref={contentWrapper} className="attachment-content-wrapper" />+ {isZoomableImage ? (+ <div key="image-viewer" ref={imageViewerWrapper} className="attachment-content-wrapper attachment-image-viewer">+ <ImageViewer key={attachment.utcDateModified} src={imageSrc} alt={attachment.title} />+ </div>+ ) : (+ <div key="rendered" ref={contentWrapper} className="attachment-content-wrapper" />+ )}</div></div>diff --git a/apps/client/src/widgets/type_widgets/Image.css b/apps/client/src/widgets/type_widgets/Image.cssindex 97c7940dc5..2c07f0476f 100644--- a/apps/client/src/widgets/type_widgets/Image.css+++ b/apps/client/src/widgets/type_widgets/Image.css@@ -11,10 +11,3 @@overflow: hidden;height: 100%;}--/* Off-screen host used only to copy a clean image reference to the clipboard. */-.image-copy-reference-source {- position: absolute;- pointer-events: none;- opacity: 0;-}diff --git a/apps/client/src/widgets/type_widgets/Image.tsx b/apps/client/src/widgets/type_widgets/Image.tsxindex 8ae6fab030..04b0482e61 100644--- a/apps/client/src/widgets/type_widgets/Image.tsx+++ b/apps/client/src/widgets/type_widgets/Image.tsx@@ -14,21 +14,16 @@ import { TypeWidgetProps } from "./type_widget";export default function Image({ note, ntxId }: TypeWidgetProps) {const containerRef = useRef<HTMLDivElement>(null);- const copyRef = useRef<HTMLDivElement>(null);const zoomRef = useRef<ReactZoomPanPinchRef>(null);const [ refreshCounter, setRefreshCounter ] = useState(0);useEffect(() => image_context_menu.setupContextMenu(refToJQuerySelector(containerRef)), []);- // The ribbon's "copy reference" button triggers this. Copy a plain <img> rather than the- // zoom wrapper so the clipboard gets clean markup without the transform containers.+ // The ribbon's "copy reference" button triggers this. Select the rendered image's wrapper so+ // the clipboard gets clean <img> markup without the surrounding zoom/transform containers.useTriliumEvent("copyImageReferenceToClipboard", ({ ntxId: eventNtxId }) => {- if (eventNtxId !== ntxId || !copyRef.current) return;- const img = document.createElement("img");- img.src = createImageSrcUrl(note);- copyRef.current.replaceChildren(img);- copyImageReferenceToClipboard(refToJQuerySelector(copyRef));- copyRef.current.replaceChildren();+ if (eventNtxId !== ntxId) return;+ copyImageReferenceToClipboard(refToJQuerySelector(containerRef).find("img").parent());});useTriliumEvents([ "imageZoomIn", "imageZoomOut", "imageZoomReset" ], ({ ntxId: eventNtxId }, eventName) =>@@ -49,8 +44,8 @@ export default function Image({ note, ntxId }: TypeWidgetProps) {apiRef={zoomRef}imgClassName="note-detail-image-view"src={createImageSrcUrl(note)}+ alt={note.title}/>- <div ref={copyRef} className="image-copy-reference-source" /></div>);}