An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Defer global shortcut registration until translations are ready https://github.com/siyuan-note/siyuan/issues/19599
Daniel27ce54b17f2c
diff --git a/app/src/boot/globalEvent/globalShortcut.test.ts b/app/src/boot/globalEvent/globalShortcut.test.tsnew file mode 100644index 0000000000..6b43bae901--- /dev/null+++ b/app/src/boot/globalEvent/globalShortcut.test.ts@@ -0,0 +1,112 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {resolve} from "node:path";+import {test} from "node:test";+import {runInNewContext} from "node:vm";+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";+import {clearDisallowedTextInputHotkey, normalizePluginHotkey} from "../../util/hotKeyPolicy";+import {getKeymapBindings, setKeymapBindings} from "../../util/keymapBindings";+import {dispatchPluginGlobalShortcut} from "../../plugin/globalShortcut";+import type {App} from "../../index";+import type {Plugin} from "../../plugin";++const compiled = Object.fromEntries(["boot/globalEvent/globalShortcut", "plugin/index", "plugin/keymap"].map(name => [+ name,+ transpileModule(readFileSync(resolve("src", name + ".ts"), "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},+ }).outputText,+]));++const setup = (detached = false) => {+ const siyuan: {languages?: {_trayMenu?: Record<string, string>}; config: {keymap: {+ general: {toggleWin: {custom: string}};+ plugin: Config.IKeymapPlugin;+ }}} = {config: {keymap: {general: {toggleWin: {custom: "⌥M"}}, plugin: {}}}};+ const messages: Array<{channel: string; data: {+ languages?: Record<string, string>; hotkeys: string[]; toggleHotkeys?: string[]; suspended?: boolean;+ }}> = [];+ const menuSyncs: boolean[] = [];+ const registeredCommands: ICommand[] = [];+ const document = {activeElement: {matches: () => false}};+ const dependencies: Record<string, unknown> = {+ "../../constants": {Constants: {SIYUAN_HOTKEY: "siyuan-hotkey"}},+ "../../util/functions": {isWindow: () => detached},+ "../../util/hotKeyPolicy": {clearDisallowedTextInputHotkey},+ "../../util/keymapBindings": {getKeymapBindings},+ "../util/hotKeyPolicy": {normalizePluginHotkey},+ "../util/keymapBindings": {getKeymapBindings, setKeymapBindings},+ "./commonHotkey": {syncAppMenuShortcuts: (suspended = false) => menuSyncs.push(suspended)},+ "electron": {ipcRenderer: {send: (channel: string, data: unknown) => {+ messages.push({channel, data: JSON.parse(JSON.stringify(data))});+ }}},+ "./commandAdapter": {registerPluginCommand: (_app: App, _plugin: Plugin, command: ICommand) => {+ registeredCommands.push(command);+ }},+ };+ const load = <T>(name: string) => {+ const exports = {} as T;+ runInNewContext(compiled[name], {+ exports, window: {siyuan}, document, console,+ require: (id: string) => dependencies[id] || {},+ });+ return exports;+ };+ const shortcuts = load<typeof import("./globalShortcut")>("boot/globalEvent/globalShortcut");+ dependencies["../boot/globalEvent/globalShortcut"] = shortcuts;+ dependencies["./keymap"] = load<typeof import("../../plugin/keymap")>("plugin/keymap");+ const {Plugin: PluginClass} = load<typeof import("../../plugin")>("plugin/index");+ const plugin = Object.create(PluginClass.prototype) as Plugin;+ const app = {plugins: [plugin]} as App;+ Object.assign(plugin, {app, name: "startup-test", commands: []});+ return {siyuan, messages, menuSyncs, registeredCommands, document, plugin,+ sync: () => shortcuts.sendGlobalShortcut(app)};+};++for (const languages of [undefined, {}]) {+ test(`plugin onload completes before tray translations are ready (${languages ? "missing tray" : "missing languages"})`, () => {+ const f = setup();+ f.siyuan.languages = languages;+ const callbacks: string[] = [];+ let loaded = false;+ f.plugin.onload = () => {+ f.plugin.addCommand({langKey: "first", hotkey: "⌥J", globalCallback: () => callbacks.push("first")});+ f.plugin.addCommand({langKey: "second", hotkey: "⌥K", globalCallback: () => callbacks.push("second")});+ loaded = true;+ };++ assert.doesNotThrow(() => f.plugin.onload());+ assert.equal(loaded, true);+ assert.equal(f.plugin.commands.length, 2);+ assert.equal(f.registeredCommands.length, 2);+ assert.deepEqual(f.messages, []);++ const trayMenu = {hideWindow: "Hide window", showWindow: "Show window"};+ f.siyuan.languages = {_trayMenu: trayMenu};+ f.sync();+ assert.deepEqual(f.messages, [{channel: "siyuan-hotkey", data: {+ languages: trayMenu, hotkeys: ["⌥M", "⌥J", "⌥K"], toggleHotkeys: ["⌥M"],+ }}]);+ assert.equal(dispatchPluginGlobalShortcut([f.plugin], "⌥K", f.siyuan.config.keymap.plugin), true);+ assert.deepEqual(callbacks, ["second"]);++ f.plugin.addCommand({langKey: "late", hotkey: "⌥L", globalCallback: () => callbacks.push("late")});+ assert.equal(f.messages.length, 2);+ assert.deepEqual(f.messages[1].data.hotkeys, ["⌥M", "⌥J", "⌥K", "⌥L"]);+ assert.deepEqual(f.messages[1].data.languages, trayMenu);+ });+}++test("recording still suspends global shortcuts before translations are ready", () => {+ const f = setup();+ f.document.activeElement.matches = () => true;+ f.sync();+ assert.deepEqual(f.messages, [{channel: "siyuan-hotkey", data: {hotkeys: [], suspended: true}}]);+ assert.deepEqual(f.menuSyncs, [true]);+});++test("detached windows sync app menus without registering global shortcuts", () => {+ const f = setup(true);+ f.sync();+ assert.deepEqual(f.messages, []);+ assert.deepEqual(f.menuSyncs, [false]);+});diff --git a/app/src/boot/globalEvent/globalShortcut.ts b/app/src/boot/globalEvent/globalShortcut.tsindex 6bacccdc46..1146e5b40c 100644--- a/app/src/boot/globalEvent/globalShortcut.ts+++ b/app/src/boot/globalEvent/globalShortcut.ts@@ -18,6 +18,11 @@ export const sendGlobalShortcut = (app: App) => {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);@@ -35,7 +40,7 @@ export const sendGlobalShortcut = (app: App) => {});});ipcRenderer.send(Constants.SIYUAN_HOTKEY, {- languages: window.siyuan.languages["_trayMenu"],+ languages,hotkeys: Array.from(hotkeys),toggleHotkeys,});