An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Support unified plugin command execution context https://github.com/siyuan-note/siyuan/issues/19076
Vanessa294b9f976a42
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.tsindex a62dcbee0b..1ef00a747b 100644--- a/app/src/boot/globalEvent/keydown.ts+++ b/app/src/boot/globalEvent/keydown.ts@@ -66,6 +66,8 @@ import {zoomOut} from "../../menus/protyle";import {getPlainText} from "../../protyle/util/paste";import {commandPanel} from "./command/panel";import {execByCommand} from "../../command/executor";+import {captureCommandContext} from "../../command/context";+import {resolvePluginCommandCallback} from "../../plugin/commandAdapter";import {filterHotkey} from "./commonHotkey";import {editorConfigApi} from "../../config/tabs/editorRuntime";import {copyPNGByLink} from "../../menus/util";@@ -618,12 +620,19 @@ const fileTreeKeydown = (app: App, event: KeyboardEvent) => {return false;}+ const liElements = Array.from(files.element.querySelectorAll(".b3-list-item--focus"));+ const commandContext = captureCommandContext({+ app,+ source: "fileTreeShortcut",+ fileLiElements: liElements,+ });let matchCommand = false;app.plugins.find(item => {item.commands.find(command => {- if (command.fileTreeCallback && matchHotKey(command.customHotkey, event)) {+ const callback = resolvePluginCommandCallback(command, commandContext);+ if (callback && matchHotKey(command.customHotkey, event)) {matchCommand = true;- command.fileTreeCallback(files);+ void callback();return true;}});@@ -635,7 +644,6 @@ const fileTreeKeydown = (app: App, event: KeyboardEvent) => {return true;}- const liElements = Array.from(files.element.querySelectorAll(".b3-list-item--focus"));if (liElements.length === 0) {if (event.key.startsWith("Arrow") && isNotCtrl(event)) {const liElement = files.element.querySelector(".b3-list-item");@@ -1050,11 +1058,17 @@ const panelTreeKeydown = (app: App, event: KeyboardEvent) => {let matchCommand = false;if (!bottomBacklink) {+ const commandContext = captureCommandContext({+ app,+ source: "dockShortcut",+ dockElement: activePanelElement as HTMLElement,+ });app.plugins.find(item => {item.commands.find(command => {- if (command.dockCallback && matchHotKey(command.customHotkey, event)) {+ const callback = resolvePluginCommandCallback(command, commandContext);+ if (callback && matchHotKey(command.customHotkey, event)) {matchCommand = true;- command.dockCallback(activePanelElement as HTMLElement);+ void callback();return true;}});@@ -1849,13 +1863,13 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => {}let matchCommand = false;+ const commandContext = captureCommandContext({app, source: "shortcut"});app.plugins.find(item => {item.commands.find(command => {- if (command.callback &&- !command.fileTreeCallback && !command.editorCallback && !command.dockCallback && !command.globalCallback- && matchHotKey(command.customHotkey, event)) {+ const callback = resolvePluginCommandCallback(command, commandContext);+ if (callback && matchHotKey(command.customHotkey, event)) {matchCommand = true;- command.callback();+ void callback();return true;}});diff --git a/app/src/command/context.ts b/app/src/command/context.tsindex 95ef16d001..7cbcda6113 100644--- a/app/src/command/context.ts+++ b/app/src/command/context.ts@@ -33,6 +33,7 @@ interface ICaptureCommandContextOptions {range?: Range;protyle?: IProtyle;fileLiElements?: Element[];+ dockElement?: HTMLElement;}const findDialogProtyle = (range?: Range) => {@@ -182,7 +183,11 @@ export const captureCommandContext = (options: ICaptureCommandContextOptions): Iconst dialogProtyle = protyle || explicitFileTree ? undefined : findDialogProtyle(range);protyle ||= dialogProtyle;let fileTreeFocused = false;- const dock = getDockElement(activeElement);+ const dock = options.dockElement ? {+ element: options.dockElement,+ type: Array.from(options.dockElement.classList)+ .find(item => item.startsWith("sy__"))?.substring(4),+ } : getDockElement(activeElement);/// #if !MOBILEconst activePanelElement = document.querySelector<HTMLElement>(".layout__tab--active");fileTreeFocused = !protyle && (explicitFileTree || activePanelElement?.classList.contains("sy__file"));diff --git a/app/src/mobile/util/keydown.ts b/app/src/mobile/util/keydown.tsindex df46c81660..f92df853a3 100644--- a/app/src/mobile/util/keydown.ts+++ b/app/src/mobile/util/keydown.ts@@ -3,6 +3,8 @@ import {execByCommand} from "../../command/executor";import {matchHotKey} from "../../protyle/util/hotKey";import {getCurrentEditor} from "../editor";import {filterHotkey} from "../../boot/globalEvent/commonHotkey";+import {captureCommandContext} from "../../command/context";+import {resolvePluginCommandCallback} from "../../plugin/commandAdapter";export const mobileKeydown = (app: App, event: KeyboardEvent) => {// 移动端输入框默认填充无 event.key@@ -25,13 +27,13 @@ export const mobileKeydown = (app: App, event: KeyboardEvent) => {}let matchCommand = false;+ const commandContext = captureCommandContext({app, source: "shortcut"});app.plugins.find(item => {item.commands.find(command => {- if (command.callback &&- !command.fileTreeCallback && !command.editorCallback && !command.dockCallback && !command.globalCallback- && matchHotKey(command.customHotkey, event)) {+ const callback = resolvePluginCommandCallback(command, commandContext);+ if (callback && matchHotKey(command.customHotkey, event)) {matchCommand = true;- command.callback();+ void callback();return true;}});diff --git a/app/src/plugin/commandAdapter.test.ts b/app/src/plugin/commandAdapter.test.tsindex fcbbaf8cd8..323d833ade 100644--- a/app/src/plugin/commandAdapter.test.ts+++ b/app/src/plugin/commandAdapter.test.ts@@ -47,19 +47,66 @@ describe("plugin command adapter", () => {it("keeps the command panel callback precedence", () => {const calls: string[] = [];+ const protyle = {} as IProtyle;+ const range = {startContainer: {isConnected: true}} as unknown as Range;+ let receivedContext: ICommandContext | undefined;const command = {langKey: "sample",- callback: () => calls.push("callback"),+ callback: (context) => {+ calls.push("callback");+ receivedContext = context;+ },editorCallback: () => calls.push("editor"),globalCallback: () => calls.push("global"),} as ICommand;const callback = resolvePluginCommandCallback(command, createContext({focus: "editor",- protyle: {} as IProtyle,+ protyle,+ range,}));callback?.();assert.deepEqual(calls, ["callback"]);+ assert.deepEqual(receivedContext, {+ source: "commandPanel",+ focus: "editor",+ protyle,+ range,+ fileTree: undefined,+ dock: undefined,+ });+ });++ it("prefers execute over legacy callbacks", async () => {+ const calls: string[] = [];+ const protyle = {} as IProtyle;+ const range = {startContainer: {isConnected: true}} as unknown as Range;+ let receivedContext: ICommandContext | undefined;+ const command = {+ langKey: "sample",+ execute(context) {+ calls.push("execute");+ receivedContext = context;+ },+ callback: () => calls.push("callback"),+ editorCallback: () => calls.push("editor"),+ } as ICommand;++ await resolvePluginCommandCallback(command, createContext({+ focus: "editor",+ protyle,+ range,+ }))?.();++ assert.deepEqual(calls, ["execute"]);+ assert.deepEqual(receivedContext, {+ source: "commandPanel",+ focus: "editor",+ protyle,+ range,+ fileTree: undefined,+ dock: undefined,+ });});it("preserves the plugin command as the callback receiver", () => {@@ -82,17 +129,28 @@ describe("plugin command adapter", () => {it("dispatches editor, file tree, and dock callbacks from the captured context", () => {const calls: unknown[] = [];+ const contexts: Array<ICommandContext | undefined> = [];const protyle = {} as IProtyle;+ const range = {startContainer: {isConnected: true}} as unknown as Range;const files = {} as import("../layout/dock/Files").Files;const dockElement = {} as HTMLElement;const command = {langKey: "sample",- editorCallback: value => calls.push(value),- fileTreeCallback: value => calls.push(value),- dockCallback: value => calls.push(value),+ editorCallback: (value, context) => {+ calls.push(value);+ contexts.push(context);+ },+ fileTreeCallback: (value, context) => {+ calls.push(value);+ contexts.push(context);+ },+ dockCallback: (value, context) => {+ calls.push(value);+ contexts.push(context);+ },} as ICommand;- resolvePluginCommandCallback(command, createContext({focus: "editor", protyle}))?.();+ resolvePluginCommandCallback(command, createContext({focus: "editor", protyle, range}))?.();resolvePluginCommandCallback(command, createContext({focus: "fileTree",fileTree: {model: files, elements: [], ids: [], paths: []},@@ -103,6 +161,75 @@ describe("plugin command adapter", () => {}))?.();assert.deepEqual(calls, [protyle, files, dockElement]);+ assert.deepEqual(contexts, [{+ source: "commandPanel",+ focus: "editor",+ protyle,+ range,+ fileTree: undefined,+ dock: undefined,+ }, {+ source: "commandPanel",+ focus: "fileTree",+ protyle: undefined,+ range: undefined,+ fileTree: files,+ dock: undefined,+ }, {+ source: "commandPanel",+ focus: "dock",+ protyle: undefined,+ range: undefined,+ fileTree: undefined,+ dock: dockElement,+ }]);+ });++ it("executes unified commands from scoped shortcuts", async () => {+ const sources: ICommandContext["source"][] = [];+ const command = {+ langKey: "sample",+ execute: (context) => {+ sources.push(context.source);+ },+ } as ICommand;+ const protyle = {} as IProtyle;+ const files = {} as import("../layout/dock/Files").Files;+ const dockElement = {} as HTMLElement;++ await resolvePluginCommandCallback(command, createContext({+ source: "editorShortcut",+ focus: "editor",+ protyle,+ }))?.();+ await resolvePluginCommandCallback(command, createContext({+ source: "fileTreeShortcut",+ focus: "fileTree",+ fileTree: {model: files, elements: [], ids: [], paths: []},+ }))?.();+ await resolvePluginCommandCallback(command, createContext({+ source: "dockShortcut",+ focus: "dock",+ dock: {element: dockElement},+ }))?.();+ await resolvePluginCommandCallback(command, createContext({source: "shortcut"}))?.();++ assert.deepEqual(sources, ["editorShortcut", "fileTreeShortcut", "dockShortcut", "shortcut"]);+ });++ it("omits a detached range from the plugin command context", () => {+ const range = {startContainer: {isConnected: false}} as unknown as Range;+ let receivedContext: ICommandContext | undefined;+ const command = {+ langKey: "sample",+ callback: (context) => {+ receivedContext = context;+ },+ } as ICommand;++ resolvePluginCommandCallback(command, createContext({range}))?.();++ assert.equal(receivedContext?.range, undefined);});it("does not enable specialized callbacks on mobile", () => {diff --git a/app/src/plugin/commandAdapter.ts b/app/src/plugin/commandAdapter.tsindex 68208647b6..4c60abfdcf 100644--- a/app/src/plugin/commandAdapter.ts+++ b/app/src/plugin/commandAdapter.ts@@ -24,47 +24,84 @@ const isMobileContext = (context: ICommandContextSnapshot) =>const hasPluginDockContext = (context: ICommandContextSnapshot) =>Boolean(context.dock?.element && context.dock.type !== "backlink-bottom");+const createPluginCommandContext = (context: ICommandContextSnapshot): ICommandContext => ({+ source: context.source,+ focus: context.focus,+ protyle: context.protyle,+ range: context.range?.startContainer.isConnected ? context.range : undefined,+ fileTree: context.fileTree?.model as import("../layout/dock/Files").Files | undefined,+ dock: hasPluginDockContext(context) ? context.dock.element : undefined,+});+export const resolvePluginCommandCallback = (command: ICommand, context: ICommandContextSnapshot) => {if (context.source === "globalShortcut") {- return command.globalCallback ? () => command.globalCallback() : undefined;+ if (!command.globalCallback) {+ return undefined;+ }+ return command.execute ?+ () => command.execute(createPluginCommandContext(context)) :+ () => command.globalCallback(createPluginCommandContext(context));}if (context.source === "commandPanel") {+ if (command.execute) {+ return () => command.execute(createPluginCommandContext(context));+ }if (command.callback) {- return () => command.callback();+ return () => command.callback(createPluginCommandContext(context));}if (!isMobileContext(context)) {if (context.focus === "editor" && context.protyle && command.editorCallback) {- return () => command.editorCallback(context.protyle);+ return () => command.editorCallback(context.protyle, createPluginCommandContext(context));}if (context.focus === "fileTree" && context.fileTree?.model && command.fileTreeCallback) {return () => command.fileTreeCallback(context.fileTree.model as import("../layout/dock/Files").Files,+ createPluginCommandContext(context),);}if (context.focus === "dock" && hasPluginDockContext(context) && command.dockCallback) {- return () => command.dockCallback(context.dock.element);+ return () => command.dockCallback(context.dock.element, createPluginCommandContext(context));}}if (command.globalCallback && ["desktop", "browser-desktop"].includes(context.environment)) {- return () => command.globalCallback();+ return () => command.globalCallback(createPluginCommandContext(context));}return undefined;}- if (context.source === "editorShortcut" && context.protyle && command.editorCallback) {- return () => command.editorCallback(context.protyle);+ if (context.source === "editorShortcut" && context.protyle) {+ if (command.execute) {+ return () => command.execute(createPluginCommandContext(context));+ }+ if (command.editorCallback) {+ return () => command.editorCallback(context.protyle, createPluginCommandContext(context));+ }}- if (context.source === "fileTreeShortcut" && context.fileTree?.model && command.fileTreeCallback) {- return () => command.fileTreeCallback(- context.fileTree.model as import("../layout/dock/Files").Files,- );+ if (context.source === "fileTreeShortcut" && context.fileTree?.model) {+ if (command.execute) {+ return () => command.execute(createPluginCommandContext(context));+ }+ if (command.fileTreeCallback) {+ return () => command.fileTreeCallback(+ context.fileTree.model as import("../layout/dock/Files").Files,+ createPluginCommandContext(context),+ );+ }}- if (context.source === "dockShortcut" && hasPluginDockContext(context) && command.dockCallback) {- return () => command.dockCallback(context.dock.element);+ if (context.source === "dockShortcut" && hasPluginDockContext(context)) {+ if (command.execute) {+ return () => command.execute(createPluginCommandContext(context));+ }+ if (command.dockCallback) {+ return () => command.dockCallback(context.dock.element, createPluginCommandContext(context));+ }}if (context.source === "shortcut") {+ if (command.execute) {+ return () => command.execute(createPluginCommandContext(context));+ }if (command.callback &&!command.fileTreeCallback && !command.editorCallback && !command.dockCallback && !command.globalCallback) {- return () => command.callback();+ return () => command.callback(createPluginCommandContext(context));}}return undefined;diff --git a/app/src/plugin/globalShortcut.test.ts b/app/src/plugin/globalShortcut.test.tsindex c21b743853..2455d928fb 100644--- a/app/src/plugin/globalShortcut.test.ts+++ b/app/src/plugin/globalShortcut.test.ts@@ -23,4 +23,21 @@ describe("plugin global shortcuts", () => {assert.equal(dispatchPluginGlobalShortcut(plugins, "⌘B"), false);assert.equal(calls, 0);});++ it("prefers execute and passes the global context", () => {+ const calls: string[] = [];+ let receivedContext: ICommandContext | undefined;+ const plugins = [{commands: [{+ customHotkey: "⌘A",+ execute: (context: ICommandContext) => {+ calls.push("execute");+ receivedContext = context;+ },+ globalCallback: () => calls.push("global"),+ }]}];++ assert.equal(dispatchPluginGlobalShortcut(plugins, "⌘A"), true);+ assert.deepEqual(calls, ["execute"]);+ assert.deepEqual(receivedContext, {source: "globalShortcut", focus: "global"});+ });});diff --git a/app/src/plugin/globalShortcut.ts b/app/src/plugin/globalShortcut.tsindex 7356d361cf..ddd4f0989d 100644--- a/app/src/plugin/globalShortcut.ts+++ b/app/src/plugin/globalShortcut.ts@@ -1,6 +1,7 @@interface IPluginGlobalShortcutCommand {customHotkey?: string;- globalCallback?: () => void;+ execute?: (context: ICommandContext) => void | Promise<void>;+ globalCallback?: (context?: ICommandContext) => void;}interface IPluginGlobalShortcutOwner {@@ -8,10 +9,15 @@ interface IPluginGlobalShortcutOwner {}export const dispatchPluginGlobalShortcut = (plugins: IPluginGlobalShortcutOwner[], hotkey: string) => {+ const context: ICommandContext = {source: "globalShortcut", focus: "global"};for (const plugin of plugins) {const command = plugin.commands.find(item => item.globalCallback && item.customHotkey === hotkey);if (command) {- command.globalCallback();+ if (command.execute) {+ void command.execute(context);+ } else {+ command.globalCallback(context);+ }return true;}}diff --git a/app/src/protyle/wysiwyg/commonHotkey.ts b/app/src/protyle/wysiwyg/commonHotkey.tsindex be611b4f2d..96b003a2a3 100644--- a/app/src/protyle/wysiwyg/commonHotkey.ts+++ b/app/src/protyle/wysiwyg/commonHotkey.ts@@ -17,6 +17,8 @@ import {removeEmbed} from "./removeEmbed";import {clearBlockElement} from "../util/clear";import {isEncryptedBox} from "../../util/pathName";import {normalizeHTMLAssetIFrameBlockDOM} from "../../asset/html";+import {captureCommandContext} from "../../command/context";+import {resolvePluginCommandCallback} from "../../plugin/commandAdapter";export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElement?: HTMLElement) => {if (matchHotKey(window.siyuan.config.keymap.editor.general.netImg2LocalAsset.custom, event)) {@@ -112,11 +114,18 @@ export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElemen}/// #if !MOBILElet matchCommand = false;+ const commandContext = captureCommandContext({+ app: protyle.app,+ source: "editorShortcut",+ protyle,+ range: protyle.toolbar.range,+ });protyle.app.plugins.find(item => {item.commands.find(command => {- if (command.editorCallback && matchHotKey(command.customHotkey, event)) {+ const callback = resolvePluginCommandCallback(command, commandContext);+ if (callback && matchHotKey(command.customHotkey, event)) {matchCommand = true;- command.editorCallback(protyle);+ void callback();return true;}});diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.tsindex aff5b894ef..82a4408a96 100644--- a/app/src/types/index.d.ts+++ b/app/src/types/index.d.ts@@ -888,16 +888,30 @@ interface ILayoutJSON extends ILayoutOptions {children?: ILayoutJSON[] | ILayoutJSON}+interface ICommandContext {+ source: "commandPanel" | "shortcut" | "editorShortcut" | "fileTreeShortcut" | "dockShortcut" |+ "globalShortcut" | "keymap" | "menu" | "api",+ focus: "global" | "editor" | "fileTree" | "dock",+ protyle?: IProtyle,+ range?: Range,+ fileTree?: import("../layout/dock/Files").Files,+ dock?: HTMLElement,+}+interface ICommand {langKey: string, // 用于区分不同快捷键的 key, 同时作为 i18n 的字段名langText?: string, // 显示的文本, 指定后不再使用 langKey 对应的 i18n 文本hotkey?: string, // 快捷键,默认为空字符串customHotkey?: string,- callback?: () => void // 其余回调存在时将不会触发- globalCallback?: () => void // 焦点不在应用内时执行的回调- fileTreeCallback?: (file: import("../layout/dock/Files").Files) => void // 焦点在文档树上时执行的回调- editorCallback?: (protyle: IProtyle) => void // 焦点在编辑器上时执行的回调- dockCallback?: (element: HTMLElement) => void // 焦点在 dock 上时执行的回调+ execute?: (context: ICommandContext) => void | Promise<void>+ callback?: (context?: ICommandContext) => void // 其余回调存在时将不会触发+ globalCallback?: (context?: ICommandContext) => void // 焦点不在应用内时执行的回调+ fileTreeCallback?: (+ file: import("../layout/dock/Files").Files,+ context?: ICommandContext+ ) => void // 焦点在文档树上时执行的回调+ editorCallback?: (protyle: IProtyle, context?: ICommandContext) => void // 焦点在编辑器上时执行的回调+ dockCallback?: (element: HTMLElement, context?: ICommandContext) => void // 焦点在 dock 上时执行的回调}interface IPluginData {