Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/18856

Vanessa66fb1d2e1035
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index 25faed0139..42328b168f 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -251,6 +251,20 @@
flex: 1;
}
+ &__plugin {
+ display: flex;
+ align-items: center;
+ flex: 0 1 auto;
+ min-width: 0;
+ overflow-x: auto;
+ overflow-y: hidden;
+ scrollbar-width: none;
+
+ &::-webkit-scrollbar {
+ display: none;
+ }
+ }
+
&__bar {
align-items: center;
flex-wrap: wrap;
diff --git a/app/src/plugin/breadcrumbButton.ts b/app/src/plugin/breadcrumbButton.ts
new file mode 100644
index 0000000000..156a47c787
--- /dev/null
+++ b/app/src/plugin/breadcrumbButton.ts
@@ -0,0 +1,80 @@
+import {BreadcrumbButtonRegistry, type IBreadcrumbButtonOptions} from "./breadcrumbButtonRegistry";
+
+const registry = new BreadcrumbButtonRegistry();
+const breadcrumbElements = new Map<IProtyle, HTMLElement>();
+
+const getDataID = (pluginName: string, id: string) => {
+ return `${encodeURIComponent(pluginName)}:${encodeURIComponent(id)}`;
+};
+
+const findButton = (element: HTMLElement, pluginName: string, id: string) => {
+ const dataID = getDataID(pluginName, id);
+ return Array.from(element.children).find(item => item.getAttribute("data-id") === dataID) as HTMLButtonElement;
+};
+
+const updateButton = (button: HTMLButtonElement, pluginName: string, options: IBreadcrumbButtonOptions,
+ protyle: IProtyle) => {
+ button.className = "block__icon fn__flex-center ariaLabel";
+ button.setAttribute("aria-label", options.title);
+ button.setAttribute("data-id", getDataID(pluginName, options.id));
+ button.setAttribute("data-plugin-name", pluginName);
+ button.type = "button";
+ button.innerHTML = options.icon.startsWith("icon") ?
+ `<svg><use xlink:href="#${options.icon}"></use></svg>` : options.icon;
+ button.onclick = (event) => {
+ options.callback(event, protyle);
+ };
+};
+
+const insertButton = (element: HTMLElement, pluginName: string, options: IBreadcrumbButtonOptions,
+ protyle: IProtyle) => {
+ const button = document.createElement("button");
+ updateButton(button, pluginName, options, protyle);
+ const nextPluginName = registry.getNextPluginName(pluginName);
+ const nextPluginButton = nextPluginName ? Array.from(element.children).find(item =>
+ item.getAttribute("data-plugin-name") === nextPluginName) : undefined;
+ element.insertBefore(button, nextPluginButton || null);
+};
+
+export const mountBreadcrumbButtons = (protyle: IProtyle, element: HTMLElement) => {
+ breadcrumbElements.set(protyle, element);
+ registry.getAll().forEach(item => {
+ insertButton(element, item.pluginName, item.options, protyle);
+ });
+};
+
+export const unmountBreadcrumbButtons = (protyle: IProtyle) => {
+ breadcrumbElements.delete(protyle);
+};
+
+export const addBreadcrumbButton = (pluginName: string, options: IBreadcrumbButtonOptions) => {
+ const replaced = registry.set(pluginName, options);
+ breadcrumbElements.forEach((element, protyle) => {
+ const button = replaced ? findButton(element, pluginName, options.id) : undefined;
+ if (button) {
+ updateButton(button, pluginName, options, protyle);
+ } else {
+ insertButton(element, pluginName, options, protyle);
+ }
+ });
+};
+
+export const removeBreadcrumbButton = (pluginName: string, id: string) => {
+ if (!registry.remove(pluginName, id)) {
+ return;
+ }
+ breadcrumbElements.forEach(element => {
+ findButton(element, pluginName, id)?.remove();
+ });
+};
+
+export const removeBreadcrumbButtons = (pluginName: string) => {
+ registry.removePlugin(pluginName);
+ breadcrumbElements.forEach(element => {
+ Array.from(element.children).forEach(item => {
+ if (item.getAttribute("data-plugin-name") === pluginName) {
+ item.remove();
+ }
+ });
+ });
+};
diff --git a/app/src/plugin/breadcrumbButtonRegistry.test.ts b/app/src/plugin/breadcrumbButtonRegistry.test.ts
new file mode 100644
index 0000000000..7187d56504
--- /dev/null
+++ b/app/src/plugin/breadcrumbButtonRegistry.test.ts
@@ -0,0 +1,50 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {BreadcrumbButtonRegistry, type IBreadcrumbButtonOptions} from "./breadcrumbButtonRegistry";
+
+const button = (id: string, title = id) => ({
+ id,
+ icon: "iconFullscreen",
+ title,
+ callback: () => undefined,
+}) as IBreadcrumbButtonOptions;
+
+describe("BreadcrumbButtonRegistry", () => {
+ it("replaces a button without changing its position", () => {
+ const registry = new BreadcrumbButtonRegistry();
+ registry.set("plugin-a", button("first"));
+ registry.set("plugin-a", button("second"));
+
+ assert.equal(registry.set("plugin-a", button("first", "updated")), true);
+ assert.deepEqual(registry.getAll().map(item => [item.pluginName, item.options.id, item.options.title]), [
+ ["plugin-a", "first", "updated"],
+ ["plugin-a", "second", "second"],
+ ]);
+ });
+
+ it("appends a re-added button to the end of its plugin group", () => {
+ const registry = new BreadcrumbButtonRegistry();
+ registry.set("plugin-a", button("first"));
+ registry.set("plugin-b", button("only"));
+ registry.set("plugin-a", button("second"));
+ registry.remove("plugin-a", "first");
+ registry.set("plugin-a", button("first"));
+
+ assert.deepEqual(registry.getAll().map(item => `${item.pluginName}/${item.options.id}`), [
+ "plugin-a/second",
+ "plugin-a/first",
+ "plugin-b/only",
+ ]);
+ assert.equal(registry.getNextPluginName("plugin-a"), "plugin-b");
+ });
+
+ it("removes all registrations owned by an unloaded plugin", () => {
+ const registry = new BreadcrumbButtonRegistry();
+ registry.set("plugin-a", button("first"));
+ registry.set("plugin-b", button("second"));
+
+ assert.equal(registry.removePlugin("plugin-a"), true);
+ assert.deepEqual(registry.getAll().map(item => item.pluginName), ["plugin-b"]);
+ assert.equal(registry.remove("plugin-a", "unknown"), false);
+ });
+});
diff --git a/app/src/plugin/breadcrumbButtonRegistry.ts b/app/src/plugin/breadcrumbButtonRegistry.ts
new file mode 100644
index 0000000000..1aad839ab9
--- /dev/null
+++ b/app/src/plugin/breadcrumbButtonRegistry.ts
@@ -0,0 +1,55 @@
+export interface IBreadcrumbButtonOptions {
+ id: string,
+ icon: string,
+ title: string,
+ callback: (event: MouseEvent, protyle: IProtyle) => void,
+}
+
+export class BreadcrumbButtonRegistry {
+ private plugins = new Map<string, Map<string, IBreadcrumbButtonOptions>>();
+
+ public set(pluginName: string, options: IBreadcrumbButtonOptions) {
+ let buttons = this.plugins.get(pluginName);
+ if (!buttons) {
+ buttons = new Map();
+ this.plugins.set(pluginName, buttons);
+ }
+ const replaced = buttons.has(options.id);
+ buttons.set(options.id, options);
+ return replaced;
+ }
+
+ public get(pluginName: string, id: string) {
+ return this.plugins.get(pluginName)?.get(id);
+ }
+
+ public remove(pluginName: string, id: string) {
+ return this.plugins.get(pluginName)?.delete(id) || false;
+ }
+
+ public removePlugin(pluginName: string) {
+ return this.plugins.delete(pluginName);
+ }
+
+ public getAll() {
+ const result: Array<{ pluginName: string, options: IBreadcrumbButtonOptions }> = [];
+ this.plugins.forEach((buttons, pluginName) => {
+ buttons.forEach(options => {
+ result.push({pluginName, options});
+ });
+ });
+ return result;
+ }
+
+ public getNextPluginName(pluginName: string) {
+ let found = false;
+ for (const [currentPluginName, buttons] of this.plugins) {
+ if (found && buttons.size > 0) {
+ return currentPluginName;
+ }
+ if (currentPluginName === pluginName) {
+ found = true;
+ }
+ }
+ }
+}
diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts
index 47f95953c4..0d24631aa4 100644
--- a/app/src/plugin/index.ts
+++ b/app/src/plugin/index.ts
@@ -25,6 +25,10 @@ import {normalizeStoragePath} from "../util/pathName";
import {Kernel} from "./kernel";
import {IAgentCapabilityEffects, registerCapability} from "../layout/dock/agent/frontendCapabilities";
import {isDisallowedTextInputHotkey, normalizePluginHotkey} from "../util/hotKeyPolicy";
+import {
+ addBreadcrumbButton as addPluginBreadcrumbButton,
+ removeBreadcrumbButton as removePluginBreadcrumbButton,
+} from "./breadcrumbButton";
const updatePluginKeymap = (pluginName: string, key: string, hotkey: unknown) => {
if (!window.siyuan.config.keymap.plugin) {
@@ -252,6 +256,25 @@ export class Plugin {
return iconElement;
}
+ public addBreadcrumbButton(options: {
+ id: string,
+ icon: string,
+ title: string,
+ callback: (event: MouseEvent, protyle: IProtyle) => void,
+ }) {
+ options.icon = options.icon.trim();
+ if (!options.icon.startsWith("icon") && !options.icon.startsWith("<svg")) {
+ console.error(`plugin ${this.name} addBreadcrumbButton error: icon must be svg id or svg tag`);
+ return options.id;
+ }
+ addPluginBreadcrumbButton(this.name, options);
+ return options.id;
+ }
+
+ public removeBreadcrumbButton(id: string) {
+ removePluginBreadcrumbButton(this.name, id);
+ }
+
public addStatusBar(options: {
element: HTMLElement,
position?: "right" | "left",
diff --git a/app/src/plugin/uninstall.ts b/app/src/plugin/uninstall.ts
index 3967c1a91a..d29759cc76 100644
--- a/app/src/plugin/uninstall.ts
+++ b/app/src/plugin/uninstall.ts
@@ -13,6 +13,7 @@ import {setStorageVal} from "../protyle/util/compatibility";
import {getAllEditor} from "../layout/getAll";
import {unregisterCapability} from "../layout/dock/agent/frontendCapabilities";
import {cancelAssetUploadsByPlugin} from "../protyle/upload/pluginEvent";
+import {removeBreadcrumbButtons} from "./breadcrumbButton";
export const uninstall = (app: App, name: string, isReload: boolean) => {
app.plugins.find((plugin: Plugin, index) => {
@@ -59,6 +60,7 @@ export const uninstall = (app: App, name: string, isReload: boolean) => {
plugin.topBarIcons.splice(i, 1);
i--;
}
+ removeBreadcrumbButtons(plugin.name);
// 移除插件注册的 Agent 能力
plugin.agentCapabilities.forEach((capability) => unregisterCapability(capability.id, capability.generation));
/// #if !MOBILE
diff --git a/app/src/protyle/breadcrumb/index.ts b/app/src/protyle/breadcrumb/index.ts
index c0a2956642..a6aa308257 100644
--- a/app/src/protyle/breadcrumb/index.ts
+++ b/app/src/protyle/breadcrumb/index.ts
@@ -39,6 +39,7 @@ import {decodeHTML, escapeAttr, escapeAriaLabel, escapeHtml, escapeSearchHighlig
import {refreshUndoButtons} from "../undo/globalUndo";
import {getAllEditor} from "../../layout/getAll";
import {genEmbedStatTip, type IBlockStat, type IEmbedStat} from "../../layout/status";
+import {mountBreadcrumbButtons} from "../../plugin/breadcrumbButton";
const genDocumentStatLabel = (stat: IBlockStat, statWithEmbed?: IBlockStat, embedStat?: IEmbedStat) => {
const runeEmbedAttrs = statWithEmbed ? ` class="ariaLabel" data-position="north" aria-label="${escapeAriaLabel(genEmbedStatTip(window.siyuan.languages.runeCountWithEmbed, statWithEmbed.runeCount, embedStat))}"` : "";
@@ -75,6 +76,7 @@ export class Breadcrumb {
`<button class="protyle-breadcrumb__icon" data-type="mobile-menu">${window.siyuan.languages.breadcrumb}</button>` :
'<div class="protyle-breadcrumb__bar"></div>'}
<span class="protyle-breadcrumb__space"></span>
+<div class="protyle-breadcrumb__plugin"></div>
<button class="protyle-breadcrumb__icon fn__none ariaLabel" aria-label="${updateHotkeyTip(window.siyuan.config.keymap.editor.general.exitFocus.custom)}" data-type="exit-focus">${window.siyuan.languages.exitFocus}</button>
${padHTML}
<button class="block__icon fn__flex-center ariaLabel${window.siyuan.config.readonly ? " fn__none" : ""}" aria-label="${window.siyuan.languages.lockEdit}" data-type="readonly" data-subtype="unlock"><svg><use xlink:href="#iconUnlock"></use></svg></button>
@@ -82,8 +84,12 @@ ${padHTML}
<button class="block__icon fn__flex-center ariaLabel" data-type="more" aria-label="${window.siyuan.languages.more}"><svg><use xlink:href="#iconMore"></use></svg></button>
<button class="block__icon fn__flex-center fn__none ariaLabel" data-type="context" aria-label="${window.siyuan.languages.context}"><svg><use xlink:href="#iconAlignCenter"></use></svg></button>`;
this.element = element.firstElementChild as HTMLElement;
+ mountBreadcrumbButtons(protyle, element.querySelector(".protyle-breadcrumb__plugin"));
element.addEventListener("click", async (event) => {
let target = event.target as HTMLElement;
+ if (event.composedPath().some(item => item instanceof HTMLElement && item.hasAttribute("data-plugin-name"))) {
+ return;
+ }
const arrowElement = target.closest(".protyle-breadcrumb__arrow");
if (arrowElement && this.element.contains(arrowElement)) {
const itemElement = arrowElement.previousElementSibling as HTMLElement;
diff --git a/app/src/protyle/util/destroy.ts b/app/src/protyle/util/destroy.ts
index 07569198ee..b93daf7d4f 100644
--- a/app/src/protyle/util/destroy.ts
+++ b/app/src/protyle/util/destroy.ts
@@ -2,12 +2,14 @@ import {hideElements} from "../ui/hideElements";
import {isSupportCSSHL} from "../render/searchMarkRender";
import {destroyAIEditor} from "../../ai/editor";
import {cancelAssetUploads} from "../upload/pluginEvent";
+import {unmountBreadcrumbButtons} from "../../plugin/breadcrumbButton";
export const destroy = (protyle: IProtyle) => {
if (!protyle) {
return;
}
cancelAssetUploads(protyle);
+ unmountBreadcrumbButtons(protyle);
hideElements(["util"], protyle, true);
destroyAIEditor(protyle);
protyle.hint?.destroy();