Skip to content

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

:bug: Refresh plugin dock icons while preserving layout https://github.com/siyuan-note/siyuan/issues/19651

Daniel80e825888fec
diff --git a/app/src/plugin/loader.test.ts b/app/src/plugin/loader.test.ts
new file mode 100644
index 0000000000..3f8ce472d1
--- /dev/null
+++ b/app/src/plugin/loader.test.ts
@@ -0,0 +1,170 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import test from "node:test";
+import {runInNewContext} from "node:vm";
+import * as ts from "typescript";
+import {getPluginDockEntryKey} from "./dockKey";
+
+const source = readFileSync(resolve(process.cwd(), "src/plugin/loader.ts"), "utf8");
+const compiled = ts.transpileModule(source, {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+}).outputText;
+
+const createConfig = (): IPluginDockTab => ({
+ position: "LeftTop",
+ index: 1000,
+ size: {width: 200, height: 240},
+ show: true,
+ icon: "iconClock",
+ title: "Updated dock",
+ hotkey: "new-hotkey",
+});
+
+const setup = (saved?: Partial<IPluginDockTab>, layoutTab?: Config.IUILayoutDockTab, visible = true) => {
+ const plugin = {
+ name: "sample",
+ docks: {sampledock: {id: "stable-id", config: createConfig()}},
+ };
+ const storage: Record<string, Record<string, Partial<IPluginDockTab>>> = saved ? {sample: {sampledock: saved}} : {};
+ const persisted: typeof storage[] = [];
+ const mounted = new Set<string>();
+ const buttons: Array<{
+ position: TDockPosition,
+ partition: number,
+ index: number,
+ tab: Config.IUILayoutDockTab & {entryId: string},
+ }> = [];
+ const createDock = (position: TDockPosition) => ({
+ genButton: (tabs: Array<Config.IUILayoutDockTab & {entryId: string}>, partition: number, index: number) => {
+ tabs.forEach(tab => {
+ mounted.add(tab.type);
+ buttons.push({position, partition, index, tab: structuredClone(tab)});
+ });
+ },
+ });
+ const dependencies = {
+ Constants: {LOCAL_PLUGIN_DOCKS: "local-plugin-docks"},
+ isWindow: () => false,
+ getPluginDockEntryKey,
+ refreshDockCatalog: (): void => undefined,
+ dispatchMobilePluginDocksChange: (): void => undefined,
+ applyDockEntryVisibility: (): void => undefined,
+ isEntryVisible: () => visible,
+ setStorageVal: (_key: string, value: typeof storage) => persisted.push(structuredClone(value)),
+ };
+ const exports: {addPluginDock?: typeof import("./loader").addPluginDock} = {};
+ runInNewContext(compiled, {
+ exports,
+ require: () => dependencies,
+ window: {siyuan: {
+ ws: {app: {plugins: [plugin]}},
+ storage: {"local-plugin-docks": storage},
+ layout: {leftDock: createDock("Left"), rightDock: createDock("Right"), bottomDock: createDock("Bottom")},
+ config: {uiLayout: {
+ left: {data: []},
+ right: {data: []},
+ bottom: {data: layoutTab ? [[], [layoutTab]] : []},
+ }},
+ }},
+ document: {
+ querySelector: (selector: string) => mounted.has(selector.match(/data-type="([^"]+)"/)[1]) ? {} : null,
+ },
+ });
+ const load = () => exports.addPluginDock(plugin as never);
+ return {plugin, storage, persisted, buttons, load};
+};
+
+test("new plugin docks use the registered metadata and layout", () => {
+ const {load, buttons} = setup();
+ load();
+ assert.deepEqual(buttons, [{
+ position: "Left",
+ partition: 0,
+ index: 1000,
+ tab: {
+ type: "sampledock",
+ entryId: getPluginDockEntryKey("sample", "stable-id"),
+ icon: "iconClock",
+ title: "Updated dock",
+ size: {width: 200, height: 240},
+ show: true,
+ },
+ }]);
+});
+
+test("cached dock layout preserves placement while plugin metadata updates across reloads", () => {
+ let saved: Partial<IPluginDockTab> = {
+ position: "RightBottom",
+ index: 0,
+ size: {width: 444, height: 333},
+ show: false,
+ icon: "iconInfo",
+ title: "Old dock",
+ hotkey: "old-hotkey",
+ };
+ for (const icon of ["iconClock", "iconInfo"]) {
+ const {load, plugin, storage, persisted, buttons} = setup(saved);
+ plugin.docks.sampledock.config.icon = icon;
+ plugin.docks.sampledock.config.title = icon;
+ load();
+ assert.equal(buttons[0].tab.icon, icon);
+ assert.equal(buttons[0].tab.title, icon);
+ assert.equal(buttons[0].tab.entryId, getPluginDockEntryKey("sample", "stable-id"));
+ assert.equal(buttons[0].tab.type, "sampledock");
+ assert.equal(buttons[0].position, "Right");
+ assert.equal(buttons[0].partition, 1);
+ assert.equal(buttons[0].index, 0);
+ assert.equal(buttons[0].tab.show, false);
+ assert.deepEqual(buttons[0].tab.size, {width: 444, height: 333});
+ assert.equal(plugin.docks.sampledock.config.hotkey, "new-hotkey");
+ assert.equal(storage.sample.sampledock, plugin.docks.sampledock.config);
+ saved = persisted.at(-1).sample.sampledock;
+ assert.equal(saved.icon, icon);
+ assert.equal(saved.title, icon);
+ load();
+ assert.equal(buttons.length, 1);
+ }
+});
+
+test("partial cached layout keeps new defaults and does not restore removed metadata", () => {
+ const {load, plugin, buttons} = setup({index: 0, show: false, size: {width: 444}, hotkey: "old-hotkey"});
+ delete plugin.docks.sampledock.config.hotkey;
+ load();
+ assert.equal(buttons[0].position, "Left");
+ assert.equal(buttons[0].index, 0);
+ assert.equal(buttons[0].tab.show, false);
+ assert.deepEqual(buttons[0].tab.size, {width: 444, height: 240});
+ assert.equal(plugin.docks.sampledock.config.hotkey, undefined);
+});
+
+test("cached automatic dimensions remain null", () => {
+ const {load, buttons} = setup({size: {width: null}});
+ load();
+ assert.deepEqual(buttons[0].tab.size, {width: null, height: 240});
+});
+
+test("saved UI layout takes priority over cached placement and keeps new metadata", () => {
+ const {load, buttons, storage} = setup({
+ ...createConfig(), position: "RightTop", icon: "iconInfo", title: "Cached dock",
+ }, {
+ type: "sampledock", icon: "iconInfo", title: "Layout dock", show: false, size: {width: 500, height: 300},
+ });
+ load();
+ assert.equal(buttons[0].position, "Bottom");
+ assert.equal(buttons[0].partition, 1);
+ assert.equal(buttons[0].index, 0);
+ assert.equal(buttons[0].tab.icon, "iconClock");
+ assert.equal(buttons[0].tab.title, "Updated dock");
+ assert.equal(buttons[0].tab.show, false);
+ assert.deepEqual(buttons[0].tab.size, {width: 500, height: 300});
+ assert.equal(storage.sample.sampledock.position, "BottomRight");
+});
+
+test("entry visibility suppresses opening without overwriting the saved open state", () => {
+ const {load, buttons, plugin} = setup({...createConfig(), icon: "iconInfo"}, undefined, false);
+ load();
+ assert.equal(buttons[0].tab.show, false);
+ assert.equal(plugin.docks.sampledock.config.show, true);
+ assert.equal(buttons[0].tab.icon, "iconClock");
+});
diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.ts
index c8135d27ec..afc2b7fc9e 100644
--- a/app/src/plugin/loader.ts
+++ b/app/src/plugin/loader.ts
@@ -300,11 +300,20 @@ export const addPluginDock = (plugin: Plugin) => {
if (!window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name]) {
window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name] = {};
}
- if (window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name] &&
- window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][key]) {
- plugin.docks[key].config = window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][key];
- }
const dock = plugin.docks[key];
+ const savedConfig = window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][key];
+ if (savedConfig) {
+ // 仅恢复用户布局,图标、标题和默认快捷键使用插件本次注册的配置。
+ dock.config = {
+ ...dock.config,
+ position: savedConfig.position ?? dock.config.position,
+ index: savedConfig.index ?? dock.config.index,
+ show: savedConfig.show ?? dock.config.show,
+ size: {...dock.config.size, ...savedConfig.size},
+ };
+ }
+ window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][key] = dock.config;
+ setStorageVal(Constants.LOCAL_PLUGIN_DOCKS, window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS]);
const entryId = getPluginDockEntryKey(plugin.name, dock.id);
const show = dock.config.show && isEntryVisible(`dock.${entryId}`);
const dockTab: Config.IUILayoutDockTab & {entryId: string} = {