Skip to content

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

:art: Bind top bar context menu actions through addTopBar https://github.com/siyuan-note/siyuan/issues/19305

Daniele9bcc8ccefa0
diff --git a/app/src/menus/topBar.test.ts b/app/src/menus/topBar.test.ts
index f8960b366d..0faed7de97 100644
--- a/app/src/menus/topBar.test.ts
+++ b/app/src/menus/topBar.test.ts
@@ -11,7 +11,7 @@ const source = ts.transpileModule(readFileSync(resolve(process.cwd(), "src/menus
const open = (pluginItems: IMenu[], target?: Element) => {
const items: IMenu[] = [];
- let detail: {element: Element | null; entryPath: string | null};
+ let calledTarget: Element | undefined;
const menu = {
remove: () => { items.length = 0; },
element: {setAttribute: (): void => undefined},
@@ -21,47 +21,50 @@ const open = (pluginItems: IMenu[], target?: Element) => {
Constants: {MENU_BAR_ENTRY: "barEntry"},
TOP_BAR_ROOT_PATH: "topBar",
MenuItem: class {constructor(public element: IMenu) {}},
+ subMenu: class {public menus: 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;
+ fillTopBarContextMenu: (element: Element, menu: {menus: IMenu[]}) => {
+ calledTarget = element;
+ menu.menus.push(...pluginItems);
},
};
const exports: {initTopBarMenu?: (target?: Element) => unknown} = {};
runInNewContext(source, {exports, require: () => dependencies, window: {siyuan: {menus: {menu}}}});
exports.initTopBarMenu(target);
- return {items, detail};
+ return {items, calledTarget};
};
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");
+ const {items, calledTarget} = open([{id: "action"}], target);
+ assert.equal(calledTarget, target);
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", () => {
+test("button menu normalizes plugin separators", () => {
const separator: IMenu = {type: "separator"};
- const {items, detail} = open([
+ const {items} = open([
separator, {id: "first"}, separator, {id: "ignored", ignore: true}, separator,
{id: "second"}, separator, separator,
- ]);
- assert.equal(detail.element, null);
- assert.equal(detail.entryPath, null);
+ ], {getAttribute: () => "plugin-test"} as unknown as Element);
assert.deepEqual(items.map(item => item.type || item.id), [
- "first", "separator", "second", "separator", "visible", "hidden",
+ "first", "separator", "second", "separator", "hide", "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"]);
+ assert.deepEqual(open(pluginItems, {getAttribute: (): null => null} as unknown as Element)
+ .items.map(item => item.id), ["visible", "hidden"]);
}
});
+
+test("blank top bar never invokes plugin callbacks", () => {
+ const {items, calledTarget} = open([{id: "action"}]);
+ assert.equal(calledTarget, undefined);
+ assert.deepEqual(items.map(item => item.id), ["visible", "hidden"]);
+});
diff --git a/app/src/menus/topBar.ts b/app/src/menus/topBar.ts
index 490663ee48..58f8c30565 100644
--- a/app/src/menus/topBar.ts
+++ b/app/src/menus/topBar.ts
@@ -1,9 +1,9 @@
import {Constants} from "../constants";
-import {MenuItem} from "./Menu";
+import {MenuItem, subMenu} 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";
+import {fillTopBarContextMenu} from "../plugin/topBarContextMenu";
export const initTopBarMenu = (target?: Element) => {
const menu = window.siyuan.menus.menu;
@@ -11,11 +11,11 @@ export const initTopBarMenu = (target?: Element) => {
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,
- });
+ const pluginMenu = new subMenu();
+ if (target) {
+ fillTopBarContextMenu(target, pluginMenu);
+ }
+ const pluginItems = pluginMenu.menus;
let hasPluginItem = false;
let separator: IMenu | undefined;
pluginItems.forEach((item) => {
diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts
index ca44a3d593..9d50f2939d 100644
--- a/app/src/plugin/index.ts
+++ b/app/src/plugin/index.ts
@@ -1,6 +1,8 @@
import {sendGlobalShortcut} from "../boot/globalEvent/globalShortcut";
import type {App} from "../index";
import {EventBus} from "./EventBus";
+import type {subMenu} from "../menus/Menu";
+import {setTopBarContextMenu} from "./topBarContextMenu";
import {fetchPost, fetchSyncPost} from "../util/fetch";
import {ContractFormData} from "../util/contractFormData";
import {isMobile, isWindow} from "../util/functions";
@@ -237,6 +239,7 @@ export class Plugin {
title: string,
position?: "right" | "left",
element?: HTMLElement,
+ contextMenu?: (menu: subMenu) => void,
callback?: (evt: MouseEvent) => void
}) {
if (isPluginDisposed(this)) {
@@ -272,6 +275,7 @@ export class Plugin {
replacement.setAttribute(name, value);
}
});
+ setTopBarContextMenu(iconElement);
iconElement.replaceWith(replacement);
this.topBarIcons[this.topBarIcons.indexOf(iconElement)] = replacement;
iconElement = replacement;
@@ -292,6 +296,11 @@ export class Plugin {
}
}
const previousLocation = iconElement.getAttribute("data-location");
+ setTopBarContextMenu(iconElement, options.contextMenu ? (menu) => {
+ if (!isPluginDisposed(this)) {
+ options.contextMenu(menu);
+ }
+ } : undefined);
if (options.element) {
this.customTopBarElements.add(iconElement);
iconElement.setAttribute("data-topbar-custom", "true");
@@ -347,6 +356,7 @@ export class Plugin {
if (index === -1) {
return;
}
+ setTopBarContextMenu(this.topBarIcons[index]);
this.topBarIcons[index].remove();
this.topBarIcons.splice(index, 1);
/// #if !MOBILE
diff --git a/app/src/plugin/topBar.test.ts b/app/src/plugin/topBar.test.ts
index 861daa21d3..dcad4392d3 100644
--- a/app/src/plugin/topBar.test.ts
+++ b/app/src/plugin/topBar.test.ts
@@ -4,6 +4,7 @@ import {readFileSync} from "node:fs";
import {resolve} from "node:path";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
+import {setTopBarContextMenu, fillTopBarContextMenu} from "./topBarContextMenu";
import {getLegacyPluginTopBarEntryKey, getPluginTopBarEntryKey} from "./topBarKey";
const buildPluginMenu = (counts: number[], settings: boolean[], mobile = false, readonly = false) => {
@@ -126,6 +127,7 @@ const setup = (mobile = false, detached = false, mounted = false) => {
const toolbar = new TestElement();
const insertions: string[] = [];
const dependencies = {
+ setTopBarContextMenu,
isMobile: () => mobile,
isWindow: () => detached,
getPluginTopBarEntryKey,
@@ -155,6 +157,26 @@ const setup = (mobile = false, detached = false, mounted = false) => {
return {plugin, toolbar, insertions, refreshes: () => refreshes};
};
+test("context menu callbacks are isolated and cleared on update, replacement and removal", () => {
+ const {plugin} = setup();
+ const calls: string[] = [];
+ const menu = {} as import("../menus/Menu").subMenu;
+ const first = plugin.addTopBar({id: "first", title: "First", icon: "iconClock", contextMenu: () => calls.push("first")});
+ const second = plugin.addTopBar({id: "second", title: "Second", icon: "iconClock", contextMenu: () => calls.push("second")});
+ fillTopBarContextMenu(first, menu);
+ assert.deepEqual(calls, ["first"]);
+ plugin.addTopBar({id: "first", title: "Updated", icon: "iconClock", contextMenu: () => calls.push("updated")});
+ fillTopBarContextMenu(first, menu);
+ plugin.addTopBar({id: "first", title: "Cleared", icon: "iconClock"});
+ fillTopBarContextMenu(first, menu);
+ const custom = plugin.addTopBar({id: "second", title: "Custom", element: new TestElement(), contextMenu: () => calls.push("custom")});
+ fillTopBarContextMenu(second, menu);
+ fillTopBarContextMenu(custom, menu);
+ plugin.removeTopBar("second");
+ fillTopBarContextMenu(custom, menu);
+ assert.deepEqual(calls, ["first", "updated", "custom"]);
+});
+
test("custom top bar preserves content and handlers and registers a configurable entry", () => {
const {plugin, refreshes} = setup();
const element = new TestElement();
diff --git a/app/src/plugin/topBarContextMenu.ts b/app/src/plugin/topBarContextMenu.ts
new file mode 100644
index 0000000000..bfd105852b
--- /dev/null
+++ b/app/src/plugin/topBarContextMenu.ts
@@ -0,0 +1,15 @@
+import type {subMenu} from "../menus/Menu";
+
+const callbacks = new WeakMap<Element, (menu: subMenu) => void>();
+
+export const setTopBarContextMenu = (element: Element, callback?: (menu: subMenu) => void) => {
+ if (callback) {
+ callbacks.set(element, callback);
+ } else {
+ callbacks.delete(element);
+ }
+};
+
+export const fillTopBarContextMenu = (element: Element, menu: subMenu) => {
+ callbacks.get(element)?.(menu);
+};
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index a896bc13ef..dfd103173e 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-topbar" |
+ "open-menu-av" | "open-menu-content" | "open-menu-breadcrumbmore" | "open-menu-doctree" | "open-menu-inbox" |
"open-siyuan-url-plugin" | "open-siyuan-url-block" | "open-asset" | "open-link" | "opened-notebook" |
"closed-notebook" |
"paste" | "before-upload-assets" | "before-search-results-render" |