Skip to content

Build your personal knowledge base with Trilium Notes

9.0 KB
Raw Blame
import dateNoteService from "../services/date_notes.js";
import froca from "../services/froca.js";
import openService from "../services/open.js";
import options from "../services/options.js";
import protectedSessionService from "../services/protected_session.js";
import { collectShortcutHints } from "../services/shortcut_hints.js";
import treeService from "../services/tree.js";
import utils from "../services/utils.js";
import appContext, { type CommandListenerData } from "./app_context.js";
import Component from "./component.js";

export default class RootCommandExecutor extends Component {
    editReadOnlyNoteCommand() {
        const noteContext = appContext.tabManager.getActiveContext();
        if (noteContext?.viewScope && noteContext.note?.isContentWritable !== false) {
            noteContext.viewScope.readOnlyTemporarilyDisabled = true;
            appContext.triggerEvent("readOnlyTemporarilyDisabled", { noteContext });
        }
    }

    async showShortcutHintsCommand() {
        const sections = collectShortcutHints(await resolveFocusedComponent());
        appContext.triggerEvent("shortcutHintsRequested", { sections });
    }

    async showSQLConsoleCommand() {
        const sqlConsoleNote = await dateNoteService.createSqlConsole();
        if (!sqlConsoleNote) {
            return;
        }

        const noteContext = await appContext.tabManager.openTabWithNoteWithHoisting(sqlConsoleNote.noteId, { activate: true });

        appContext.triggerEvent("focusOnDetail", { ntxId: noteContext.ntxId });
    }

    async searchNotesCommand({ searchString, ancestorNoteId }: CommandListenerData<"searchNotes">) {
        const searchNote = await dateNoteService.createSearchNote({ searchString, ancestorNoteId });
        if (!searchNote) {
            return;
        }

        // force immediate search
        await froca.loadSearchNote(searchNote.noteId);

        const noteContext = await appContext.tabManager.openTabWithNoteWithHoisting(searchNote.noteId, {
            activate: true
        });
    }

    async searchInSubtreeCommand({ notePath }: CommandListenerData<"searchInSubtree">) {
        const noteId = treeService.getNoteIdFromUrl(notePath);

        this.searchNotesCommand({ ancestorNoteId: noteId });
    }

    openNoteExternallyCommand({ ntxId }: CommandListenerData<"openNoteExternally">) {
        const note = appContext.tabManager.getCommandContext(ntxId)?.note;
        if (note) {
            openService.openNoteExternally(note.noteId, note.mime);
        }
    }

    openNoteCustomCommand({ ntxId }: CommandListenerData<"openNoteCustom">) {
        const note = appContext.tabManager.getCommandContext(ntxId)?.note;
        if (note) {
            openService.openNoteCustom(note.noteId);
        }
    }

    openNoteOnServerCommand({ ntxId }: CommandListenerData<"openNoteOnServer">) {
        const noteId = appContext.tabManager.getCommandContext(ntxId)?.noteId;
        if (noteId) {
            openService.openNoteOnServer(noteId);
        }
    }

    enterProtectedSessionCommand() {
        protectedSessionService.enterProtectedSession();
    }

    leaveProtectedSessionCommand() {
        protectedSessionService.leaveProtectedSession();
    }

    hideLeftPaneCommand() {
        appContext.triggerEvent("setLeftPaneVisibility", { leftPaneVisible: false });
    }

    showLeftPaneCommand() {
        appContext.triggerEvent("setLeftPaneVisibility", { leftPaneVisible: true });
    }

    toggleLeftPaneCommand() {
        appContext.triggerEvent("setLeftPaneVisibility", { leftPaneVisible: null });
    }

    async showBackendLogCommand() {
        await appContext.tabManager.openTabWithNoteWithHoisting("_backendLog", { activate: true });
    }

    async showSpaceUsageCommand() {
        await appContext.tabManager.openTabWithNoteWithHoisting("_spaceUsage", { activate: true });
    }

    async showHelpCommand() {
        await this.showAndHoistSubtree("_help");
    }

    async showLaunchBarSubtreeCommand() {
        const rootNote = utils.isMobile() ? "_lbMobileRoot" : "_lbRoot";
        appContext.triggerCommand("openInTreePopup", { noteIdOrPath: rootNote, hoistedNoteId: rootNote });
    }

    async showShareSubtreeCommand() {
        await this.showAndHoistSubtree("_share");
    }

    async showHiddenSubtreeCommand() {
        await this.showAndHoistSubtree("_hidden");
    }

    async showSQLConsoleHistoryCommand() {
        await this.showAndHoistSubtree("_sqlConsole");
    }

    async showSearchHistoryCommand() {
        await this.showAndHoistSubtree("_search");
    }

    async showAndHoistSubtree(subtreeNoteId: string) {
        await appContext.tabManager.openContextWithNote(subtreeNoteId, {
            activate: true,
            hoistedNoteId: subtreeNoteId
        });
    }

    async showNoteSourceCommand({ ntxId }: CommandListenerData<"showNoteSource">) {
        const notePath = appContext.tabManager.getCommandContext(ntxId)?.notePath;

        if (notePath) {
            await appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
                activate: true,
                viewScope: {
                    viewMode: "source"
                }
            });
        }
    }

    showNoteOCRTextCommand({ ntxId }: CommandListenerData<"showNoteOCRText">) {
        const noteId = appContext.tabManager.getCommandContext(ntxId)?.noteId;
        if (noteId) {
            appContext.triggerCommand("showOcrTextDialog", {
                textUrl: `ocr/notes/${noteId}/text`,
                processUrl: `ocr/process-note/${noteId}`
            });
        }
    }

    async showAttachmentsCommand({ ntxId }: CommandListenerData<"showAttachments">) {
        const notePath = appContext.tabManager.getCommandContext(ntxId)?.notePath;

        if (notePath) {
            await appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
                activate: true,
                viewScope: {
                    viewMode: "attachments"
                }
            });
        }
    }

    async showAttachmentDetailCommand() {
        const notePath = appContext.tabManager.getActiveContextNotePath();

        if (notePath) {
            await appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
                activate: true,
                viewScope: {
                    viewMode: "attachments"
                }
            });
        }
    }

    toggleTrayCommand() {
        if (!utils.isElectron() || options.is("disableTray")) return;

        window.electronApi?.window.toggleAllWindows();
    }

    toggleZenModeCommand() {
        const $body = $("body");
        $body.toggleClass("zen");
        const isEnabled = $body.hasClass("zen");
        appContext.triggerEvent("zenModeChanged", { isEnabled });
    }

    toggleRibbonTabNoteMapCommand() {
        // Mobile has no right pane, so the map opens in the quick edit popup with the `note-map` view
        // mode, the same view the sidebar card's expand button opens.
        if (utils.isMobile()) {
            const notePath = appContext.tabManager.getActiveContext()?.notePath;
            if (notePath) {
                void appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath, viewScope: { viewMode: "note-map" } });
            }
            return;
        }

        // Peeks the right pane rather than docking it: with the connections tab already docked,
        // `reduceTabSelection()` closes the pane instead of expanding the card.
        void appContext.triggerEvent("selectRightPaneTab", {
            tabId: "connections",
            peek: true,
            expandWidgetId: "noteMap"
        });
    }

    firstTabCommand() {
        this.#goToTab(1);
    }
    secondTabCommand() {
        this.#goToTab(2);
    }
    thirdTabCommand() {
        this.#goToTab(3);
    }
    fourthTabCommand() {
        this.#goToTab(4);
    }
    fifthTabCommand() {
        this.#goToTab(5);
    }
    sixthTabCommand() {
        this.#goToTab(6);
    }
    seventhTabCommand() {
        this.#goToTab(7);
    }
    eigthTabCommand() {
        this.#goToTab(8);
    }
    ninthTabCommand() {
        this.#goToTab(9);
    }
    lastTabCommand() {
        this.#goToTab(Number.POSITIVE_INFINITY);
    }

    #goToTab(tabNumber: number) {
        const mainNoteContexts = appContext.tabManager.getMainNoteContexts();

        const index = tabNumber === Number.POSITIVE_INFINITY ? mainNoteContexts.length - 1 : tabNumber - 1;
        const tab = mainNoteContexts[index];

        if (tab) {
            appContext.tabManager.activateTabContext(tab.ntxId);
        }
    }

}

/**
 * The component to start collecting shortcut hints from: the one owning the focused element, or —
 * when nothing focusable is (e.g. the image/media viewers) — the active pane's type widget.
 */
async function resolveFocusedComponent(): Promise<Component | undefined> {
    const activeEl = document.activeElement;
    if (activeEl instanceof HTMLElement) {
        const component = appContext.getComponentByEl(activeEl) as Component | undefined;
        if (component) {
            return component;
        }
    }

    return appContext.tabManager.getActiveContext()?.getTypeWidget();
}