Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): show a shared image note in the pan and zoom viewer

Elian Doranc31fa9c54e28
diff --git a/packages/share-theme/src/content/note_view.tsx b/packages/share-theme/src/content/note_view.tsx
index 3e81330719..78cfb96e0c 100644
--- a/packages/share-theme/src/content/note_view.tsx
+++ b/packages/share-theme/src/content/note_view.tsx
@@ -11,20 +11,28 @@ import { drawMermaid, loadMermaid, readMermaidTheme } from "./mermaid.js";
import { ZoomViewer } from "./zoom_viewer.js";
/**
- * Mounts the note in place of the content the page rendered for visitors without scripts: a
- * Mermaid note as a diagram that pans and zooms, any other note with the app's own widget for its
- * type, read-only.
+ * Mounts the note in place of the content the page rendered for visitors without scripts: an image
+ * or a Mermaid note's diagram in a viewer that pans and zooms it, any other note with the app's own
+ * widget for its type, read-only.
*/
export default function mountNoteView(container: HTMLElement, payload: AppPayload) {
container.replaceChildren();
render(
<ShareAppHost noteId={container.dataset.noteId ?? ""} payload={payload}>
- {({ note }) => (note.type === "mermaid" ? <MermaidView note={note} /> : <NoteView note={note} />)}
+ {({ note }) => <SharedNoteView note={note} />}
</ShareAppHost>,
container
);
}
+function SharedNoteView({ note }: { note: FNote }) {
+ switch (note.type) {
+ case "image": return <ImageView note={note} />;
+ case "mermaid": return <MermaidView note={note} />;
+ default: return <NoteView note={note} />;
+ }
+}
+
function NoteView({ note }: { note: FNote }) {
const [ Widget, setWidget ] = useState<TypeWidget>();
const mapping = TYPE_MAPPINGS[note.type as keyof typeof TYPE_MAPPINGS];
@@ -50,6 +58,16 @@ function NoteView({ note }: { note: FNote }) {
);
}
+/** Shows an image note's image, which the share serves, in a viewer that takes the page. */
+function ImageView({ note }: { note: FNote }) {
+ const src = `api/images/${note.noteId}/${encodeURIComponent(note.title)}?${note.blobId}`;
+ return (
+ <ZoomViewer labels={getZoomPanLabels("image_viewer.viewport")} fillsPage>
+ <img src={src} alt={note.title} />
+ </ZoomViewer>
+ );
+}
+
/**
* Draws a Mermaid note the way the page draws the diagrams of a text note, in a viewer that takes
* the page, and draws it again when the theme changes.
@@ -75,7 +93,7 @@ function MermaidView({ note }: { note: FNote }) {
}, [ blob, theme ]);
return (
- <ZoomViewer labels={getZoomPanLabels()} fillsPage>
+ <ZoomViewer labels={getZoomPanLabels("svg.preview")} fillsPage>
{svg && <RawHtmlBlock className="mermaid" html={svg} />}
</ZoomViewer>
);
@@ -94,10 +112,10 @@ function useMermaidTheme() {
return theme;
}
-/** The texts of the viewer, from the app's catalogue. */
-function getZoomPanLabels() {
+/** The texts of a viewer, from the app's catalogue, with `labelKey` naming what it shows. */
+function getZoomPanLabels(labelKey: string) {
return {
- label: t("svg.preview"),
+ label: t(labelKey),
zoomIn: t("zoom_controls.zoom_in"),
zoomOut: t("zoom_controls.zoom_out"),
zoomReset: t("zoom_controls.reset")
diff --git a/packages/share-theme/src/content/zoom_viewer.css b/packages/share-theme/src/content/zoom_viewer.css
index 74ba68b81e..97fa416e6a 100644
--- a/packages/share-theme/src/content/zoom_viewer.css
+++ b/packages/share-theme/src/content/zoom_viewer.css
@@ -85,11 +85,20 @@
.zoom-viewer-viewport,
.zoom-viewer-wrapper,
.zoom-viewer-content,
- .zoom-viewer-content > * {
+ .zoom-viewer-content > :not(img) {
width: 100%;
height: 100%;
}
+ .zoom-viewer-content {
+ align-items: center;
+ }
+
+ .zoom-viewer-content > img {
+ max-width: 100%;
+ max-height: 100%;
+ }
+
.zoom-viewer-content svg {
width: 100%;
height: 100%;
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 948a1d0122..f1ad0db7fe 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1692,10 +1692,15 @@ describe("content_renderer pages", () => {
expect(auto.classList.contains("hljs")).toBe(false);
expect(render({ type: "code", mime: "text/x-markdown", content: " " }).isEmpty).toBe(true);
- for (const type of [ "image", "canvas", "mindMap" ]) {
+ for (const type of [ "canvas", "mindMap" ]) {
expect(render({ id: `${type}Note`, type, title: "A picture", content: "" }).content)
.toMatch(new RegExp(`^<img src="api/images/${type}Note/A%20picture\\?`));
}
+ const image = render({ id: "imageNote", type: "image", title: "A picture", content: "" });
+ expect(image.isAppView).toBe(true);
+ expect(parse(String(image.content))
+ .querySelector(".share-note-view[data-note-id=imageNote] > img")?.getAttribute("src"))
+ .toMatch(/^api\/images\/imageNote\/A%20picture\?/);
const file = (id: string, mime: string) =>
render({ id, type: "file", mime, content: "" }).content;
expect(file("pdfNote", "application/pdf"))
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index 48b669ff60..92da7ddf72 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -60,9 +60,9 @@ export interface Result {
/** Set to `true` if the provided content should be rendered as empty. */
isEmpty?: boolean;
/**
- * Set to `true` if the content is shown with the app's own view, such as a map or a Mermaid
- * note's editor, which takes the page without the title, the subpages, the date and the links
- * to the neighboring pages.
+ * Set to `true` if the content is shown with the app's own view, such as a map or the viewer of
+ * an image, which takes the page without the title, the subpages, the date and the links to the
+ * neighboring pages.
*/
isAppView?: boolean;
}
@@ -491,7 +491,10 @@ export function getContent(note: SNote | BNote, options: ShareRenderOptions = {}
} else if (note.type === "mermaid") {
renderMermaid(result, note);
hostNoteView(result, note);
- } else if (["image", "canvas", "mindMap"].includes(note.type)) {
+ } else if (note.type === "image") {
+ renderImage(result, note);
+ hostNoteView(result, note);
+ } else if (["canvas", "mindMap"].includes(note.type)) {
renderImage(result, note);
} else if (note.type === "file") {
renderFile(note, result);