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
feat(tree): add option to disable scroll-follows-navigation
Kevin Leutzinger726ec1810c20
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex ece0f8981b..97de6aa0eb 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2068,6 +2068,7 @@"hide-archived-notes": "Hide archived notes","automatically-collapse-notes": "Automatically collapse notes","automatically-collapse-notes-title": "Notes will be collapsed after period of inactivity to declutter the tree.",+ "scroll-follow-navigation": "Scroll tree to follow navigation","save-changes": "Save & apply changes","auto-collapsing-notes-after-inactivity": "Auto collapsing notes after inactivity...","saved-search-note-refreshed": "Saved search note refreshed.",diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.tsindex 4314ee4df7..8651731fb6 100644--- a/apps/client/src/widgets/note_tree.ts+++ b/apps/client/src/widgets/note_tree.ts@@ -138,6 +138,12 @@ const TPL = /*html*/`title="${t("note_tree.automatically-collapse-notes-title")}"></span></label></div>+ <div class="form-check">+ <label class="form-check-label tn-checkbox">+ <input class="form-check-input scroll-follow-navigation" type="checkbox" value="">+ ${t("note_tree.scroll-follow-navigation")}+ </label>+ </div><br/>@@ -197,6 +203,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {private $saveTreeSettingsButton!: JQuery<HTMLElement>;private $hideArchivedNotesCheckbox!: JQuery<HTMLElement>;private $autoCollapseNoteTree!: JQuery<HTMLElement>;+ private $scrollFollowNavigationCheckbox!: JQuery<HTMLElement>;private treeName: "main";private autoCollapseTimeoutId?: Timeout;private lastFilteredHoistedNotePath?: string | null;@@ -261,6 +268,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {this.$treeSettingsPopup = this.$widget.find(".tree-settings-popup");this.$hideArchivedNotesCheckbox = this.$treeSettingsPopup.find(".hide-archived-notes");this.$autoCollapseNoteTree = this.$treeSettingsPopup.find(".auto-collapse-note-tree");+ this.$scrollFollowNavigationCheckbox = this.$treeSettingsPopup.find(".scroll-follow-navigation");this.$treeSettingsButton = this.$widget.find(".tree-settings-button");this.$treeSettingsButton.on("click", (e) => {@@ -271,6 +279,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {this.$hideArchivedNotesCheckbox.prop("checked", this.hideArchivedNotes);this.$autoCollapseNoteTree.prop("checked", this.autoCollapseNoteTree);+ this.$scrollFollowNavigationCheckbox.prop("checked", this.treeScrollFollowNavigation);const top = this.$treeActions[0].offsetTop - (this.$treeSettingsPopup.outerHeight() ?? 0);const left = Math.max(0, this.$treeActions[0].offsetLeft - (this.$treeSettingsPopup.outerWidth() ?? 0) + (this.$treeActions.outerWidth() ?? 0));@@ -295,6 +304,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {this.$saveTreeSettingsButton.on("click", async () => {await this.setHideArchivedNotes(this.$hideArchivedNotesCheckbox.prop("checked"));await this.setAutoCollapseNoteTree(this.$autoCollapseNoteTree.prop("checked"));+ await this.setTreeScrollFollowNavigation(this.$scrollFollowNavigationCheckbox.prop("checked"));this.$treeSettingsPopup.hide();@@ -354,6 +364,14 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {await options.save("autoCollapseNoteTree", val.toString());}+ get treeScrollFollowNavigation() {+ return options.is("treeScrollFollowNavigation");+ }++ async setTreeScrollFollowNavigation(val: boolean) {+ await options.save("treeScrollFollowNavigation", val.toString());+ }+initFancyTree() {const treeData = [this.prepareRootNode()];@@ -429,7 +447,9 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {return true;},- beforeActivate: (event, { node }) => {+ beforeActivate: (event, data) => {+ const { node } = data;+// hidden subtree is hidden hackily - we want it to be present in the tree so that we can switch to it// without reloading the whole tree, but we want it to be hidden when hoisted to root. FancyTree allows// filtering the display only by ascendant - i.e. if the root is visible, all the descendants are as well.@@ -440,11 +460,23 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {// if we're hoisted in hidden subtree, we want to avoid crossing to "visible" tree,// which could happen via UP key from hidden root- return node.data.noteId !== "root";+ if (node.data.noteId === "root") return false;+ } else {+ // we're not hoisted to hidden subtree, the only way to cross is via DOWN key to the hidden root+ if (node.data.noteId === "_hidden") return false;}- // we're not hoisted to hidden subtree, the only way to cross is via DOWN key to the hidden root- return node.data.noteId !== "_hidden";+ // Synchronously disable fancytree's activeVisible before it checks it in nodeSetActive,+ // preventing the automatic makeVisible/scrollIntoView call on direct tree interaction.+ // Restored after the current task via setTimeout so only this activation is affected.+ if (!this.treeScrollFollowNavigation) {+ this.tree.options.activeVisible = false;+ setTimeout(() => {+ this.tree.options.activeVisible = true;+ }, 0);+ }++ return true;},activate: async (event, data) => {// click event won't propagate so let's close context menu manually@@ -1141,8 +1173,11 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {await this.expandToNote(this.noteContext.notePath);}- newActiveNode.setActive(true, { noEvents: true, noFocus: !oldActiveNodeFocused });- newActiveNode.makeVisible({ scrollIntoView: true });+ newActiveNode.setActive(true, {+ noEvents: true,+ noFocus: !oldActiveNodeFocused,+ scrollIntoView: this.treeScrollFollowNavigation,+ });}}diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.tsindex c84a3d4852..7ee7e87bf9 100644--- a/packages/commons/src/lib/options_interface.ts+++ b/packages/commons/src/lib/options_interface.ts@@ -160,6 +160,7 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActihideArchivedNotes_main: boolean;debugModeEnabled: boolean;autoCollapseNoteTree: boolean;+ treeScrollFollowNavigation: boolean;dailyBackupEnabled: boolean;weeklyBackupEnabled: boolean;monthlyBackupEnabled: boolean;diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.tsindex 0c0b1c59f8..c17ef81d02 100644--- a/packages/trilium-core/src/routes/api/options.ts+++ b/packages/trilium-core/src/routes/api/options.ts@@ -72,6 +72,7 @@ const ALLOWED_OPTIONS = new Set<OptionNames>(["nativeTitleBarVisible","headingStyle","autoCollapseNoteTree",+ "treeScrollFollowNavigation","autoReadonlySizeText","customDateTimeFormat","autoReadonlySizeCode",diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.tsindex 6c731d1974..de398efac0 100644--- a/packages/trilium-core/src/services/options_init.ts+++ b/packages/trilium-core/src/services/options_init.ts@@ -152,6 +152,7 @@ const defaultOptions: DefaultOption[] = [{ name: "debugModeEnabled", value: "false", isSynced: false },{ name: "headingStyle", value: "underline", isSynced: true },{ name: "autoCollapseNoteTree", value: "true", isSynced: true },+ { name: "treeScrollFollowNavigation", value: "true", isSynced: true },{ name: "autoReadonlySizeText", value: "32000", isSynced: false },{ name: "autoReadonlySizeCode", value: "64000", isSynced: false },{ name: "dailyBackupEnabled", value: "true", isSynced: false },