Skip to content

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

:recycle: Share snapshot actions and frontend capability registration https://github.com/siyuan-note/siyuan/issues/20261

Daniele63007b656eb
diff --git a/app/src/history/history.ts b/app/src/history/history.ts
index 564a89717a..a056c0aece 100644
--- a/app/src/history/history.ts
+++ b/app/src/history/history.ts
@@ -23,6 +23,7 @@ import {openDocHistory} from "./doc";
import {getRepoSnapshotRange, getRepoSnapshotType, initRepoPanel, updateRepoSelection} from "./repoPanel";
import {repoSnapshotInRange} from "./repoRange";
import {MenuItem} from "../menus/Menu";
+import {renderSnapshotActions} from "./snapshotActions";
let historyEditor: Protyle;
const repoPanelCleanup = new WeakMap<Element, () => void>();
@@ -175,103 +176,7 @@ const renderRepoItem = (response: IWebSocketData, element: Element, type: string
updateRepoSelection(element);
return;
}
- let actionHTML = "";
- /// #if MOBILE
- if (type === "getCloudRepoTagSnapshots") {
- actionHTML = `<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="downloadSnapshot">
- <svg><use xlink:href="#iconDownload"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.download}
-</span>
-<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="downloadRollback">
- <svg><use xlink:href="#iconUndo"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.downloadRollback}
-</span>
-<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="removeCloudRepoTagSnapshot">
- <svg><use xlink:href="#iconTrashcan"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.remove}
-</span>
-<span class="fn__flex-1"></span>`;
- } else if (type === "getCloudRepoSnapshots") {
- actionHTML = `<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="downloadSnapshot">
- <svg><use xlink:href="#iconDownload"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.download}
-</span>
-<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="downloadRollback">
- <svg><use xlink:href="#iconUndo"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.downloadRollback}
-</span>
-<span class="fn__flex-1"></span>`;
- } else if (type === "getRepoTagSnapshots") {
- actionHTML = `<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="uploadSnapshot">
- <svg><use xlink:href="#iconUpload"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.upload}
-</span>
-<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="rollback">
- <svg><use xlink:href="#iconUndo"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.rollback}
-</span>
-<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="removeRepoTagSnapshot">
- <svg><use xlink:href="#iconTrashcan"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.remove}
-</span>
-<span class="fn__flex-1"></span>`;
- } else if (type === "getRepoSnapshots") {
- actionHTML = `<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="genTag">
- <svg><use xlink:href="#iconTag"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.tagSnapshot}
-</span>
-<span class="fn__flex-1"></span>
-<span class="b3-list-item__action" data-type="rollback">
- <svg><use xlink:href="#iconUndo"></use></svg>
- <span class="fn__space"></span>
- ${window.siyuan.languages.rollback}
-</span>
-<span class="fn__flex-1"></span>`;
- }
- /// #else
- if (type === "getCloudRepoTagSnapshots") {
- actionHTML = `<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="downloadSnapshot" aria-label="${window.siyuan.languages.download}"><svg><use xlink:href="#iconDownload"></use></svg></span>
-<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="downloadRollback" aria-label="${window.siyuan.languages.downloadRollback}"><svg><use xlink:href="#iconUndo"></use></svg></span>
-<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="removeCloudRepoTagSnapshot" aria-label="${window.siyuan.languages.remove}"><svg><use xlink:href="#iconTrashcan"></use></svg></span>`;
- } else if (type === "getCloudRepoSnapshots") {
- actionHTML = `<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="downloadSnapshot" aria-label="${window.siyuan.languages.download}"><svg><use xlink:href="#iconDownload"></use></svg></span>
-<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="downloadRollback" aria-label="${window.siyuan.languages.downloadRollback}"><svg><use xlink:href="#iconUndo"></use></svg></span>`;
- } else if (type === "getRepoTagSnapshots") {
- actionHTML = `<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="uploadSnapshot" aria-label="${window.siyuan.languages.upload}"><svg><use xlink:href="#iconUpload"></use></svg></span>
-<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="rollback" aria-label="${window.siyuan.languages.rollback}"><svg><use xlink:href="#iconUndo"></use></svg></span>
-<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="removeRepoTagSnapshot" aria-label="${window.siyuan.languages.remove}"><svg><use xlink:href="#iconTrashcan"></use></svg></span>`;
- } else if (type === "getRepoSnapshots") {
- actionHTML = `<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="genTag" aria-label="${window.siyuan.languages.tagSnapshot}"><svg><use xlink:href="#iconTag"></use></svg></span>
-<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="rollback" aria-label="${window.siyuan.languages.rollback}"><svg><use xlink:href="#iconUndo"></use></svg></span>`;
- }
- /// #endif
- if (["getRepoTagSnapshots", "getRepoSnapshots"].includes(type) && !window.siyuan.config.readonly) {
- actionHTML = `<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="editSnapshotMemo" aria-label="${window.siyuan.languages.editSnapshotMemo}"><svg><use xlink:href="#iconEdit"></use></svg></span>` + actionHTML;
- }
- if (isMobile()) {
- actionHTML = actionHTML.replace(/class="b3-list-item__action([^"]*)" data-type="([^"]+)"/g,
- (match, classes, action) => ["rollback", "downloadRollback"].includes(action) ? match :
- `class="b3-list-item__action history__snapshot-menu-action${classes}" data-type="${action}"`);
- actionHTML = actionHTML.replaceAll('<span class="fn__flex-1"></span>', "");
- }
+ const actionHTML = renderSnapshotActions(type, window.siyuan.languages, isMobile(), window.siyuan.config.readonly);
const memos = new Map<string, string>();
snapshotMemos.set(element, memos);
let repoHTML = "";
diff --git a/app/src/history/repoTags.test.ts b/app/src/history/repoTags.test.ts
index 0bdee6958a..156231ebad 100644
--- a/app/src/history/repoTags.test.ts
+++ b/app/src/history/repoTags.test.ts
@@ -3,6 +3,7 @@ import {readFileSync} from "node:fs";
import {join} from "node:path";
import {test} from "node:test";
import {ScriptTarget, transpileModule} from "typescript";
+import {renderSnapshotActions} from "./snapshotActions";
import {escapeHtml, escapeAttr} from "../util/escape";
test("snapshot lists display all escaped tags and preserve tag-specific actions on desktop and mobile", () => {
@@ -25,6 +26,7 @@ test("snapshot lists display all escaped tags and preserve tag-specific actions
const list = {innerHTML: ""};
const pane = {querySelector: (selector: string) => selector.includes("repoList") ? list : {getAttribute: () => "[]"}};
const dependencies = {
+ renderSnapshotActions,
window: {siyuan: {config: {}, languages: {tagSnapshot: "Tag snapshot"}}},
isMobile: () => mobile, snapshotMemos: new WeakMap(), updateRepoSelection: () => {}, escapeHtml, escapeAttr,
};
diff --git a/app/src/history/snapshotActions.test.ts b/app/src/history/snapshotActions.test.ts
new file mode 100644
index 0000000000..35290ad4b4
--- /dev/null
+++ b/app/src/history/snapshotActions.test.ts
@@ -0,0 +1,33 @@
+import {test} from "node:test";
+import * as assert from "node:assert/strict";
+import {renderSnapshotActions} from "./snapshotActions";
+
+test("snapshot actions retain platform presentation, order and read-only behavior", () => {
+ const languages = {download: "Download", downloadRollback: "Download and rollback", remove: "Remove",
+ upload: "Upload", rollback: "Rollback", tagSnapshot: "Tag", editSnapshotMemo: "Edit memo"};
+ const cases = {
+ getCloudRepoTagSnapshots: ["downloadSnapshot", "downloadRollback", "removeCloudRepoTagSnapshot"],
+ getCloudRepoSnapshots: ["downloadSnapshot", "downloadRollback"],
+ getRepoTagSnapshots: ["uploadSnapshot", "rollback", "removeRepoTagSnapshot"],
+ getRepoSnapshots: ["genTag", "rollback"],
+ };
+ for (const [type, expected] of Object.entries(cases)) {
+ for (const mobile of [false, true]) {
+ for (const readonly of [false, true]) {
+ const editable = type.startsWith("getRepo") && !readonly;
+ const html = renderSnapshotActions(type, languages, mobile, readonly);
+ assert.deepEqual([...html.matchAll(/data-type="([^"]+)"/g)].map(match => match[1]),
+ editable ? ["editSnapshotMemo", ...expected] : expected);
+ assert.doesNotMatch(html, /fn__flex-1/);
+ if (mobile) {
+ assert.match(html, /fn__space/);
+ assert.match(html, /history__snapshot-menu-action/);
+ assert.doesNotMatch(html, /history__snapshot-menu-action[^>]*data-type="(?:rollback|downloadRollback)"/);
+ } else {
+ assert.match(html, /b3-tooltips/);
+ assert.doesNotMatch(html, /fn__space|history__snapshot-menu-action/);
+ }
+ }
+ }
+ }
+});
diff --git a/app/src/history/snapshotActions.ts b/app/src/history/snapshotActions.ts
new file mode 100644
index 0000000000..189ac3a27e
--- /dev/null
+++ b/app/src/history/snapshotActions.ts
@@ -0,0 +1,30 @@
+const actions: Record<string, {icon: string, label: string}> = {
+ downloadSnapshot: {icon: "Download", label: "download"},
+ downloadRollback: {icon: "Undo", label: "downloadRollback"},
+ removeCloudRepoTagSnapshot: {icon: "Trashcan", label: "remove"},
+ uploadSnapshot: {icon: "Upload", label: "upload"},
+ rollback: {icon: "Undo", label: "rollback"},
+ removeRepoTagSnapshot: {icon: "Trashcan", label: "remove"},
+ genTag: {icon: "Tag", label: "tagSnapshot"},
+ editSnapshotMemo: {icon: "Edit", label: "editSnapshotMemo"},
+};
+
+const snapshotActions: Record<string, string[]> = {
+ getCloudRepoTagSnapshots: ["downloadSnapshot", "downloadRollback", "removeCloudRepoTagSnapshot"],
+ getCloudRepoSnapshots: ["downloadSnapshot", "downloadRollback"],
+ getRepoTagSnapshots: ["uploadSnapshot", "rollback", "removeRepoTagSnapshot"],
+ getRepoSnapshots: ["genTag", "rollback"],
+};
+
+export const renderSnapshotActions = (type: string, languages: Record<string, string>, mobile: boolean, readonly: boolean) => {
+ const entries = [...(snapshotActions[type] || [])];
+ if (["getRepoTagSnapshots", "getRepoSnapshots"].includes(type) && !readonly) {
+ entries.unshift("editSnapshotMemo");
+ }
+ return entries.map(id => {
+ const {icon, label} = actions[id];
+ const tooltip = !mobile || id === "editSnapshotMemo";
+ const menuAction = mobile && !["rollback", "downloadRollback"].includes(id);
+ return `<span class="b3-list-item__action${menuAction ? " history__snapshot-menu-action" : ""}${tooltip ? " b3-tooltips b3-tooltips__w" : ""}" data-type="${id}"${tooltip ? ` aria-label="${languages[label]}"` : ""}><svg><use xlink:href="#icon${icon}"></use></svg>${tooltip ? "" : `<span class="fn__space"></span>${languages[label]}`}</span>`;
+ }).join("\n");
+};
diff --git a/app/src/layout/dock/agent/frontendCapabilities.test.ts b/app/src/layout/dock/agent/frontendCapabilities.test.ts
new file mode 100644
index 0000000000..81e7982e3e
--- /dev/null
+++ b/app/src/layout/dock/agent/frontendCapabilities.test.ts
@@ -0,0 +1,66 @@
+import {test} from "node:test";
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import type {App} from "../../../index";
+
+const {parse} = require("ifdef-loader/preprocessor");
+
+for (const mobile of [false, true]) {
+ test(`frontend capabilities keep platform actions and shared validation (${mobile ? "mobile" : "desktop"})`, async () => {
+ const actions: string[] = [];
+ const delayed: (() => void)[] = [];
+ const block = {scrollIntoView: () => actions.push("scroll"),
+ classList: {add: () => actions.push("highlight"), remove: () => actions.push("unhighlight")}};
+ const editor = {protyle: {wysiwyg: {element: {querySelector: () => block}}}};
+ const input = {value: "", dispatchEvent: () => actions.push("filter")};
+ const dialog = {element: {querySelector: () => input}};
+ const constants = {CB_GET_FOCUS: "focus", DIALOG_GLOBALSEARCH: "search"};
+ const dependencies: Record<string, unknown> = {
+ "../../../config": {openSetting: () => { actions.push("settings"); return dialog; }},
+ "../../getAll": {getAllEditor: () => [{protyle: {wysiwyg: {element: {querySelector: (): null => null}}}}, editor]},
+ "../../../mobile/editor": {getCurrentEditor: () => editor,
+ openMobileFileById: (_app: unknown, id: string, flags: string[]) => actions.push("document:" + id + ":" + flags[0])},
+ "../../../mobile/agent/MobileAgentChat": {hideMobileAgent: () => actions.push("hide"), reopenMobileAgent: () => {}},
+ "../../../mobile/menu": {openMobileSetting: () => actions.push("settings")},
+ "../../../editor/util": {openFileById: (options: {id: string, action: string[]}) =>
+ actions.push("document:" + options.id + ":" + options.action[0])},
+ "../../../constants": {Constants: constants},
+ "../../../search/spread": {openSearch: (options: {key: string}) => actions.push("search:" + options.key)},
+ "../../../mobile/menu/search": {popSearch: () => actions.push("search")},
+ };
+ const source = parse(readFileSync("src/layout/dock/agent/frontendCapabilities.ts", "utf8"),
+ {MOBILE: mobile, BROWSER: true}, false, true, "frontendCapabilities.ts");
+ const code = transpileModule(source, {compilerOptions: {
+ module: ModuleKind.CommonJS, target: ScriptTarget.ES2021,
+ }}).outputText;
+ const exports = {} as typeof import("./frontendCapabilities");
+ runInNewContext(code, {exports, require: (name: string) => {
+ assert.ok(name in dependencies, name);
+ return dependencies[name];
+ }, window: {siyuan: {dialogs: [dialog]}}, document: {getElementById: () => input},
+ Event: class {}, InputEvent: class {}, setTimeout: (callback: () => void) => delayed.push(callback)});
+ const capability = (name: string) => exports.lookupCapability("native/frontend/" + name);
+ assert.equal(exports.listCapabilityManifests().length, 4);
+ for (const name of ["focus_block", "open_document"]) {
+ assert.equal((await capability(name).handler({}, {} as App)).error, "missing required argument: id");
+ }
+ assert.deepEqual(actions, []);
+ await capability("open_setting").handler({query: " option "}, {} as App);
+ assert.deepEqual(actions, mobile ? ["hide", "settings"] : ["filter"]);
+ actions.length = 0;
+ assert.equal((await capability("focus_block").handler({id: "block"}, {} as App)).result,
+ "Focused block block in the active editor.");
+ assert.deepEqual(actions, mobile ? ["hide", "scroll", "highlight"] : ["scroll", "highlight"]);
+ delayed[0]();
+ assert.equal(actions.at(-1), "unhighlight");
+ actions.length = 0;
+ await capability("open_document").handler({id: "doc"}, {} as App);
+ assert.deepEqual(actions, mobile ? ["hide", "document:doc:focus"] : ["document:doc:focus"]);
+ actions.length = 0;
+ await capability("open_search").handler({query: " foo "}, {} as App);
+ assert.deepEqual(actions, mobile ? ["hide", "search", "filter"] : ["search:foo"]);
+ if (mobile) assert.equal(input.value, "foo");
+ });
+}
diff --git a/app/src/layout/dock/agent/frontendCapabilities.ts b/app/src/layout/dock/agent/frontendCapabilities.ts
index 7076fda633..00c71c98e7 100644
--- a/app/src/layout/dock/agent/frontendCapabilities.ts
+++ b/app/src/layout/dock/agent/frontendCapabilities.ts
@@ -92,24 +92,26 @@ export const unregisterCapability = (id: string, generation?: number) => {
capabilityRegistry.delete(id);
};
-/// #if !MOBILE
+let mobileFrontend = false;
+/// #if MOBILE
+mobileFrontend = true;
+/// #endif
+
registerCapability({
id: "native/frontend/open_setting",
title: "Open settings",
- description: "Open SiYuan settings and optionally filter settings by a search query.",
+ description: mobileFrontend ? "Open SiYuan settings and optionally provide a search query." :
+ "Open SiYuan settings and optionally filter settings by a search query.",
inputSchema: {type: "object", properties: {query: {type: "string"}}, additionalProperties: false},
source: "native",
handler: async (args, app) => {
const query = (args.query as string | undefined)?.trim();
+ /// #if !MOBILE
const {openSetting} = await import("../../../config");
- // 已有设置对话框时复用该实例,避免 openSetting() 销毁现有实例后返回待销毁的对象。
+ // 已有设置对话框时复用该实例,避免销毁现有实例后返回待销毁的对象。
const existing = window.siyuan.dialogs.find(d => d.element.querySelector(".config__tab-container"));
- let dialog = existing;
- if (!dialog) {
- dialog = openSetting(app);
- }
+ const dialog = existing || openSetting(app);
if (query) {
- // 填充设置面板的内置搜索框并触发实时筛选。
const input = dialog.element.querySelector(".config__side .b3-text-field") as HTMLInputElement;
if (input) {
input.value = query;
@@ -118,13 +120,23 @@ registerCapability({
return {result: `Opened the settings panel and filtered by "${query}".`};
}
return {result: "Opened the settings panel."};
+ /// #else
+ const [{hideMobileAgent, reopenMobileAgent}, {openMobileSetting}] = await Promise.all([
+ import("../../../mobile/agent/MobileAgentChat"),
+ import("../../../mobile/menu"),
+ ]);
+ hideMobileAgent();
+ openMobileSetting(app, undefined, reopenMobileAgent);
+ return {result: query ? `Opened mobile settings for "${query}".` : "Opened mobile settings."};
+ /// #endif
},
});
registerCapability({
id: "native/frontend/focus_block",
title: "Focus block",
- description: "Scroll a block already loaded in an editor into view and highlight it.",
+ description: mobileFrontend ? "Scroll a block already loaded in the current editor into view and highlight it." :
+ "Scroll a block already loaded in an editor into view and highlight it.",
inputSchema: {type: "object", properties: {id: {type: "string"}}, required: ["id"], additionalProperties: false},
source: "native",
handler: async (args) => {
@@ -132,21 +144,32 @@ registerCapability({
if (!id) {
return {error: "missing required argument: id"};
}
- const {getAllEditor} = await import("../../getAll");
- // 找到包含目标块的编辑器并滚动到该块。
let blockEl: HTMLElement | null = null;
+ let editorScope = "any open editor";
+ /// #if !MOBILE
+ const {getAllEditor} = await import("../../getAll");
for (const editor of getAllEditor()) {
- const el = editor.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement | null;
- if (el) {
- blockEl = el;
+ blockEl = editor.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
+ if (blockEl) {
break;
}
}
+ /// #else
+ const [{getCurrentEditor}, {hideMobileAgent}] = await Promise.all([
+ import("../../../mobile/editor"),
+ import("../../../mobile/agent/MobileAgentChat"),
+ ]);
+ const editor = getCurrentEditor();
+ blockEl = editor?.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) || null;
+ editorScope = "the current editor";
+ if (blockEl) {
+ hideMobileAgent();
+ }
+ /// #endif
if (!blockEl) {
- return {error: `Block ${id} is not loaded in any open editor. Use open_document to open it first.`};
+ return {error: `Block ${id} is not loaded in ${editorScope}. Use open_document to open it first.`};
}
blockEl.scrollIntoView({behavior: "smooth", block: "center"});
- // 短暂高亮目标块以便用户定位。
blockEl.classList.add("protyle-wysiwyg--hl");
setTimeout(() => blockEl?.classList.remove("protyle-wysiwyg--hl"), 2000);
return {result: `Focused block ${id} in the active editor.`};
@@ -156,7 +179,8 @@ registerCapability({
registerCapability({
id: "native/frontend/open_document",
title: "Open document",
- description: "Open a SiYuan document by its block ID in the current app.",
+ description: mobileFrontend ? "Open a SiYuan document by its block ID in the mobile app." :
+ "Open a SiYuan document by its block ID in the current app.",
inputSchema: {type: "object", properties: {id: {type: "string"}}, required: ["id"], additionalProperties: false},
source: "native",
handler: async (args, app) => {
@@ -165,11 +189,21 @@ registerCapability({
return {error: "missing required argument: id"};
}
try {
+ /// #if !MOBILE
const [{openFileById}, {Constants}] = await Promise.all([
import("../../../editor/util"),
import("../../../constants"),
]);
await openFileById({app, id, action: [Constants.CB_GET_FOCUS]});
+ /// #else
+ const [{openMobileFileById}, {hideMobileAgent}, {Constants: mobileConstants}] = await Promise.all([
+ import("../../../mobile/editor"),
+ import("../../../mobile/agent/MobileAgentChat"),
+ import("../../../constants"),
+ ]);
+ hideMobileAgent();
+ openMobileFileById(app, id, [mobileConstants.CB_GET_FOCUS]);
+ /// #endif
return {result: `Opened document ${id}.`};
} catch (e) {
return {error: `Failed to open document ${id}: ${(e as Error).message}`};
@@ -180,100 +214,20 @@ registerCapability({
registerCapability({
id: "native/frontend/open_search",
title: "Open search",
- description: "Open the SiYuan search interface and optionally fill in a query.",
+ description: mobileFrontend ? "Open the SiYuan mobile search interface and optionally fill in a query." :
+ "Open the SiYuan search interface and optionally fill in a query.",
inputSchema: {type: "object", properties: {query: {type: "string"}}, additionalProperties: false},
source: "native",
handler: async (args, app) => {
const query = (args.query as string | undefined)?.trim();
+ /// #if !MOBILE
const [{openSearch}, {Constants}] = await Promise.all([
import("../../../search/spread"),
import("../../../constants"),
]);
await openSearch({app, hotkey: Constants.DIALOG_GLOBALSEARCH, key: query});
return {result: query ? `Opened search dialog with query "${query}".` : "Opened search dialog."};
- },
-});
-/// #else
-registerCapability({
- id: "native/frontend/open_setting",
- title: "Open settings",
- description: "Open SiYuan settings and optionally provide a search query.",
- inputSchema: {type: "object", properties: {query: {type: "string"}}, additionalProperties: false},
- source: "native",
- handler: async (args, app) => {
- const query = (args.query as string | undefined)?.trim();
- const [{hideMobileAgent, reopenMobileAgent}, {openMobileSetting}] = await Promise.all([
- import("../../../mobile/agent/MobileAgentChat"),
- import("../../../mobile/menu"),
- ]);
- hideMobileAgent();
- openMobileSetting(app, undefined, reopenMobileAgent);
- return {result: query ? `Opened mobile settings for "${query}".` : "Opened mobile settings."};
- },
-});
-
-registerCapability({
- id: "native/frontend/focus_block",
- title: "Focus block",
- description: "Scroll a block already loaded in the current editor into view and highlight it.",
- inputSchema: {type: "object", properties: {id: {type: "string"}}, required: ["id"], additionalProperties: false},
- source: "native",
- handler: async (args) => {
- const id = args.id as string | undefined;
- if (!id) {
- return {error: "missing required argument: id"};
- }
- const [{getCurrentEditor}, {hideMobileAgent}] = await Promise.all([
- import("../../../mobile/editor"),
- import("../../../mobile/agent/MobileAgentChat"),
- ]);
- const editor = getCurrentEditor();
- const block = editor?.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement | null;
- if (!block) {
- return {error: `Block ${id} is not loaded in the current editor. Use open_document to open it first.`};
- }
- hideMobileAgent();
- block.scrollIntoView({behavior: "smooth", block: "center"});
- block.classList.add("protyle-wysiwyg--hl");
- setTimeout(() => block.classList.remove("protyle-wysiwyg--hl"), 2000);
- return {result: `Focused block ${id} in the active editor.`};
- },
-});
-
-registerCapability({
- id: "native/frontend/open_document",
- title: "Open document",
- description: "Open a SiYuan document by its block ID in the mobile app.",
- inputSchema: {type: "object", properties: {id: {type: "string"}}, required: ["id"], additionalProperties: false},
- source: "native",
- handler: async (args, app) => {
- const id = args.id as string | undefined;
- if (!id) {
- return {error: "missing required argument: id"};
- }
- try {
- const [{openMobileFileById}, {hideMobileAgent}, {Constants}] = await Promise.all([
- import("../../../mobile/editor"),
- import("../../../mobile/agent/MobileAgentChat"),
- import("../../../constants"),
- ]);
- hideMobileAgent();
- openMobileFileById(app, id, [Constants.CB_GET_FOCUS]);
- return {result: `Opened document ${id}.`};
- } catch (e) {
- return {error: `Failed to open document ${id}: ${(e as Error).message}`};
- }
- },
-});
-
-registerCapability({
- id: "native/frontend/open_search",
- title: "Open search",
- description: "Open the SiYuan mobile search interface and optionally fill in a query.",
- inputSchema: {type: "object", properties: {query: {type: "string"}}, additionalProperties: false},
- source: "native",
- handler: async (args, app) => {
- const query = (args.query as string | undefined)?.trim();
+ /// #else
const [{popSearch}, {hideMobileAgent}] = await Promise.all([
import("../../../mobile/menu/search"),
import("../../../mobile/agent/MobileAgentChat"),
@@ -288,6 +242,6 @@ registerCapability({
}
}
return {result: query ? `Opened mobile search with query "${query}".` : "Opened mobile search."};
+ /// #endif
},
});
-/// #endif