Skip to content

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

:bug: Skip outline expand persistence without an open document https://github.com/siyuan-note/siyuan/issues/20307

Jeffrey Chenbdd7de6f1303
diff --git a/app/src/editor/outlineStorage.test.ts b/app/src/editor/outlineStorage.test.ts
new file mode 100644
index 0000000000..a8313a0759
--- /dev/null
+++ b/app/src/editor/outlineStorage.test.ts
@@ -0,0 +1,50 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+
+for (const path of ["src/layout/dock/Outline.ts", "src/mobile/dock/MobileOutline.ts"]) {
+ const source = createSourceFile(path, readFileSync(path, "utf8"), ScriptTarget.ES2021, true);
+ const method = source.statements.find(isClassDeclaration).members.find(member =>
+ member.name?.getText(source) === "saveExpendIds");
+ const compiled = transpileModule(`class Harness {
+ blockId = ""; isPreview = false; type = "pin";
+ ${method.getText(source)}
+ }
+ globalThis.Harness = Harness;`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+
+ const setup = () => {
+ const requests: {url: string, params: any}[] = [];
+ const context: any = {
+ window: {siyuan: {config: {readonly: false}, isPublish: false}},
+ fetchPost: (url: string, params: unknown) => requests.push({url, params}),
+ };
+ runInNewContext(compiled, context);
+ return {context, requests};
+ };
+
+ // 沙箱内构造的对象原型与宿主不同,转为宿主对象后再比较
+ const hostRequests = (requests: {url: string, params: any}[]) =>
+ requests.map(item => ({url: item.url, params: JSON.parse(JSON.stringify(item.params))}));
+
+ test(`${path}: an empty outline panel does not persist heading state`, () => {
+ const {context, requests} = setup();
+ const outline = new context.Harness();
+ outline.tree = {getExpandIds: (): string[] => []};
+ outline.saveExpendIds();
+ assert.deepEqual(hostRequests(requests), []);
+ });
+
+ test(`${path}: an opened document persists the expanded heading ids`, () => {
+ const {context, requests} = setup();
+ const outline = new context.Harness();
+ outline.blockId = "doc";
+ outline.tree = {getExpandIds: () => ["h1"]};
+ outline.saveExpendIds();
+ assert.deepEqual(hostRequests(requests), [{
+ url: "/api/storage/setOutlineStorage",
+ params: {docID: "doc", val: {expandIds: ["h1"]}},
+ }]);
+ });
+}
diff --git a/app/src/layout/dock/Outline.ts b/app/src/layout/dock/Outline.ts
index fe3ea5b0bc..ba722ce60e 100644
--- a/app/src/layout/dock/Outline.ts
+++ b/app/src/layout/dock/Outline.ts
@@ -761,6 +761,11 @@ export class Outline extends Model {
return;
}
+ // 未打开文档时没有标题状态可持久化
+ if (!this.blockId) {
+ return;
+ }
+
if (!this.isPreview && this.type === "pin") {
fetchPost("/api/storage/setOutlineStorage", {
docID: this.blockId,
diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.ts
index 83544d9f6e..65d1415086 100644
--- a/app/src/mobile/dock/MobileOutline.ts
+++ b/app/src/mobile/dock/MobileOutline.ts
@@ -635,6 +635,11 @@ export class MobileOutline extends Model {
return;
}
+ // 未打开文档时没有标题状态可持久化
+ if (!this.blockId) {
+ return;
+ }
+
if (!this.isPreview) {
fetchPost("/api/storage/setOutlineStorage", {
docID: this.blockId,