Skip to content

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

2.4 KB
Raw Blame
import type {App} from "../../index";
import {Constants} from "../../constants";
import {isWindow} from "../../util/functions";
import {clearDisallowedTextInputHotkey} from "../../util/hotKeyPolicy";
import {getKeymapBindings} from "../../util/keymapBindings";
import {syncAppMenuShortcuts} from "./commonHotkey";
import {getSettingsWindowHost} from "../../config/setting/windowContext";
/// #if !BROWSER
import {ipcRenderer} from "electron";
/// #endif

export const sendGlobalShortcut = (app: App) => {
    const host = getSettingsWindowHost();
    if (host) {
        if (document.activeElement?.matches(".config-keymap__record, #searchByKey")) host.suspendShortcuts();
        else host.restoreShortcuts();
        return;
    }
    /// #if !BROWSER
    if (document.activeElement?.matches(".config-keymap__record, #searchByKey")) {
        sendUnregisterGlobalShortcut(app);
        return;
    }
    syncAppMenuShortcuts();
    if (isWindow()) {
        return;
    }
    const languages = window.siyuan.languages?.["_trayMenu"];
    if (!languages) {
        // 插件可能在语言包加载完成前注册命令,由 onGetConfig 在布局初始化后补发。
        return;
    }
    const toggleHotkeys = getKeymapBindings(window.siyuan.config.keymap.general.toggleWin)
        .map(clearDisallowedTextInputHotkey).filter(Boolean);
    const hotkeys = new Set(toggleHotkeys);
    app.plugins.forEach(plugin => {
        plugin.commands.forEach(command => {
            if (command.globalCallback) {
                getKeymapBindings(window.siyuan.config.keymap.plugin?.[plugin.name]?.[command.langKey] ||
                    {custom: command.customHotkey}).forEach(key => {
                    const hotkey = clearDisallowedTextInputHotkey(key);
                    if (hotkey) {
                        hotkeys.add(hotkey);
                    }
                });
            }
        });
    });
    ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
        languages,
        hotkeys: Array.from(hotkeys),
        toggleHotkeys,
    });
    /// #endif
};

export const sendUnregisterGlobalShortcut = (app: App) => {
    const host = getSettingsWindowHost();
    if (host) {
        host.suspendShortcuts();
        return;
    }
    /// #if !BROWSER
    if (app) {
        syncAppMenuShortcuts(true);
        if (!isWindow()) {
            ipcRenderer.send(Constants.SIYUAN_HOTKEY, {hotkeys: [], suspended: true});
        }
    }
    /// #endif
};