Skip to content

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

:sparkles: Support stable plugin top bar button IDs and removal https://github.com/siyuan-note/siyuan/issues/18911

Daniela86e91930a7b
diff --git a/app/src/layout/topBar.ts b/app/src/layout/topBar.ts
index c38b0a96bb..0751330052 100644
--- a/app/src/layout/topBar.ts
+++ b/app/src/layout/topBar.ts
@@ -130,7 +130,7 @@ export const initBar = (app: App) => {
if (!itemId) {
return;
}
- const hideElement = toolbarElement.querySelector("#" + itemId);
+ const hideElement = document.getElementById(itemId);
const useElement = hideElement.querySelector("use");
const menuOptions: IMenu = {
label: itemId === "toolbarVIP" ? window.siyuan.languages.account : hideElement.getAttribute("aria-label"),
diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts
index 9c5a55afca..bfbb32c9a3 100644
--- a/app/src/layout/util.ts
+++ b/app/src/layout/util.ts
@@ -855,7 +855,7 @@ export const resizeTopBar = () => {
}
window.siyuan.storage[Constants.LOCAL_PLUGINTOPUNPIN].forEach((id: string) => {
- toolbarElement.querySelector("#" + id)?.classList.add("fn__none");
+ document.getElementById(id)?.classList.add("fn__none");
});
};
diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts
index 0d24631aa4..c5ca482efa 100644
--- a/app/src/plugin/index.ts
+++ b/app/src/plugin/index.ts
@@ -208,9 +208,10 @@ export class Plugin {
}
public addTopBar(options: {
+ id?: string,
icon: string,
title: string,
- position?: "south" | "left",
+ position?: "right" | "left",
callback: (evt: MouseEvent) => void
}) {
options.icon = options.icon.trim();
@@ -218,10 +219,25 @@ export class Plugin {
console.error(`plugin ${this.name} addTopBar error: icon must be svg id or svg tag`);
return;
}
- const iconElement = document.createElement("div");
+ let iconElement = typeof options.id === "string" ? this.topBarIcons.find(item =>
+ item.getAttribute("data-id") === options.id) as HTMLElement : undefined;
+ const isNew = !iconElement;
+ if (!iconElement) {
+ iconElement = document.createElement("div");
+ if (typeof options.id === "string") {
+ iconElement.id = `plugin_${encodeURIComponent(this.name)}:${encodeURIComponent(options.id)}`;
+ iconElement.setAttribute("data-id", options.id);
+ } else {
+ let index = this.topBarIcons.length;
+ do {
+ iconElement.id = `plugin_${this.name}_${index}`;
+ index++;
+ } while (this.topBarIcons.some(item => item.getAttribute("id") === iconElement.id));
+ }
+ }
+ const previousLocation = iconElement.getAttribute("data-location");
iconElement.setAttribute("data-menu", "true");
- iconElement.addEventListener("click", options.callback);
- iconElement.id = `plugin_${this.name}_${this.topBarIcons.length}`;
+ iconElement.onclick = options.callback;
if (isMobile()) {
iconElement.className = "b3-menu__item";
const iconHTML = options.icon.startsWith("icon") ?
@@ -233,29 +249,48 @@ export class Plugin {
iconElement.className = "toolbar__item ariaLabel";
iconElement.setAttribute("aria-label", options.title);
iconElement.innerHTML = options.icon.startsWith("icon") ? `<svg><use xlink:href="#${options.icon}"></use></svg>` : options.icon;
- iconElement.addEventListener("click", options.callback);
iconElement.setAttribute("data-location", options.position || "right");
- resizeTopBar();
}
if (isMobile() && window.siyuan.storage) {
- if (!window.siyuan.storage[Constants.LOCAL_PLUGINTOPUNPIN].includes(iconElement.id)) {
+ if (!window.siyuan.storage[Constants.LOCAL_PLUGINTOPUNPIN].includes(iconElement.id) &&
+ !document.contains(iconElement)) {
document.getElementById("menuPluginTopBar")?.after(iconElement);
}
} else if (!isWindow() && window.siyuan.storage) {
if (window.siyuan.storage[Constants.LOCAL_PLUGINTOPUNPIN].includes(iconElement.id)) {
iconElement.classList.add("fn__none");
}
- document.querySelector("#" + (iconElement.getAttribute("data-location") === "right" ? "barPlugins" : "drag"))?.before(iconElement);
+ if (!document.contains(iconElement) || previousLocation !== iconElement.getAttribute("data-location")) {
+ document.querySelector("#" + (iconElement.getAttribute("data-location") === "right" ? "barPlugins" : "drag"))?.before(iconElement);
+ }
+ }
+ if (isNew) {
+ this.topBarIcons.push(iconElement);
}
- this.topBarIcons.push(iconElement);
/// #if !MOBILE
if (!isWindow()) {
+ resizeTopBar();
setTabPosition(true);
}
/// #endif
return iconElement;
}
+ public removeTopBar(id: string) {
+ const index = this.topBarIcons.findIndex(item => item.getAttribute("data-id") === id);
+ if (index === -1) {
+ return;
+ }
+ this.topBarIcons[index].remove();
+ this.topBarIcons.splice(index, 1);
+ /// #if !MOBILE
+ if (!isWindow()) {
+ resizeTopBar();
+ setTabPosition(true);
+ }
+ /// #endif
+ }
+
public addBreadcrumbButton(options: {
id: string,
icon: string,