Skip to content

Build your personal knowledge base with Trilium Notes

8.5 KB
Raw Blame
import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons";

import { runBackendScript, showBackendScriptingDisabledToast } from "../services/backend_scripting.js";
import bundleService from "../services/bundle.js";
import dialog from "../services/dialog.js";
import dateNoteService from "../services/date_notes.js";
import froca from "../services/froca.js";
import { t } from "../services/i18n.js";
import linkService from "../services/link.js";
import options from "../services/options.js";
import protectedSessionHolder from "../services/protected_session_holder.js";
import server from "../services/server.js";
import toastService from "../services/toast.js";
import utils from "../services/utils.js";
import ws from "../services/ws.js";
import appContext, { type CommandListenerData, type NoteCommandData } from "./app_context.js";
import Component from "./component.js";

export default class Entrypoints extends Component {
    constructor() {
        super();
    }

    openDevToolsCommand() {
        window.electronApi?.window.toggleDevTools();
    }

    async createNoteIntoInboxCommand() {
        const inboxNote = await dateNoteService.getInboxNote();
        if (!inboxNote) {
            console.warn("Missing inbox note.");
            return;
        }

        const { note } = await server.post<CreateChildrenResponse>(`notes/${inboxNote.noteId}/children?target=into`, {
            content: "",
            type: "text",
            isProtected: inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable()
        });

        await ws.waitForMaxKnownEntityChangeId();

        await appContext.tabManager.openTabWithNoteWithHoisting(note.noteId, { activate: true });

        appContext.triggerEvent("focusAndSelectTitle", { isNewNote: true });
    }

    async toggleNoteHoistingCommand({ noteId = appContext.tabManager.getActiveContextNoteId() }) {
        const activeNoteContext = appContext.tabManager.getActiveContext();

        if (!activeNoteContext || !noteId) {
            return;
        }

        const noteToHoist = await froca.getNote(noteId);

        if (noteToHoist?.noteId === activeNoteContext.hoistedNoteId) {
            await activeNoteContext.unhoist();
        } else if (noteToHoist?.type !== "search") {
            await activeNoteContext.setHoistedNoteId(noteId);
        }
    }

    async hoistNoteCommand({ noteId }: { noteId: string }) {
        const noteContext = appContext.tabManager.getActiveContext();

        if (!noteContext) {
            logError("hoistNoteCommand: noteContext is null");
            return;
        }

        if (noteContext.hoistedNoteId !== noteId) {
            await noteContext.setHoistedNoteId(noteId);
        }
    }

    async unhoistCommand() {
        const activeNoteContext = appContext.tabManager.getActiveContext();

        if (activeNoteContext) {
            activeNoteContext.unhoist();
        }
    }

    copyWithoutFormattingCommand() {
        utils.copySelectionToClipboard();
    }

    toggleFullscreenCommand() {
        const api = window.electronApi?.window;
        if (api) {
            api.setFullScreen(!api.isFullScreen());
        } else if (document.fullscreenElement) {
            document.exitFullscreen();
        } else {
            document.documentElement.requestFullscreen();
        }
    }

    reloadFrontendAppCommand() {
        utils.reloadFrontendApp();
    }

    logoutCommand() {
        // A form submission keeps the OIDC provider's redirect a top-level navigation; an XHR
        // follows it cross-origin and fails the provider's CORS preflight.
        const form = document.createElement("form");
        form.method = "POST";
        form.action = `${window.glob.baseApiUrl}../logout`;
        form.hidden = true;

        const csrfToken = document.createElement("input");
        csrfToken.type = "hidden";
        csrfToken.name = "x-csrf-token";
        csrfToken.value = window.glob.csrfToken ?? "";
        form.append(csrfToken);

        document.body.append(form);
        form.submit();
    }

    backInNoteHistoryCommand() {
        const api = window.electronApi?.navigation;
        if (api) {
            const activeIndex = api.navigationGetActiveIndex();
            api.navigationGoToIndex(activeIndex - 1);
        } else {
            window.history.back();
        }
    }

    forwardInNoteHistoryCommand() {
        const api = window.electronApi?.navigation;
        if (api) {
            const activeIndex = api.navigationGetActiveIndex();
            api.navigationGoToIndex(activeIndex + 1);
        } else {
            window.history.forward();
        }
    }

    async switchToDesktopVersionCommand() {
        utils.setCookie("trilium-device", "desktop");

        utils.reloadFrontendApp("Switching to desktop version");
    }

    async switchToMobileVersionCommand() {
        utils.setCookie("trilium-device", "mobile");

        utils.reloadFrontendApp("Switching to mobile version");
    }

    async openInWindowCommand({ notePath, hoistedNoteId, viewScope, splits, activeSplit }: NoteCommandData) {
        const target = { notePath, hoistedNoteId, viewScope, splits, activeSplit };

        // On desktop the main process turns this into an extra window that shares
        // this renderer's process (`installWindowOpenPolicy`).
        window.open(linkService.calculateExtraWindowUrl(target), "", "width=1000,height=800");
    }

    async openNewWindowCommand() {
        this.openInWindowCommand({ notePath: "", hoistedNoteId: "root" });
    }

    async openTodayNoteCommand() {
        const todayNote = await dateNoteService.getTodayNote();
        if (!todayNote) {
            console.warn("Missing today note.");
            return;
        }

        await appContext.tabManager.openInSameTab(todayNote.noteId);
    }

    async runActiveNoteCommand({ ntxId: requestedNtxId }: CommandListenerData<"runActiveNote"> = {}) {
        const noteContext = appContext.tabManager.getCommandContext(requestedNtxId);
        if (!noteContext) {
            return;
        }
        const { ntxId, note } = noteContext;

        // ctrl+enter is also used elsewhere, so make sure we're running only when appropriate
        if (!note || note.type !== "code") {
            return;
        }

        // TODO: use note.executeScript()
        if (note.mime.endsWith("env=frontend")) {
            try {
                // rethrow so a failed script doesn't fall through to the "Note executed" confirmation;
                // executeBundle has already surfaced the error toast.
                await bundleService.getAndExecuteBundle(note.noteId, null, null, null, { rethrow: true });
            } catch {
                return;
            }
        } else if (note.mime.endsWith("env=backend")) {
            if (!options.is("backendScriptingEnabled")) {
                // Show the same friendly toast as the frontend runOnBackend path, rather than letting
                // the /script/run request 500 and surface as an "Uncaught error" via the global handler.
                showBackendScriptingDisabledToast(note.noteId);
                return;
            }
            try {
                await runBackendScript(note.noteId);
            } catch {
                // Already reported as a scripting error; don't fall through to "Note executed".
                return;
            }
        } else if (note.mime === "text/x-sqlite;schema=trilium") {
            const response = await server.post<SqlExecuteResponse>(`sql/execute/${note.noteId}`);
            await appContext.triggerEvent("sqlQueryResults", { ntxId, response });
        }

        toastService.showMessage(t("entrypoints.note-executed"));
    }

    async forceSaveRevisionCommand({ ntxId }: CommandListenerData<"forceSaveRevision"> = {}) {
        const noteId = appContext.tabManager.getCommandContext(ntxId)?.noteId;

        await server.post(`notes/${noteId}/revision`);

        toastService.showMessage(t("entrypoints.note-revision-created"));
    }

    async saveNamedRevisionCommand({ ntxId }: CommandListenerData<"saveNamedRevision"> = {}) {
        const noteId = appContext.tabManager.getCommandContext(ntxId)?.noteId;
        if (!noteId) return;

        const name = await dialog.prompt({
            title: t("entrypoints.save-named-revision-title"),
            message: t("entrypoints.save-named-revision-message"),
            defaultValue: ""
        });

        // null means the user cancelled
        if (name === null) return;

        await server.post(`notes/${noteId}/revision`, { description: name || undefined });
        toastService.showMessage(t("entrypoints.note-revision-created"));
    }
}