Skip to content

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

:sparkles: Support insert actions in desktop command panel https://github.com/siyuan-note/siyuan/issues/19811

Daniel62bf88c98129
diff --git a/app/src/boot/globalEvent/command/panel.ts b/app/src/boot/globalEvent/command/panel.ts
index a549e2e909..5743abcbbe 100644
--- a/app/src/boot/globalEvent/command/panel.ts
+++ b/app/src/boot/globalEvent/command/panel.ts
@@ -15,9 +15,9 @@ 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";
-import {ensureMobileInsertCommands} from "../../../mobile/util/mobileInsertCommands";
/// #endif
const renderCommands = (listElement: HTMLElement, commands: ICommandDefinition[]) => {
@@ -67,9 +67,7 @@ export const commandPanel = (app: App) => {
}
const context = captureCommandContext({app, source: "commandPanel"});
const registry = ensureCommandSystem(app);
- /// #if MOBILE
- ensureMobileInsertCommands(app);
- /// #endif
+ ensureInsertCommands(app, isMobile());
const restoreFocusAfterCancel = !isMobile() || document.body.classList.contains("mobile-keyboard--open");
const focusLifecycle = createPaletteFocusLifecycle(() => {
if (context.range?.startContainer.isConnected) {
diff --git a/app/src/mobile/util/mobileInsertCommands.test.ts b/app/src/command/insertCommands.test.ts
similarity index 67%
rename from app/src/mobile/util/mobileInsertCommands.test.ts
rename to app/src/command/insertCommands.test.ts
index 6ce0e06057..00950f3020 100644
--- a/app/src/mobile/util/mobileInsertCommands.test.ts
+++ b/app/src/command/insertCommands.test.ts
@@ -4,15 +4,15 @@ import {join} from "node:path";
import test from "node:test";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
-import {getCommandRegistry} from "../../command/service";
-import {queryCommandPalette, recordPaletteCommand} from "../../command/paletteCore";
-import type {ICommandContextSnapshot} from "../../command/types";
+import {getCommandRegistry} from "./service";
+import {queryCommandPalette, recordPaletteCommand} from "./paletteCore";
+import type {ICommandContextSnapshot} from "./types";
-const source = ts.transpileModule(readFileSync(join(process.cwd(), "src/mobile/util/mobileInsertCommands.ts"), "utf8"), {
+const source = ts.transpileModule(readFileSync(join(process.cwd(), "src/command/insertCommands.ts"), "utf8"), {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022},
}).outputText;
-test("mobile insert commands search and execute the existing insertion actions at the saved cursor", async () => {
+test("insert commands search and execute existing insertion actions at the saved cursor on mobile and desktop", async () => {
const languages = new Proxy({template: "模板", assets: "资源", callout: "提示"} as Record<string, string>, {
get: (target, key: string) => target[key] || key,
});
@@ -31,22 +31,22 @@ test("mobile insert commands search and execute the existing insertion actions a
body: {append: (host: unknown) => assert.equal(host, uploadHost)}};
const module = {exports: {}};
const mocks: Record<string, unknown> = {
- "../../command/service": {getCommandRegistry},
- "../../command/english": {getEnglishCommandLabel: (key: string) => key},
- "../../protyle/hint/extend": {getBuiltinSlashMenuItems: () => [
+ "./service": {getCommandRegistry},
+ "./english": {getEnglishCommandLabel: (key: string) => key},
+ "../protyle/hint/extend": {getBuiltinSlashMenuItems: () => [
{id: "template", value: "template-value"}, {id: "ref", value: "(("},
]},
- "../../protyle/toolbar/inlineStyle": {isBuiltinInlineStyleVisible: () => true},
- "../../protyle/util/selection": {focusByRange: (range: unknown) => focused.push(range)},
- "../../protyle/util/compatibility": {isDisabledFeature: () => false, isInAndroid: () => false},
- "../../util/hostCapabilities": {getHostCapabilities: () => ({widgets: true, remoteKernel: false})},
- "./mobileAppUtil": {callMobileAppShowKeyboard: () => keyboards++},
+ "../protyle/toolbar/inlineStyle": {isBuiltinInlineStyleVisible: () => true},
+ "../protyle/util/selection": {focusByRange: (range: unknown) => focused.push(range)},
+ "../protyle/util/compatibility": {isDisabledFeature: () => false, isInAndroid: () => false},
+ "../util/hostCapabilities": {getHostCapabilities: () => ({widgets: true, remoteKernel: false})},
+ "../mobile/util/mobileAppUtil": {callMobileAppShowKeyboard: () => keyboards++},
};
runInNewContext(source, {module, exports: module.exports, require: (id: string) => mocks[id] || {},
window: {siyuan}, document});
- const {ensureMobileInsertCommands} = module.exports as typeof import("./mobileInsertCommands");
+ const {ensureInsertCommands} = module.exports as typeof import("./insertCommands");
const app = {};
- ensureMobileInsertCommands(app);
+ ensureInsertCommands(app, true);
const registry = getCommandRegistry(app);
const container = {isConnected: true};
const range = {startContainer: container, endContainer: container, cloneRange() { return {...this}; }};
@@ -82,6 +82,18 @@ test("mobile insert commands search and execute the existing insertion actions a
assert.equal(input.accept, "image/*");
assert.equal(uploadHost.hidden, true);
assert.equal(registry.list({...context, environment: "desktop"}).length, 0);
+ const desktopApp = {};
+ ensureInsertCommands(desktopApp, false);
+ const desktopRegistry = getCommandRegistry(desktopApp);
+ const desktopContext = {...context, app: desktopApp, environment: "desktop"} as ICommandContextSnapshot;
+ const desktopVisible = desktopRegistry.list(desktopContext).map(command => command.id);
+ assert.ok(desktopVisible.includes("core.insert.template"));
+ assert.ok(desktopVisible.includes("core.insert.mindmap"));
+ assert.ok(!desktopVisible.includes("core.insert.insertPhoto"));
+ assert.equal(queryCommandPalette(desktopRegistry, desktopContext, "模板")[0].id, "core.insert.template");
+ assert.equal((await desktopRegistry.execute("core.insert.template", desktopContext)).status, "executed");
+ assert.deepEqual(fills, ["template-value", "((", "template-value"]);
+ assert.equal(keyboards, 1);
siyuan.config.readonly = true;
assert.equal(registry.list(context).length, 0);
siyuan.config.readonly = false;
diff --git a/app/src/mobile/util/mobileInsertCommands.ts b/app/src/command/insertCommands.ts
similarity index 80%
rename from app/src/mobile/util/mobileInsertCommands.ts
rename to app/src/command/insertCommands.ts
index 4fef2feaa4..154f697f21 100644
--- a/app/src/mobile/util/mobileInsertCommands.ts
+++ b/app/src/command/insertCommands.ts
@@ -1,16 +1,18 @@
-import {getCommandRegistry} from "../../command/service";
-import {getEnglishCommandLabel} from "../../command/english";
-import type {ICommandContextSnapshot, ICommandDefinition} from "../../command/types";
-import {getBuiltinSlashMenuItems} from "../../protyle/hint/extend";
-import {isBuiltinInlineStyleVisible, type TBuiltinInlineStyleID} from "../../protyle/toolbar/inlineStyle";
-import {focusByRange} from "../../protyle/util/selection";
-import {isDisabledFeature, isInAndroid} from "../../protyle/util/compatibility";
-import {getHostCapabilities} from "../../util/hostCapabilities";
-import {callMobileAppShowKeyboard} from "./mobileAppUtil";
+import {getCommandRegistry} from "./service";
+import {getEnglishCommandLabel} from "./english";
+import type {ICommandContextSnapshot, ICommandDefinition} from "./types";
+import {getBuiltinSlashMenuItems} from "../protyle/hint/extend";
+import {isBuiltinInlineStyleVisible, type TBuiltinInlineStyleID} from "../protyle/toolbar/inlineStyle";
+import {focusByRange} from "../protyle/util/selection";
+import {isDisabledFeature, isInAndroid} from "../protyle/util/compatibility";
+import {getHostCapabilities} from "../util/hostCapabilities";
+/// #if MOBILE
+import {callMobileAppShowKeyboard} from "../mobile/util/mobileAppUtil";
+/// #endif
const initializedApps = new WeakSet<object>();
-// 与移动端加号菜单的内置插入项保持一致,具体插入值仍由斜杠菜单提供。
+// 与加号菜单的内置插入项保持一致,具体插入值仍由斜杠菜单提供。
const INSERT_IDS = [
"template", "widget", "assets", "ref", "blockEmbed", "aiWriting", "database", "newSubDocRef",
"insertAsset", "insertIframeURL", "insertImgURL", "insertVideoURL", "insertAudioURL", "emoji",
@@ -109,12 +111,15 @@ const executeInsert = (context: ICommandContextSnapshot, id: string) => {
protyle.hint.lastIndex = -1;
focusByRange(protyle.toolbar.range);
protyle.hint.fill(item.value, protyle, false);
- if (id === "ref" || id === "blockEmbed") {
+ if ((context.environment === "mobile" || context.environment === "browser-mobile") &&
+ (id === "ref" || id === "blockEmbed")) {
+ /// #if MOBILE
callMobileAppShowKeyboard();
+ /// #endif
}
};
-export const ensureMobileInsertCommands = (app: object) => {
+export const ensureInsertCommands = (app: object, mobile: boolean) => {
if (initializedApps.has(app)) {
return;
}
@@ -122,15 +127,16 @@ export const ensureMobileInsertCommands = (app: object) => {
const owner = {};
const disposers: Array<() => boolean> = [];
try {
- [...INSERT_IDS, "insertImage", "insertPhoto"].forEach((id, order) => {
+ [...INSERT_IDS, ...(mobile ? ["insertImage", "insertPhoto"] : [])].forEach((id, order) => {
const command: ICommandDefinition = {
- id: `core.mobile.insert.${id}`,
+ id: `core.${mobile ? "mobile." : ""}insert.${id}`,
category: "core",
label: () => label(id),
englishLabel: () => label(id, true),
keywords: () => [id],
surfaces: ["commandPanel"],
- platform: environment => environment === "mobile" || environment === "browser-mobile",
+ platform: environment => mobile ? environment === "mobile" || environment === "browser-mobile" :
+ environment === "desktop" || environment === "desktop-window" || environment === "browser-desktop",
order: 2000 + order,
when: context => available(context, id),
execute: context => executeInsert(context, id),