Skip to content

Build your personal knowledge base with Trilium Notes

refactor(share): draw the zoom viewer's controls with the app's ZoomControls

Elian Dorandfe7bac7ff72
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
index a5873ce02e..c1d8f202cb 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
@@ -140,16 +140,15 @@ class="image">
<td>
<ul>
<li>The diagram is drawn in the page's light or dark mode and redrawn when
- the visitor switches it. Until it is drawn, and in browsers with JavaScript
- disabled, the image saved when the note was last edited is shown instead.</li>
- <li>The source of the diagram can be expanded below it.</li>
- <li>The diagram can be zoomed with the <span class="tn-icon bx bx-minus-circle"></span> and <span
- class="tn-icon bx bx-plus-circle"></span> buttons in its bottom corner,
- between 50% and 1000% of its width on the page. Clicking the percentage
- between them, or double-clicking the diagram, shows it whole again. Once
- zoomed in, the diagram can be panned by dragging it, and on touch screens
- it can be zoomed by pinching. After clicking the diagram, the mouse wheel
- zooms it instead of scrolling the page.</li>
+ the visitor switches it. In browsers with JavaScript disabled, the image
+ saved when the note was last edited is shown instead, and the source of
+ the diagram can be expanded below it.</li>
+ <li>The diagram fills the page and can be zoomed with the <span class="tn-icon bx bx-minus-circle"></span> and <span
+ class="tn-icon bx bx-plus-circle"></span> buttons in its bottom corner
+ or with the mouse wheel, between 50% and 1000% of its fitted size, and
+ on touch screens by pinching. Clicking the percentage between the buttons,
+ or double-clicking the diagram, shows it whole again. The diagram can be
+ panned by dragging it.</li>
</ul>
</td>
<td>&nbsp;</td>
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json
index 89772ccf2a..1908181376 100644
--- a/apps/server/src/assets/translations/en/server.json
+++ b/apps/server/src/assets/translations/en/server.json
@@ -470,11 +470,7 @@
"heading-link": "Link to This Section",
"previous": "Previous",
"next": "Next",
- "page-navigation": "Previous and next pages",
- "diagram": "Diagram",
- "zoom-in": "Zoom in",
- "zoom-out": "Zoom out",
- "zoom-reset": "Reset zoom"
+ "page-navigation": "Previous and next pages"
},
"hidden_subtree_templates": {
"text-snippet": "Text Snippet",
diff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing.md b/docs/User Guide/User Guide/Advanced Usage/Sharing.md
index 0f6ab0ef36..15cdfae797 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Sharing.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing.md
@@ -69,7 +69,7 @@ Trilium allows you to share selected notes as **publicly accessible** read-only
</tr>
<tr>
<th><a class="reference-link" href="../Note%20Types/Mermaid%20Diagrams.md">Mermaid Diagrams</a></th>
- <td><ul><li>The diagram is drawn in the page's light or dark mode and redrawn when the visitor switches it. Until it is drawn, and in browsers with JavaScript disabled, the image saved when the note was last edited is shown instead.</li><li>The source of the diagram can be expanded below it.</li><li>The diagram can be zoomed with the <span class="tn-icon bx bx-minus-circle"></span> and <span class="tn-icon bx bx-plus-circle"></span> buttons in its bottom corner, between 50% and 1000% of its width on the page. Clicking the percentage between them, or double-clicking the diagram, shows it whole again. Once zoomed in, the diagram can be panned by dragging it, and on touch screens it can be zoomed by pinching. After clicking the diagram, the mouse wheel zooms it instead of scrolling the page.</li></ul></td>
+ <td><ul><li>The diagram is drawn in the page's light or dark mode and redrawn when the visitor switches it. In browsers with JavaScript disabled, the image saved when the note was last edited is shown instead, and the source of the diagram can be expanded below it.</li><li>The diagram fills the page and can be zoomed with the <span class="tn-icon bx bx-minus-circle"></span> and <span class="tn-icon bx bx-plus-circle"></span> buttons in its bottom corner or with the mouse wheel, between 50% and 1000% of its fitted size, and on touch screens by pinching. Clicking the percentage between the buttons, or double-clicking the diagram, shows it whole again. The diagram can be panned by dragging it.</li></ul></td>
<td>&nbsp;</td>
</tr>
<tr>
diff --git a/packages/share-theme/src/content/mermaid.spec.ts b/packages/share-theme/src/content/mermaid.spec.ts
index 68509272d3..68fabc130a 100644
--- a/packages/share-theme/src/content/mermaid.spec.ts
+++ b/packages/share-theme/src/content/mermaid.spec.ts
@@ -18,12 +18,8 @@ vi.mock("mermaid", () => ({
}
}));
-const mountZoomPan = vi.hoisted(() => vi.fn());
-vi.mock("./zoom_viewer.js", () => ({ default: mountZoomPan }));
-
describe("setupMermaid", () => {
afterEach(() => {
- mountZoomPan.mockReset();
document.documentElement.removeAttribute("style");
document.documentElement.removeAttribute("class");
document.body.innerHTML = "";
@@ -56,10 +52,9 @@ describe("setupMermaid", () => {
expect(document.querySelector("#content > .mermaid")).toBe(diagram);
expect(diagram?.querySelectorAll("svg")).toHaveLength(1);
expect(diagram?.querySelector("svg")?.id).not.toBe(firstId);
- expect(mountZoomPan).not.toHaveBeenCalled();
});
- it("draws a Mermaid note in place of its image, in a viewer, keeping the source", async () => {
+ it("draws a Mermaid note in place of its image, keeping the source", async () => {
const fakeMermaid = stubMermaid();
document.body.innerHTML = MERMAID_NOTE;
@@ -70,27 +65,6 @@ describe("setupMermaid", () => {
expect(container?.querySelectorAll(":scope > .mermaid > svg")).toHaveLength(1);
expect(container?.querySelector("details pre.mermaid-note-source")).not.toBeNull();
expect(fakeMermaid.render).toHaveBeenLastCalledWith(expect.any(String), "graph TD; A-->B");
- const diagram = container?.querySelector(":scope > .mermaid");
- expect(mountZoomPan).toHaveBeenCalledExactlyOnceWith(diagram, {
- label: "Diagram", zoomIn: "Zoom in", zoomOut: "Zoom out", zoomReset: "Reset zoom"
- });
- });
-
- it("keeps a Mermaid note's diagram as drawn when its viewer cannot be loaded", async () => {
- stubMermaid();
- const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
- mountZoomPan.mockImplementationOnce(() => {
- throw new Error("Failed to fetch the viewer");
- });
- document.body.innerHTML = MERMAID_NOTE;
-
- await setupMermaid();
-
- expect(document.querySelectorAll("#content > .mermaid-note > .mermaid > svg"))
- .toHaveLength(1);
- const error = expect.objectContaining({ message: "Failed to fetch the viewer" });
- expect(consoleError).toHaveBeenCalledWith(error);
- consoleError.mockRestore();
});
it("keeps what a diagram replaces until Mermaid draws it, and draws every diagram again on a theme change", async () => {
@@ -153,8 +127,7 @@ describe("setupMermaid without diagrams", () => {
});
});
-const MERMAID_NOTE = `<div id="content"><div class="mermaid-note" data-label="Diagram"`
- + ` data-zoom-in="Zoom in" data-zoom-out="Zoom out" data-zoom-reset="Reset zoom">`
+const MERMAID_NOTE = `<div id="content"><div class="mermaid-note">`
+ `<img class="mermaid-note-image" src="api/images/abc/diagram">`
+ `<hr><details><summary>Chart source</summary>`
+ `<pre class="mermaid-note-source">graph TD; A--&gt;B</pre></details>`
diff --git a/packages/share-theme/src/content/mermaid.ts b/packages/share-theme/src/content/mermaid.ts
index 4e01e07262..abb0c8fa18 100644
--- a/packages/share-theme/src/content/mermaid.ts
+++ b/packages/share-theme/src/content/mermaid.ts
@@ -1,12 +1,9 @@
import { getMermaidConfig, type MermaidTheme, parseMermaidTheme } from "@triliumnext/commons/src/lib/mermaid_config.js";
import type { Mermaid } from "mermaid";
-import type { ZoomPanLabels } from "./zoom_viewer.js";
-
/**
* Draws the Mermaid diagrams on the page: code blocks in a text note, and Mermaid notes. A code
- * block or a note's saved image stays in place until Mermaid has drawn its diagram, and a Mermaid
- * note's diagram then goes into a viewer that pans and zooms it.
+ * block or a note's saved image stays in place until Mermaid has drawn its diagram.
*/
export default async function setupMermaid() {
const diagrams = findDiagrams();
@@ -39,8 +36,6 @@ interface Diagram {
/** The element the drawings go into. */
element: HTMLElement;
source: string;
- /** The texts of the viewer a Mermaid note's diagram goes into. A code block has none. */
- labels?: ZoomPanLabels;
}
/** The diagrams on the page, each with the element its drawing replaces. */
@@ -58,8 +53,7 @@ function findDiagrams() {
const image = note.querySelector(":scope > .mermaid-note-image");
const source = note.querySelector(".mermaid-note-source");
if (image && source) {
- const labels = readLabels(note);
- placeholders.push({ placeholder: image, source: source.textContent, labels });
+ placeholders.push({ placeholder: image, source: source.textContent });
}
}
@@ -70,12 +64,6 @@ function findDiagrams() {
});
}
-/** The texts `content_renderer.ts` gives a Mermaid note's viewer in its `data-*` attributes. */
-function readLabels(note: HTMLElement): ZoomPanLabels {
- const { label = "", zoomIn = "", zoomOut = "", zoomReset = "" } = note.dataset;
- return { label, zoomIn, zoomOut, zoomReset };
-}
-
let renderCount = 0;
/**
@@ -83,7 +71,7 @@ let renderCount = 0;
* for it before: its placeholder, or its drawing in the previous theme.
*/
async function renderDiagrams(mermaid: Mermaid, diagrams: Diagram[], theme: MermaidTheme) {
- for (const { placeholder, element, source, labels } of diagrams) {
+ for (const { placeholder, element, source } of diagrams) {
try {
element.innerHTML = await drawMermaid(mermaid, source, theme);
} catch (error) {
@@ -93,9 +81,6 @@ async function renderDiagrams(mermaid: Mermaid, diagrams: Diagram[], theme: Merm
if (placeholder.isConnected) {
placeholder.replaceWith(element);
- if (labels) {
- await addZoomPan(element, labels);
- }
}
}
}
@@ -113,19 +98,6 @@ export async function drawMermaid(mermaid: Mermaid, source: string, theme: Merma
return svg;
}
-/**
- * Puts a drawn diagram in a viewer that pans and zooms it. Loaded on demand, so that only a page
- * with a Mermaid note loads it. A diagram whose viewer fails to load stays as it is.
- */
-async function addZoomPan(element: HTMLElement, labels: ZoomPanLabels) {
- try {
- const { default: mountZoomPan } = await import("./zoom_viewer.js");
- mountZoomPan(element, labels);
- } catch (error) {
- console.error(error);
- }
-}
-
/** The Mermaid theme of the page's current theme, which the theme switch changes. */
export function readMermaidTheme() {
return parseMermaidTheme(getComputedStyle(document.documentElement).getPropertyValue("--mermaid-theme"));
diff --git a/packages/share-theme/src/content/note_view.tsx b/packages/share-theme/src/content/note_view.tsx
index 09c05749ec..9dbafb8c9e 100644
--- a/packages/share-theme/src/content/note_view.tsx
+++ b/packages/share-theme/src/content/note_view.tsx
@@ -88,7 +88,7 @@ async function loadView(type: string | undefined): Promise<SharedView> {
function ImageView({ app: { note } }: { app: HostedApp }) {
const src = getNoteImageUrl(note.noteId, note.title, note.blobId);
return (
- <ZoomViewer labels={getZoomPanLabels("image_viewer.viewport")} fillsPage>
+ <ZoomViewer label={t("image_viewer.viewport")}>
<img src={src} alt={note.title} />
</ZoomViewer>
);
@@ -119,7 +119,7 @@ function MermaidView({ app: { note } }: { app: HostedApp }) {
}, [ blob, theme ]);
return (
- <ZoomViewer labels={getZoomPanLabels("svg.preview")} fillsPage>
+ <ZoomViewer label={t("svg.preview")}>
{svg && <RawHtmlBlock className="mermaid" html={svg} />}
</ZoomViewer>
);
@@ -176,13 +176,3 @@ function useMermaidTheme() {
return theme;
}
-
-/** The texts of a viewer, from the app's catalogue, with `labelKey` naming what it shows. */
-function getZoomPanLabels(labelKey: string) {
- return {
- 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 97fa416e6a..ecec7b50f6 100644
--- a/packages/share-theme/src/content/zoom_viewer.css
+++ b/packages/share-theme/src/content/zoom_viewer.css
@@ -1,85 +1,18 @@
.zoom-viewer {
position: relative;
-}
-
-.zoom-viewer-viewport {
- border-radius: 6px;
- outline: 2px solid transparent;
- outline-offset: 2px;
-}
-
-.zoom-viewer-viewport:focus-visible {
- outline-color: var(--link-focus-outline-color);
-}
-
-.zoom-viewer .zoom-viewer-wrapper,
-.zoom-viewer .zoom-viewer-content {
- width: 100%;
-}
-
-.zoom-viewer .zoom-viewer-content {
- justify-content: center;
-}
-
-.zoom-viewer .zoom-viewer-content > div {
- width: 100%;
-}
-
-.zoom-viewer-controls {
- position: absolute;
- bottom: 8px;
- inset-inline-end: 8px;
- display: flex;
- gap: 2px;
- padding: 2px;
- border-radius: 8px;
- background: var(--background-highlight);
-}
-
-.zoom-viewer-controls button {
- display: flex;
- align-items: center;
- justify-content: center;
- min-width: 30px;
- height: 26px;
- padding: 0 4px;
- border: 0;
- border-radius: 6px;
- background: transparent;
- color: var(--text-menu);
- font-size: 18px;
- cursor: pointer;
- transition: background-color 150ms ease, color 150ms ease;
-}
-
-.zoom-viewer-controls button:hover:not(:disabled) {
- background: var(--background-primary);
- color: var(--text-primary);
-}
-
-.zoom-viewer-controls button:focus-visible {
- outline: 2px solid var(--link-focus-outline-color);
- outline-offset: 1px;
-}
-
-.zoom-viewer-controls button:disabled {
- opacity: 0.4;
- cursor: default;
-}
-
-.zoom-viewer-controls .zoom-viewer-reset {
- min-width: 48px;
- font-size: 12px;
- font-variant-numeric: tabular-nums;
-}
-
-.zoom-viewer.fills-page {
display: flex;
flex-direction: column;
.zoom-viewer-viewport {
flex: 1;
min-height: 0;
+ border-radius: 6px;
+ outline: 2px solid transparent;
+ outline-offset: 2px;
+
+ &:focus-visible {
+ outline-color: var(--link-focus-outline-color);
+ }
}
.zoom-viewer-viewport,
@@ -91,6 +24,7 @@
}
.zoom-viewer-content {
+ justify-content: center;
align-items: center;
}
@@ -104,4 +38,8 @@
height: 100%;
max-width: 100% !important;
}
+
+ .zoom-viewer-controls {
+ z-index: 10;
+ }
}
diff --git a/packages/share-theme/src/content/zoom_viewer.spec.tsx b/packages/share-theme/src/content/zoom_viewer.spec.tsx
index e2519be9b3..8c5ea3db4e 100644
--- a/packages/share-theme/src/content/zoom_viewer.spec.tsx
+++ b/packages/share-theme/src/content/zoom_viewer.spec.tsx
@@ -3,7 +3,12 @@ import { type ComponentChildren, render } from "preact";
import { act } from "preact/test-utils";
import { afterEach, describe, expect, it, vi } from "vitest";
-import mountZoomPan, { ZoomViewer } from "./zoom_viewer.js";
+import { ZoomViewer } from "./zoom_viewer.js";
+
+// The real modules reach `window.glob`, which only the app's boot script defines, and the
+// Bootstrap tooltip needs real layout.
+vi.mock("@triliumnext/client/src/widgets/react/hooks.js", () => ({ useStaticTooltip: () => {} }));
+vi.mock("@triliumnext/client/src/services/i18n.js", () => ({ t: (key: string) => key }));
// react-zoom-pan-pinch measures its boxes, which happy-dom cannot do. As in the client's
// `SvgSplitEditor.spec.tsx`, this fake keeps what the controls drive: `zoomIn`/`zoomOut` add their
@@ -52,9 +57,9 @@ vi.mock("react-zoom-pan-pinch", async () => {
};
});
-describe("mountZoomPan", () => {
+describe("ZoomViewer", () => {
afterEach(() => {
- const host = document.querySelector(".zoom-viewer")?.parentElement;
+ const host = document.getElementById("content");
if (host) {
act(() => render(null, host));
}
@@ -62,18 +67,19 @@ describe("mountZoomPan", () => {
transformWrapperSpy.mockClear();
});
- it("moves the diagram into a labelled viewer in its place", () => {
- const diagram = mountDiagram();
+ it("shows its content in a labelled viewer with the app's zoom controls", () => {
+ const diagram = mountViewer();
- expect(document.querySelector("#content > div > .zoom-viewer:not(.fills-page)")).not.toBeNull();
+ expect(document.querySelector("#content > .zoom-viewer")).not.toBeNull();
const viewport = document.querySelector(".zoom-viewer > .zoom-viewer-viewport");
expect(viewport?.getAttribute("aria-label")).toBe("Diagram");
expect(viewport?.getAttribute("tabindex")).toBe("0");
expect(viewport?.querySelector(".zoom-viewer-content")?.contains(diagram)).toBe(true);
- expect(buttons().map((button) => button.title))
- .toEqual([ "Zoom out", "Reset zoom", "Zoom in" ]);
+ const group = document.querySelector(".zoom-viewer > .tn-overlay-control-group");
+ expect(group?.classList.contains("zoom-viewer-controls")).toBe(true);
+ expect(group?.getAttribute("data-placement")).toBe("bottom-end");
expect(buttons().map((button) => button.getAttribute("aria-label")))
- .toEqual([ "Zoom out", null, "Zoom in" ]);
+ .toEqual([ "zoom_controls.zoom_out", null, "zoom_controls.zoom_in" ]);
expect(readout()).toBe("100%");
expect(transformWrapperSpy).toHaveBeenLastCalledWith(expect.objectContaining({
minScale: 0.5,
@@ -81,16 +87,15 @@ describe("mountZoomPan", () => {
wheel: { disabled: true },
doubleClick: { mode: "reset" }
}));
+ expect(lastProps().panning).toBeUndefined();
});
- it("zooms in steps, pans only when zoomed in, and keeps the diagram it shows", () => {
- const diagram = mountDiagram();
+ it("zooms in steps and keeps the content it shows", () => {
+ const diagram = mountViewer();
const [ zoomOut, reset, zoomIn ] = buttons();
- expect(lastProps().panning).toEqual({ disabled: true });
act(() => zoomIn.click());
expect(readout()).toBe("120%");
- expect(lastProps().panning).toEqual({ disabled: false });
expect(document.querySelector(".zoom-viewer-content")?.contains(diagram)).toBe(true);
act(() => reset.click());
@@ -103,39 +108,22 @@ describe("mountZoomPan", () => {
expect(zoomOut.disabled).toBe(true);
expect(zoomIn.disabled).toBe(false);
});
-
- it("pans at any zoom when it takes the page", () => {
- document.body.innerHTML = `<div id="content"></div>`;
- const host = document.getElementById("content");
- if (!host) {
- throw new Error("The host is missing.");
- }
-
- act(() => render(<ZoomViewer labels={LABELS} fillsPage><svg /></ZoomViewer>, host));
-
- expect(document.querySelector("#content > .zoom-viewer.fills-page .zoom-viewer-content > svg"))
- .not.toBeNull();
- expect(lastProps().panning).toEqual({ disabled: false });
- });
});
-const LABELS = {
- label: "Diagram",
- zoomIn: "Zoom in",
- zoomOut: "Zoom out",
- zoomReset: "Reset zoom"
-};
-
-/** Mounts a viewer over a drawn diagram in `#content` and returns the diagram. */
-function mountDiagram() {
- document.body.innerHTML = `<div id="content"><div class="mermaid"><svg></svg></div></div>`;
- const diagram = document.querySelector<HTMLElement>(".mermaid");
- if (!diagram) {
- throw new Error("The diagram is missing.");
+/** Mounts a viewer over a drawing in `#content` and returns the drawing. */
+function mountViewer() {
+ document.body.innerHTML = `<div id="content"></div>`;
+ const host = document.getElementById("content");
+ if (!host) {
+ throw new Error("The host is missing.");
}
- act(() => mountZoomPan(diagram, LABELS));
- return diagram;
+ act(() => render(<ZoomViewer label="Diagram"><svg /></ZoomViewer>, host));
+ const drawing = host.querySelector("svg");
+ if (!drawing) {
+ throw new Error("The drawing is missing.");
+ }
+ return drawing;
}
function buttons() {
@@ -143,7 +131,7 @@ function buttons() {
}
function readout() {
- return document.querySelector(".zoom-viewer-reset")?.textContent;
+ return document.querySelector(".zoom-viewer-controls .tn-overlay-text-button")?.textContent;
}
function lastProps() {
diff --git a/packages/share-theme/src/content/zoom_viewer.tsx b/packages/share-theme/src/content/zoom_viewer.tsx
index e337a02ebf..ef86402877 100644
--- a/packages/share-theme/src/content/zoom_viewer.tsx
+++ b/packages/share-theme/src/content/zoom_viewer.tsx
@@ -1,58 +1,38 @@
import "./zoom_viewer.css";
+import OverlayControlGroup, {
+ ZoomControls
+} from "@triliumnext/client/src/widgets/react/OverlayControlGroup.js";
import {
useZoomPanPinch, useZoomPanWheel
} from "@triliumnext/client/src/widgets/react/zoom_pan.js";
-import { type ComponentChildren, render } from "preact";
-import { useLayoutEffect, useRef, useState } from "preact/hooks";
+import type { ComponentChildren } from "preact";
+import { useState } from "preact/hooks";
import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";
-/** The texts of a viewer, which the page or the app's catalogue gives in the visitor's language. */
-export interface ZoomPanLabels {
- label: string;
- zoomIn: string;
- zoomOut: string;
- zoomReset: string;
-}
-
-/**
- * Moves `element` into a viewer that pans and zooms it, in the flow of the page. The element
- * stays the same node, so a script that draws into it, such as `mermaid.ts`, keeps drawing there.
- */
-export default function mountZoomPan(element: HTMLElement, labels: ZoomPanLabels) {
- const host = document.createElement("div");
- element.replaceWith(host);
- render(<ZoomViewer labels={labels}><ElementSlot element={element} /></ZoomViewer>, host);
-}
-
interface ZoomViewerProps {
- labels: ZoomPanLabels;
- /**
- * Whether the viewer takes the page, so that the wheel and dragging always zoom and pan it, as
- * there is no page around it to scroll.
- */
- fillsPage?: boolean;
+ /** What the viewer shows, as its accessible name. */
+ label: string;
children: ComponentChildren;
}
/**
- * Pans and zooms its `children`, with the zoom steps and wheel handling of the app's diagram
- * preview. In the flow of a page, the wheel zooms only while the viewer has focus and dragging pans
- * only once the content is zoomed in, so that both scroll the page otherwise.
+ * Pans and zooms its `children` across the page, with the zoom steps, wheel handling and
+ * `ZoomControls` of the app's diagram preview.
*/
-export function ZoomViewer({ labels, fillsPage = false, children }: ZoomViewerProps) {
+export function ZoomViewer({ label, children }: ZoomViewerProps) {
const zoom = useZoomPanPinch({ minScale: MIN_ZOOM, maxScale: MAX_ZOOM });
const [ viewport, setViewport ] = useState<HTMLDivElement | null>(null);
- useZoomPanWheel(zoom.ref, viewport, !fillsPage);
+ useZoomPanWheel(zoom.ref, viewport);
return (
- <div className={fillsPage ? "zoom-viewer fills-page" : "zoom-viewer"}>
+ <div className="zoom-viewer">
<div
ref={setViewport}
className="zoom-viewer-viewport"
tabIndex={0}
role="group"
- aria-label={labels.label}
+ aria-label={label}
>
<TransformWrapper
ref={zoom.ref}
@@ -60,7 +40,6 @@ export function ZoomViewer({ labels, fillsPage = false, children }: ZoomViewerPr
maxScale={MAX_ZOOM}
centerZoomedOut
wheel={zoom.wheel}
- panning={{ disabled: !fillsPage && zoom.scale <= 1 }}
doubleClick={{ mode: "reset" }}
onTransform={zoom.onTransform}
>
@@ -72,47 +51,20 @@ export function ZoomViewer({ labels, fillsPage = false, children }: ZoomViewerPr
</TransformComponent>
</TransformWrapper>
</div>
- <div className="zoom-viewer-controls">
- <button
- type="button"
- title={labels.zoomOut}
- aria-label={labels.zoomOut}
- disabled={!zoom.canZoomOut}
- onClick={zoom.zoomOut}
- >
- <span className="tn-icon bx bx-minus-circle" aria-hidden="true" />
- </button>
- <button
- type="button"
- className="zoom-viewer-reset"
- title={labels.zoomReset}
- onClick={zoom.reset}
- >
- {`${Math.round(zoom.scale * 100)}%`}
- </button>
- <button
- type="button"
- title={labels.zoomIn}
- aria-label={labels.zoomIn}
- disabled={!zoom.canZoomIn}
- onClick={zoom.zoomIn}
- >
- <span className="tn-icon bx bx-plus-circle" aria-hidden="true" />
- </button>
- </div>
+ <OverlayControlGroup className="zoom-viewer-controls" placement="bottom-end">
+ <ZoomControls
+ percent={zoom.scale * 100}
+ canZoomIn={zoom.canZoomIn}
+ canZoomOut={zoom.canZoomOut}
+ onZoomIn={zoom.zoomIn}
+ onZoomOut={zoom.zoomOut}
+ onReset={zoom.reset}
+ />
+ </OverlayControlGroup>
</div>
);
}
-/** Holds an element that something other than Preact renders. */
-function ElementSlot({ element }: { element: HTMLElement }) {
- const ref = useRef<HTMLDivElement>(null);
- useLayoutEffect(() => {
- ref.current?.append(element);
- }, [ element ]);
- return <div ref={ref} />;
-}
-
/** The zoom bounds of the app's diagram preview, as a multiple of the content's fitted width. */
const MIN_ZOOM = 0.5;
const MAX_ZOOM = 10;
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 540120804a..73f7b33956 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1030,9 +1030,6 @@ describe("content_renderer", () => {
expect(scripts.map((script) => script.getAttribute("type"))).toEqual([ "application/json" ]);
expect(JSON.parse(scripts[0].textContent).notes.map((row: { noteId: string }) => row.noteId))
.toContain("mermaidNote");
- expect([ "label", "zoom-in", "zoom-out", "zoom-reset" ]
- .map((name) => container?.getAttribute(`data-${name}`)))
- .toEqual([ "Diagram", "Zoom in", "Zoom out", "Reset zoom" ]);
});
});
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index e263afbb6c..6cf302de17 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -901,19 +901,15 @@ export function renderCode(result: Result, mime?: string) {
/**
* Renders a Mermaid note as the image the app saved, which the share theme's script replaces with
- * a diagram drawn from `.mermaid-note-source` in the page's light or dark theme, inside a viewer
- * that pans and zooms it. The `data-*` attributes label that viewer.
+ * a diagram drawn from `.mermaid-note-source` in the page's light or dark theme.
*/
function renderMermaid(result: Result, note: SNote | BNote) {
if (typeof result.content !== "string") {
return;
}
- const labels = Object.entries(MERMAID_VIEWER_LABELS)
- .map(([ name, key ]) => ` data-${name}="${escapeHtml(t(key))}"`)
- .join("");
result.content = `
-<div class="mermaid-note"${labels}>
+<div class="mermaid-note">
<img class="mermaid-note-image" src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}">
<hr>
<details>
@@ -1031,14 +1027,6 @@ const SHARED_OPTIONS = [
"splitEditorOrientation"
] as const;
-/** The `data-*` attributes of a Mermaid note that label its viewer, and their translations. */
-const MERMAID_VIEWER_LABELS = {
- "label": "share_theme.diagram",
- "zoom-in": "share_theme.zoom-in",
- "zoom-out": "share_theme.zoom-out",
- "zoom-reset": "share_theme.zoom-reset"
-};
-
function renderImage(result: Result, note: SNote | BNote) {
result.content = `<img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}">`;
}