Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

9.1 KB
Raw Blame
import {getKeymapBindings, getKeymapItem} from "../../../util/keymapBindings";
import {Dialog} from "../../../dialog";
import type {App} from "../../../index";
import {upDownHint} from "../../../util/upDownHint";
import {setStorageVal, updateHotkeyTip} from "../../../protyle/util/compatibility";
import {isMobile} from "../../../util/functions";
import {Constants} from "../../../constants";
import {hasClosestByClassName} from "../../../protyle/util/hasClosest";
import {focusByRange} from "../../../protyle/util/selectionOffsets";
import {matchHotKey} from "../../../protyle/util/hotKey";
import {captureCommandContext} from "../../../command/context";
import {ensureCommandSystem, executeCommandById} from "../../../command/executor";
import {initializeEnglishCommandTranslations} from "../../../command/english";
import {
    COMMAND_PALETTE_HISTORY_KEY, createPaletteFocusLifecycle, queryCommandPalette, recordPaletteCommand,
} from "../../../command/paletteCore";
import type {ICommandContextSnapshot, ICommandDefinition} from "../../../command/types";
import {ensureInsertCommands} from "../../../command/insertCommands";
/// #if MOBILE
import {activeBlur} from "../../../mobile/util/keyboardToolbar";
/// #endif

const renderCommands = (listElement: HTMLElement, commands: ICommandDefinition[]) => {
    const fragment = document.createDocumentFragment();
    commands.forEach(command => {
        const itemElement = document.createElement("li");
        itemElement.className = "b3-list-item";
        itemElement.dataset.commandId = command.id;
        const textElement = document.createElement("span");
        textElement.className = "b3-list-item__text";
        textElement.textContent = command.label();
        const hotkey = command.keymapPath ?
            getKeymapBindings(getKeymapItem(window.siyuan.config.keymap, command.keymapPath)).map(key => updateHotkeyTip(key)).join(" / ") :
            updateHotkeyTip(command.hotkey?.() || "");
        itemElement.append(textElement);
        if (hotkey) {
            const hotkeyElement = document.createElement("span");
            hotkeyElement.className = "b3-list-item__meta";
            hotkeyElement.textContent = hotkey;
            itemElement.append(hotkeyElement);
        }
        fragment.append(itemElement);
    });
    listElement.replaceChildren(fragment);
    listElement.firstElementChild?.classList.add("b3-list-item--focus");
};

const executePaletteCommand = (app: App, commandId: string, context: ICommandContextSnapshot) => {
    void executeCommandById(app, commandId, context).then(result => {
        if (result.status === "executed") {
            const history = recordPaletteCommand(window.siyuan.storage[COMMAND_PALETTE_HISTORY_KEY], commandId);
            window.siyuan.storage[COMMAND_PALETTE_HISTORY_KEY] = history;
            setStorageVal(COMMAND_PALETTE_HISTORY_KEY, history);
        }
    }).catch(error => {
        console.error(`Unable to execute command "${commandId}":`, error);
    });
};

export const commandPanel = (app: App, options: {
    protyle?: IProtyle;
    range?: Range;
    restoreKeyboard?: () => void;
    openOnly?: boolean;
} = {}) => {
    const menu = window.siyuan.menus.menu;
    if (isMobile() && menu.element.getAttribute("data-name") === Constants.DIALOG_COMMANDPANEL) {
        if (!options.openOnly) {
            menu.closeSheet();
        }
        return;
    }
    const openCommandPanelDialog = window.siyuan.dialogs.find(item =>
        item.element.getAttribute("data-key") === Constants.DIALOG_COMMANDPANEL);
    if (openCommandPanelDialog) {
        if (!options.openOnly) {
            openCommandPanelDialog.destroy();
        }
        return;
    }
    const context = captureCommandContext({app, source: "commandPanel", protyle: options.protyle, range: options.range});
    const registry = ensureCommandSystem(app);
    ensureInsertCommands(app, isMobile());
    const restoreEditorKeyboard = isMobile() && Boolean(options.restoreKeyboard);
    const restoreFocusAfterCancel = !isMobile() ||
        (!restoreEditorKeyboard && document.body.classList.contains("mobile-keyboard--open"));
    const focusLifecycle = createPaletteFocusLifecycle(() => {
        if (context.range?.startContainer.isConnected) {
            focusByRange(context.range);
        }
    });
    const content = `<div class="fn__flex-column${isMobile() ? " mobile-command-panel" : ""}">
    <div class="b3-form__icon search__header" style="border-top: 0;border-bottom: 1px solid var(--b3-theme-surface-lighter);">
        <svg class="b3-form__icon-icon"><use xlink:href="#iconSearch"></use></svg>
        <input spellcheck="false" class="b3-text-field b3-text-field--text" style="padding-left: 32px !important;">
    </div>
    <ul class="b3-list b3-list--background search__list" id="commands"></ul>
    <div class="search__tip${isMobile() ? " fn__none" : ""}">
        <kbd>↑/↓</kbd> ${window.siyuan.languages.searchTip1}
        <kbd>${window.siyuan.languages.enterKey}/${window.siyuan.languages.click}</kbd> ${window.siyuan.languages.confirm}
        <kbd>Esc</kbd> ${window.siyuan.languages.close}
    </div>
</div>`;
    const onClose = () => {
        const canceled = focusLifecycle.restoreAfterCancel(restoreFocusAfterCancel);
        /// #if MOBILE
        if (canceled && !restoreFocusAfterCancel && !restoreEditorKeyboard) {
            activeBlur(true);
        }
        /// #endif
    };
    let dialog: {element: HTMLElement, destroy: () => void};
    menu.remove();
    if (isMobile()) {
        const element = document.createElement("div");
        element.innerHTML = content;
        element.className = "fn__flex-column fn__flex-1";
        menu.append(element);
        const itemsElement = menu.element.lastElementChild as HTMLElement;
        itemsElement.style.display = "flex";
        itemsElement.style.overflow = "hidden";
        menu.element.setAttribute("data-name", Constants.DIALOG_COMMANDPANEL);
        menu.removeCB = onClose;
        // 搜索框会立即接管输入焦点,按可见视口展示菜单。
        menu.fullscreen("bottom", options.restoreKeyboard, {preserveKeyboard: true});
        dialog = {element, destroy: () => menu.remove()};
    } else {
        const desktopDialog = new Dialog({
            width: "80vw",
            height: "70vh",
            title: window.siyuan.languages.commandPanel,
            content,
            disableAnimation: true,
            destroyCallback: onClose,
        });
        desktopDialog.element.setAttribute("data-key", Constants.DIALOG_COMMANDPANEL);
        dialog = desktopDialog;
    }
    const listElement = dialog.element.querySelector("#commands") as HTMLElement;
    const inputElement = dialog.element.querySelector(".b3-text-field") as HTMLInputElement;
    inputElement.setAttribute("aria-label", window.siyuan.languages.commandPanel);
    if (isMobile()) {
        inputElement.placeholder = window.siyuan.languages.commandPanel;
    }
    const refresh = () => {
        renderCommands(listElement, queryCommandPalette(
            registry, context, inputElement.value, window.siyuan.storage[COMMAND_PALETTE_HISTORY_KEY],
        ));
    };
    refresh();
    inputElement.focus();

    const close = () => {
        if (isMobile()) {
            menu.closeSheet();
        } else {
            dialog.destroy();
        }
    };

    const run = (commandId: string, event?: Event) => {
        focusLifecycle.prepareCommand(() => event?.preventDefault());
        dialog.destroy();
        if (restoreEditorKeyboard) {
            options.restoreKeyboard?.();
        }
        executePaletteCommand(app, commandId, context);
    };

    listElement.addEventListener("click", (event: MouseEvent) => {
        const itemElement = hasClosestByClassName(event.target as HTMLElement, "b3-list-item");
        const commandId = itemElement && itemElement.dataset.commandId;
        if (commandId) {
            run(commandId, event);
            event.stopPropagation();
        }
    });
    inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
        event.stopPropagation();
        if (event.isComposing) {
            return;
        }
        if (matchHotKey(window.siyuan.config.keymap.general.commandPanel, event)) {
            event.preventDefault();
            return;
        }
        upDownHint(listElement, event);
        if (event.key === "Enter") {
            const commandId = listElement.querySelector<HTMLElement>(".b3-list-item--focus")?.dataset.commandId;
            if (commandId) {
                run(commandId, event);
            } else {
                event.preventDefault();
                close();
            }
        } else if (event.key === "Escape") {
            close();
        }
    });
    inputElement.addEventListener("compositionend", refresh);
    inputElement.addEventListener("input", (event: InputEvent) => {
        if (!event.isComposing) {
            event.stopPropagation();
            refresh();
        }
    });
    void initializeEnglishCommandTranslations(
        window.siyuan.config.appearance.lang,
        window.siyuan.languages as Record<string, string>,
        Constants.SIYUAN_VERSION,
    ).then(() => {
        if (dialog.element.isConnected) {
            refresh();
        }
    });
};