Skip to content

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

:sparkles: Support plugin actions in top bar context menus https://github.com/siyuan-note/siyuan/issues/19305

Daniel4d10b4c25791
diff --git a/app/src/menus/topBar.test.ts b/app/src/menus/topBar.test.ts
new file mode 100644
index 0000000000..f8960b366d
--- /dev/null
+++ b/app/src/menus/topBar.test.ts
@@ -0,0 +1,67 @@
+import * as assert from "node:assert/strict";
+import test from "node:test";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import {runInNewContext} from "node:vm";
+import * as ts from "typescript";
+
+const source = ts.transpileModule(readFileSync(resolve(process.cwd(), "src/menus/topBar.ts"), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+}).outputText;
+
+const open = (pluginItems: IMenu[], target?: Element) => {
+ const items: IMenu[] = [];
+ let detail: {element: Element | null; entryPath: string | null};
+ const menu = {
+ remove: () => { items.length = 0; },
+ element: {setAttribute: (): void => undefined},
+ append: (item: IMenu) => items.push(item),
+ };
+ const dependencies = {
+ Constants: {MENU_BAR_ENTRY: "barEntry"},
+ TOP_BAR_ROOT_PATH: "topBar",
+ MenuItem: class {constructor(public element: IMenu) {}},
+ refreshTopBarEntryCatalog: (): void => undefined,
+ buildEntryVisibilityToggleItem: () => ({id: "hide"}),
+ buildEntryVisibilityMenuItems: () => [{id: "visible"}, {id: "hidden", checked: false}],
+ emitOpenMenu: (options: {type: string; appendToMenu: boolean; detail: typeof detail}) => {
+ assert.equal(options.type, "open-menu-topbar");
+ assert.equal(options.appendToMenu, false);
+ detail = options.detail;
+ return pluginItems;
+ },
+ };
+ const exports: {initTopBarMenu?: (target?: Element) => unknown} = {};
+ runInNewContext(source, {exports, require: () => dependencies, window: {siyuan: {menus: {menu}}}});
+ exports.initTopBarMenu(target);
+ return {items, detail};
+};
+
+test("top bar plugin actions precede visibility controls and retain target identity", () => {
+ const target = {getAttribute: () => "plugin-test"} as unknown as Element;
+ const {items, detail} = open([{id: "action"}], target);
+ assert.equal(detail.element, target);
+ assert.equal(detail.entryPath, "topBar.plugin-test");
+ assert.deepEqual(items.map(item => item.type || item.id), [
+ "action", "separator", "hide", "separator", "visible", "hidden",
+ ]);
+});
+
+test("blank top bar supplies null context and normalizes plugin separators", () => {
+ const separator: IMenu = {type: "separator"};
+ const {items, detail} = open([
+ separator, {id: "first"}, separator, {id: "ignored", ignore: true}, separator,
+ {id: "second"}, separator, separator,
+ ]);
+ assert.equal(detail.element, null);
+ assert.equal(detail.entryPath, null);
+ assert.deepEqual(items.map(item => item.type || item.id), [
+ "first", "separator", "second", "separator", "visible", "hidden",
+ ]);
+});
+
+test("empty and ignored plugin groups do not add separators", () => {
+ for (const pluginItems of [[], [{type: "separator"}, {id: "ignored", ignore: true}] as IMenu[]]) {
+ assert.deepEqual(open(pluginItems).items.map(item => item.id), ["visible", "hidden"]);
+ }
+});
diff --git a/app/src/menus/topBar.ts b/app/src/menus/topBar.ts
index ec8ead7149..490663ee48 100644
--- a/app/src/menus/topBar.ts
+++ b/app/src/menus/topBar.ts
@@ -3,14 +3,41 @@ import {MenuItem} from "./Menu";
import {buildEntryVisibilityMenuItems, buildEntryVisibilityToggleItem} from "../config/entryVisibility/menu";
import {TOP_BAR_ROOT_PATH} from "../config/entryVisibility/catalog";
import {refreshTopBarEntryCatalog} from "../config/entryVisibility/runtime";
+import {emitOpenMenu} from "../plugin/EventBus";
export const initTopBarMenu = (target?: Element) => {
const menu = window.siyuan.menus.menu;
menu.remove();
menu.element.setAttribute("data-name", Constants.MENU_BAR_ENTRY);
refreshTopBarEntryCatalog();
+ const key = target?.getAttribute("data-topbar-entry") || null;
+ const pluginItems = emitOpenMenu({
+ type: "open-menu-topbar",
+ detail: {element: target || null, entryPath: key ? `${TOP_BAR_ROOT_PATH}.${key}` : null},
+ appendToMenu: false,
+ });
+ let hasPluginItem = false;
+ let separator: IMenu | undefined;
+ pluginItems.forEach((item) => {
+ if (item.ignore) {
+ return;
+ }
+ if (item.type === "separator") {
+ separator = item;
+ return;
+ }
+ // 仅在有效项目之间保留分隔线,组尾分隔线由顶栏菜单统一添加。
+ if (hasPluginItem && separator) {
+ menu.append(new MenuItem(separator).element);
+ }
+ menu.append(new MenuItem(item).element);
+ hasPluginItem = true;
+ separator = undefined;
+ });
+ if (hasPluginItem) {
+ menu.append(new MenuItem({id: "separator_pluginTop", type: "separator"}).element);
+ }
if (target) {
- const key = target.getAttribute("data-topbar-entry");
const item = key ? buildEntryVisibilityToggleItem(`${TOP_BAR_ROOT_PATH}.${key}`) : undefined;
if (item) {
menu.append(new MenuItem(item).element);
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index dfd103173e..a896bc13ef 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -11,7 +11,7 @@ type TEventBus = "ws-main" | "sync-start" | "sync-end" | "sync-fail" |
"click-blockicon" | "click-editorcontent" | "click-pdf" | "click-editortitleicon" | "click-flashcard-action" |
"open-noneditableblock" |
"open-menu-blockref" | "open-menu-fileannotationref" | "open-menu-tag" | "open-menu-link" | "open-menu-image" |
- "open-menu-av" | "open-menu-content" | "open-menu-breadcrumbmore" | "open-menu-doctree" | "open-menu-inbox" |
+ "open-menu-av" | "open-menu-content" | "open-menu-breadcrumbmore" | "open-menu-doctree" | "open-menu-inbox" | "open-menu-topbar" |
"open-siyuan-url-plugin" | "open-siyuan-url-block" | "open-asset" | "open-link" | "opened-notebook" |
"closed-notebook" |
"paste" | "before-upload-assets" | "before-search-results-render" |