Skip to content

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

:bug: Preserve plugin dock layout state https://github.com/siyuan-note/siyuan/issues/19655

Daniel37c1dbaea3e4
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 1dde899341..9648f3f3a0 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -1100,7 +1100,8 @@ export class Dock {
}
public remove(key: TDock | string) {
- this.toggleModel(key, false, true, true);
+ // 移除插件停靠栏时保留用户的打开状态,供重新启用时恢复。
+ this.toggleModel(key, false, true, true, false);
this.elements[0].parentElement.querySelector(`[data-type="${key}"]`).remove();
const custom = this.data[key] as Custom;
if (custom.parent) {
diff --git a/app/src/plugin/loader.test.ts b/app/src/plugin/loader.test.ts
index 3f8ce472d1..039c2186f9 100644
--- a/app/src/plugin/loader.test.ts
+++ b/app/src/plugin/loader.test.ts
@@ -5,6 +5,7 @@ import test from "node:test";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
import {getPluginDockEntryKey} from "./dockKey";
+import {updatePluginDockShowStates} from "../layout/dock/pluginDockState";
const source = readFileSync(resolve(process.cwd(), "src/plugin/loader.ts"), "utf8");
const compiled = ts.transpileModule(source, {
@@ -144,9 +145,9 @@ test("cached automatic dimensions remain null", () => {
assert.deepEqual(buttons[0].tab.size, {width: null, height: 240});
});
-test("saved UI layout takes priority over cached placement and keeps new metadata", () => {
+test("saved UI layout updates placement while cached size and show remain authoritative", () => {
const {load, buttons, storage} = setup({
- ...createConfig(), position: "RightTop", icon: "iconInfo", title: "Cached dock",
+ ...createConfig(), position: "RightTop", size: {width: 444, height: 333}, icon: "iconInfo", title: "Cached dock",
}, {
type: "sampledock", icon: "iconInfo", title: "Layout dock", show: false, size: {width: 500, height: 300},
});
@@ -156,11 +157,41 @@ test("saved UI layout takes priority over cached placement and keeps new metadat
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(buttons[0].tab.show, true);
+ assert.deepEqual(buttons[0].tab.size, {width: 444, height: 333});
assert.equal(storage.sample.sampledock.position, "BottomRight");
});
+test("layout snapshots seed missing cache fields without replacing explicit false or null", () => {
+ const layoutTab = {
+ type: "sampledock", show: true, size: {width: 500, height: 300},
+ };
+ const fresh = setup(undefined, layoutTab);
+ fresh.plugin.docks.sampledock.config.show = false;
+ fresh.load();
+ assert.deepEqual(fresh.buttons[0].tab.size, layoutTab.size);
+ assert.equal(fresh.buttons[0].tab.show, true);
+
+ const partial = setup({show: false, size: {width: null}}, layoutTab);
+ partial.load();
+ assert.equal(partial.buttons[0].tab.show, false);
+ assert.deepEqual(partial.buttons[0].tab.size, {width: null, height: 300});
+});
+
+test("invalid snapshot dimensions do not erase cached dimensions or registered defaults", () => {
+ const layoutTab = {
+ type: "sampledock", show: false, size: {width: NaN, height: Infinity},
+ };
+ const cached = setup({show: true, size: {width: 444}}, layoutTab);
+ cached.load();
+ assert.deepEqual(cached.buttons[0].tab.size, {width: 444, height: 240});
+ assert.equal(cached.buttons[0].tab.show, true);
+
+ const fresh = setup(undefined, layoutTab);
+ fresh.load();
+ assert.deepEqual(fresh.buttons[0].tab.size, {width: 200, height: 240});
+});
+
test("entry visibility suppresses opening without overwriting the saved open state", () => {
const {load, buttons, plugin} = setup({...createConfig(), icon: "iconInfo"}, undefined, false);
load();
@@ -168,3 +199,79 @@ test("entry visibility suppresses opening without overwriting the saved open sta
assert.equal(plugin.docks.sampledock.config.show, true);
assert.equal(buttons[0].tab.icon, "iconClock");
});
+
+test("removing a dock preserves its open state for re-enabling while explicitly closing saves false", () => {
+ const dockSource = readFileSync(resolve(process.cwd(), "src/layout/dock/index.ts"), "utf8");
+ const dockCompiled = ts.transpileModule(dockSource, {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText;
+ for (const remove of [true, false]) {
+ const storage = {sample: {sampledock: createConfig()}};
+ let active = true;
+ let removed = false;
+ const classList = {
+ contains: () => active,
+ remove: () => { active = false; },
+ add: (): void => undefined,
+ };
+ const target = {
+ classList,
+ getAttribute: (name: string) => name === "data-index" ? "0" : "sampledock",
+ remove: () => { removed = true; },
+ };
+ const partition = {
+ querySelector: () => active ? target : null,
+ querySelectorAll: (selector: string) => selector === ".dock__item" || active ? [target] : [],
+ parentElement: {
+ querySelector: () => removed ? null : target,
+ classList,
+ },
+ };
+ const element = {
+ classList,
+ style: {},
+ previousElementSibling: {classList},
+ nextElementSibling: {classList},
+ };
+ const dependencies = {
+ resizeTabs: (): void => undefined,
+ adjustDockPadding: (): void => undefined,
+ updatePluginDockShowStates,
+ setStorageVal: (): void => undefined,
+ Constants: {LOCAL_PLUGIN_DOCKS: "local-plugin-docks"},
+ };
+ const exports: {Dock?: {prototype: object}} = {};
+ runInNewContext(dockCompiled, {
+ exports,
+ require: () => dependencies,
+ document: {querySelector: (selector: string) => selector.includes("data-type") ? target : null},
+ window: {siyuan: {storage: {"local-plugin-docks": storage}}},
+ clearTimeout: (): void => undefined,
+ });
+ const dock = Object.assign(Object.create(exports.Dock.prototype), {
+ app: {plugins: [{name: "sample", docks: {sampledock: {config: createConfig()}}}]},
+ elements: [partition, {...partition, querySelector: (): null => null, querySelectorAll: (): typeof target[] => []}],
+ layout: {element, children: [{element}, {element}]},
+ data: {sampledock: {}},
+ resizeElement: {classList},
+ panelVisible: true,
+ position: "Left",
+ isFloating: () => true,
+ hideDock: (): void => undefined,
+ adjustSplit: (): void => undefined,
+ });
+ if (remove) {
+ dock.remove("sampledock");
+ assert.equal(removed, true);
+ assert.equal(dock.data.sampledock, undefined);
+ } else {
+ dock.toggleModel("sampledock", false, true);
+ }
+ assert.equal(storage.sample.sampledock.show, remove);
+ const restored = setup(storage.sample.sampledock, {
+ type: "sampledock", show: false, size: {width: 500},
+ });
+ restored.load();
+ assert.equal(restored.buttons[0].tab.show, remove);
+ }
+});
diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.ts
index afc2b7fc9e..79ac2110fb 100644
--- a/app/src/plugin/loader.ts
+++ b/app/src/plugin/loader.ts
@@ -188,6 +188,21 @@ export const loadPlugin = async (app: App, item: IPluginData) => {
return manager.getInstance(item.name);
};
+type TPluginDockLayout = Partial<Pick<IPluginDockTab, "position" | "index" | "show" | "size">>;
+
+const mergeDockSize = (...sizes: Partial<Config.IUILayoutDockPanelSize>[]) => {
+ const result: Config.IUILayoutDockPanelSize = {};
+ sizes.forEach(size => {
+ (["width", "height"] as const).forEach(key => {
+ const value = size?.[key];
+ if (value === null || (typeof value === "number" && Number.isFinite(value))) {
+ result[key] = value;
+ }
+ });
+ });
+ return result;
+};
+
const updateDock = (dockItem: Config.IUILayoutDockTab[], index: number, plugin: Plugin, type: string) => {
const dockKeys = Object.keys(plugin.docks);
if (dockKeys.length === 0) {
@@ -204,12 +219,17 @@ const updateDock = (dockItem: Config.IUILayoutDockTab[], index: number, plugin:
plugin.docks[tabItem.type].config.position = index === 0 ? "BottomLeft" : "BottomRight";
}
plugin.docks[tabItem.type].config.index = tabIndex;
- plugin.docks[tabItem.type].config.show = tabItem.show;
- plugin.docks[tabItem.type].config.size = tabItem.size;
if (!window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name]) {
window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name] = {};
}
- window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][tabItem.type] = plugin.docks[tabItem.type].config;
+ const config = plugin.docks[tabItem.type].config;
+ const saved: TPluginDockLayout = window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][tabItem.type];
+ // 结构位置跟随工作区布局;缓存已有的尺寸和打开状态优先,仅用布局快照补齐缺失字段。
+ window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][tabItem.type] = {
+ ...config,
+ show: saved?.show ?? tabItem.show ?? config.show,
+ size: mergeDockSize(config.size, tabItem.size, saved?.size),
+ };
setStorageVal(Constants.LOCAL_PLUGIN_DOCKS, window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS]);
}
});
@@ -301,7 +321,7 @@ export const addPluginDock = (plugin: Plugin) => {
window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name] = {};
}
const dock = plugin.docks[key];
- const savedConfig = window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][key];
+ const savedConfig: TPluginDockLayout = window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][key];
if (savedConfig) {
// 仅恢复用户布局,图标、标题和默认快捷键使用插件本次注册的配置。
dock.config = {
@@ -309,7 +329,7 @@ export const addPluginDock = (plugin: Plugin) => {
position: savedConfig.position ?? dock.config.position,
index: savedConfig.index ?? dock.config.index,
show: savedConfig.show ?? dock.config.show,
- size: {...dock.config.size, ...savedConfig.size},
+ size: mergeDockSize(dock.config.size, savedConfig.size),
};
}
window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS][plugin.name][key] = dock.config;