An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Support plugin editor toolbar visibility and ordering https://github.com/siyuan-note/siyuan/issues/20211
Danielf46d1026df6c
diff --git a/app/src/config/entryVisibility/catalog.test.ts b/app/src/config/entryVisibility/catalog.test.tsindex f2f8fd3cad..e7976f5c43 100644--- a/app/src/config/entryVisibility/catalog.test.ts+++ b/app/src/config/entryVisibility/catalog.test.ts@@ -33,6 +33,7 @@ import {refreshSlashMenuCatalog,refreshTopBarCatalog,refreshToolbarCatalog,+ refreshToolbarCatalogEntries,SLASH_MENU_ROOT_PATH,TOP_BAR_ROOT_PATH,} from "./catalog";@@ -419,6 +420,24 @@ test("toolbar catalog follows plugin insertion slots and removes unloaded pluginassert.equal(getEntryCatalogNode(`${TOOLBAR_ENTRY_ROOT_PATH}.${pluginKey}`), undefined);});+test("toolbar catalog accepts plugin entries from the owner window snapshot", () => {+ const pluginKey = getPluginToolbarEntryKey("owner", "action");+ const defaults = getDefaultToolbar(false).map((item) => typeof item === "string" ? {name: item} : item);+ try {+ refreshToolbarCatalogEntries([+ {key: "block-type", label: "Block type", separator: false},+ {key: pluginKey, label: "Owner - Action", separator: false},+ {key: "block-ref", label: "Block reference", separator: false},+ ]);+ const children = getEntryCatalogChildren(TOOLBAR_ENTRY_ROOT_PATH)+ .filter(item => !item.key.startsWith("mobile-"));+ assert.deepEqual(children.slice(0, 3).map(item => item.key), ["block-type", pluginKey, "block-ref"]);+ assert.equal(getEntryCatalogNode(`${TOOLBAR_ENTRY_ROOT_PATH}.${pluginKey}`)?.label(), "Owner - Action");+ } finally {+ refreshToolbarCatalog(defaults);+ }+});+test("dock catalog refreshes unique plugin docks and removes unloaded entries", () => {const firstKey = getPluginDockEntryKey("plugin.one", "shared.id");const secondKey = getPluginDockEntryKey("plugin.two", "shared.id");diff --git a/app/src/config/entryVisibility/catalog.ts b/app/src/config/entryVisibility/catalog.tsindex 693051be3a..d95cd04fde 100644--- a/app/src/config/entryVisibility/catalog.ts+++ b/app/src/config/entryVisibility/catalog.ts@@ -4,6 +4,7 @@ import {DESKTOP_TOOLBAR_ENTRIES,getToolbarEntryId,getToolbarEntryLabel,+ IToolbarCatalogEntry,MOBILE_TOOLBAR_NAMES,TOOLBAR_ENTRY_ROOT_PATH,} from "../../protyle/toolbar/defaults";@@ -1314,13 +1315,12 @@ const toolbarCatalogNodeSignature = (item: IEntryCatalogNode, pluginLabels: Map<let toolbarCatalogSignature = JSON.stringify(toolbarBuiltinChildren.map((item) =>toolbarCatalogNodeSignature(item, new Map())));-export const refreshToolbarCatalog = (items: Array<string | IMenuItem>) => {+export const refreshToolbarCatalogEntries = (items: IToolbarCatalogEntry[]) => {const nodes = new Map(toolbarBuiltinNodeMap);const pluginLabels = new Map<string, string>();const actualOrder: string[] = [];items.forEach((item) => {- const menuItem = typeof item === "string" ? {name: item} : item;- const key = getToolbarEntryId(menuItem);+ const key = item.key;if (!key || actualOrder.includes(key)) {return;}@@ -1328,9 +1328,9 @@ export const refreshToolbarCatalog = (items: Array<string | IMenuItem>) => {if (nodes.has(key)) {return;}- const label = getToolbarEntryLabel(menuItem) || menuItem.tip || menuItem.name;+ const label = item.label;pluginLabels.set(key, label);- nodes.set(key, menuItem.name === "|" ? separator(key) : node(key, literal(label)));+ nodes.set(key, item.separator ? separator(key) : node(key, literal(label)));});const order = mergeEntryOrderPreservingUnknown(DESKTOP_TOOLBAR_ENTRIES.map((item) => item.key), actualOrder);const children = normalizeToolbarCatalogSeparators([@@ -1347,6 +1347,14 @@ export const refreshToolbarCatalog = (items: Array<string | IMenuItem>) => {rebuildCatalogIndexes();};+export const refreshToolbarCatalog = (items: Array<string | IMenuItem>) => {+ refreshToolbarCatalogEntries(items.map((item) => {+ const menuItem = typeof item === "string" ? {name: item} : item;+ return {key: getToolbarEntryId(menuItem), label: getToolbarEntryLabel(menuItem) || menuItem.tip || menuItem.name,+ separator: menuItem.name === "|"};+ }));+};+interface ISlashMenuCatalogPlugin {name: string;displayName?: string;diff --git a/app/src/config/entryVisibility/ui.test.ts b/app/src/config/entryVisibility/ui.test.tsindex aa1ac547cf..8533a6eff2 100644--- a/app/src/config/entryVisibility/ui.test.ts+++ b/app/src/config/entryVisibility/ui.test.ts@@ -44,6 +44,31 @@ test("entry settings show exit only on native tablets without changing the persi}});+test("detached entry settings read the owner's plugin toolbar catalog", () => {+ const code = transpileModule(readFileSync("src/config/entryVisibility/ui.ts", "utf8") ++ "\nexports.refreshToolbar = refreshEditorToolbarCatalog;", {+ compilerOptions: {module: ModuleKind.CommonJS},+ }).outputText;+ const exports = {} as {refreshToolbar: (plugins: unknown[]) => void};+ const plugins = [{name: "local"}];+ const owner = [{key: "plugin:owner:item", label: "Owner item", separator: false}];+ const seen: Array<Array<{key: string}>> = [];+ let host: {getEditorToolbarCatalogSnapshot: () => typeof owner} | undefined = {+ getEditorToolbarCatalogSnapshot: () => owner,+ };+ runInNewContext(code, {exports, require: () => ({+ getSettingsWindowHost: () => host,+ getEditorToolbarCatalogSnapshot: (items: Array<{name: string}>) => [{key: items[0].name}],+ refreshToolbarCatalogEntries: (items: Array<{key: string}>) => seen.push(items),+ DOCK_ORDER_SCOPES_BY_SIDE: {},+ })});+ exports.refreshToolbar(plugins);+ assert.equal(seen[0], owner);+ host = undefined;+ exports.refreshToolbar(plugins);+ assert.equal(seen[1][0].key, "local");+});+const loadDockSnapshot = () => {const code = transpileModule(readFileSync("src/config/entryVisibility/ui.ts", "utf8") +"\nexports.snapshot = getProfileDockOrderSnapshot;", {diff --git a/app/src/config/entryVisibility/ui.ts b/app/src/config/entryVisibility/ui.tsindex 4b3bb73790..838c33462c 100644--- a/app/src/config/entryVisibility/ui.ts+++ b/app/src/config/entryVisibility/ui.ts@@ -15,6 +15,7 @@ import {refreshDockCatalog,refreshSlashMenuCatalog,refreshTopBarCatalog,+ refreshToolbarCatalogEntries,TOP_BAR_ROOT_PATH,STATUS_BAR_ROOT_PATH,} from "./catalog";@@ -51,6 +52,7 @@ import {MOBILE_TOOLBAR_CONTEXT_KEYS} from "./mobileToolbarContext";import {getSettingsOwnerApp} from "../setting/windowContext";/// #if !MOBILEimport {getSettingsWindowHost, isSettingsWindow} from "../setting/windowContext";+import {getEditorToolbarCatalogSnapshot} from "../../protyle/toolbar/catalogSnapshot";/// #endifimport {DOCK_ORDER_SCOPES,@@ -580,8 +582,16 @@ const getProfileDockOrderSnapshot = (builtin: boolean) => {return builtin ? snapshot : getDefaultDockEntryOrderSnapshot(snapshot);};+const refreshEditorToolbarCatalog = (plugins: NonNullable<ReturnType<typeof getSettingsOwnerApp>>["plugins"]) => {+ /// #if !MOBILE+ refreshToolbarCatalogEntries(getSettingsWindowHost()?.getEditorToolbarCatalogSnapshot() ||+ getEditorToolbarCatalogSnapshot(plugins));+ /// #endif+};+const openProfileEditor = (root: HTMLElement, profileID?: string) => {const plugins = getSettingsOwnerApp()?.plugins || [];+ refreshEditorToolbarCatalog(plugins);refreshTopBarCatalog(plugins);refreshDockCatalog(plugins);refreshSlashMenuCatalog(plugins);diff --git a/app/src/config/setting/nativeWindow.ts b/app/src/config/setting/nativeWindow.tsindex 02a7ccb476..822df3fc57 100644--- a/app/src/config/setting/nativeWindow.ts+++ b/app/src/config/setting/nativeWindow.ts@@ -12,6 +12,7 @@ import {Constants} from "../../constants";import {hasPluginSetting} from "../../plugin";import {getWorkspaceName} from "../../util/processTitle";import {getDockEntryOrderSnapshot} from "../entryVisibility/dockOrder";+import {getEditorToolbarCatalogSnapshot} from "../../protyle/toolbar/catalogSnapshot";import {hasNativeSettingTasks} from "./taskBlocker";import {openBazaarPath} from "../bazaar/openPath";import type {App} from "../../index";@@ -107,6 +108,10 @@ export const openNativeSettings = async (app: App, command: ISettingsCommand = {reload: () => { assertActive(); return reloadUI(); },resetLayout: () => { assertActive(); return resetLayout(); },getDockOrderSnapshot: () => { assertActive(); return getDockEntryOrderSnapshot(); },+ getEditorToolbarCatalogSnapshot: () => {+ assertActive();+ return getEditorToolbarCatalogSnapshot(app.plugins);+ },openBazaarPath: (type, name, storage) => { assertActive(); openBazaarPath(type, name, storage); },exit: setCurrentWorkspace => { assertActive(); return exitSiYuan(setCurrentWorkspace); },plugin: plugin && {diff --git a/app/src/config/setting/windowContext.ts b/app/src/config/setting/windowContext.tsindex 70e6feb850..d5c45d9b9b 100644--- a/app/src/config/setting/windowContext.ts+++ b/app/src/config/setting/windowContext.ts@@ -3,6 +3,7 @@ import type {Dialog} from "../../dialog";import type {IPluginReloadData} from "../../plugin/loader";import type {IGlobalPluginStateSnapshot} from "../../plugin/globalStateCoordinator";import type {TDockOrderSnapshot} from "../entryVisibility/dockOrder";+import type {IToolbarCatalogEntry} from "../../protyle/toolbar/defaults";/// #if !BROWSERimport {ipcRenderer} from "electron";/// #endif@@ -15,6 +16,7 @@ export interface ISettingsWindowHost {reload: () => Promise<void>;resetLayout: () => Promise<void>;getDockOrderSnapshot: () => TDockOrderSnapshot;+ getEditorToolbarCatalogSnapshot: () => IToolbarCatalogEntry[];openBazaarPath: (type: TBazaarType, name: string, storage?: boolean) => void;exportLayout: (options: {cb: () => void; errorExit: boolean}) => Promise<void>;exit: (setCurrentWorkspace?: boolean) => Promise<void>;diff --git a/app/src/protyle/toolbar/catalogSnapshot.test.ts b/app/src/protyle/toolbar/catalogSnapshot.test.tsnew file mode 100644index 0000000000..9d95a6a835--- /dev/null+++ b/app/src/protyle/toolbar/catalogSnapshot.test.ts@@ -0,0 +1,51 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {test} from "node:test";+import {runInNewContext} from "node:vm";+import {ModuleKind, transpileModule} from "typescript";+import {getDefaultToolbar, getPluginToolbarEntryKey, getToolbarEntryId, getToolbarEntryLabel,+ markPluginToolbarEntries} from "./defaults";+import {removePluginToolbarItem, resolvePluginToolbar, setPluginToolbarItem} from "../../plugin/toolbarItem";++test("editor toolbar catalog snapshot lists plugin items without an open editor", () => {+ const source = readFileSync("src/protyle/toolbar/catalogSnapshot.ts", "utf8");+ const compiled = transpileModule(source, {compilerOptions: {module: ModuleKind.CommonJS}}).outputText;+ const exports = {} as typeof import("./catalogSnapshot");+ runInNewContext(compiled, {exports, window: {siyuan: {languages: {pluginAction: "Translated action"}}},+ require: (id: string) => {+ if (id === "./defaults") {+ return {getDefaultToolbar, getToolbarEntryId, getToolbarEntryLabel, markPluginToolbarEntries};+ }+ if (id === "./util") {+ return {toolbarKeyToMenu: (items: Array<string | IMenuItem>) => items.map(item =>+ typeof item === "string" ? {name: item} : item)};+ }+ if (id === "../../plugin/toolbarItem") {+ return {resolvePluginToolbar};+ }+ throw new Error(id);+ }});+ const plugin = {+ name: "example",+ displayName: "Example",+ updateProtyleToolbar: (toolbar: Array<string | IMenuItem>) => [toolbar[0],+ {name: "legacy", lang: "pluginAction"}, "|", ...toolbar.slice(1)],+ };+ setPluginToolbarItem(plugin, {name: "dynamic", tip: "Dynamic action"});+ try {+ const snapshot = exports.getEditorToolbarCatalogSnapshot([plugin]);+ const legacyKey = getPluginToolbarEntryKey("example", "legacy");+ const dynamicKey = getPluginToolbarEntryKey("example", "dynamic");+ assert.deepEqual(Array.from(snapshot, item => item.key).slice(0, 4), [+ "block-type", legacyKey, getPluginToolbarEntryKey("example", "1", "separator"), "block-ref",+ ]);+ assert.equal(snapshot.find(item => item.key === legacyKey)?.label, "Example - Translated action");+ assert.equal(snapshot.find(item => item.key === dynamicKey)?.label, "Example - Dynamic action");+ assert.equal(snapshot.find(item => item.key === dynamicKey)?.separator, false);+ assert.equal(JSON.stringify(snapshot).includes("callback"), false);+ removePluginToolbarItem(plugin, "dynamic");+ assert.equal(exports.getEditorToolbarCatalogSnapshot([plugin]).some(item => item.key === dynamicKey), false);+ } finally {+ removePluginToolbarItem(plugin, "dynamic");+ }+});diff --git a/app/src/protyle/toolbar/catalogSnapshot.ts b/app/src/protyle/toolbar/catalogSnapshot.tsnew file mode 100644index 0000000000..6a354a6265--- /dev/null+++ b/app/src/protyle/toolbar/catalogSnapshot.ts@@ -0,0 +1,28 @@+import {getDefaultToolbar, getToolbarEntryId, getToolbarEntryLabel, IToolbarCatalogEntry,+ markPluginToolbarEntries} from "./defaults";+import {toolbarKeyToMenu} from "./util";+import {resolvePluginToolbar} from "../../plugin/toolbarItem";++interface IToolbarCatalogPlugin {+ name: string;+ displayName?: string;+ updateProtyleToolbar(toolbar: Array<string | IMenuItem>): Array<string | IMenuItem>;+}++export const getEditorToolbarCatalogSnapshot = (plugins: IToolbarCatalogPlugin[]): IToolbarCatalogEntry[] => {+ let toolbar: Array<string | IMenuItem> = toolbarKeyToMenu(getDefaultToolbar(false));+ plugins.forEach((plugin) => {+ const previous = [...toolbar];+ toolbar = markPluginToolbarEntries(previous, resolvePluginToolbar(plugin, toolbar), plugin.name, (item) => {+ const pluginName = plugin.displayName?.trim() || plugin.name;+ const label = item.tip || (item.lang ? window.siyuan.languages[item.lang] : "") || item.name;+ return `${pluginName} - ${label}`;+ });+ toolbar = toolbarKeyToMenu(toolbar);+ });+ return toolbar.flatMap((item: IMenuItem) => {+ const key = getToolbarEntryId(item);+ return key ? [{key, label: getToolbarEntryLabel(item) || item.tip || item.name,+ separator: item.name === "|"}] : [];+ });+};diff --git a/app/src/protyle/toolbar/defaults.ts b/app/src/protyle/toolbar/defaults.tsindex 4b1ce52e18..531d51cb1b 100644--- a/app/src/protyle/toolbar/defaults.ts+++ b/app/src/protyle/toolbar/defaults.ts@@ -8,6 +8,12 @@ interface IToolbarEntry {separator?: boolean;}+export interface IToolbarCatalogEntry {+ key: string;+ label: string;+ separator: boolean;+}+type TConfigurableToolbarItem = IMenuItem & {[toolbarEntryMetadata]?: {id: string;