Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): keep the clicked clone selected when the page is reloaded

Elian Doran2fca66a4d56b
diff --git a/packages/share-theme/src/page/tree_state.spec.ts b/packages/share-theme/src/page/tree_state.spec.ts
index 3ee2d7fc8c..18f7fa9c37 100644
--- a/packages/share-theme/src/page/tree_state.spec.ts
+++ b/packages/share-theme/src/page/tree_state.spec.ts
@@ -86,6 +86,12 @@ describe("setupTreeState", () => {
expect(expandedIds()).toStrictEqual([ "x", "c", "y", "c" ]);
expect(toggle("y").getAttribute("aria-expanded")).toBe("true");
+ // A reload without another click keeps the clicked clone.
+ window.dispatchEvent(new Event("pagehide"));
+ renderPane("site1", { activeTop: 50, menu: CLONES });
+ setupTreeState();
+ expect(selected()).toStrictEqual([ "y/row", "y/link" ]);
+
// The server's way to its own clone opens nothing above the clicked one that was closed.
const fromY = { siteId: "site1", top: 0, expanded: [ "y" ], activePath: [ "y", "c" ] };
sessionStorage.setItem("share-tree-state", JSON.stringify(fromY));
diff --git a/packages/share-theme/src/page/tree_state.ts b/packages/share-theme/src/page/tree_state.ts
index c5c979e06c..2a3bbd851c 100644
--- a/packages/share-theme/src/page/tree_state.ts
+++ b/packages/share-theme/src/page/tree_state.ts
@@ -23,8 +23,9 @@ export default function setupTreeState() {
const siteId = document.body.dataset.ancestorNoteId;
const saved = readTreeState();
+ let activePath: string[] | undefined;
if (saved && saved.siteId === siteId) {
- restoreTree(pane, saved);
+ activePath = restoreTree(pane, saved);
pane.scrollTop = saved.top;
}
@@ -42,7 +43,6 @@ 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) {
@@ -74,7 +74,7 @@ export default function setupTreeState() {
/**
* Expands the saved entries, every clone of each, and selects the clicked entry of the current
- * note, all without animating.
+ * note, all without animating. Returns the path to that entry when the tree still has it.
*/
function restoreTree(pane: HTMLElement, state: TreeState) {
pane.classList.add("tree-restoring");
@@ -107,6 +107,7 @@ function restoreTree(pane: HTMLElement, state: TreeState) {
// Applies the expanded styles while transitions are off, so the chevrons do not rotate.
void pane.offsetHeight;
pane.classList.remove("tree-restoring");
+ return clicked ? path : undefined;
}
/** Moves the current note's card to `item`, one of its entries. */