Skip to content

Build your personal knowledge base with Trilium Notes

client: prevent a note refresh each time the tree item of the currently opened note is clicked

Adorian Dorana46fce6b0ae2
diff --git a/apps/client/src/components/note_context.spec.ts b/apps/client/src/components/note_context.spec.ts
index 94be608a47..c1e603c2f9 100644
--- a/apps/client/src/components/note_context.spec.ts
+++ b/apps/client/src/components/note_context.spec.ts
@@ -105,4 +105,30 @@ describe("NoteContext view scope", () => {
if (noteContext.viewScope) noteContext.viewScope.block = undefined;
expect(viewScope).toEqual({ block: "a" });
});
+
+ it("stays on the open note when it is navigated to again", async () => {
+ buildNote({
+ id: "root", title: "root", children: [ { id: "reopened", title: "Reopened" } ]
+ });
+ const noteContext = new NoteContext();
+ const navigate = (viewScope?: ViewScope) => noteContext.setNote("root/reopened", {
+ viewScope, skipRecentNotes: true, triggerSwitchEvent: false
+ });
+
+ await navigate();
+ expect(await noteContext.isReadOnly()).toBe(false);
+ const shownScope = noteContext.viewScope;
+ expect(shownScope).toEqual({ viewMode: "default", isReadOnly: false });
+ // What the "Edit" button of a read-only note and `consumeBookmark()` write into the scope.
+ if (shownScope) {
+ shownScope.readOnlyTemporarilyDisabled = true;
+ shownScope.bookmark = undefined;
+ }
+
+ await navigate();
+ expect(noteContext.viewScope).toBe(shownScope);
+
+ await navigate({ bookmark: "intro" });
+ expect(noteContext.viewScope).toEqual({ viewMode: "default", bookmark: "intro" });
+ });
});
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.ts
index 66e03fc60b..86606b18cb 100644
--- a/apps/client/src/components/note_context.ts
+++ b/apps/client/src/components/note_context.ts
@@ -59,6 +59,9 @@ const READ_ONLY_CAPABLE_VIEW_TYPES: string[] = [
"table"
];
+/** View scope keys that hold the context's own state rather than what a navigation asks for. */
+const VIEW_STATE_KEYS: string[] = [ "isReadOnly", "readOnlyTemporarilyDisabled" ];
+
export interface NoteContextDataMap {
toc: HeadingContext;
highlights: HighlightContext;
@@ -188,8 +191,7 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
return;
}
- const isSameView = utils.areObjectsEqual(this.viewScope, viewScope);
- if (this.notePath === resolvedNotePath && isSameView) {
+ if (this.notePath === resolvedNotePath && isSameViewScope(this.viewScope, viewScope)) {
return this;
}
@@ -696,4 +698,18 @@ export function openInCurrentNoteContext(evt: MouseEvent | JQuery.ClickEvent | J
}
}
+/**
+ * Whether navigating with `requested` shows what `current` already shows. Ignores the
+ * `VIEW_STATE_KEYS` and the keys a reader such as `consumeBookmark()` set to `undefined`.
+ */
+function isSameViewScope(current: ViewScope | undefined, requested: ViewScope) {
+ return utils.areObjectsEqual(
+ getNavigationTarget(current ?? {}), getNavigationTarget(requested));
+}
+
+function getNavigationTarget(viewScope: ViewScope) {
+ return Object.fromEntries(Object.entries(viewScope).filter(([ key, value ]) =>
+ value !== undefined && !VIEW_STATE_KEYS.includes(key)));
+}
+
export default NoteContext;