Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/block references: fix some issues

Adorian Doranadb7d237ae76
diff --git a/apps/client/src/components/note_context.spec.ts b/apps/client/src/components/note_context.spec.ts
index 75861a93f1..44d2e67e9a 100644
--- a/apps/client/src/components/note_context.spec.ts
+++ b/apps/client/src/components/note_context.spec.ts
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it } from "vitest";
+import type { ViewScope } from "../services/link.js";
import { buildNote } from "../test/easy-froca.js";
import NoteContext from "./note_context.js";
@@ -43,3 +44,20 @@ describe("NoteContext read-only capability", () => {
expect(await noteContext.isReadOnly()).toBe(false);
});
});
+
+describe("NoteContext view scope", () => {
+ it("keeps its own copy of the view scope it is given", async () => {
+ buildNote({ id: "root", title: "root", children: [ { id: "blocks", title: "Blocks" } ] });
+ const viewScope: ViewScope = { block: "a" };
+ const noteContext = new NoteContext();
+
+ await noteContext.setNote("root/blocks", {
+ viewScope, skipRecentNotes: true, triggerSwitchEvent: false
+ });
+ expect(noteContext.viewScope).toEqual({ block: "a", viewMode: "default" });
+
+ // What `revealBlockReference()` does once it has scrolled to the block.
+ if (noteContext.viewScope) noteContext.viewScope.block = undefined;
+ expect(viewScope).toEqual({ block: "a" });
+ });
+});
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.ts
index f8bc14e539..a8b78b7266 100644
--- a/apps/client/src/components/note_context.ts
+++ b/apps/client/src/components/note_context.ts
@@ -162,8 +162,11 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
/** @returns the note context that ended up showing the note: usually `this`, or a new tab when a pinned tab redirected the navigation. `undefined` if nothing was navigated. */
async setNote(inputNotePath: string | undefined, opts: SetNoteOpts = {}): Promise<NoteContext | undefined> {
opts.triggerSwitchEvent = opts.triggerSwitchEvent !== undefined ? opts.triggerSwitchEvent : true;
- opts.viewScope = opts.viewScope || {};
- opts.viewScope.viewMode = opts.viewScope.viewMode || "default";
+ // Readers like `revealBlockReference()` change the scope, so the context keeps a copy.
+ const viewScope: ViewScope = {
+ ...opts.viewScope,
+ viewMode: opts.viewScope?.viewMode || "default"
+ };
if (!inputNotePath) {
return;
@@ -175,7 +178,8 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
return;
}
- if (this.notePath === resolvedNotePath && utils.areObjectsEqual(this.viewScope, opts.viewScope)) {
+ const isSameView = utils.areObjectsEqual(this.viewScope, viewScope);
+ if (this.notePath === resolvedNotePath && isSameView) {
return this;
}
@@ -184,7 +188,7 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
if (shouldRedirectPinnedNavigation(this.pinned, this.noteId, targetNoteId)) {
return appContext.tabManager.openContextWithNote(resolvedNotePath, {
activate: true,
- viewScope: opts.viewScope,
+ viewScope,
hoistedNoteId: this.hoistedNoteId
});
}
@@ -198,7 +202,7 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
const previousNoteId = this.noteId;
this.notePath = resolvedNotePath;
- this.viewScope = opts.viewScope;
+ this.viewScope = viewScope;
({ noteId: this.noteId, parentNoteId: this.parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(resolvedNotePath));
// Clear context data only when actually switching to a different note.