Skip to content

Build your personal knowledge base with Trilium Notes

fix(presentation): keep slide links working after an edit

Elian Doran6231008b04e7
diff --git a/apps/client/src/widgets/collections/presentation/index.spec.ts b/apps/client/src/widgets/collections/presentation/index.spec.ts
index 55ce97f0e1..addb7b03f0 100644
--- a/apps/client/src/widgets/collections/presentation/index.spec.ts
+++ b/apps/client/src/widgets/collections/presentation/index.spec.ts
@@ -1,6 +1,9 @@
+import type { RevealApi } from "reveal.js";
import { afterEach, describe, expect, it, vi } from "vitest";
-import { createFitObserver, fitSlideContent, keepKeyboardInPresentation } from "./index";
+import {
+ createFitObserver, fitSlideContent, followSlideLinks, keepKeyboardInPresentation
+} from "./index";
describe("fitSlideContent", () => {
it("zooms out only the content that is larger than the slide", () => {
@@ -143,3 +146,36 @@ function buildFrame(tag: string) {
container.append(frame);
return { container, frame };
}
+
+describe("followSlideLinks", () => {
+ it("moves to the slide a link points at, for links added after it starts", () => {
+ const container = document.createElement("div");
+ const target = document.createElement("section");
+ target.id = "slide-target";
+ container.append(target);
+ const api = {
+ getIndices: vi.fn(() => ({ h: 2, v: 1, f: undefined })),
+ slide: vi.fn()
+ };
+ const stop = followSlideLinks(container, api as unknown as RevealApi);
+
+ const link = document.createElement("a");
+ link.className = "reference-link";
+ link.href = "#/slide-target";
+ const label = document.createElement("span");
+ link.append(label);
+ container.append(link);
+ label.click();
+ expect(api.getIndices).toHaveBeenCalledWith(target);
+ expect(api.slide).toHaveBeenCalledWith(2, 1, undefined);
+
+ // A link to a note that is not a slide, or a link after stopping, does not move.
+ api.slide.mockClear();
+ link.href = "#/slide-missing";
+ link.click();
+ link.href = "#/slide-target";
+ stop();
+ link.click();
+ expect(api.slide).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/client/src/widgets/collections/presentation/index.tsx b/apps/client/src/widgets/collections/presentation/index.tsx
index 58ea8eb915..08d3af56a0 100644
--- a/apps/client/src/widgets/collections/presentation/index.tsx
+++ b/apps/client/src/widgets/collections/presentation/index.tsx
@@ -350,7 +350,6 @@ function Presentation({ presentation, setApi, ntxId, editedNoteId, isEditorReady
api.initialize().then(() => {
setRevealApi(api);
setApi(api);
- rewireLinks(container, api);
});
// Reveal.js listens only to window resizes, not to the note pane changing size.
@@ -359,8 +358,10 @@ function Presentation({ presentation, setApi, ntxId, editedNoteId, isEditorReady
});
resizeObserver.observe(container);
const releaseKeyboard = keepKeyboardInPresentation(container);
+ const stopFollowingLinks = followSlideLinks(container, api);
return () => {
+ stopFollowingLinks();
releaseKeyboard();
fitObserver.disconnect();
fitObserverRef.current = null;
@@ -577,22 +578,29 @@ export function keepKeyboardInPresentation(container: HTMLElement) {
};
}
-function rewireLinks(container: HTMLElement, api: RevealApi) {
- const links = container.querySelectorAll<HTMLLinkElement>("a.reference-link");
- for (const link of links) {
- link.addEventListener("click", () => {
- /**
- * Reveal.js has built-in navigation by either index or ID. However, the ID-based navigation doesn't work because it tries to look
- * outside the shadom DOM (via document.getElementById).
- */
- const url = new URL(link.href);
- if (!url.hash.startsWith("#/slide-")) return;
- const targetId = url.hash.substring(8);
- const slide = container.querySelector<HTMLElement>(`#slide-${targetId}`);
- if (!slide) return;
-
- const { h, v, f } = api.getIndices(slide);
- api.slide(h, v, f);
- });
- }
+/**
+ * Moves to the slide that a clicked link in `container` points at, including the links of content
+ * rendered after this starts. Returns the function that stops it.
+ *
+ * Reveal.js navigates by slide ID on its own, but looks the slide up with
+ * `document.getElementById()`, which does not find it inside the shadow root.
+ */
+export function followSlideLinks(container: HTMLElement, api: RevealApi) {
+ const onClick = (e: MouseEvent) => {
+ const link = e.target instanceof Element
+ ? e.target.closest<HTMLAnchorElement>("a.reference-link")
+ : null;
+ if (!link) return;
+ const url = new URL(link.href);
+ if (!url.hash.startsWith("#/slide-")) return;
+ const targetId = url.hash.substring(8);
+ const slide = container.querySelector<HTMLElement>(`#slide-${targetId}`);
+ if (!slide) return;
+
+ const { h, v, f } = api.getIndices(slide);
+ api.slide(h, v, f);
+ };
+
+ container.addEventListener("click", onClick);
+ return () => container.removeEventListener("click", onClick);
}