Skip to content

Build your personal knowledge base with Trilium Notes

77.8 KB
Raw Blame
import "jquery.fancytree";
import "jquery.fancytree/dist/modules/jquery.fancytree.dnd5.js";
import "jquery.fancytree/dist/modules/jquery.fancytree.clones.js";
import "jquery.fancytree/dist/modules/jquery.fancytree.filter.js";
import "../stylesheets/tree.css";
import "./note_tree.css";

import { GEO_LOCATION_ATTRIBUTE, MAP_POSITION_ATTRIBUTE } from "@triliumnext/commons";
import { h } from "preact";

import appContext, { type CommandListenerData, type EventData } from "../components/app_context.js";
import type { SetNoteOpts } from "../components/note_context.js";
import type FBranch from "../entities/fbranch.js";
import type FNote from "../entities/fnote.js";
import type { TreeCommandNames } from "../menus/tree_context_menu.js";
import branchService from "../services/branches.js";
import froca from "../services/froca.js";
import hoistedNoteService from "../services/hoisted_note.js";
import { t } from "../services/i18n.js";
import keyboardActionsService from "../services/keyboard_actions.js";
import linkService from "../services/link.js";
import type LoadResults from "../services/load_results.js";
import type { AttributeRow, BranchRow } from "../services/load_results.js";
import noteCreateService from "../services/note_create.js";
import options from "../services/options.js";
import server from "../services/server.js";
import { buildShareLink } from "../services/share_link.js";
import shortcutService from "../services/shortcuts.js";
import toastService from "../services/toast.js";
import treeService from "../services/tree.js";
import utils from "../services/utils.js";
import ws from "../services/ws.js";
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import TreeSettings from "./note_tree_settings.jsx";
import { disposeReactWidget, renderReactWidgetAtElement } from "./react/react_utils.jsx";

const TPL = /*html*/`
<div class="tree-wrapper">
    <style>
    .tree-wrapper {
        flex-grow: 1;
        flex-shrink: 1;
        flex-basis: 60%;
        font-family: var(--tree-font-family);
        font-size: var(--tree-font-size);
        position: relative;
        min-height: 0;
    }

    .tree {
        height: 100%;
        overflow: auto;
        padding-bottom: 35px;
        padding-top: 5px;
    }

    .tree-actions {
        background-color: var(--launcher-pane-background-color);
        z-index: 100;
        position: absolute;
        bottom: 0;
        display: flex;
        align-items: flex-end;
        justify-content: flex-end;
        inset-inline-end: 17px;
        border-radius: 7px;
        border: 1px solid var(--main-border-color);
    }

    button.tree-floating-button {
        margin: 1px;
        font-size: 1.5em;
        padding: 5px;
        max-height: 34px;
        color: var(--launcher-pane-text-color);
        background-color: var(--button-background-color);
        border-radius: var(--button-border-radius);
        border: 1px solid transparent;
    }

    button.tree-floating-button:hover {
        border: 1px solid var(--button-border-color);
    }

    .collapse-tree-button {
        inset-inline-end: 100px;
    }

    .scroll-to-active-note-button {
        inset-inline-end: 55px;
    }

    .tree-settings-button {
        inset-inline-end: 10px;
    }

    .tree .hidden-node-is-hidden {
        display: none;
    }
    </style>

    <div class="tree scroll-edge-fade"></div>

    <div class="tree-actions">
        <button class="tree-floating-button bx bx-layer-minus collapse-tree-button"
                title="${t("note_tree.collapse-title")}"
                data-trigger-command="collapseTree"></button>

        <button class="tree-floating-button bx bx-crosshair scroll-to-active-note-button"
                title="${t("note_tree.scroll-active-title")}"
                data-trigger-command="scrollToActiveNote"></button>

        <button class="tree-floating-button bx bxs-tree tree-settings-button"
                title="${t("note_tree.tree-settings-title")}"></button>
    </div>
</div>
`;

const MAX_SEARCH_RESULTS_IN_TREE = 100;

// this has to be hanged on the actual elements to effectively intercept and stop click event
const cancelClickPropagation: (e: Event) => void = (e) => e.stopPropagation();

// TODO: Fix once we remove Node.js API from public
type Timeout = NodeJS.Timeout | string | number | undefined;

// TODO: Deduplicate with server special_notes
type LauncherType = "launcher" | "note" | "script" | "customWidget" | "spacer";

// TODO: Deduplicate with the server
interface CreateLauncherResponse {
    success: boolean;
    message: string;
    note: {
        noteId: string;
    };
}

interface ExpandedSubtreeResponse {
    branchIds: string[];
}

interface RefreshContext {
    noteIdsToUpdate: Set<string>;
    noteIdsToReload: Set<string>;
}

/** Entity changes below the given threshold will be processed without batching to avoid performance degradation. */
const BATCH_UPDATE_THRESHOLD = 10;

export default class NoteTreeWidget extends NoteContextAwareWidget {
    private $tree!: JQuery<HTMLElement>;
    private $treeActions!: JQuery<HTMLElement>;
    private settingsContainer = document.createElement("div");
    private isSettingsShown = false;
    private treeName: "main";
    private autoCollapseTimeoutId?: Timeout;
    private lastFilteredHoistedNotePath?: string | null;
    private spotlightedNotePath?: string | null;
    private spotlightedNode: Fancytree.FancytreeNode | null = null;
    private tree!: Fancytree.Fancytree;

    constructor() {
        super();

        this.treeName = "main"; // legacy value
    }

    doRender() {
        this.$widget = $(TPL);
        this.$tree = this.$widget.find(".tree");
        this.$treeActions = this.$widget.find(".tree-actions");

        this.$tree.on("mousedown", (e: JQuery.MouseDownEvent) => {
            const target = e.target as HTMLElement;
            if (e.button !== 0) return;

            if (target.classList.contains("unhoist-button")) {
                hoistedNoteService.unhoist();
            } else if (target.classList.contains("refresh-search-button")) {
                this.refreshSearch(e);
            } else if (target.classList.contains("add-note-button")) {
                const node = $.ui.fancytree.getNode(e as unknown as Event);
                const parentNotePath = treeService.getNotePath(node);
                noteCreateService.createNote(parentNotePath, {
                    isProtected: node.data.isProtected,
                    // Activate in this tree's own context — in popup dialogs (e.g. the task states
                    // tree popup) it is not the tab manager's active context.
                    noteContext: this.noteContext
                });
            } else if (target.classList.contains("enter-workspace-button")) {
                const node = $.ui.fancytree.getNode(e as unknown as Event);
                this.triggerCommand("hoistNote", { noteId: node.data.noteId });
            }
        });

        // fancytree doesn't support middle click, so this is a way to support it
        this.$tree.on("mousedown", ".fancytree-title", (e) => {
            if (e.which === 2) {
                const node = $.ui.fancytree.getNode(e as unknown as Event);
                const notePath = treeService.getNotePath(node);

                if (notePath) {
                    e.stopPropagation();
                    e.preventDefault();

                    appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
                        activate: !!e.shiftKey,
                        placement: "afterCurrent"
                    });
                }
            }
        });
        this.$tree.on("mouseup", ".fancytree-title", (e) => {
            // Prevent middle click from pasting in the editor.
            if (e.which === 2) {
                e.stopPropagation();
                e.preventDefault();
            }
        });

        this.$widget.find(".tree-settings-button").on("click", () => {
            this.toggleSettings(!this.isSettingsShown);
        });

        // note tree starts initializing already during render which is atypical
        Promise.all([options.initializedPromise, froca.initializedPromise]).then(() => {
            this.applyDensity();
            this.initFancyTree();
        });

        this.setupNoteTitleTooltip();
    }

    setupNoteTitleTooltip() {
        // the following will dynamically set tree item's tooltip if the whole item's text is not currently visible
        // if the whole text is visible then no tooltip is show since that's unnecessarily distracting
        // see https://github.com/zadam/trilium/pull/1120 for discussion

        // code inspired by https://gist.github.com/jtsternberg/c272d7de5b967cec2d3d
        const isEnclosing = ($container: JQuery<HTMLElement>, $sub: JQuery<HTMLElement>) => {
            const conOffset = $container.offset();
            const conDistanceFromTop = (conOffset?.top ?? 0) + ($container.outerHeight(true) ?? 0);
            const conDistanceFromLeft = (conOffset?.left ?? 0) + ($container.outerWidth(true) ?? 0);

            const subOffset = $sub.offset();
            const subDistanceFromTop = (subOffset?.top ?? 0) + ($sub.outerHeight(true) ?? 0);
            const subDistanceFromLeft = (subOffset?.left ?? 0) + ($sub.outerWidth(true) ?? 0);

            return conDistanceFromTop > subDistanceFromTop
                && (conOffset?.top ?? 0) < (subOffset?.top ?? 0)
                && conDistanceFromLeft > subDistanceFromLeft
                && (conOffset?.left ?? 0) < (subOffset?.left ?? 0);
        };

        this.$tree.on("mouseenter", "span.fancytree-title", (e) => {
            e.currentTarget.title = isEnclosing(this.$tree, $(e.currentTarget)) ? "" : e.currentTarget.innerText;
        });
    }

    get hideArchivedNotes() {
        return options.is(`hideArchivedNotes_${this.treeName}`);
    }

    async toggleArchivedNotes() {
        await options.toggle(`hideArchivedNotes_${this.treeName}`);
        await this.reloadTreeFromCache();
    }

    get autoCollapseNoteTree() {
        return options.is("autoCollapseNoteTree");
    }

    get treeScrollFollowNavigation() {
        return options.is("treeScrollFollowNavigation");
    }

    /** Makes the rows compact while the `treeDensity` option is `dense`. */
    private applyDensity() {
        this.$widget.toggleClass("dense", options.get("treeDensity") === "dense");
    }

    private toggleSettings(isShown: boolean) {
        if (isShown === this.isSettingsShown) {
            return;
        }

        this.isSettingsShown = isShown;
        if (!isShown) {
            disposeReactWidget(this.settingsContainer);
            return;
        }

        renderReactWidgetAtElement(this, h(TreeSettings, {
            anchor: this.$treeActions[0],
            onHideArchivedNotesChange: () => this.reloadTreeFromCache(),
            onClose: () => this.toggleSettings(false)
        }), this.settingsContainer);
    }

    cleanup() {
        this.toggleSettings(false);
    }

    initFancyTree() {
        const treeData = [this.prepareRootNode()];

        this.$tree.fancytree({
            titlesTabbable: true,
            keyboard: true,
            toggleEffect: options.is("motionEnabled") ? undefined : false,
            extensions: ["dnd5", "clones", "filter"],
            source: treeData,
            scrollOfs: {
                top: 100,
                bottom: 100
            },
            scrollParent: this.$tree,
            minExpandLevel: 2, // root can't be collapsed
            click: (event, data): boolean => {
                this.activityDetected();

                const targetType = data.targetType;
                const node = data.node;
                const ctrlKey = utils.isCtrlKey(event);

                if (node.isSelected() && targetType === "icon") {
                    this.triggerCommand("openBulkActionsDialog", {
                        selectedOrActiveNoteIds: this.getSelectedOrActiveNoteIds(node)
                    });

                    return false;
                } else if (targetType === "title" || targetType === "icon") {
                    if (event.shiftKey && !ctrlKey) {
                        const activeNode = this.getActiveNode();

                        if (activeNode?.getParent() !== node.getParent()) {
                            return true;
                        }

                        this.clearSelectedNodes();

                        function selectInBetween(first: Fancytree.FancytreeNode, second: Fancytree.FancytreeNode) {
                            for (let i = 0; first && first !== second && i < 10000; i++) {
                                first.setSelected(true);
                                first = first.getNextSibling();
                            }

                            second.setSelected();
                        }

                        if (activeNode.getIndex() < node.getIndex()) {
                            selectInBetween(activeNode, node);
                        } else {
                            selectInBetween(node, activeNode);
                        }

                        node.setFocus(true);
                    } else if (ctrlKey) {
                        const notePath = treeService.getNotePath(node);
                        appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
                            activate: !!event.shiftKey,
                            placement: "afterCurrent"
                        });
                    } else if (event.altKey) {
                        node.setSelected(!node.isSelected());
                        node.setFocus(true);
                    } else if (data.node.isActive()) {
                        // this is important for single column mobile view, otherwise it's not possible to see again previously displayed note
                        this.tree.reactivate();
                    } else {
                        node.setActive();
                    }

                    return false;
                }

                return true;
            },
            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.
                // We solve it by hiding the hidden subtree via CSS (class "hidden-node-is-hidden"),
                // but then we need to prevent activating it, e.g. by keyboard

                if (hoistedNoteService.getHoistedNoteId() === "_hidden") {
                    // if we're hoisted in hidden subtree, we want to avoid crossing to "visible" tree,
                    // which could happen via UP key from hidden 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;
                }

                // 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) {
                    const prevActiveVisible = data.tree.options.activeVisible;
                    data.tree.options.activeVisible = false;
                    setTimeout(() => {
                        data.tree.options.activeVisible = prevActiveVisible;
                    }, 0);
                }

                return true;
            },
            activate: async (event, data) => {
                this.clearSelectedNodes();

                const notePath = treeService.getNotePath(data.node);

                const { noteContext, opts } = this.getNavigationTarget();
                if (noteContext?.viewScope?.viewMode === "contextual-help") {
                    opts.viewScope = noteContext.viewScope;
                }
                await noteContext?.setNote(notePath, opts);
            },
            expand: (event, data) => this.setExpanded(data.node.data.branchId, true),
            collapse: (event, data) => this.setExpanded(data.node.data.branchId, false),
            filter: {
                counter: false,
                mode: "hide",
                autoExpand: true
            },
            dnd5: {
                autoExpandMS: 600,
                preventLazyParents: false,
                dragStart: (node, data) => {
                    if (node.data.noteId === "root" || utils.isLaunchBarConfig(node.data.noteId) || node.data.noteId.startsWith("_options")) {
                        return false;
                    }

                    const notes = this.getSelectedOrActiveNodes(node).map((node) => ({
                        noteId: node.data.noteId,
                        branchId: node.data.branchId,
                        title: node.title
                    }));

                    if (notes.length === 1) {
                        linkService.createLink(notes[0].noteId, { referenceLink: true, autoConvertToImage: true }).then(($link) => data.dataTransfer.setData("text/html", $link[0].outerHTML));
                    } else {
                        Promise.all(notes.map((note) => linkService.createLink(note.noteId, { referenceLink: true, autoConvertToImage: true }))).then((links) => {
                            const $list = $("<ul>").append(...links.map(($link) => $("<li>").append($link)));

                            data.dataTransfer.setData("text/html", $list[0].outerHTML);
                        });
                    }

                    data.dataTransfer.setData("text", JSON.stringify(notes));
                    return true; // allow dragging to start
                },
                dragEnter: (node, data) => {
                    publishDropMarkerShift(node);

                    // Content dragged out of a text note lists no notes, and its editor deletes
                    // the content once a drop accepts it as a move.
                    if (data.dataTransfer.types.includes("application/ckeditor5-dragging-uid")) {
                        return false;
                    } else if (node.data.noteType === "search") {
                        return false;
                    } else if (node.data.noteId === "_lbRoot") {
                        return false;
                    } else if (node.data.noteId.startsWith("_options") || node.data.noteId.startsWith("_help")) {
                        return false;
                    } else if (node.data.noteId === hoistedNoteService.getHoistedNoteId()) {
                        // The hoisted note is the tree root, so it has no visible siblings to drop before/after.
                        // Only allow dropping into it to avoid the easy-to-hit "dropping not allowed" strips.
                        return ["over"];
                    } else if (node.data.noteType === "launcher") {
                        return ["before", "after"];
                    } else if (["_lbAvailableLaunchers", "_lbVisibleLaunchers"].includes(node.data.noteId)) {
                        return ["over"];
                    }
                    return true;

                },
                dragDrop: async (node, data) => {
                    if (
                        (data.hitMode === "over" && node.data.noteType === "search") ||
                        (["after", "before"].includes(data.hitMode) && (node.data.noteId === hoistedNoteService.getHoistedNoteId() || node.getParent().data.noteType === "search"))
                    ) {
                        toastService.showError(t("note_tree.dropping-not-allowed"));

                        return;
                    }

                    const dataTransfer = data.dataTransfer;

                    if (dataTransfer && dataTransfer.files && dataTransfer.files.length > 0) {
                        const files = [...dataTransfer.files]; // chrome has issue that dataTransfer.files empties after async operation

                        const importService = await import("../services/import.js");

                        importService.uploadFiles("notes", node.data.noteId, files, {
                            // Nothing was chosen here the way the import dialog makes a user choose, so
                            // let the importer recognize an archive it knows (an Obsidian vault).
                            format: "auto",
                            safeImport: true,
                            shrinkImages: true,
                            textImportedAsText: true,
                            codeImportedAsCode: true,
                            explodeArchives: true,
                            replaceUnderscoresWithSpaces: true
                        });
                    } else {
                        const jsonStr = dataTransfer.getData("text");
                        let notes: BranchRow[];

                        try {
                            notes = JSON.parse(jsonStr);
                        } catch (e) {
                            logError(`Cannot parse JSON '${jsonStr}' into notes for drop`);
                            return;
                        }

                        // Help pages are virtual notes, which the server never moves.
                        if (notes.some((note) => note.noteId?.startsWith("_help"))) {
                            toastService.showError(t("note_tree.dropping-not-allowed"));
                            return;
                        }

                        // This function MUST be defined to enable dropping of items on the tree.
                        // data.hitMode is 'before', 'after', or 'over'.

                        const selectedBranchIds = notes
                            .map((note) => note.branchId)
                            .filter((branchId) => branchId) as string[];

                        if (data.hitMode === "before") {
                            branchService.moveBeforeBranch(selectedBranchIds, node.data.branchId);
                        } else if (data.hitMode === "after") {
                            branchService.moveAfterBranch(selectedBranchIds, node.data.branchId);
                        } else if (data.hitMode === "over") {
                            branchService.moveToParentNote(selectedBranchIds, node.data.branchId, this.componentId);
                        } else {
                            throw new Error(`Unknown hitMode '${data.hitMode}'`);
                        }
                    }
                }
            },
            lazyLoad: (event, data) => {
                const { noteId, noteType } = data.node.data;

                if (noteType === "search") {
                    const notePath = treeService.getNotePath(data.node.getParent());

                    // this is a search cycle (search note is a descendant of its own search result)
                    if (notePath.includes(noteId)) {
                        data.result = [];
                        return;
                    }

                    data.result = froca
                        .loadSearchNote(noteId)
                        .then(() => {
                            const note = froca.getNoteFromCache(noteId);
                            if (!note) return [];

                            let childNoteIds = note.getChildNoteIds();

                            if (note.type === "search" && childNoteIds.length > MAX_SEARCH_RESULTS_IN_TREE) {
                                childNoteIds = childNoteIds.slice(0, MAX_SEARCH_RESULTS_IN_TREE);
                            }

                            return froca.getNotes(childNoteIds);
                        })
                        .then(() => {
                            const note = froca.getNoteFromCache(noteId);
                            if (!note) return [];

                            return this.prepareChildren(note);
                        });
                } else {
                    data.result = froca.loadSubTree(noteId).then((note) => this.prepareChildren(note));
                }
            },
            clones: {
                highlightActiveClones: true
            },
            enhanceTitle: buildEnhanceTitle(),
            // this is done to automatically lazy load all expanded notes after tree load
            loadChildren: (event, data) => {
                data.node.visit((subNode) => {
                    // Load all lazy/unloaded child nodes
                    // (which will trigger `loadChildren` recursively)
                    if (subNode.isUndefined() && subNode.isExpanded()) {
                        subNode.load();
                    }
                });
            },
            select: (event, { node }) => {
                if (hoistedNoteService.getHoistedNoteId() === "root" && node.data.noteId === "_hidden" && node.isSelected()) {
                    // hidden is hackily hidden from the tree via CSS when root is hoisted
                    // make sure it's not selected by mistake, it could be e.g. deleted by mistake otherwise
                    node.setSelected(false);

                    return;
                }

                $(node.span)
                    .find(".fancytree-custom-icon")
                    .attr("title", node.isSelected() ? "Apply bulk actions on selected notes" : "");
            }
        });

        const isMobile = utils.isMobile();

        if (isMobile) {
            let showTimeout: Timeout;

            this.$tree.on("touchstart", ".fancytree-node", (e) => {
                touchStart = new Date().getTime();
                showTimeout = setTimeout(() => {
                    this.showContextMenu(e);
                }, 300);
            });

            this.$tree.on("touchmove", ".fancytree-node", (e) => {
                clearTimeout(showTimeout);
            });

            this.$tree.on("touchend", ".fancytree-node", (e) => {
                clearTimeout(showTimeout);
            });
        } else {
            this.$tree.on("contextmenu", ".fancytree-node", (e) => {
                if (!utils.isCtrlKey(e)) {
                    this.showContextMenu(e);
                } else {
                    const node = $.ui.fancytree.getNode(e as unknown as Event);
                    const notePath = treeService.getNotePath(node);
                    appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath });
                }
                return false; // blocks default browser right click menu
            });

            this.getHotKeys().then((hotKeys) => {
                for (const key in hotKeys) {
                    const handler = hotKeys[key];

                    shortcutService.bindElShortcut($(this.tree.$container), key, () => {
                        const node = this.tree.getActiveNode();
                        return node && handler(node, {} as JQuery.KeyDownEvent);
                        // return false from the handler will stop default handling.
                    });
                }
            });
        }

        let touchStart;

        this.tree = $.ui.fancytree.getTree(this.$tree);
    }

    showContextMenu(e: PointerEvent | JQuery.TouchStartEvent | JQuery.ContextMenuEvent) {
        const node = $.ui.fancytree.getNode(e as unknown as Event);
        const note = froca.getNoteFromCache(node.data.noteId);

        if (note?.isLaunchBarConfig()) {
            import("../menus/launcher_context_menu.js").then(({ default: LauncherContextMenu }) => {
                const launcherContextMenu = new LauncherContextMenu(this, node);
                launcherContextMenu.show(e);
            });
        } else {
            import("../menus/tree_context_menu.js").then(({ default: TreeContextMenu }) => {
                const treeContextMenu = new TreeContextMenu(this, node);
                treeContextMenu.show(e);
            });
        }
    }

    prepareRootNode() {
        const branch = froca.getBranch("none_root");
        return branch && this.prepareNode(branch);
    }

    prepareChildren(parentNote: FNote) {
        utils.assertArguments(parentNote);

        const noteList: Fancytree.FancytreeNewNode[] = [];

        const hideArchivedNotes = this.hideArchivedNotes;

        let childBranches = parentNote.getFilteredChildBranches();

        if (parentNote.type === "search" && childBranches.length > MAX_SEARCH_RESULTS_IN_TREE) {
            childBranches = childBranches.slice(0, MAX_SEARCH_RESULTS_IN_TREE);
        }

        if (parentNote.isLabelTruthy("subtreeHidden")) {
            // If we have a spotlighted note path, show only the child that leads to it
            if (this.spotlightedNotePath) {
                const spotlightPathSegments = this.spotlightedNotePath.split('/');
                const parentIndex = spotlightPathSegments.indexOf(parentNote.noteId);

                if (parentIndex >= 0 && parentIndex < spotlightPathSegments.length - 1) {
                    const nextNoteIdInPath = spotlightPathSegments[parentIndex + 1];
                    childBranches = childBranches.filter(branch => branch.noteId === nextNoteIdInPath);
                } else {
                    childBranches = [];
                }
            } else {
                childBranches = [];
            }
        }

        for (const branch of childBranches) {
            if (hideArchivedNotes) {
                const note = branch.getNoteFromCache();

                if (!note || note.hasLabel("archived")) {
                    continue;
                }
            }

            const node = this.prepareNode(branch);
            if (node) {
                noteList.push(node);
            }
        }

        return noteList;
    }

    async updateNode(node: Fancytree.FancytreeNode) {
        const note = froca.getNoteFromCache(node.data.noteId);
        const branch = froca.getBranch(node.data.branchId);

        if (!note) {
            console.log(`Node update not possible because note '${node.data.noteId}' was not found.`);
            return;
        } else if (!branch) {
            console.log(`Node update not possible because branch '${node.data.branchId}' was not found.`);
            return;
        }

        node.data.isProtected = note.isProtected;
        node.data.noteType = note.type;
        node.folder = note.isFolder();
        node.icon = note.getIcon();
        node.extraClasses = this.getExtraClasses(note);
        node.title = this.getTitleHtml(note, branch);

        if (node.isExpanded() !== branch.isExpanded) {
            await node.setExpanded(branch.isExpanded, { noEvents: true, noAnimation: true });
        }

        node.renderTitle();
    }

    prepareNode(branch: FBranch, forceLazy = false) {
        const note = branch.getNoteFromCache();

        if (!note) {
            console.warn(`Branch '${branch.branchId}' has no child note '${branch.noteId}'`);
            return null;
        }

        const isFolder = note.isFolder();

        const node: Fancytree.FancytreeNewNode = {
            noteId: note.noteId,
            parentNoteId: branch.parentNoteId,
            branchId: branch.branchId,
            isProtected: note.isProtected,
            noteType: note.type,
            title: this.getTitleHtml(note, branch),
            extraClasses: this.getExtraClasses(note),
            icon: note.getIcon(),
            refKey: note.noteId,
            lazy: true,
            folder: isFolder,
            expanded: !!branch.isExpanded && note.type !== "search",
            subtreeHidden: note.isLabelTruthy("subtreeHidden"),
            key: utils.randomString(12) // this should prevent some "duplicate key" errors
        };

        if (isFolder && node.expanded && !forceLazy) {
            node.children = this.prepareChildren(note);
        }

        return node;
    }

    getTitleHtml(note: FNote, branch: FBranch) {
        const title = note.isContentAvailable()
            ? utils.escapeHtml(note.title)
            : `<span class="protected-title">${t("protected_session.locked_note_title")}</span>`;

        return branch.prefix ? `${utils.escapeHtml(branch.prefix)} - ${title}` : title;
    }

    getExtraClasses(note: FNote) {
        utils.assertArguments(note);

        const extraClasses: string[] = [];

        if (note.isProtected) {
            extraClasses.push("protected");
        }

        if (note.isShared()) {
            extraClasses.push("shared");
        }

        if (note.getRealParentNotes().length > 1) {
            extraClasses.push("multiple-parents");
        }

        const cssClass = note.getCssClass();

        if (cssClass) {
            extraClasses.push(cssClass);
        }

        extraClasses.push(utils.getNoteTypeClass(note.type));

        if (note.mime) {
            // some notes should not have mime type (e.g. render)
            extraClasses.push(utils.getMimeTypeClass(note.mime));
        }

        if (note.hasLabel("archived")) {
            extraClasses.push("archived");
        }

        const colorClass = note.getColorClass();

        if (colorClass) {
            extraClasses.push(...["tinted", colorClass]);
        }

        if (this.spotlightedNotePath && this.spotlightedNotePath.endsWith(`/${note.noteId}`)) {
            extraClasses.push("spotlighted-node");
        }

        return extraClasses.join(" ");
    }

    getSelectedNodes(stopOnParents = false) {
        return this.tree.getSelectedNodes(stopOnParents);
    }

    getSelectedOrActiveNodes(node: Fancytree.FancytreeNode | null = null) {
        const nodes = this.getSelectedNodes(true);

        // the node you start dragging should be included even if not selected
        if (node && !nodes.find((n) => n.key === node.key)) {
            nodes.push(node);
        }

        const activeNode = this.getActiveNode();
        if (nodes.length === 0 && activeNode) {
            nodes.push(activeNode);
        }

        // hidden subtree is hackily hidden via CSS when hoisted to root
        // make sure it's never selected for e.g. deletion in such a case
        return nodes.filter((node) => hoistedNoteService.getHoistedNoteId() !== "root" || node.data.noteId !== "_hidden");
    }

    async setExpandedStatusForSubtree(node: Fancytree.FancytreeNode | null, isExpanded: boolean) {
        if (!node) {
            const hoistedNoteId = hoistedNoteService.getHoistedNoteId();

            node = this.getNodesByNoteId(hoistedNoteId)[0];
        }

        const { branchIds } = await server.put<ExpandedSubtreeResponse>(`branches/${node.data.branchId}/expanded-subtree/${isExpanded ? 1 : 0}`);

        froca.getBranches(branchIds, true).forEach((branch) => (branch.isExpanded = !!isExpanded));

        await this.batchUpdate(async () => {
            await node.load(true);

            if (node.data.noteId !== hoistedNoteService.getHoistedNoteId()) {
                // hoisted note should always be expanded
                await node.setExpanded(isExpanded, { noEvents: true, noAnimation: true });
            }
        });

        await this.filterHoistedBranch(true);

        // don't activate the active note, see discussion in https://github.com/zadam/trilium/issues/3664
    }

    async expandTree(node: Fancytree.FancytreeNode | null = null) {
        await this.setExpandedStatusForSubtree(node, true);
    }

    async collapseTree(node: Fancytree.FancytreeNode | null = null) {
        await this.setExpandedStatusForSubtree(node, false);
    }

    collapseTreeEvent() {
        this.collapseTree();
    }

    getActiveNode() {
        return this.tree.getActiveNode();
    }

    /**
     * focused & not active node can happen during multiselection where the node is selected
     * but not activated (its content is not displayed in the detail)
     * @returns {FancytreeNode|null}
     */
    getFocusedNode() {
        return this.tree.getFocusNode();
    }

    clearSelectedNodes() {
        for (const selectedNode of this.getSelectedNodes()) {
            selectedNode.setSelected(false);
        }
    }

    async scrollToActiveNoteEvent() {
        const activeContext = appContext.tabManager.getActiveContext();

        if (activeContext && activeContext.notePath) {
            this.tree.$container.focus();
            this.tree.setFocus(true);

            const node = await this.expandToNote(activeContext.notePath);

            if (node) {
                await node.makeVisible({ scrollIntoView: true });
                node.setActive(true, { noEvents: true, noFocus: false });
            }
        }
    }

    async focusTreeEvent() {
        this.tree.$container.focus();
        this.tree.setFocus(true);
    }

    async getNodeFromPath(notePath: string, expand = false, logErrors = true) {
        utils.assertArguments(notePath);
        /** @let {FancytreeNode} */
        let parentNode = this.getNodesByNoteId("root")[0];

        let resolvedNotePathSegments = await treeService.resolveNotePathToSegments(notePath, this.hoistedNoteId, logErrors);

        if (!resolvedNotePathSegments) {
            if (logErrors) {
                logError("Could not find run path for notePath:", notePath);
            }

            return;
        }

        resolvedNotePathSegments = resolvedNotePathSegments.slice(1);

        for (const childNoteId of resolvedNotePathSegments) {
            // we expand only after hoisted note since before then nodes are not actually present in the tree
            if (parentNode) {
                if (!parentNode.isLoaded()) {
                    await parentNode.load();
                }

                if (expand) {
                    if (!parentNode.isExpanded()) {
                        await parentNode.setExpanded(true, { noAnimation: true });
                    }

                    // although the previous line should set the expanded status, it seems to happen asynchronously,
                    // so we need to make sure it is set properly before calling updateNode which uses this flag
                    const branch = froca.getBranch(parentNode.data.branchId);
                    if (branch) {
                        branch.isExpanded = true;
                    }
                }

                await this.updateNode(parentNode);

                let foundChildNode = this.findChildNode(parentNode, childNoteId);

                if (!foundChildNode) {
                    // note might be recently created, so we'll force reload and try again
                    await parentNode.load(true);

                    foundChildNode = this.findChildNode(parentNode, childNoteId);

                    if (!foundChildNode) {
                        const childNote = await froca.getNote(childNoteId);

                        if (childNote?.type === "image") return;

                        // The child note can be part of a note with #subtreeHidden, case in which we need to "spotlight" it.
                        const parentNote = froca.getNoteFromCache(parentNode.data.noteId);
                        if (parentNote?.isLabelTruthy("subtreeHidden")) {
                            // Enable spotlight mode and reload the parent to show only the path to this note
                            this.spotlightedNotePath = notePath;
                            await parentNode.load(true);

                            // Try to find the child again after reload
                            foundChildNode = this.findChildNode(parentNode, childNoteId);
                            this.spotlightedNode = foundChildNode ?? null;

                            if (!foundChildNode) {
                                if (logErrors || !childNote) {
                                    ws.logError(
                                        `Can't find node for child node of noteId=${childNoteId} for parent of noteId=${parentNode.data.noteId} and hoistedNoteId=${hoistedNoteService.getHoistedNoteId()}, requested path is ${notePath}`
                                    );
                                    return;
                                }
                                return;
                            }

                            parentNode = foundChildNode;
                            continue;
                        }

                        // besides real errors, this can be also caused by hiding of e.g. included images
                        // these are real notes with real notePath, user can display them in a detail,
                        // but they don't have a node in the tree
                        if (logErrors || !childNote) {
                            ws.logError(
                                `Can't find node for child node of noteId=${childNoteId} for parent of noteId=${parentNode.data.noteId} and hoistedNoteId=${hoistedNoteService.getHoistedNoteId()}, requested path is ${notePath}`
                            );
                            return;
                        }

                        return;
                    }
                }

                parentNode = foundChildNode;
            }
        }

        return parentNode;
    }

    findChildNode(parentNode: Fancytree.FancytreeNode, childNoteId: string) {
        return parentNode.getChildren()?.find((childNode) => childNode.data.noteId === childNoteId);
    }

    async expandToNote(notePath: string, logErrors = true) {
        return this.getNodeFromPath(notePath, true, logErrors);
    }

    getNodesByBranch(branch: BranchRow) {
        utils.assertArguments(branch);

        if (!branch.noteId) {
            return [];
        }

        return this.getNodesByNoteId(branch.noteId).filter((node) => node.data.branchId === branch.branchId);
    }

    getNodesByNoteId(noteId: string) {
        utils.assertArguments(noteId);

        const list = this.tree.getNodesByRef(noteId);
        return list ? list : []; // if no nodes with this refKey are found, fancy tree returns null
    }

    isEnabled() {
        return !!this.noteContext;
    }

    async refresh() {
        this.toggleInt(this.isEnabled());
        this.toggleSettings(false);

        this.activityDetected();

        const oldActiveNode = this.getActiveNode();

        const newActiveNode =
            this.noteContext?.notePath &&
            // Pass this tree's own hoisted note (e.g. a popup hoisted into the hidden subtree) rather
            // than the active tab's — otherwise a hidden-subtree note never gets an active node here.
            (!treeService.isNotePathInHiddenSubtree(this.noteContext.notePath) || (await hoistedNoteService.isHoistedInHiddenSubtree(this.hoistedNoteId))) &&
            (await this.getNodeFromPath(this.noteContext.notePath));

        if (this.spotlightedNode && newActiveNode !== this.spotlightedNode) {
            // Can get removed when switching to another note in a spotlighted subtree.
            if (this.spotlightedNode.parent) {
                this.spotlightedNode.remove();
            }
            this.spotlightedNode = null;
            this.spotlightedNotePath = null;
        }

        if (newActiveNode !== oldActiveNode) {
            let oldActiveNodeFocused = false;

            if (oldActiveNode) {
                oldActiveNodeFocused = oldActiveNode.hasFocus();
                oldActiveNode.setActive(false);
                oldActiveNode.setFocus(false);
            }

            if (newActiveNode) {
                if (this.treeScrollFollowNavigation && !newActiveNode.isVisible() && this.noteContext?.notePath) {
                    await this.expandToNote(this.noteContext.notePath);
                }

                if (!this.treeScrollFollowNavigation) {
                    this.tree.options.activeVisible = false;
                    setTimeout(() => {
                        this.tree.options.activeVisible = true;
                    }, 0);
                }

                newActiveNode.setActive(true, {
                    noEvents: true,
                    noFocus: !oldActiveNodeFocused,
                    scrollIntoView: this.treeScrollFollowNavigation,
                });
            }
        }

        this.filterHoistedBranch(false);
    }

    async refreshSearch(e: JQuery.MouseDownEvent) {
        const activeNode = $.ui.fancytree.getNode(e as unknown as Event);

        activeNode.load(true);
        activeNode.setExpanded(true, { noAnimation: true });

        toastService.showMessage(t("note_tree.saved-search-note-refreshed"));
    }

    async batchUpdate(cb: () => Promise<void>) {
        try {
            // disable rendering during update for increased performance
            this.tree.enableUpdate(false);

            await cb();
        } finally {
            this.tree.enableUpdate(true);
        }
    }

    activityDetected() {
        if (this.autoCollapseTimeoutId) {
            clearTimeout(this.autoCollapseTimeoutId);
        }

        this.autoCollapseTimeoutId = setTimeout(() => {
            if (!this.autoCollapseNoteTree) {
                return;
            }

            /*
             * We're collapsing notes after a period of inactivity to "cleanup" the tree - users rarely
             * collapse the notes and the tree becomes unusuably large.
             * Some context: https://github.com/zadam/trilium/issues/1192
             */

            const noteIdsToKeepExpanded = new Set(
                appContext.tabManager
                    .getNoteContexts()
                    .map((nc) => nc.notePathArray)
                    .flat()
            );

            let noneCollapsedYet = true;

            if (!options.is("databaseReadonly")) {
                // can't change expanded notes when database is readonly
                this.tree.getRootNode().visit((node) => {
                    if (node.isExpanded() && !noteIdsToKeepExpanded.has(node.data.noteId)) {
                        node.setExpanded(false);

                        if (noneCollapsedYet) {
                            toastService.showMessage(t("note_tree.auto-collapsing-notes-after-inactivity"));
                            noneCollapsedYet = false;
                        }
                    }
                }, false);
            }

            this.filterHoistedBranch(true);
        }, 600 * 1000);
    }

    async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
        this.activityDetected();

        if (loadResults.isOptionReloaded("treeDensity")) {
            this.applyDensity();
        }

        if (loadResults.isEmptyForTree()) {
            return;
        }

        const activeNode = this.getActiveNode();
        const activeNodeFocused = !!activeNode?.hasFocus();
        const activeNotePath = activeNode ? treeService.getNotePath(activeNode) : null;

        const refreshCtx: RefreshContext = {
            noteIdsToUpdate: new Set(),
            noteIdsToReload: new Set()
        };

        this.#processAttributeRows(loadResults.getAttributeRows(), refreshCtx);

        const branchRows = loadResults.getBranchRows();
        const { movedActiveNode, parentsOfAddedNodes } = await this.#processBranchRows(branchRows, refreshCtx);

        for (const noteId of loadResults.getNoteIds()) {
            const contentReloaded = loadResults.isNoteContentReloaded(noteId);
            if (contentReloaded && !loadResults.isNoteReloaded(noteId, contentReloaded.componentId)) {
                // Only the note content was reloaded, not the note itself. This would cause a redundant update on every few seconds while editing a note.
                continue;
            }

            refreshCtx.noteIdsToUpdate.add(noteId);
        }

        const hasNotesToUpdateOrReload = refreshCtx.noteIdsToUpdate.size + refreshCtx.noteIdsToReload.size > 0;
        const hasNoteReorderingChange = loadResults.getNoteReorderings().length > 0;
        if (hasNotesToUpdateOrReload || hasNoteReorderingChange) {
            await this.#executeTreeUpdates(refreshCtx, loadResults);
        }

        await this.#setActiveNode(activeNotePath, activeNodeFocused, movedActiveNode, parentsOfAddedNodes);

        if (refreshCtx.noteIdsToReload.size > 0 || refreshCtx.noteIdsToUpdate.size > 0) {
            // workaround for https://github.com/mar10/fancytree/issues/1054
            this.filterHoistedBranch(true);
        }
    }

    #processAttributeRows(attributeRows: AttributeRow[], refreshCtx: RefreshContext) {
        for (const attrRow of attributeRows) {
            // `geolocation`, `mapPosition` and `startDate` decide the icon of a note that has none
            // of its own (see getNoteIcon).
            const dirtyingLabels = ["iconClass", "cssClass", "workspace", "workspaceIconClass",
                "color", GEO_LOCATION_ATTRIBUTE, MAP_POSITION_ATTRIBUTE, "startDate"];

            if (attrRow.type === "label" && dirtyingLabels.includes(attrRow.name ?? "") && attrRow.noteId) {
                if (attrRow.isInheritable) {
                    refreshCtx.noteIdsToReload.add(attrRow.noteId);
                } else {
                    refreshCtx.noteIdsToUpdate.add(attrRow.noteId);
                }
            } else if (attrRow.type === "label" && (attrRow.name === "archived" || attrRow.name === "subtreeHidden") && attrRow.noteId) {
                const note = froca.getNoteFromCache(attrRow.noteId);

                if (note) {
                    // change of archived status can mean the note should not be displayed in the tree at all
                    // depending on the value of this.hideArchivedNotes
                    for (const parentNote of note.getParentNotes()) {
                        refreshCtx.noteIdsToReload.add(parentNote.noteId);
                    }
                }
            } else if (attrRow.type === "relation" && (attrRow.name === "template" || attrRow.name === "inherit") && attrRow.noteId) {
                // missing handling of things inherited from template
                refreshCtx.noteIdsToReload.add(attrRow.noteId);
            } else if (attrRow.type === "relation" && attrRow.name === "imageLink" && attrRow.noteId) {
                const note = froca.getNoteFromCache(attrRow.noteId);

                if (note && note.getChildNoteIds().includes(attrRow.value ?? "")) {
                    // there's a new /deleted imageLink between note and its image child - which can show/hide
                    // the image (if there is an imageLink relation between parent and child,
                    // then it is assumed to be "contained" in the note and thus does not have to be displayed in the tree)
                    refreshCtx.noteIdsToReload.add(attrRow.noteId);
                }
            }
        }
    }

    async #processBranchRows(branchRows: BranchRow[], refreshCtx: RefreshContext) {
        const allBranchesDeleted = branchRows.every((branchRow) => !!branchRow.isDeleted);

        // activeNode is supposed to be moved when we find out activeNode is deleted but not all branches are deleted. save it for fixing activeNodePath after all nodes loaded.
        let movedActiveNode: Fancytree.FancytreeNode | null = null;
        const parentsOfAddedNodes: Fancytree.FancytreeNode[] = [];

        for (const branchRow of branchRows) {
            if (branchRow.noteId) {
                if (branchRow.parentNoteId === "_share") {
                    // all shared notes have a sign in the tree, even the descendants of shared notes
                    refreshCtx.noteIdsToReload.add(branchRow.noteId);
                } else {
                    // adding noteId itself to update all potential clones
                    refreshCtx.noteIdsToUpdate.add(branchRow.noteId);
                }
            }

            if (branchRow.isDeleted) {
                for (const node of this.getNodesByBranch(branchRow)) {
                    if (node.isActive()) {
                        if (allBranchesDeleted) {
                            // `_hidden` stays in the tree under root, hidden only by CSS.
                            const newActiveNode = [
                                node.getNextSibling(), node.getPrevSibling(), node.getParent()
                            ].find((candidate) => candidate && candidate.data.noteId !== "_hidden");

                            if (newActiveNode) {
                                newActiveNode.setActive(true, { noEvents: true, noFocus: true });
                            }
                        } else {
                            movedActiveNode = node;
                        }
                    }

                    if (node.getParent()) {
                        node.remove();
                    }

                    if (branchRow.parentNoteId) {
                        refreshCtx.noteIdsToUpdate.add(branchRow.parentNoteId);
                    }
                }
            } else if (branchRow.parentNoteId) {
                for (const parentNode of this.getNodesByNoteId(branchRow.parentNoteId)) {
                    parentsOfAddedNodes.push(parentNode);

                    if (!branchRow.noteId || (parentNode.isFolder() && !parentNode.isLoaded())) {
                        continue;
                    }

                    const note = await froca.getNote(branchRow.noteId);
                    const frocaBranch = branchRow.branchId ? froca.getBranch(branchRow.branchId) : null;
                    const foundNode = (parentNode.getChildren() || []).find((child) => child.data.noteId === branchRow.noteId);
                    if (foundNode) {
                        // the branch already exists in the tree
                        if (branchRow.isExpanded !== foundNode.isExpanded() && frocaBranch) {
                            refreshCtx.noteIdsToReload.add(frocaBranch.noteId);
                        }
                    } else if (frocaBranch) {
                        // make sure it's loaded
                        // we're forcing lazy since it's not clear if the whole required subtree is in froca
                        if (!parentNode.data.subtreeHidden) {
                            const newNode = this.prepareNode(frocaBranch, true);
                            if (newNode) {
                                parentNode.addChildren([newNode]);
                            }

                            if (frocaBranch?.isExpanded && note && note.hasChildren()) {
                                refreshCtx.noteIdsToReload.add(frocaBranch.noteId);
                            }

                            this.sortChildren(parentNode);
                        } else if (branchRow.componentId === this.componentId) {
                            // Display the toast and focus to parent note only if we know for sure that the operation comes from the tree.
                            const parentNote = froca.getNoteFromCache(parentNode.data.noteId || "");
                            toastService.showPersistent({
                                id: `subtree-hidden-moved`,
                                title: t("note_tree.subtree-hidden-moved-title", { title: parentNote?.title }),
                                message: parentNote?.type === "book"
                                    ? t("note_tree.subtree-hidden-moved-description-collection")
                                    : t("note_tree.subtree-hidden-moved-description-other"),
                                icon: "bx bx-hide",
                                timeout: 5_000,
                            });
                            parentNode.setActive(true);
                        }

                        // this might be a first child which would force an icon change
                        // also update the count if the subtree is hidden.
                        if (branchRow.parentNoteId) {
                            refreshCtx.noteIdsToUpdate.add(branchRow.parentNoteId);
                        }
                    }
                }
            }
        }

        return {
            movedActiveNode,
            parentsOfAddedNodes
        };
    }

    async #executeTreeUpdates(refreshCtx: RefreshContext, loadResults: LoadResults) {
        const performUpdates = async () => {
            for (const noteId of refreshCtx.noteIdsToReload) {
                for (const node of this.getNodesByNoteId(noteId)) {
                    await node.load(true);

                    refreshCtx.noteIdsToUpdate.add(noteId);
                }
            }

            for (const parentNoteId of loadResults.getNoteReorderings()) {
                for (const node of this.getNodesByNoteId(parentNoteId)) {
                    console.log("Reordering ", node);
                    if (node.isLoaded()) {
                        this.sortChildren(node);
                    }
                }
            }
        };

        if (refreshCtx.noteIdsToReload.size + refreshCtx.noteIdsToUpdate.size >= BATCH_UPDATE_THRESHOLD) {
            /**
             * Batch updates are used for large number of updates to prevent multiple re-renders, however in the context of small updates (such as changing a note title)
             * it can cause up to 400ms of delay for ~8k notes which is not acceptable. Therefore we use batching only for larger number of updates.
             * Without batching, the updates would take a couple of milliseconds.
             * We still keep the batching for potential cases where there are many updates, for example in a sync.
             */
            await this.batchUpdate(performUpdates);
        } else {
            await performUpdates();
        }

        // for some reason, node update cannot be in the batchUpdate() block (node is not re-rendered)
        for (const noteId of refreshCtx.noteIdsToUpdate) {
            for (const node of this.getNodesByNoteId(noteId)) {
                await this.updateNode(node);
            }
        }
    }

    async #setActiveNode(activeNotePath: string | null, activeNodeFocused: boolean, movedActiveNode: Fancytree.FancytreeNode | null, parentsOfAddedNodes: Fancytree.FancytreeNode[]) {
        if (movedActiveNode) {
            for (const parentNode of parentsOfAddedNodes) {
                const foundNode = (parentNode.getChildren() || []).find((child) => child.data.noteId === movedActiveNode.data.noteId);
                if (foundNode) {
                    activeNotePath = treeService.getNotePath(foundNode);
                    break;
                }
            }
        }

        if (!activeNotePath) {
            return;
        }

        const follow = this.treeScrollFollowNavigation;
        let node: Fancytree.FancytreeNode | null | undefined = await this.getNodeFromPath(activeNotePath, follow, false);

        if (node && node.data.noteId !== treeService.getNoteIdFromUrl(activeNotePath)) {
            // if the active note has been moved elsewhere then it won't be found by the path,
            // so we switch to the alternative of trying to find it by noteId
            const noteId = treeService.getNoteIdFromUrl(activeNotePath);

            if (noteId) {
                const notesById = this.getNodesByNoteId(noteId);

                // if there are multiple clones, then we'd rather not activate anyone
                node = notesById.length === 1 ? notesById[0] : null;
            }
        }

        if (!node) {
            return;
        }

        if (activeNodeFocused) {
            // needed by Firefox: https://github.com/zadam/trilium/issues/1865
            this.tree.$container.focus();
        }

        // fancytree's `activeVisible` expands the ancestors of a node it activates.
        const prevActiveVisible = this.tree.options.activeVisible;
        this.tree.options.activeVisible = follow;
        try {
            await node.setActive(true, { noEvents: true, noFocus: !activeNodeFocused });
        } finally {
            this.tree.options.activeVisible = prevActiveVisible;
        }
    }

    sortChildren(node: Fancytree.FancytreeNode) {
        node.sortChildren((nodeA, nodeB) => {
            const branchA = froca.branches[nodeA.data.branchId];
            const branchB = froca.branches[nodeB.data.branchId];

            if (!branchA || !branchB) {
                return 0;
            }

            return branchA.notePosition - branchB.notePosition;
        });
    }

    setExpanded(branchId: string, isExpanded: boolean) {
        utils.assertArguments(branchId);

        const branch = froca.getBranch(branchId, true);

        if (!branch) {
            if (branchId && branchId.startsWith("virt")) {
                // in case of virtual branches there's nothing to update
                return;
            }
            logError(`Cannot find branch=${branchId}`);
            return;

        }

        branch.isExpanded = isExpanded;

        server.put(`branches/${branchId}/expanded/${isExpanded ? 1 : 0}`);
    }

    async reloadTreeFromCache() {
        const activeNode = this.getActiveNode();

        const activeNotePath = activeNode !== null ? treeService.getNotePath(activeNode) : null;

        const rootNode = this.prepareRootNode();

        await this.batchUpdate(async () => {
            await this.tree.reload([rootNode]);
        });

        await this.filterHoistedBranch(true);

        if (activeNotePath) {
            const node = await this.getNodeFromPath(activeNotePath, true);

            if (node) {
                await node.setActive(true, { noEvents: true, noFocus: true });
            }
        }
    }

    async hoistedNoteChangedEvent({ ntxId }: EventData<"hoistedNoteChanged">) {
        if (this.isNoteContext(ntxId)) {
            await this.filterHoistedBranch(true);
        }
    }

    async filterHoistedBranch(forceUpdate = false) {
        if (!this.noteContext) {
            return;
        }

        const hoistedNotePath = await treeService.resolveNotePath(this.noteContext.hoistedNoteId);
        const hoistChanged = this.lastFilteredHoistedNotePath !== hoistedNotePath;

        if (!forceUpdate && !hoistChanged) {
            // Hoisting did not change, so skip the expensive re-filter (avoids flickering on
            // simple note changes with large subtrees). The hidden-node class must still be
            // reapplied — the <li> may have been recreated by a lazy reload (e.g. via
            // `getNodeFromPath` → `parentNode.load(true)` after an import into root).
            this.toggleHiddenNode(this.noteContext.hoistedNoteId !== "root");
            return;
        }

        this.lastFilteredHoistedNotePath = hoistedNotePath;

        if (hoistedNotePath) {
            await this.getNodeFromPath(hoistedNotePath);
        }

        if (this.noteContext.hoistedNoteId === "root") {
            this.tree.clearFilter();
            this.toggleHiddenNode(false); // show everything but the hidden subtree
        } else {
            // hack when hoisted note is cloned then it could be filtered multiple times while we want only 1
            this.tree.filterBranches(
                (node) =>
                    node.data.noteId === this.noteContext?.hoistedNoteId && // optimization to not having always resolve the node path
                    treeService.getNotePath(node) === hoistedNotePath
            );

            this.toggleHiddenNode(true); // hoisting will handle hidden note visibility

            // Automatically expand the hoisted note by default
            const node = this.getActiveNode();
            if (node && node.data.noteId === this.noteContext.hoistedNoteId){
                this.setExpanded(node.data.branchId, true);
            }
        }

        // Re-applying the filter (clearFilter / filterBranches) rebuilds the visible node set and
        // resets the tree's scroll position to the top. When the hoisted note actually changed —
        // e.g. switching between tabs belonging to different workspaces — bring the active note
        // back into view so the tree doesn't jump to the top. We deliberately skip this when the
        // hoist is unchanged (frequent forceUpdate re-filters during editing/sync) to avoid
        // yanking the tree away from wherever the user scrolled.
        if (hoistChanged) {
            this.getActiveNode()?.makeVisible({ scrollIntoView: true });
        }
    }

    toggleHiddenNode(show: boolean) {
        const hiddenNode = this.getNodesByNoteId("_hidden")[0];
        if (hiddenNode?.li) {
            $(hiddenNode.li).toggleClass("hidden-node-is-hidden", !show);
        }
    }

    async frocaReloadedEvent() {
        this.reloadTreeFromCache();
    }

    async getHotKeys() {
        const actions = await keyboardActionsService.getActionsForScope("note-tree");
        const hotKeyMap: Record<string, (node: Fancytree.FancytreeNode, e: JQuery.KeyDownEvent) => boolean> = {};

        for (const action of actions) {
            for (const shortcut of action.effectiveShortcuts ?? []) {
                hotKeyMap[shortcutService.normalizeShortcut(shortcut)] = (node) => {
                    const notePath = treeService.getNotePath(node);

                    this.triggerCommand(action.actionName, {
                        node,
                        notePath,
                        noteId: node.data.noteId,
                        selectedOrActiveBranchIds: this.getSelectedOrActiveBranchIds(node),
                        selectedOrActiveNoteIds: this.getSelectedOrActiveNoteIds(node)
                    });

                    return false;
                };
            }
        }

        return hotKeyMap;
    }

    getSelectedOrActiveBranchIds(node: Fancytree.FancytreeNode) {
        const nodes = this.getSelectedOrActiveNodes(node);

        return nodes.map((node) => node.data.branchId);
    }

    getSelectedOrActiveNoteIds(node: Fancytree.FancytreeNode): string[] {
        const nodes = this.getSelectedOrActiveNodes(node);

        return nodes.map((node) => node.data.noteId);
    }

    canBeMovedUpOrDown(node: Fancytree.FancytreeNode) {
        if (node.data.noteId === "root") {
            return false;
        }

        const parentNote = froca.getNoteFromCache(node.getParent().data.noteId);

        return !parentNote?.hasLabel("sorted");
    }

    async moveNoteUpCommand({ node }: CommandListenerData<"moveNoteUp">) {
        if (!node || !this.canBeMovedUpOrDown(node)) {
            return;
        }

        const beforeNode = node.getPrevSibling();

        if (beforeNode !== null) {
            await branchService.moveBeforeBranch([node.data.branchId], beforeNode.data.branchId);
            node.makeVisible({ scrollIntoView: true });
        }
    }

    async moveNoteDownCommand({ node }: CommandListenerData<"moveNoteDown">) {
        if (!node || !this.canBeMovedUpOrDown(node)) {
            return;
        }

        const afterNode = node.getNextSibling();

        if (afterNode !== null) {
            await branchService.moveAfterBranch([node.data.branchId], afterNode.data.branchId);
            node.makeVisible({ scrollIntoView: true });
        }
    }

    moveNoteUpInHierarchyCommand({ node }: CommandListenerData<"moveNoteUpInHierarchy">) {
        if (!node) return;
        branchService.moveNodeUpInHierarchy(node);
    }

    moveNoteDownInHierarchyCommand({ node }: CommandListenerData<"moveNoteDownInHierarchy">) {
        if (!node) return;
        const toNode = node.getPrevSibling();

        if (toNode !== null) {
            branchService.moveToParentNote([node.data.branchId], toNode.data.branchId, this.componentId);
        }
    }

    addNoteAboveToSelectionCommand() {
        const node = this.getFocusedNode();

        if (!node) {
            return;
        }

        if (node.isActive()) {
            node.setSelected(true);
        }

        const prevSibling = node.getPrevSibling();

        if (prevSibling) {
            prevSibling.setActive(true, { noEvents: true });

            if (prevSibling.isSelected()) {
                node.setSelected(false);
            }

            prevSibling.setSelected(true);
        }
    }

    addNoteBelowToSelectionCommand() {
        const node = this.getFocusedNode();

        if (!node) {
            return;
        }

        if (node.isActive()) {
            node.setSelected(true);
        }

        const nextSibling = node.getNextSibling();

        if (nextSibling) {
            nextSibling.setActive(true, { noEvents: true });

            if (nextSibling.isSelected()) {
                node.setSelected(false);
            }

            nextSibling.setSelected(true);
        }
    }

    expandSubtreeCommand({ node }: CommandListenerData<"expandSubtree">) {
        this.expandTree(node);
    }

    collapseSubtreeCommand({ node }: CommandListenerData<"collapseSubtree">) {
        this.collapseTree(node);
    }

    selectAllNotesInParentCommand({ node }: CommandListenerData<"selectAllNotesInParent">) {
        if (!node) return;
        for (const child of node.getParent().getChildren()) {
            child.setSelected(true);
        }
    }

    editNoteTitleCommand() {
        appContext.triggerCommand("focusOnTitle");
    }

    moveLauncherToVisibleCommand({ selectedOrActiveBranchIds }: CommandListenerData<"moveLauncherToVisible">) {
        this.#moveLaunchers(selectedOrActiveBranchIds, "_lbVisibleLaunchers", "_lbMobileVisibleLaunchers");
    }

    moveLauncherToAvailableCommand({ selectedOrActiveBranchIds }: CommandListenerData<"moveLauncherToAvailable">) {
        this.#moveLaunchers(selectedOrActiveBranchIds, "_lbAvailableLaunchers", "_lbMobileAvailableLaunchers");
    }

    #moveLaunchers(selectedOrActiveBranchIds: string[], desktopParent: string, mobileParent: string) {
        const desktopLaunchersToMove = selectedOrActiveBranchIds.filter((branchId) => !branchId.startsWith("_lbMobile"));
        if (desktopLaunchersToMove) {
            branchService.moveToParentNote(desktopLaunchersToMove, `_lbRoot_${  desktopParent}`, this.componentId);
        }

        const mobileLaunchersToMove = selectedOrActiveBranchIds.filter((branchId) => branchId.startsWith("_lbMobile"));
        if (mobileLaunchersToMove) {
            branchService.moveToParentNote(mobileLaunchersToMove, `_lbMobileRoot_${mobileParent}`, this.componentId);
        }
    }

    addNoteLauncherCommand({ node }: CommandListenerData<"addNoteLauncher">) {
        if (!node) return;
        this.createLauncherNote(node, "note");
    }

    addScriptLauncherCommand({ node }: CommandListenerData<"addScriptLauncher">) {
        if (!node) return;
        this.createLauncherNote(node, "script");
    }

    addWidgetLauncherCommand({ node }: CommandListenerData<"addWidgetLauncher">) {
        if (!node) return;
        this.createLauncherNote(node, "customWidget");
    }

    addSpacerLauncherCommand({ node }: CommandListenerData<"addSpacerLauncher">) {
        if (!node) return;
        this.createLauncherNote(node, "spacer");
    }

    async createLauncherNote(node: Fancytree.FancytreeNode, launcherType: LauncherType) {
        const resp = await server.post<CreateLauncherResponse>(`special-notes/launchers/${node.data.noteId}/${launcherType}`);

        if (!resp.success) {
            toastService.showError(resp.message);
        }

        await ws.waitForMaxKnownEntityChangeId();

        const { noteContext, opts } = this.getNavigationTarget();
        await noteContext?.setNote(resp.note.noteId, opts);
    }

    /** Returns the tree's own note context (or the active one), with `keepActiveDialog` set when they differ. */
    private getNavigationTarget() {
        const activeContext = appContext.tabManager.getActiveContext();
        const noteContext = this.noteContext ?? activeContext;
        const opts: SetNoteOpts = {};
        if (noteContext && noteContext !== activeContext) {
            opts.keepActiveDialog = true;
        }
        return { noteContext, opts };
    }

}

function buildEnhanceTitle() {
    const createChildTemplate = document.createElement("span");
    createChildTemplate.className = "tree-item-button tn-icon add-note-button bx bx-plus";
    createChildTemplate.title = t("note_tree.create-child-note");

    return async function enhanceTitle(event: Event,
        data: {
            node: Fancytree.FancytreeNode;
            noteId: string;
        }) {
        const node = data.node;

        if (!node.data.noteId) {
            // if there's "non-note" node, then don't enhance
            // this can happen for e.g. "Load error!" node
            return;
        }

        const note = froca.getNoteFromCache(node.data.noteId);
        if (!note) return;

        const activeNoteContext = appContext.tabManager.getActiveContext();

        const $span = $(node.span);

        $span.find(".tree-item-button").remove();
        $span.find(".note-indicator-icon").remove();

        const isHoistedNote = activeNoteContext && activeNoteContext.hoistedNoteId === note.noteId && note.noteId !== "root";

        if (note.hasLabel("workspace") && !isHoistedNote) {
            const $enterWorkspaceButton = $(`<span class="tree-item-button tn-icon enter-workspace-button bx bx-door-open" title="${t("note_tree.hoist-this-note-workspace")}"></span>`).on(
                "click",
                cancelClickPropagation
            );

            $span.append($enterWorkspaceButton);
        }

        if (note.type === "search") {
            const $refreshSearchButton = $(`<span class="tree-item-button tn-icon refresh-search-button bx bx-refresh" title="${t("note_tree.refresh-saved-search-results")}"></span>`).on(
                "click",
                cancelClickPropagation
            );

            $span.append($refreshSearchButton);
        }

        // TODO: Deduplicate with server's notes.ts#getAndValidateParent
        const isSubtreeHidden = note.isLabelTruthy("subtreeHidden");
        if (!["search", "launcher"].includes(note.type)
            && !note.isOptions()
            && !note.isLaunchBarConfig()
            && !note.noteId.startsWith("_help")
            && !isSubtreeHidden
            && !node.extraClasses.includes("spotlighted-node")
        ) {
            const createChildItem = createChildTemplate.cloneNode();
            createChildItem.addEventListener("click", cancelClickPropagation);
            node.span.append(createChildItem);
        }

        if (isHoistedNote) {
            const $unhoistButton = $(`<span class="tree-item-button tn-icon unhoist-button bx bx-door-open" title="${t("note_tree.unhoist")}"></span>`).on("click", cancelClickPropagation);

            $span.append($unhoistButton);
        }

        // Add clone indicator with tooltip if note has multiple parents
        const realParents = note.getRealParentNotes();
        if (realParents.length > 1) {
            const tooltipText = treeService.getCloneIndicatorTooltip(realParents);

            const $cloneIndicator = $(`<span class="note-indicator-icon clone-indicator"></span>`);
            $cloneIndicator.attr("title", tooltipText);
            $span.find(".fancytree-title").append($cloneIndicator);
        }

        // Add shared indicator with tooltip if note is shared
        if (note.isShared()) {
            const shareId = note.getOwnedLabelValue("shareAlias") || note.noteId;
            // Pass no sync host to preserve this tooltip's prior local-origin behavior; the helper
            // still substitutes the loopback origin for the trilium-app:// desktop renderer (#10589).
            const shareUrl = buildShareLink(shareId, undefined);
            const tooltipText = t("note_tree.shared-indicator-tooltip-with-url", { url: shareUrl });

            const $sharedIndicator = $(`<span class="note-indicator-icon shared-indicator"></span>`);
            $sharedIndicator.attr("title", tooltipText);
            $span.find(".fancytree-title").append($sharedIndicator);
        }

        // Add a badge with the number of items if it hides children.
        const count = note.getChildNoteIds().length;
        if (isSubtreeHidden && count > 0) {
            const $badge = $(`<span class="note-indicator-icon subtree-hidden-badge">${count}</span>`);
            $badge.attr("title", t("note_tree.subtree-hidden-tooltip", { count }));
            $span.append($badge);
        }
    };
}

/**
 * Tells the drop marker how far it sits from the boundary between two rows, in pixels.
 *
 * dnd5 anchors the marker on the target *title* — its bottom edge for "after", its top edge for
 * "before" — and those are half the row's spare height either side of the boundary the note
 * actually lands on. Left alone, dropping between two notes draws the line in one of two places
 * depending on which of them the pointer is over.
 *
 * Measured rather than written down: the distance is the row height less the title's line box, and
 * both follow whichever family and size the tree font names.
 */
export function publishDropMarkerShift(node: Fancytree.FancytreeNode) {
    const row = node.span;
    const title = row?.querySelector(".fancytree-title");
    if (!row || !title) {
        return;
    }

    const shift = (row.getBoundingClientRect().height - title.getBoundingClientRect().height) / 2;
    // On the body, since the marker is parented there and custom properties inherit; it is also
    // created lazily, so it cannot be relied on to exist when the first note is dragged over.
    document.body.style.setProperty("--tree-drop-marker-shift", `${shift}px`);
}

type ScrollIntoViewFn = (this: Fancytree.FancytreeNode, effects?: boolean | object, options?: object) => JQueryPromise<unknown>;

/**
 * jquery.fancytree's `FancytreeNode.scrollIntoView()` reads `$(this.span).offset().top`, which throws
 * "Cannot read properties of undefined (reading 'top')" for a node that has no DOM markup — e.g. nodes
 * recreated by `load(true)` while rendering is suspended by `batchUpdate()` during a large sync update.
 * Its `isVisible()` guard only checks the ancestors' expanded flags, not the actual markup, and the
 * exception then aborts the whole tab activation (see #10407). Skip the scroll instead, mirroring the
 * library's own early return for invisible nodes; the node gets rendered (and styled as active) by the
 * full `tree.render()` that re-enabling updates triggers anyway.
 */
function patchScrollIntoViewCrash() {
    const { _FancytreeNodeClass } = $.ui.fancytree as Fancytree.FancytreeStatic & {
        _FancytreeNodeClass: { prototype: Fancytree.FancytreeNode };
    };
    const originalScrollIntoView = _FancytreeNodeClass.prototype.scrollIntoView as ScrollIntoViewFn;

    _FancytreeNodeClass.prototype.scrollIntoView = function (this: Fancytree.FancytreeNode, effects?: boolean | object, options?: object) {
        if (!this.span) {
            return $.Deferred().resolveWith(this).promise();
        }

        return originalScrollIntoView.call(this, effects, options);
    };
}

patchScrollIntoViewCrash();