Skip to content

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

:bug: Preserve plugin command shortcut scopes https://github.com/siyuan-note/siyuan/issues/19076

Vanessa0121eb3ad50d
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index 1ef00a747b..bd66a518b4 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -67,7 +67,7 @@ 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 {resolvePluginCommandCallback, supportsPluginCommandSource} from "../../plugin/commandAdapter";
import {filterHotkey} from "./commonHotkey";
import {editorConfigApi} from "../../config/tabs/editorRuntime";
import {copyPNGByLink} from "../../menus/util";
@@ -621,27 +621,28 @@ const fileTreeKeydown = (app: App, event: KeyboardEvent) => {
}
const liElements = Array.from(files.element.querySelectorAll(".b3-list-item--focus"));
- const commandContext = captureCommandContext({
- app,
- source: "fileTreeShortcut",
- fileLiElements: liElements,
- });
- let matchCommand = false;
+ let matchedCommand: ICommand | undefined;
app.plugins.find(item => {
item.commands.find(command => {
- const callback = resolvePluginCommandCallback(command, commandContext);
- if (callback && matchHotKey(command.customHotkey, event)) {
- matchCommand = true;
- void callback();
+ if (supportsPluginCommandSource(command, "fileTreeShortcut") &&
+ matchHotKey(command.customHotkey, event)) {
+ matchedCommand = command;
return true;
}
});
- if (matchCommand) {
+ return Boolean(matchedCommand);
+ });
+ if (matchedCommand) {
+ const commandContext = captureCommandContext({
+ app,
+ source: "fileTreeShortcut",
+ fileLiElements: liElements,
+ });
+ const callback = resolvePluginCommandCallback(matchedCommand, commandContext);
+ if (callback) {
+ void callback();
return true;
}
- });
- if (matchCommand) {
- return true;
}
if (liElements.length === 0) {
@@ -1056,29 +1057,30 @@ const panelTreeKeydown = (app: App, event: KeyboardEvent) => {
return false;
}
- let matchCommand = false;
+ let matchedCommand: ICommand | undefined;
if (!bottomBacklink) {
- const commandContext = captureCommandContext({
- app,
- source: "dockShortcut",
- dockElement: activePanelElement as HTMLElement,
- });
app.plugins.find(item => {
item.commands.find(command => {
- const callback = resolvePluginCommandCallback(command, commandContext);
- if (callback && matchHotKey(command.customHotkey, event)) {
- matchCommand = true;
- void callback();
+ if (supportsPluginCommandSource(command, "dockShortcut") &&
+ matchHotKey(command.customHotkey, event)) {
+ matchedCommand = command;
return true;
}
});
- if (matchCommand) {
- return true;
- }
+ return Boolean(matchedCommand);
});
}
- if (matchCommand) {
- return true;
+ if (matchedCommand) {
+ const commandContext = captureCommandContext({
+ app,
+ source: "dockShortcut",
+ dockElement: activePanelElement as HTMLElement,
+ });
+ const callback = resolvePluginCommandCallback(matchedCommand, commandContext);
+ if (callback) {
+ void callback();
+ return true;
+ }
}
const matchCollapse = matchHotKey(window.siyuan.config.keymap.editor.general.collapse.custom, event);
const matchExpand = matchHotKey(window.siyuan.config.keymap.editor.general.expand.custom, event);
@@ -1862,25 +1864,25 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => {
return;
}
- let matchCommand = false;
- const commandContext = captureCommandContext({app, source: "shortcut"});
+ let matchedCommand: ICommand | undefined;
app.plugins.find(item => {
item.commands.find(command => {
- const callback = resolvePluginCommandCallback(command, commandContext);
- if (callback && matchHotKey(command.customHotkey, event)) {
- matchCommand = true;
- void callback();
+ if (supportsPluginCommandSource(command, "shortcut") && matchHotKey(command.customHotkey, event)) {
+ matchedCommand = command;
return true;
}
});
- if (matchCommand) {
+ return Boolean(matchedCommand);
+ });
+ if (matchedCommand) {
+ const commandContext = captureCommandContext({app, source: "shortcut"});
+ const callback = resolvePluginCommandCallback(matchedCommand, commandContext);
+ if (callback) {
+ void callback();
+ event.stopPropagation();
+ event.preventDefault();
return true;
}
- });
- if (matchCommand) {
- event.stopPropagation();
- event.preventDefault();
- return true;
}
if (matchHotKey(window.siyuan.config.keymap.general.replace.custom, event)) {
diff --git a/app/src/mobile/util/keydown.ts b/app/src/mobile/util/keydown.ts
index f92df853a3..6b0856efd5 100644
--- a/app/src/mobile/util/keydown.ts
+++ b/app/src/mobile/util/keydown.ts
@@ -4,7 +4,7 @@ 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";
+import {resolvePluginCommandCallback, supportsPluginCommandSource} from "../../plugin/commandAdapter";
export const mobileKeydown = (app: App, event: KeyboardEvent) => {
// 移动端输入框默认填充无 event.key
@@ -26,23 +26,23 @@ export const mobileKeydown = (app: App, event: KeyboardEvent) => {
return;
}
- let matchCommand = false;
- const commandContext = captureCommandContext({app, source: "shortcut"});
+ let matchedCommand: ICommand | undefined;
app.plugins.find(item => {
item.commands.find(command => {
- const callback = resolvePluginCommandCallback(command, commandContext);
- if (callback && matchHotKey(command.customHotkey, event)) {
- matchCommand = true;
- void callback();
+ if (supportsPluginCommandSource(command, "shortcut") && matchHotKey(command.customHotkey, event)) {
+ matchedCommand = command;
return true;
}
});
- if (matchCommand) {
+ return Boolean(matchedCommand);
+ });
+ if (matchedCommand) {
+ const commandContext = captureCommandContext({app, source: "shortcut"});
+ const callback = resolvePluginCommandCallback(matchedCommand, commandContext);
+ if (callback) {
+ void callback();
+ event.preventDefault();
return true;
}
- });
- if (matchCommand) {
- event.preventDefault();
- return true;
}
};
diff --git a/app/src/plugin/commandAdapter.test.ts b/app/src/plugin/commandAdapter.test.ts
index 323d833ade..0e2f7d7369 100644
--- a/app/src/plugin/commandAdapter.test.ts
+++ b/app/src/plugin/commandAdapter.test.ts
@@ -7,6 +7,7 @@ import {
getPluginCommandId,
registerPluginCommand,
resolvePluginCommandCallback,
+ supportsPluginCommandSource,
unregisterPluginCommands,
} from "./commandAdapter";
@@ -185,13 +186,16 @@ describe("plugin command adapter", () => {
}]);
});
- it("executes unified commands from scoped shortcuts", async () => {
+ it("uses legacy callbacks as unified command scope markers", async () => {
const sources: ICommandContext["source"][] = [];
const command = {
langKey: "sample",
execute: (context) => {
sources.push(context.source);
},
+ editorCallback: () => undefined,
+ fileTreeCallback: () => undefined,
+ dockCallback: () => undefined,
} as ICommand;
const protyle = {} as IProtyle;
const files = {} as import("../layout/dock/Files").Files;
@@ -212,9 +216,33 @@ describe("plugin command adapter", () => {
focus: "dock",
dock: {element: dockElement},
}))?.();
+
+ assert.equal(resolvePluginCommandCallback(command, createContext({source: "shortcut"})), undefined);
+ assert.deepEqual(sources, ["editorShortcut", "fileTreeShortcut", "dockShortcut"]);
+ });
+
+ it("treats execute-only commands as generic shortcuts", async () => {
+ const sources: ICommandContext["source"][] = [];
+ const command = {
+ langKey: "sample",
+ execute: (context) => {
+ sources.push(context.source);
+ },
+ } as ICommand;
+
+ assert.equal(supportsPluginCommandSource(command, "editorShortcut"), false);
+ assert.equal(supportsPluginCommandSource(command, "fileTreeShortcut"), false);
+ assert.equal(supportsPluginCommandSource(command, "dockShortcut"), false);
+ assert.equal(supportsPluginCommandSource(command, "shortcut"), true);
+ assert.equal(resolvePluginCommandCallback(command, createContext({
+ source: "editorShortcut",
+ focus: "editor",
+ protyle: {} as IProtyle,
+ })), undefined);
+
await resolvePluginCommandCallback(command, createContext({source: "shortcut"}))?.();
- assert.deepEqual(sources, ["editorShortcut", "fileTreeShortcut", "dockShortcut", "shortcut"]);
+ assert.deepEqual(sources, ["shortcut"]);
});
it("omits a detached range from the plugin command context", () => {
diff --git a/app/src/plugin/commandAdapter.ts b/app/src/plugin/commandAdapter.ts
index 4c60abfdcf..e3d77de7c8 100644
--- a/app/src/plugin/commandAdapter.ts
+++ b/app/src/plugin/commandAdapter.ts
@@ -1,5 +1,5 @@
import {getCommandRegistry} from "../command/service";
-import type {ICommandContextSnapshot, ICommandDefinition} from "../command/types";
+import type {ICommandContextSnapshot, ICommandDefinition, TCommandSource} from "../command/types";
interface IPluginCommandOwner {
name: string;
@@ -33,75 +33,102 @@ const createPluginCommandContext = (context: ICommandContextSnapshot): ICommandC
dock: hasPluginDockContext(context) ? context.dock.element : undefined,
});
+const hasPluginCommandScopedCallback = (command: ICommand) =>
+ Boolean(command.globalCallback || command.fileTreeCallback || command.editorCallback || command.dockCallback);
+
+const hasPluginCommandLegacyCallback = (command: ICommand) =>
+ Boolean(command.callback || hasPluginCommandScopedCallback(command));
+
+export const supportsPluginCommandSource = (command: ICommand, source: TCommandSource) => {
+ if (source === "globalShortcut") {
+ return Boolean(command.globalCallback);
+ }
+ if (source === "editorShortcut") {
+ return Boolean(command.editorCallback);
+ }
+ if (source === "fileTreeShortcut") {
+ return Boolean(command.fileTreeCallback);
+ }
+ if (source === "dockShortcut") {
+ return Boolean(command.dockCallback);
+ }
+ if (source === "shortcut") {
+ return Boolean((command.execute || command.callback) && !hasPluginCommandScopedCallback(command));
+ }
+ return false;
+};
+
+const resolvePluginCommandExecution = (
+ command: ICommand,
+ context: ICommandContextSnapshot,
+ legacyCallback: (pluginContext: ICommandContext) => void,
+) => () => {
+ const pluginContext = createPluginCommandContext(context);
+ return command.execute ? command.execute(pluginContext) : legacyCallback(pluginContext);
+};
+
export const resolvePluginCommandCallback = (command: ICommand, context: ICommandContextSnapshot) => {
if (context.source === "globalShortcut") {
if (!command.globalCallback) {
return undefined;
}
- return command.execute ?
- () => command.execute(createPluginCommandContext(context)) :
- () => command.globalCallback(createPluginCommandContext(context));
+ return resolvePluginCommandExecution(command, context, pluginContext => command.globalCallback(pluginContext));
}
if (context.source === "commandPanel") {
- if (command.execute) {
- return () => command.execute(createPluginCommandContext(context));
- }
if (command.callback) {
- return () => command.callback(createPluginCommandContext(context));
+ return resolvePluginCommandExecution(command, context, pluginContext => command.callback(pluginContext));
}
if (!isMobileContext(context)) {
if (context.focus === "editor" && context.protyle && command.editorCallback) {
- return () => command.editorCallback(context.protyle, createPluginCommandContext(context));
+ return resolvePluginCommandExecution(command, context, pluginContext => {
+ command.editorCallback(context.protyle, pluginContext);
+ });
}
if (context.focus === "fileTree" && context.fileTree?.model && command.fileTreeCallback) {
- return () => command.fileTreeCallback(
- context.fileTree.model as import("../layout/dock/Files").Files,
- createPluginCommandContext(context),
- );
+ return resolvePluginCommandExecution(command, context, pluginContext => {
+ command.fileTreeCallback(
+ context.fileTree.model as import("../layout/dock/Files").Files,
+ pluginContext,
+ );
+ });
}
if (context.focus === "dock" && hasPluginDockContext(context) && command.dockCallback) {
- return () => command.dockCallback(context.dock.element, createPluginCommandContext(context));
+ return resolvePluginCommandExecution(command, context, pluginContext => {
+ command.dockCallback(context.dock.element, pluginContext);
+ });
}
}
if (command.globalCallback && ["desktop", "browser-desktop"].includes(context.environment)) {
- return () => command.globalCallback(createPluginCommandContext(context));
+ return resolvePluginCommandExecution(command, context, pluginContext => {
+ command.globalCallback(pluginContext);
+ });
+ }
+ if (command.execute && !hasPluginCommandLegacyCallback(command)) {
+ return resolvePluginCommandExecution(command, context, () => undefined);
}
return undefined;
}
- 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 === "editorShortcut" && context.protyle && command.editorCallback) {
+ return resolvePluginCommandExecution(command, context, pluginContext => {
+ command.editorCallback(context.protyle, pluginContext);
+ });
}
- if (context.source === "fileTreeShortcut" && context.fileTree?.model) {
- if (command.execute) {
- return () => command.execute(createPluginCommandContext(context));
- }
- if (command.fileTreeCallback) {
- return () => command.fileTreeCallback(
+ if (context.source === "fileTreeShortcut" && context.fileTree?.model && command.fileTreeCallback) {
+ return resolvePluginCommandExecution(command, context, pluginContext => {
+ command.fileTreeCallback(
context.fileTree.model as import("../layout/dock/Files").Files,
- createPluginCommandContext(context),
+ pluginContext,
);
- }
+ });
}
- 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 === "dockShortcut" && hasPluginDockContext(context) && command.dockCallback) {
+ return resolvePluginCommandExecution(command, context, pluginContext => {
+ command.dockCallback(context.dock.element, pluginContext);
+ });
}
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(createPluginCommandContext(context));
+ if ((command.execute || command.callback) && !hasPluginCommandScopedCallback(command)) {
+ return resolvePluginCommandExecution(command, context, pluginContext => command.callback(pluginContext));
}
}
return undefined;
diff --git a/app/src/protyle/wysiwyg/commonHotkey.ts b/app/src/protyle/wysiwyg/commonHotkey.ts
index 96b003a2a3..d51b23e75c 100644
--- a/app/src/protyle/wysiwyg/commonHotkey.ts
+++ b/app/src/protyle/wysiwyg/commonHotkey.ts
@@ -18,7 +18,7 @@ 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";
+import {resolvePluginCommandCallback, supportsPluginCommandSource} from "../../plugin/commandAdapter";
export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElement?: HTMLElement) => {
if (matchHotKey(window.siyuan.config.keymap.editor.general.netImg2LocalAsset.custom, event)) {
@@ -113,28 +113,29 @@ export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent, nodeElemen
return true;
}
/// #if !MOBILE
- let matchCommand = false;
- const commandContext = captureCommandContext({
- app: protyle.app,
- source: "editorShortcut",
- protyle,
- range: protyle.toolbar.range,
- });
+ let matchedCommand: ICommand | undefined;
protyle.app.plugins.find(item => {
item.commands.find(command => {
- const callback = resolvePluginCommandCallback(command, commandContext);
- if (callback && matchHotKey(command.customHotkey, event)) {
- matchCommand = true;
- void callback();
+ if (supportsPluginCommandSource(command, "editorShortcut") &&
+ matchHotKey(command.customHotkey, event)) {
+ matchedCommand = command;
return true;
}
});
- if (matchCommand) {
+ return Boolean(matchedCommand);
+ });
+ if (matchedCommand) {
+ const commandContext = captureCommandContext({
+ app: protyle.app,
+ source: "editorShortcut",
+ protyle,
+ range: protyle.toolbar.range,
+ });
+ const callback = resolvePluginCommandCallback(matchedCommand, commandContext);
+ if (callback) {
+ void callback();
return true;
}
- });
- if (matchCommand) {
- return true;
}
/// #endif
};