Skip to content

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

:bug: Restore the selected dock panel after reload https://github.com/siyuan-note/siyuan/issues/18637

Daniel99e85433b290
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 371e2e2d22..0776bfe5b9 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -22,6 +22,7 @@ import {Plugin} from "../../plugin";
import {Custom} from "./Custom";
import {clearBeforeResizeTop, recordBeforeResizeTop} from "../../protyle/util/resize";
import {Constants} from "../../constants";
+import {updatePluginDockShowStates} from "./pluginDockState";
const TYPES = ["file", "outline", "inbox", "bookmark", "tag", "graph", "globalGraph", "backlink", "agentChat"];
@@ -529,9 +530,6 @@ export class Dock {
});
}
}
- if (isSaveLayout) {
- this.saveLocalPlugin(type, {show: false});
- }
} else {
this.elements[index].querySelectorAll(".dock__item--active").forEach(item => {
item.classList.remove("dock__item--active", "dock__item--activefocus");
@@ -704,12 +702,13 @@ export class Dock {
if (document.activeElement) {
(document.activeElement as HTMLElement).blur();
}
- if (isSaveLayout) {
- this.saveLocalPlugin(type, {show: true});
- }
this.showDock();
}
+ if (isSaveLayout) {
+ this.saveLocalPluginShow(index);
+ }
+
// dock 中两个面板的显示关系
const anotherIndex = index === 0 ? 1 : 0;
const anotherWnd = this.layout.children[anotherIndex] as Wnd;
@@ -1012,4 +1011,24 @@ export class Dock {
}
});
}
+
+ private saveLocalPluginShow(index: number) {
+ const states: {type: string, show: boolean}[] = [];
+ this.elements[index].querySelectorAll(".dock__item").forEach((item) => {
+ const type = item.getAttribute("data-type");
+ if (type) {
+ states.push({
+ type,
+ show: item.classList.contains("dock__item--active"),
+ });
+ }
+ });
+ if (updatePluginDockShowStates(
+ states,
+ this.app.plugins,
+ window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS],
+ )) {
+ setStorageVal(Constants.LOCAL_PLUGIN_DOCKS, window.siyuan.storage[Constants.LOCAL_PLUGIN_DOCKS]);
+ }
+ }
}
diff --git a/app/src/layout/dock/pluginDockState.test.ts b/app/src/layout/dock/pluginDockState.test.ts
new file mode 100644
index 0000000000..6cdc659fee
--- /dev/null
+++ b/app/src/layout/dock/pluginDockState.test.ts
@@ -0,0 +1,76 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {updatePluginDockShowStates} from "./pluginDockState";
+
+const dock = (show: boolean) => ({
+ config: {show} as IPluginDockTab,
+});
+
+describe("updatePluginDockShowStates", () => {
+ it("closes the plugin replaced by an internal dock", () => {
+ const plugins = [{
+ name: "plugin-a",
+ docks: {dockA: dock(true)},
+ }];
+ const storage = {
+ "plugin-a": {dockA: {show: true} as IPluginDockTab},
+ };
+
+ assert.equal(updatePluginDockShowStates([{type: "dockA", show: false}], plugins, storage), true);
+ assert.equal(storage["plugin-a"].dockA.show, false);
+ });
+
+ it("updates both plugins from one partition snapshot", () => {
+ const plugins = [{
+ name: "plugin-a",
+ docks: {dockA: dock(true)},
+ }, {
+ name: "plugin-b",
+ docks: {dockB: dock(false)},
+ }];
+ const storage = {
+ "plugin-a": {dockA: {show: true} as IPluginDockTab},
+ "plugin-b": {dockB: {show: false} as IPluginDockTab},
+ };
+ const states = [
+ {type: "dockA", show: false},
+ {type: "dockB", show: true},
+ ];
+
+ assert.equal(updatePluginDockShowStates(states, plugins, storage), true);
+ assert.equal(storage["plugin-a"].dockA.show, false);
+ assert.equal(storage["plugin-b"].dockB.show, true);
+ assert.equal(updatePluginDockShowStates(states, plugins, storage), false);
+ });
+
+ it("does not change plugins outside the partition snapshot", () => {
+ const plugins = [{
+ name: "plugin-a",
+ docks: {dockA: dock(true)},
+ }, {
+ name: "plugin-b",
+ docks: {dockB: dock(true)},
+ }];
+ const storage = {
+ "plugin-a": {dockA: {show: true} as IPluginDockTab},
+ "plugin-b": {dockB: {show: true} as IPluginDockTab},
+ };
+
+ assert.equal(updatePluginDockShowStates([{type: "dockA", show: false}], plugins, storage), true);
+ assert.equal(storage["plugin-a"].dockA.show, false);
+ assert.equal(storage["plugin-b"].dockB.show, true);
+ });
+
+ it("initializes missing plugin storage", () => {
+ const config = {show: false} as IPluginDockTab;
+ const plugins = [{
+ name: "plugin-a",
+ docks: {dockA: {config}},
+ }];
+ const storage: Record<string, Record<string, IPluginDockTab>> = {};
+
+ assert.equal(updatePluginDockShowStates([{type: "dockA", show: true}], plugins, storage), true);
+ assert.equal(storage["plugin-a"].dockA, config);
+ assert.equal(storage["plugin-a"].dockA.show, true);
+ });
+});
diff --git a/app/src/layout/dock/pluginDockState.ts b/app/src/layout/dock/pluginDockState.ts
new file mode 100644
index 0000000000..6859938812
--- /dev/null
+++ b/app/src/layout/dock/pluginDockState.ts
@@ -0,0 +1,41 @@
+interface IPluginDockShowState {
+ type: string,
+ show: boolean,
+}
+
+interface IPluginDockOwner {
+ name: string,
+ docks: Record<string, {
+ config: IPluginDockTab,
+ }>,
+}
+
+type TPluginDockStorage = Record<string, Record<string, IPluginDockTab>>;
+
+export const updatePluginDockShowStates = (
+ states: IPluginDockShowState[],
+ plugins: IPluginDockOwner[],
+ storage: TPluginDockStorage,
+) => {
+ let changed = false;
+ states.forEach((state) => {
+ const plugin = plugins.find((item) => Object.prototype.hasOwnProperty.call(item.docks, state.type));
+ if (!plugin) {
+ return;
+ }
+ if (!storage[plugin.name]) {
+ storage[plugin.name] = {};
+ changed = true;
+ }
+ if (!storage[plugin.name][state.type]) {
+ storage[plugin.name][state.type] = plugin.docks[state.type].config;
+ changed = true;
+ }
+ const config = storage[plugin.name][state.type];
+ if (config.show !== state.show) {
+ config.show = state.show;
+ changed = true;
+ }
+ });
+ return changed;
+};