Skip to content

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

:bug: Leave menu icons blank for custom plugin top bar elements https://github.com/siyuan-note/siyuan/issues/19305

Danielfff52770cdc7
diff --git a/app/src/config/entryVisibility/menu.ts b/app/src/config/entryVisibility/menu.ts
index 1ff2e88ac0..3d7b8b73dd 100644
--- a/app/src/config/entryVisibility/menu.ts
+++ b/app/src/config/entryVisibility/menu.ts
@@ -37,6 +37,10 @@ const getEntryIcon = (path: string): Pick<IMenu, "icon" | "iconHTML"> => {
if (!element) {
return {};
}
+ if (element.getAttribute("data-topbar-custom") === "true") {
+ // 插件自定义控件保留空图标占位,不提取其内部内容。
+ return {};
+ }
const customElement = element.querySelector(":scope > .b3-menu__icon--custom");
if (customElement) {
const iconElement = customElement.cloneNode(true) as HTMLElement;
diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts
index 2f81cf640a..ae8b4dbc22 100644
--- a/app/src/plugin/index.ts
+++ b/app/src/plugin/index.ts
@@ -291,6 +291,7 @@ export class Plugin {
const previousLocation = iconElement.getAttribute("data-location");
if (options.element) {
this.customTopBarElements.add(iconElement);
+ iconElement.setAttribute("data-topbar-custom", "true");
iconElement.setAttribute("aria-label", options.title);
iconElement.setAttribute("data-location", options.position || "right");
} else {
diff --git a/app/src/plugin/topBar.test.ts b/app/src/plugin/topBar.test.ts
index 70c227a4ba..63780c3570 100644
--- a/app/src/plugin/topBar.test.ts
+++ b/app/src/plugin/topBar.test.ts
@@ -70,6 +70,7 @@ test("custom top bar preserves content and handlers and registers a configurable
assert.equal(element.innerHTML, "Countdown");
assert.equal(element.onclick, onclick);
assert.equal(element.getAttribute("data-menu"), null);
+ assert.equal(element.getAttribute("data-topbar-custom"), "true");
assert.equal(element.id, "plugin_test:timer");
assert.equal(element.getAttribute("data-id"), "timer");
assert.equal(element.getAttribute("data-topbar-entry"), getPluginTopBarEntryKey("test", "timer"));
@@ -94,6 +95,7 @@ test("same ID replaces custom elements and supports switching between icons and
assert.equal(replacement.replacement, newIcon);
assert.equal(replacement.innerHTML, "Countdown");
assert.equal(newIcon.className, "toolbar__item ariaLabel");
+ assert.equal(newIcon.getAttribute("data-topbar-custom"), null);
assert.equal(plugin.topBarIcons.length, 1);
plugin.removeTopBar("timer");
assert.equal(newIcon.removed, true);
@@ -144,6 +146,38 @@ test("custom elements move into the toolbar and keep their slot until their side
assert.deepEqual(insertions, ["#barPlugins", "#drag"]);
});
+test("visibility menu leaves custom controls blank while preserving ordinary plugin icons", () => {
+ const source = readFileSync(resolve(process.cwd(), "src/config/entryVisibility/menu.ts"), "utf8");
+ const compiled = ts.transpileModule(source, {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText;
+ const custom = new TestElement();
+ custom.setAttribute("data-topbar-entry", "custom");
+ custom.setAttribute("data-topbar-custom", "true");
+ Object.assign(custom, {querySelector: () => { throw new Error("Custom content must not be inspected"); }});
+ const icon = new TestElement();
+ icon.setAttribute("data-topbar-entry", "icon");
+ Object.assign(icon, {querySelector: (selector: string) => selector === "use" ?
+ {getAttribute: () => "#iconClock"} : null});
+ const dependencies = {
+ TOP_BAR_ROOT_PATH: "topBar",
+ STATUS_BAR_ROOT_PATH: "statusBar",
+ buildEntryVisibilityMenuItems: (_path: string, runtime: {getEntryIcon: (path: string) => object}) => [
+ runtime.getEntryIcon("topBar.custom"), runtime.getEntryIcon("topBar.icon"),
+ ],
+ };
+ const exports: {buildEntryVisibilityMenuItems?: (path: string) => Array<Pick<IMenu, "icon" | "iconHTML">>} = {};
+ runInNewContext(compiled, {
+ exports,
+ require: () => dependencies,
+ document: {querySelectorAll: () => [custom, icon]},
+ window: {siyuan: {config: {readonly: false}, languages: {}}},
+ });
+ const items = exports.buildEntryVisibilityMenuItems("topBar");
+ assert.equal(Object.keys(items[0]).length, 0);
+ assert.equal(items[1].icon, "iconClock");
+});
+
test("plugin menu supports custom content without SVG icons", () => {
const source = readFileSync(resolve(process.cwd(), "src/plugin/openTopBarMenu.ts"), "utf8");
const compiled = ts.transpileModule(source, {