Skip to content

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

:bug: Clear stale outline content when notebooks close https://github.com/siyuan-note/siyuan/issues/19938

Danielcf49b1a887c3
diff --git a/app/src/layout/dock/Outline.title.test.ts b/app/src/layout/dock/Outline.title.test.ts
new file mode 100644
index 0000000000..89b33282af
--- /dev/null
+++ b/app/src/layout/dock/Outline.title.test.ts
@@ -0,0 +1,40 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import * as ts from "typescript";
+import {escapeHtml} from "../../util/escape";
+
+const source = ts.createSourceFile("Outline.ts", readFileSync("src/layout/dock/Outline.ts", "utf8"), ts.ScriptTarget.Latest, true);
+const method = source.statements.find(ts.isClassDeclaration).members.find(member =>
+ member.name?.getText(source) === "updateDocTitle") as ts.MethodDeclaration;
+const renderer = {} as {render: (this: unknown, ial?: Record<string, string>, count?: number) => void};
+runInNewContext(ts.transpileModule(`exports.render = function(ial, count) ${method.body.getText(source)};`, {
+ compilerOptions: {target: ts.ScriptTarget.ES2021},
+}).outputText, {
+ exports: renderer, escapeHtml,
+ getDocDisplayName: (title: string) => title,
+ getFileTreeIconHTML: () => "",
+ Constants: {ZWSP: "\u200b", CUSTOM_SY_TITLE_EMPTY: "custom-sy-title-empty"},
+});
+
+test("clearing the desktop outline removes its hidden title text and tooltip", () => {
+ const attributes = new Map<string, string>();
+ const hidden = new Set<string>();
+ const title = {
+ innerHTML: "",
+ classList: {add: (name: string) => hidden.add(name), remove: (name: string) => hidden.delete(name)},
+ querySelector: (): null => null,
+ setAttribute: (key: string, value: string) => attributes.set(key, value),
+ removeAttribute: (key: string) => attributes.delete(key),
+ };
+ const outline = {type: "pin", headerElement: {nextElementSibling: title}};
+ renderer.render.call(outline, {title: "Secret/title.sy"}, 0);
+ assert.equal(attributes.get("title"), "Secret/title.sy");
+ renderer.render.call(outline, null, 0);
+ assert.equal(attributes.get("title"), "Secret/title.sy", "count-only updates preserve the title");
+ renderer.render.call(outline);
+ assert.equal(title.innerHTML, "");
+ assert.equal(attributes.has("title"), false);
+ assert.equal(hidden.has("fn__none"), true);
+});
diff --git a/app/src/layout/dock/Outline.ts b/app/src/layout/dock/Outline.ts
index efe0dd6e2a..72bae64ac4 100644
--- a/app/src/layout/dock/Outline.ts
+++ b/app/src/layout/dock/Outline.ts
@@ -504,6 +504,8 @@ export class Outline extends Model {
const docTitleElement = this.headerElement.nextElementSibling as HTMLElement;
if (this.type === "pin") {
if (!ial && typeof count === "undefined") {
+ docTitleElement.innerHTML = "";
+ docTitleElement.removeAttribute("title");
docTitleElement.classList.add("fn__none");
return;
}
diff --git a/app/src/mobile/dock/MobileOutline.lifecycle.test.ts b/app/src/mobile/dock/MobileOutline.lifecycle.test.ts
new file mode 100644
index 0000000000..1d76e9e925
--- /dev/null
+++ b/app/src/mobile/dock/MobileOutline.lifecycle.test.ts
@@ -0,0 +1,119 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import {escapeHtml} from "../../util/escape";
+
+const compiled = transpileModule(readFileSync("src/mobile/dock/MobileOutline.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+}).outputText;
+
+const setup = () => {
+ const attributes = new Map<string, string>();
+ const hidden = new Set<string>();
+ const title = {
+ innerHTML: "",
+ insertAdjacentHTML(_position: string, html: string) { this.innerHTML += html; },
+ classList: {add: (name: string) => hidden.add(name), remove: (name: string) => hidden.delete(name)},
+ querySelector: (): null => null,
+ setAttribute: (key: string, value: string) => attributes.set(key, value),
+ removeAttribute: (key: string) => attributes.delete(key),
+ };
+ const input = {value: ""};
+ const tree = {data: null as unknown, element: {scrollTop: 0}, updateData(data: unknown) { this.data = data; }};
+ const state = {
+ empty: false,
+ editor: {protyle: {
+ block: {rootID: "secret-doc"}, notebookId: "secret-box",
+ background: {ial: {title: "Secret/title.sy"}},
+ preview: {element: {classList: {contains: () => true}}},
+ }},
+ };
+ const pending: {data: {id: string, notebook?: string}, respond: (response: unknown) => void}[] = [];
+ const exports = {} as {MobileOutline: {prototype: Record<string, unknown>}};
+ runInNewContext(compiled, {
+ exports,
+ window: {siyuan: {mobile: {get editor() { return state.editor; }}}},
+ document: {getElementById: () => ({classList: {contains: () => state.empty}})},
+ require: (id: string) => {
+ switch (id) {
+ case "../../layout/Model": return {Model: class {}};
+ case "../../util/pathName": return {getDocDisplayName: (value: string) => value, isEncryptedBox: (box: string) => box === "secret-box"};
+ case "../../util/escape": return {escapeHtml};
+ case "../../emoji/fileTreeIcon": return {getFileTreeIconHTML: () => ""};
+ case "../../constants": return {Constants: {ZWSP: "\u200b", CUSTOM_SY_TITLE_EMPTY: "custom-sy-title-empty"}};
+ case "../../util/fetch": return {fetchPost: (_url: string, data: {id: string, notebook?: string}, respond: (response: unknown) => void) => pending.push({data, respond})};
+ default: return {};
+ }
+ },
+ });
+ const outline = Object.assign(Object.create(exports.MobileOutline.prototype), {
+ currentRequestID: 0, reloadId: 0, blockId: "", notebookId: "", isPreview: false, preFilterExpandIds: null,
+ tree,
+ element: {
+ querySelector: (selector: string) => selector === '[data-type="doc-title"]' ? title :
+ selector === "input.b3-text-field.search__label" ? input : null,
+ removeAttribute: () => {},
+ },
+ });
+ outline.reload();
+ const headings = [{id: "secret-heading", content: "Secret heading"}];
+ pending.shift().respond({data: headings});
+ return {outline, state, tree, title, attributes, hidden, pending, headings};
+};
+
+for (const cmd of ["closeBox", "removeBox", "removeDoc"]) {
+ test(`mobile outline clears cached content on ${cmd} even after the editor switches notebooks`, () => {
+ const h = setup();
+ h.state.editor.protyle.block.rootID = "public-doc";
+ h.state.editor.protyle.notebookId = "public-box";
+ h.outline.handleMsgCallback({cmd, data: {box: "secret-box", ids: ["secret-doc"]}});
+ assert.equal(h.tree.data, null);
+ assert.equal(h.title.innerHTML, "");
+ assert.equal(h.attributes.has("title"), false);
+ assert.equal(h.hidden.has("fn__none"), true);
+ assert.equal(h.outline.blockId, "");
+ });
+
+ test(`mobile outline preserves another document on unrelated ${cmd}`, () => {
+ const h = setup();
+ h.outline.handleMsgCallback({cmd, data: {box: "other-box", ids: ["other-doc"]}});
+ assert.equal(h.tree.data, h.headings);
+ assert.equal(h.attributes.get("title"), "Secret/title.sy");
+ assert.equal(h.outline.blockId, "secret-doc");
+ });
+}
+
+test("late mobile outline responses cannot restore locked content, and reopening loads fresh content", () => {
+ const h = setup();
+ h.outline.reload();
+ const old = h.pending.shift();
+ assert.equal(old.data.notebook, "secret-box");
+ h.outline.handleMsgCallback({cmd: "closeBox", data: {box: "secret-box"}});
+ old.respond({data: h.headings});
+ assert.equal(h.tree.data, null);
+ assert.equal(h.title.innerHTML, "");
+ assert.equal(h.attributes.has("title"), false);
+ h.state.editor.protyle.background.ial.title = "Reopened/title.sy";
+ h.outline.reload();
+ old.respond({data: h.headings});
+ assert.equal(h.tree.data, null, "a response from before locking cannot repaint the reopened document");
+ assert.equal(h.attributes.has("title"), false);
+ h.pending.shift().respond({data: []});
+ assert.equal(h.attributes.get("title"), "Reopened/title.sy");
+ assert.equal(h.hidden.has("fn__none"), false);
+});
+
+test("empty mobile editor removes cached outline titles and invalidates pending reloads", () => {
+ const h = setup();
+ h.outline.reload();
+ const old = h.pending.shift();
+ h.state.empty = true;
+ h.outline.reload();
+ old.respond({data: h.headings});
+ assert.equal(h.tree.data, null);
+ assert.equal(h.title.innerHTML, "");
+ assert.equal(h.attributes.has("title"), false);
+ assert.equal(h.outline.blockId, "");
+});
diff --git a/app/src/mobile/dock/MobileOutline.title.test.ts b/app/src/mobile/dock/MobileOutline.title.test.ts
index 1ce52efbb0..8c5c766e15 100644
--- a/app/src/mobile/dock/MobileOutline.title.test.ts
+++ b/app/src/mobile/dock/MobileOutline.title.test.ts
@@ -19,7 +19,7 @@ const source = ts.createSourceFile("MobileOutline.ts", readFileSync("src/mobile/
ts.ScriptTarget.Latest, true);
const declaration = source.statements.find(ts.isClassDeclaration);
const method = declaration.members.find(member => member.name?.getText(source) === "updateDocTitle") as ts.MethodDeclaration;
-const renderer = {} as {render: (this: unknown, ial: Record<string, string>, count: number) => void};
+const renderer = {} as {render: (this: unknown, ial?: Record<string, string>, count?: number) => void};
runInNewContext(compile(`exports.render = function(ial, count) ${method.body.getText(source)};`), {
exports: renderer,
getDocDisplayName: names.getDocDisplayName,
@@ -28,7 +28,7 @@ runInNewContext(compile(`exports.render = function(ial, count) ${method.body.get
Constants: {ZWSP: "\u200b", CUSTOM_SY_TITLE_EMPTY: "custom-sy-title-empty"},
});
-test("mobile outline preserves full titles after reload and rename, including literal .sy suffixes", () => {
+test("mobile outline preserves complete literal titles and removes hidden title content", () => {
let visible = false;
const attributes = new Map<string, string>();
const element = {
@@ -36,6 +36,7 @@ test("mobile outline preserves full titles after reload and rename, including li
classList: {add: () => visible = false, remove: () => visible = true},
querySelector: (): null => null,
setAttribute: (key: string, value: string) => attributes.set(key, value),
+ removeAttribute: (key: string) => attributes.delete(key),
};
const outline = {isPreview: false, element: {querySelector: () => element}};
for (const title of ["加密/QA19938//%2F/renamed", 'A/<tag> & "B".sy']) {
@@ -49,4 +50,12 @@ test("mobile outline preserves full titles after reload and rename, including li
outline.isPreview = true;
renderer.render.call(outline, {title: "Hidden"}, 0);
assert.equal(visible, false);
+ assert.equal(element.innerHTML, "");
+ assert.equal(attributes.has("title"), false);
+ outline.isPreview = false;
+ renderer.render.call(outline, {title: "Secret/title.sy"}, 0);
+ renderer.render.call(outline);
+ assert.equal(visible, false);
+ assert.equal(element.innerHTML, "");
+ assert.equal(attributes.has("title"), false);
});
diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.ts
index 58fc4f87a4..91ce8df0ed 100644
--- a/app/src/mobile/dock/MobileOutline.ts
+++ b/app/src/mobile/dock/MobileOutline.ts
@@ -38,6 +38,7 @@ export class MobileOutline extends Model {
public tree: Tree;
public element: HTMLElement;
public blockId: string;
+ private notebookId: string;
public isPreview: boolean;
private preFilterExpandIds: string[] | null = null;
private reloadId = 0;
@@ -423,6 +424,17 @@ export class MobileOutline extends Model {
}, data.data.title, data.data.empty, Constants.CUSTOM_SY_TITLE_EMPTY), -1);
}
break;
+ case "closeBox":
+ case "removeBox":
+ if (this.notebookId === data.data.box) {
+ this.clear();
+ }
+ break;
+ case "removeDoc":
+ if (data.data.ids.includes(this.blockId)) {
+ this.clear();
+ }
+ break;
}
}
}
@@ -531,17 +543,23 @@ export class MobileOutline extends Model {
}
}
+ private clear() {
+ // 关闭后清除隐藏面板的内容,并使在途响应失效。
+ this.reloadId++;
+ this.currentRequestID++;
+ this.blockId = "";
+ this.notebookId = "";
+ this.isPreview = false;
+ this.preFilterExpandIds = null;
+ this.tree.updateData(null);
+ this.tree.element.scrollTop = 0;
+ this.updateDocTitle();
+ this.element.removeAttribute("data-loading");
+ }
+
public reload(callback?: () => void) {
if (document.getElementById("editor").classList.contains("fn__none")) {
- this.reloadId++;
- this.currentRequestID++;
- this.blockId = "";
- this.isPreview = false;
- this.preFilterExpandIds = null;
- this.tree.updateData(null);
- this.tree.element.scrollTop = 0;
- this.updateDocTitle();
- this.element.removeAttribute("data-loading");
+ this.clear();
return;
}
const protyle = window.siyuan.mobile.editor?.protyle;
@@ -556,6 +574,7 @@ export class MobileOutline extends Model {
this.tree.element.scrollTop = 0;
}
this.blockId = blockId;
+ this.notebookId = protyle?.notebookId || "";
this.isPreview = isPreview;
const reloadId = ++this.reloadId;
const outlineParam: IObject = {
@@ -607,6 +626,8 @@ export class MobileOutline extends Model {
private updateDocTitle(ial?: Record<string, string>, count?: number) {
const docTitleElement = this.element.querySelector('[data-type="doc-title"]') as HTMLElement;
if (this.isPreview || !ial) {
+ docTitleElement.innerHTML = "";
+ docTitleElement.removeAttribute("title");
docTitleElement.classList.add("fn__none");
return;
}