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
fix(share): keep the page in place when a heading's link is clicked
Elian Doran5b3e4e33eb2d
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.htmlindex c96a67b8ef..d72974bab4 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@@ -44,8 +44,8 @@ class="image"><ul><li>Table of contents.</li><li>A link next to each heading, shown while the pointer is over the heading.- Clicking it goes to the section and copies the address of the section,- to share a link to it.</li>+ Clicking it shows the address of the section in the address bar of the+ browser and copies it, to share a link to the section.</li><li>Syntax highlight of code blocks, in the language selected for each blockand in the colors of the VS Code light or dark theme, following the page'smode. For “Auto-detected” blocks, the language is guessed among the onesdiff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing.md b/docs/User Guide/User Guide/Advanced Usage/Sharing.mdindex 58aab17640..0c509d3bd0 100644--- a/docs/User Guide/User Guide/Advanced Usage/Sharing.md+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing.md@@ -33,7 +33,7 @@ Trilium allows you to share selected notes as **publicly accessible** read-only<tbody><tr><th><a class="reference-link" href="../Note%20Types/Text.md">Text</a></th>- <td><ul><li>Table of contents.</li><li>A link next to each heading, shown while the pointer is over the heading. Clicking it goes to the section and copies the address of the section, to share a link to it.</li><li>Syntax highlight of code blocks, in the language selected for each block and in the colors of the VS Code light or dark theme, following the page's mode. For “Auto-detected” blocks, the language is guessed among the ones enabled in <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Options.md">Options</a> → <em>Code Notes</em>; a language not enabled there is not highlighted.</li><li>Rendering for math equations.</li><li><a href="../Note%20Types/Text/Include%20Note.md">Including notes</a> (only if the included notes are also shared).</li><li>Mermaid diagrams in code blocks, drawn in the page's light or dark mode and redrawn when the visitor switches it.</li></ul></td>+ <td><ul><li>Table of contents.</li><li>A link next to each heading, shown while the pointer is over the heading. Clicking it shows the address of the section in the address bar of the browser and copies it, to share a link to the section.</li><li>Syntax highlight of code blocks, in the language selected for each block and in the colors of the VS Code light or dark theme, following the page's mode. For “Auto-detected” blocks, the language is guessed among the ones enabled in <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Options.md">Options</a> → <em>Code Notes</em>; a language not enabled there is not highlighted.</li><li>Rendering for math equations.</li><li><a href="../Note%20Types/Text/Include%20Note.md">Including notes</a> (only if the included notes are also shared).</li><li>Mermaid diagrams in code blocks, drawn in the page's light or dark mode and redrawn when the visitor switches it.</li></ul></td><td> </td></tr><tr>diff --git a/packages/share-theme/src/page/toc.spec.ts b/packages/share-theme/src/page/toc.spec.tsindex 47723d90d3..e715e3a539 100644--- a/packages/share-theme/src/page/toc.spec.ts+++ b/packages/share-theme/src/page/toc.spec.ts@@ -81,21 +81,51 @@ describe("setupToC", () => {expect(link.classList.contains("copied")).toBe(false);});- it("only jumps to the section when the address cannot be copied", async () => {+ it("puts the section's address in the location bar instead of jumping to it", () => {+ vi.stubGlobal("navigator", {});+ renderPage([ [ 2, "intro", 100 ], [ 2, "later", 500 ] ]);+ history.replaceState(null, "", "#later");+ const historyLength = history.length;+ const scrollIntoView = vi.fn();+ const heading = document.getElementById("intro");+ if (!heading) {+ throw new Error("The heading is missing.");+ }+ heading.scrollIntoView = scrollIntoView;++ const click = new MouseEvent("click", { bubbles: true, cancelable: true });+ headingLink("intro").dispatchEvent(click);++ expect(click.defaultPrevented).toBe(true);+ expect(location.hash).toBe("#intro");+ expect(history.length).toBe(historyLength);+ expect(scrollIntoView).not.toHaveBeenCalled();+ });++ it("leaves a click with a modifier key or another button to the browser", () => {+ const writeText = vi.fn().mockResolvedValue(undefined);+ vi.stubGlobal("navigator", { clipboard: { writeText } });+ renderPage([ [ 2, "intro", 100 ] ]);++ for (const init of [ { ctrlKey: true }, { metaKey: true }, { shiftKey: true },+ { altKey: true }, { button: 1 } ]) {+ const click = new MouseEvent("click", { bubbles: true, cancelable: true, ...init });+ headingLink("intro").dispatchEvent(click);+ expect(click.defaultPrevented).toBe(false);+ }+ expect(writeText).not.toHaveBeenCalled();+ });++ it("shows no check mark when the address cannot be copied", async () => {const writeText = vi.fn().mockRejectedValue(new Error("denied"));vi.stubGlobal("navigator", { clipboard: { writeText } });renderPage([ [ 2, "intro", 100 ] ]);const link = headingLink("intro");- const click = new MouseEvent("click", { bubbles: true, cancelable: true });- link.dispatchEvent(click);+ link.click();await vi.waitFor(() => expect(writeText).toHaveBeenCalledOnce());await Promise.resolve();- expect(click.defaultPrevented).toBe(false);expect(link.classList.contains("copied")).toBe(false);-- vi.stubGlobal("navigator", {});- expect(() => link.click()).not.toThrow();});it("sets the heading links up on a page without a table of contents", async () => {diff --git a/packages/share-theme/src/page/toc.ts b/packages/share-theme/src/page/toc.tsindex 4f58d4e91f..1c2da01292 100644--- a/packages/share-theme/src/page/toc.ts+++ b/packages/share-theme/src/page/toc.ts@@ -58,8 +58,9 @@ export default function setupToC() {const COPIED_DURATION = 1500;/**- * Makes the link of each heading copy the address of its section when clicked, besides jumping to- * it, and show a check mark for a moment. Without clipboard access, a click only jumps.+ * Makes a click on the link of a heading put the address of its section in the location bar, in+ * place of the current one and without scrolling, and copy it, showing a check mark for a moment.+ * A click with a modifier key or another button than the main one is left to the browser.*/function setupHeadingLinks() {for (const link of document.querySelectorAll<HTMLAnchorElement>("#content .toc-anchor")) {@@ -71,7 +72,13 @@ function setupHeadingLinks() {icon?.classList.toggle("bx-check", copied);};- link.addEventListener("click", () => {+ link.addEventListener("click", (e) => {+ if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) {+ return;+ }++ e.preventDefault();+ history.replaceState(history.state, "", link.href);navigator.clipboard?.writeText(link.href).then(() => {setCopied(true);clearTimeout(resetTimer);