Skip to content

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

:bug: Preserve startup compatibility with persisted configuration https://github.com/siyuan-note/siyuan/issues/19527

Danielb56a905ddc51
diff --git a/app/src/config/keymapPayload.test.ts b/app/src/config/keymapPayload.test.ts
index 8f0e9742aa..18cb382669 100644
--- a/app/src/config/keymapPayload.test.ts
+++ b/app/src/config/keymapPayload.test.ts
@@ -2,6 +2,7 @@ import * as assert from "node:assert/strict";
import {test} from "node:test";
import {keymapPayload} from "./keymapPayload";
import {systemConfigCollections} from "./systemConfig";
+import {clearDisallowedKeymapItems} from "../util/hotKeyPolicy";
test("shortcut payload accepts missing plugin configuration after first boot defaults are merged", () => {
for (const plugin of [undefined, null, {}]) {
@@ -26,3 +27,33 @@ test("shortcut payload preserves plugin bindings and extension fields", () => {
}, uiLayout: {}});
assert.deepEqual(keymapPayload(keymap), keymap);
});
+
+test("startup shortcut cleanup preserves empty commands from unloaded plugins", () => {
+ for (const empty of [null, false, 0, ""]) {
+ const {keymap} = systemConfigCollections({keymap: {
+ general: {},
+ editor: {general: {}, heading: {}, insert: {}, list: {}, table: {}},
+ plugin: {unloaded: {empty, valid: {custom: "F1", default: "F1"}}},
+ }, uiLayout: {}});
+ const before = structuredClone(keymap);
+ assert.equal(clearDisallowedKeymapItems(keymap.plugin.unloaded, true), false);
+ assert.deepEqual(keymap, before);
+ assert.deepEqual(keymapPayload(keymap), before);
+ }
+});
+
+test("startup shortcut payload preserves empty plugin groups after default updates", () => {
+ for (const empty of [null, false, 0, ""]) {
+ const {keymap} = systemConfigCollections({keymap: {
+ general: {command: {custom: "F1", default: "F2"}},
+ editor: {general: {}, heading: {}, insert: {}, list: {}, table: {}},
+ plugin: {unloaded: empty, loaded: {command: {custom: "F3", default: "F3"}}},
+ }, uiLayout: {}});
+ const before = structuredClone(keymap);
+ for (const group of Object.values(keymap.plugin)) {
+ assert.equal(clearDisallowedKeymapItems(group, true), false);
+ }
+ assert.deepEqual(keymapPayload(keymap), before);
+ assert.deepEqual(keymap, before);
+ }
+});
diff --git a/app/src/config/keymapPayload.ts b/app/src/config/keymapPayload.ts
index e951dc3c6a..a905705d25 100644
--- a/app/src/config/keymapPayload.ts
+++ b/app/src/config/keymapPayload.ts
@@ -5,13 +5,21 @@ export const keymapPayload = (keymap: Config.IKeymap): {[key: string]: JSONValue
const keys = (items: Config.IKeys): {[key: string]: JSONValue} => {
const result: {[key: string]: JSONValue} = {};
for (const [name, item] of Object.entries(items)) {
- result[name] = {...item};
+ if (item == null) {
+ result[name] = item === null ? null : undefined;
+ } else {
+ result[name] = {...item};
+ }
}
return result;
};
const plugin: {[key: string]: JSONValue} = {};
for (const [name, items] of Object.entries(keymap.plugin || {})) {
- plugin[name] = keys(items);
+ if (items == null) {
+ plugin[name] = items === null ? null : undefined;
+ } else {
+ plugin[name] = keys(items);
+ }
}
return {
...keymap,
diff --git a/app/src/config/systemConfig.test.ts b/app/src/config/systemConfig.test.ts
index b93abe64d1..31aedac084 100644
--- a/app/src/config/systemConfig.test.ts
+++ b/app/src/config/systemConfig.test.ts
@@ -1,9 +1,103 @@
import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
import {test} from "node:test";
import {systemConfigCollections} from "./systemConfig";
-import type {JSONValue} from "../types/api";
+import type {JSONValue, SystemAppConf} from "../types/api";
import {getDefaultKeymapBindings, getKeymapBindings, mergeKeymapDefault, setKeymapBindings} from "../util/keymapBindings";
+const existingLayoutDefaults = [undefined, (): Config.IUiLayout => assert.fail("Existing layout must not be reset")];
+
+const searchLayout = (config: JSONValue) => {
+ const data: [] = [];
+ return {
+ left: {data},
+ layout: {
+ instance: "Layout",
+ children: [{
+ instance: "Wnd",
+ children: [{instance: "Tab", children: {instance: "Search", config}}],
+ }],
+ },
+ };
+};
+
+const assertLayoutPreserved = (uiLayout: SystemAppConf["uiLayout"]) => {
+ const before = structuredClone(uiLayout);
+ for (const defaultLayout of existingLayoutDefaults) {
+ const actual = systemConfigCollections({keymap: {}, uiLayout}, defaultLayout);
+ assert.equal(actual.uiLayout, uiLayout);
+ assert.deepEqual(actual.uiLayout, before);
+ }
+};
+
+const assertLayoutRejected = (uiLayout: SystemAppConf["uiLayout"]) => {
+ const before = structuredClone(uiLayout);
+ for (const defaultLayout of existingLayoutDefaults) {
+ assert.throws(() => systemConfigCollections({keymap: {}, uiLayout}, defaultLayout), /Invalid dock configuration/);
+ assert.deepEqual(uiLayout, before);
+ }
+};
+
+test("persisted split layout with legacy search subtypes loads without resetting", () => {
+ // 保留议题中的完整布局结构,文档标识、标题、搜索词和激活时间均已脱敏。
+ // https://github.com/siyuan-note/siyuan/issues/19527#issuecomment-5673357561
+ const uiLayout: SystemAppConf["uiLayout"] = JSON.parse(readFileSync(join(__dirname, "testdata/system-config-legacy-search-layout.json"), "utf8"));
+ assertLayoutPreserved(uiLayout);
+});
+
+test("legacy search subtype selections remain unchanged and do not become grouped filters", () => {
+ for (const selected of [false, true]) {
+ const subTypes = {h1: selected, h2: selected, h3: selected, h4: selected, h5: selected, h6: selected,
+ o: selected, u: selected, t: selected};
+ assertLayoutPreserved(searchLayout({subTypes}));
+ }
+});
+
+test("grouped search subtypes coexist with legacy keys and unknown extension data", () => {
+ const groups = {heading: {h1: true, h2: false}, list: {o: true}, listItem: {t: true}};
+ assertLayoutPreserved(searchLayout({subTypes: groups}));
+ for (const extension of [null, false, 17, "extension", [true], {nested: [null, "data"]}]) {
+ assertLayoutPreserved(searchLayout({subTypes: {...groups, h1: false, o: false, future: extension}}));
+ }
+});
+
+test("search subtype validation preserves absent empty and nullable configurations", () => {
+ for (const config of [{}, {subTypes: null}, {subTypes: {}},
+ {subTypes: {heading: {}, list: null, listItem: {t: null}}}]) {
+ assertLayoutPreserved(searchLayout(config));
+ }
+});
+
+test("search subtype validation still rejects malformed known groups and selections", () => {
+ const invalidObjects: JSONValue[] = [false, 17, "invalid", []];
+ const invalidSelections: JSONValue[] = [1, "true", [], {}];
+ for (const subTypes of invalidObjects) {
+ assertLayoutRejected(searchLayout({subTypes}));
+ }
+ for (const [group, key] of [["heading", "h1"], ["list", "o"], ["listItem", "t"]]) {
+ for (const value of invalidObjects) {
+ assertLayoutRejected(searchLayout({subTypes: {h1: true, [group]: value}}));
+ }
+ for (const value of invalidSelections) {
+ assertLayoutRejected(searchLayout({subTypes: {h1: true, [group]: {[key]: value}}}));
+ }
+ }
+});
+
+test("legacy search subtypes do not bypass other search and layout validation", () => {
+ const subTypes = {h1: false, o: false};
+ for (const config of [{query: true}, {idPath: [1]}, {types: {heading: "true"}}, {replaceTypes: {text: 1}}]) {
+ assertLayoutRejected(searchLayout({...config, subTypes}));
+ }
+ const uiLayout = searchLayout({subTypes});
+ assertLayoutRejected({...uiLayout, hideDock: "false"});
+ assertLayoutRejected({...uiLayout, left: {data: [[{type: "file", show: "true"}]]}});
+ assertLayoutRejected({...uiLayout, layout: {...uiLayout.layout, children: [
+ ...uiLayout.layout.children, {instance: "Unexpected"},
+ ]}});
+});
+
test("system configuration keeps layout and shortcut extension data", () => {
const width: number = null;
const value = {
diff --git a/app/src/config/systemConfig.ts b/app/src/config/systemConfig.ts
index df2ca251d5..95f8cc807e 100644
--- a/app/src/config/systemConfig.ts
+++ b/app/src/config/systemConfig.ts
@@ -112,8 +112,10 @@ const isSearchConfig = (value: unknown): boolean => {
return false;
}
const flags = (group: unknown) => group == null || isObject(group) && Object.values(group).every(optionalBoolean);
- return flags(value.types) && flags(value.replaceTypes) && (value.subTypes == null || isObject(value.subTypes) &&
- Object.values(value.subTypes).every(flags));
+ const subTypes = value.subTypes;
+ // 只核对参与筛选的已知分组,未知顶层键保留但不参与筛选。
+ return flags(value.types) && flags(value.replaceTypes) && (subTypes == null || isObject(subTypes) &&
+ ["heading", "list", "listItem"].every(key => flags(subTypes[key])));
};
const isLayoutItem = (value: unknown): boolean => {
diff --git a/app/src/config/testdata/system-config-legacy-search-layout.json b/app/src/config/testdata/system-config-legacy-search-layout.json
new file mode 100644
index 0000000000..8dc7b01666
--- /dev/null
+++ b/app/src/config/testdata/system-config-legacy-search-layout.json
@@ -0,0 +1,331 @@
+{
+ "bottom": {
+ "data": [],
+ "pin": true
+ },
+ "hideDock": false,
+ "layout": {
+ "children": [
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "children": [],
+ "instance": "Wnd"
+ },
+ {
+ "children": [],
+ "instance": "Wnd",
+ "resize": "tb"
+ }
+ ],
+ "direction": "tb",
+ "instance": "Layout",
+ "size": "343px",
+ "type": "left"
+ },
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "active": true,
+ "activeTime": "1789400000000",
+ "children": {
+ "action": "cb-get-scroll",
+ "blockId": "20260915000000-0000001",
+ "instance": "Editor",
+ "mode": "wysiwyg",
+ "notebookId": "20260915000000-0000002",
+ "rootId": "20260915000000-0000001"
+ },
+ "docIcon": "",
+ "instance": "Tab",
+ "pin": false,
+ "title": "Document"
+ }
+ ],
+ "height": "",
+ "instance": "Wnd",
+ "width": "50%"
+ },
+ {
+ "children": [
+ {
+ "active": true,
+ "activeTime": "1789400000000",
+ "children": {
+ "config": {
+ "group": 0,
+ "hPath": "",
+ "hasReplace": false,
+ "idPath": [],
+ "k": "example",
+ "method": 0,
+ "page": 1,
+ "query": "example",
+ "r": "",
+ "removed": true,
+ "replaceTypes": {
+ "aHref": true,
+ "aText": true,
+ "aTitle": true,
+ "blockRef": true,
+ "code": true,
+ "codeBlock": true,
+ "docTitle": true,
+ "em": true,
+ "fileAnnotationRef": true,
+ "htmlBlock": true,
+ "imgSrc": true,
+ "imgText": true,
+ "imgTitle": true,
+ "inlineMath": true,
+ "inlineMemo": true,
+ "kbd": true,
+ "mark": true,
+ "mathBlock": true,
+ "s": true,
+ "strong": true,
+ "sub": true,
+ "sup": true,
+ "tag": true,
+ "text": true,
+ "u": true
+ },
+ "sensitive": false,
+ "sort": 0,
+ "subTypes": {
+ "h1": false,
+ "h2": false,
+ "h3": false,
+ "h4": false,
+ "h5": false,
+ "h6": false,
+ "o": false,
+ "t": false,
+ "u": false
+ },
+ "types": {
+ "audioBlock": false,
+ "blockquote": false,
+ "callout": false,
+ "codeBlock": true,
+ "customBlock": true,
+ "databaseBlock": true,
+ "document": true,
+ "embedBlock": false,
+ "heading": true,
+ "htmlBlock": true,
+ "iframeBlock": false,
+ "list": false,
+ "listItem": false,
+ "mathBlock": true,
+ "paragraph": true,
+ "superBlock": false,
+ "table": true,
+ "videoBlock": false,
+ "widgetBlock": false
+ }
+ },
+ "instance": "Search"
+ },
+ "icon": "iconSearch",
+ "instance": "Tab",
+ "pin": false,
+ "title": "Search"
+ },
+ {
+ "activeTime": "1789400000000",
+ "children": {
+ "action": "cb-get-scroll",
+ "blockId": "20260915000000-0000003",
+ "instance": "Editor",
+ "mode": "wysiwyg",
+ "notebookId": "20260915000000-0000004",
+ "rootId": "20260915000000-0000003"
+ },
+ "docIcon": "1f389",
+ "instance": "Tab",
+ "pin": false,
+ "title": "Document"
+ }
+ ],
+ "height": "",
+ "instance": "Wnd",
+ "resize": "lr",
+ "width": "50%"
+ }
+ ],
+ "direction": "lr",
+ "instance": "Layout",
+ "resize": "lr",
+ "size": "auto",
+ "type": "center"
+ },
+ {
+ "children": [
+ {
+ "children": [],
+ "instance": "Wnd"
+ },
+ {
+ "children": [],
+ "instance": "Wnd",
+ "resize": "tb"
+ }
+ ],
+ "direction": "tb",
+ "instance": "Layout",
+ "resize": "lr",
+ "size": "0px",
+ "type": "right"
+ }
+ ],
+ "direction": "lr",
+ "instance": "Layout",
+ "size": "auto",
+ "type": "normal"
+ },
+ {
+ "children": [
+ {
+ "children": [],
+ "instance": "Wnd"
+ },
+ {
+ "children": [],
+ "instance": "Wnd",
+ "resize": "lr"
+ }
+ ],
+ "direction": "lr",
+ "instance": "Layout",
+ "resize": "tb",
+ "size": "0px",
+ "type": "bottom"
+ }
+ ],
+ "direction": "tb",
+ "instance": "Layout",
+ "type": "normal"
+ },
+ "left": {
+ "data": [
+ [
+ {
+ "hotkeyLangId": "fileTree",
+ "icon": "iconFiles",
+ "show": true,
+ "size": {
+ "height": 0,
+ "width": 343
+ },
+ "title": "文档",
+ "type": "file"
+ },
+ {
+ "hotkeyLangId": "outline",
+ "icon": "iconOutline",
+ "show": false,
+ "size": {
+ "height": 0,
+ "width": 396
+ },
+ "title": "大纲",
+ "type": "outline"
+ },
+ {
+ "hotkeyLangId": "inbox",
+ "icon": "iconInbox",
+ "show": false,
+ "size": {
+ "height": 0,
+ "width": 442
+ },
+ "title": "收集箱",
+ "type": "inbox"
+ }
+ ],
+ [
+ {
+ "hotkeyLangId": "tag",
+ "icon": "iconTag",
+ "show": true,
+ "size": {
+ "height": 140,
+ "width": 343
+ },
+ "title": "标签",
+ "type": "tag"
+ },
+ {
+ "hotkeyLangId": "bookmark",
+ "icon": "iconBookmark",
+ "show": false,
+ "size": {
+ "height": 319,
+ "width": 259
+ },
+ "title": "书签",
+ "type": "bookmark"
+ }
+ ]
+ ],
+ "pin": true
+ },
+ "right": {
+ "data": [
+ [
+ {
+ "hotkeyLangId": "agentChat",
+ "icon": "iconSparkles",
+ "show": false,
+ "size": {
+ "height": 0,
+ "width": 454
+ },
+ "title": "智能体",
+ "type": "agentChat"
+ },
+ {
+ "hotkeyLangId": "graphView",
+ "icon": "iconGraph",
+ "show": false,
+ "size": {
+ "height": 0,
+ "width": 331
+ },
+ "title": "关系图",
+ "type": "graph"
+ }
+ ],
+ [
+ {
+ "hotkeyLangId": "globalGraph",
+ "icon": "iconGlobalGraph",
+ "show": false,
+ "size": {
+ "height": 82,
+ "width": 563
+ },
+ "title": "全局关系图",
+ "type": "globalGraph"
+ },
+ {
+ "hotkeyLangId": "backlinks",
+ "icon": "iconLink",
+ "show": false,
+ "size": {
+ "height": 691,
+ "width": 563
+ },
+ "title": "反向链接",
+ "type": "backlink"
+ }
+ ]
+ ],
+ "pin": true
+ }
+}
diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts
index 38adb75994..c910c9782f 100644
--- a/app/src/protyle/util/compatibility.ts
+++ b/app/src/protyle/util/compatibility.ts
@@ -834,6 +834,10 @@ export const getLocalStorage = (cb: () => void) => {
window.siyuan.storage[key] = defaultStorage[key];
}
});
+ // 只使用当前支持的缩放档位,确保窗口初始化能取得对应的按钮位置。
+ if (!Constants.SIZE_ZOOM.some(item => item.zoom === window.siyuan.storage[Constants.LOCAL_ZOOM])) {
+ window.siyuan.storage[Constants.LOCAL_ZOOM] = defaultStorage[Constants.LOCAL_ZOOM];
+ }
window.siyuan.storage[Constants.LOCAL_SEARCHDATA].types = normalizeSearchTypes(window.siyuan.storage[Constants.LOCAL_SEARCHDATA].types);
// 搜索数据添加 replaceTypes 兼容
if (!window.siyuan.storage[Constants.LOCAL_SEARCHDATA].replaceTypes ||
diff --git a/app/src/protyle/util/storageStartup.test.ts b/app/src/protyle/util/storageStartup.test.ts
new file mode 100644
index 0000000000..cd2aa990be
--- /dev/null
+++ b/app/src/protyle/util/storageStartup.test.ts
@@ -0,0 +1,66 @@
+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 {ModuleKind, transpileModule} from "typescript";
+import type {Constants as AppConstants} from "../../constants";
+
+const constantsExports: {Constants?: typeof AppConstants} = {};
+runInNewContext(transpileModule(readFileSync(resolve("src/constants.ts"), "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS},
+}).outputText, {exports: constantsExports, SIYUAN_VERSION: "test", NODE_ENV: "test"});
+const Constants = constantsExports.Constants;
+
+// 单独执行真实的存储加载函数,隔离编辑器和原生窗口依赖。
+const source = readFileSync(resolve("src/protyle/util/compatibility.ts"), "utf8");
+const loader = transpileModule(source.slice(source.indexOf("export const getLocalStorage ="),
+ source.indexOf("export const isSensitiveSearchConfig =")), {
+ compilerOptions: {module: ModuleKind.CommonJS},
+}).outputText;
+
+const loadZoom = (value: unknown) => {
+ const storage: Record<string, unknown> = {};
+ const siyuan = {storage};
+ const exports: {getLocalStorage?: (cb: () => void) => void} = {};
+ runInNewContext(loader, {
+ exports,
+ Constants,
+ window: {siyuan},
+ fetchPost: (_url: string, _payload: unknown, cb: (response: {data: Record<string, unknown>}) => void) => {
+ cb({data: {[Constants.LOCAL_ZOOM]: value}});
+ },
+ getDefaultType: () => ({}),
+ getDefaultSubType: () => ({}),
+ normalizeSearchTypes: (types: unknown) => types,
+ sanitizeClosedTabs: (tabs: unknown[]) => tabs,
+ setStorageVal: () => assert.fail("Loading zoom must not overwrite persisted data"),
+ });
+ let loaded = false;
+ exports.getLocalStorage(() => {
+ loaded = true;
+ });
+ assert.equal(loaded, true);
+ const zoom = siyuan.storage[Constants.LOCAL_ZOOM];
+ assert.ok(Constants.SIZE_ZOOM.find(item => item.zoom === zoom)?.position);
+ return zoom;
+};
+
+test("startup storage preserves every supported zoom level", () => {
+ for (const {zoom} of Constants.SIZE_ZOOM) {
+ assert.equal(loadZoom(JSON.stringify(zoom)), zoom);
+ }
+});
+
+test("startup storage restores default zoom for retired zoom levels", () => {
+ // 旧版缩放档位包括 25%、33% 和 50%,窗口按钮位置表只覆盖当前档位。
+ for (const zoom of [0.25, 0.33, 0.5]) {
+ assert.equal(loadZoom(JSON.stringify(zoom)), 1);
+ }
+});
+
+test("startup storage restores default zoom for absent or malformed values", () => {
+ for (const value of [undefined, null, "null", "false", "{}", "[]", '"1"', "invalid", "0", "-1", "4"]) {
+ assert.equal(loadZoom(value), 1);
+ }
+});
diff --git a/app/src/util/hotKeyPolicy.ts b/app/src/util/hotKeyPolicy.ts
index 6f1979cf40..3a683e5db4 100644
--- a/app/src/util/hotKeyPolicy.ts
+++ b/app/src/util/hotKeyPolicy.ts
@@ -47,6 +47,9 @@ export const clearDisallowedKeymapItems = (
) => {
let changed = false;
Object.values(keymap || {}).forEach((item) => {
+ if (!item) {
+ return;
+ }
const multi = item as IShortcutKeymap;
if (multi.bindings?.version === 1) {
const keys = getKeymapBindings(multi);