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): stay on the page when the tree's entry of it is clicked
Elian Doranc3fa2641c26b
diff --git a/packages/share-theme/src/page/tree_state.spec.ts b/packages/share-theme/src/page/tree_state.spec.tsindex 8c13d34c4e..3ee2d7fc8c 100644--- a/packages/share-theme/src/page/tree_state.spec.ts+++ b/packages/share-theme/src/page/tree_state.spec.ts@@ -115,6 +115,40 @@ describe("setupTreeState", () => {.toStrictEqual([ "" ]);});+ it("stays on the page when an entry of the current note is clicked, selecting it", () => {+ const click = (link: HTMLElement, init: MouseEventInit = {}) => {+ const event = new MouseEvent("click", { bubbles: true, cancelable: true, ...init });+ link.dispatchEvent(event);+ return event.defaultPrevented;+ };+ renderPane("site1", { activeTop: 50, menu: CLONES });+ document.body.dataset.noteId = "c";+ setupTreeState();++ expect(click(cloneLink("x"))).toBe(true);+ expect(selected()).toStrictEqual([ "x/row", "x/link" ]);++ expect(click(cloneLink("y"))).toBe(true);+ expect(selected()).toStrictEqual([ "y/row", "y/link" ]);+ window.dispatchEvent(new Event("pagehide"));+ expect(JSON.parse(sessionStorage.getItem("share-tree-state") ?? "null").activePath)+ .toStrictEqual([ "y", "c" ]);++ // Opening it in another tab is left to the browser, as is a link to another note.+ const newTab = [ { ctrlKey: true }, { metaKey: true }, { shiftKey: true }, { button: 1 } ];+ for (const init of newTab) {+ const event = new MouseEvent("click", { bubbles: true, cancelable: true, ...init });+ event.preventDefault = vi.fn();+ cloneLink("x").dispatchEvent(event);+ expect(event.preventDefault).not.toHaveBeenCalled();+ }+ document.body.dataset.noteId = "other";+ const event = new MouseEvent("click", { bubbles: true, cancelable: true });+ event.preventDefault = vi.fn();+ cloneLink("x").dispatchEvent(event);+ expect(event.preventDefault).not.toHaveBeenCalled();+ });+it("centers the current note when it is out of view, and leaves it when it is in view", () => {renderPane("site1", { activeTop: 700 });setupTreeState();diff --git a/packages/share-theme/src/page/tree_state.ts b/packages/share-theme/src/page/tree_state.tsindex d863598816..c5c979e06c 100644--- a/packages/share-theme/src/page/tree_state.ts+++ b/packages/share-theme/src/page/tree_state.ts@@ -45,8 +45,15 @@ export default function setupTreeState() {let activePath: string[] | undefined;pane.addEventListener("click", (e) => {const item = e.target instanceof Element && e.target.closest("#menu a")?.closest("li");- if (item) {- activePath = getItemPath(item);+ if (!item) {+ return;+ }+ activePath = getItemPath(item);+ // An entry of the page shown, maybe another clone of it, is selected in place.+ const opensNewTab = e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.button !== 0;+ if (!opensNewTab && item.getAttribute("data-note-id") === document.body.dataset.noteId) {+ e.preventDefault();+ selectItem(pane, item);}});@@ -82,13 +89,7 @@ function restoreTree(pane: HTMLElement, state: TreeState) {const path = state.activePath;const clicked = path?.at(-1) === document.body.dataset.noteId && path && findItem(pane, path);if (clicked) {- for (const selected of pane.querySelectorAll("#menu .active")) {- selected.classList.remove("active");- }- const rowAndLink = ":scope > .tree-item-row, :scope > * > a, :scope > a";- for (const selected of clicked.querySelectorAll(rowAndLink)) {- selected.classList.add("active");- }+ selectItem(pane, clicked);const ancestors = getAncestorItems(clicked);for (const ancestor of ancestors) {expandItem(ancestor);@@ -108,6 +109,17 @@ function restoreTree(pane: HTMLElement, state: TreeState) {pane.classList.remove("tree-restoring");}+/** Moves the current note's card to `item`, one of its entries. */+function selectItem(pane: HTMLElement, item: Element) {+ for (const selected of pane.querySelectorAll("#menu .active")) {+ selected.classList.remove("active");+ }+ const rowAndLink = ":scope > .tree-item-row, :scope > * > a, :scope > a";+ for (const selected of item.querySelectorAll(rowAndLink)) {+ selected.classList.add("active");+ }+}+function expandItem(item: Element) {item.classList.add("expanded");item.querySelector(":scope > * > .collapse-button")?.setAttribute("aria-expanded", "true");