Skip to content

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

:bug: Collapse pinned documents with the document panel https://github.com/siyuan-note/siyuan/issues/19412

Daniel3215f803d1dc
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index d330c19b19..0d9e4ddd0a 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -185,6 +185,7 @@ export class Files extends Model {
});
// 为了快捷键的 dispatch
this.actionsElement.querySelector('[data-type="collapse"]').addEventListener("click", () => {
+ this.pinnedDocs.collapse();
Array.from(this.element.children).forEach(item => {
const liElement = item.firstElementChild;
const toggleElement = liElement.querySelector(".b3-list-item__arrow");
diff --git a/app/src/layout/dock/PinnedDocs.test.ts b/app/src/layout/dock/PinnedDocs.test.ts
index 0c5a20ff5e..b8c81ef258 100644
--- a/app/src/layout/dock/PinnedDocs.test.ts
+++ b/app/src/layout/dock/PinnedDocs.test.ts
@@ -8,6 +8,8 @@ import * as ts from "typescript";
interface IPanelHarness {
element: unknown;
refresh(): Promise<void>;
+ collapse(): void;
+ expanded: Set<string>;
list: unknown;
generation: number;
names: Map<string, string>;
@@ -27,6 +29,7 @@ interface IPanelHarness {
}
const loadPanel = (fetchCode = 0) => {
+ const storage = new Map<string, string>();
const docs: {id: string, notebook: string, name: string}[] = [];
const config = {readonly: false, fileTree: {docIconClickExpand: false, parentDocClickExpand: false}};
const calls: {kind: string, args: unknown[]}[] = [];
@@ -40,6 +43,7 @@ const loadPanel = (fetchCode = 0) => {
}).outputText;
runInNewContext(source, {
exports,
+ localStorage: {setItem: (key: string, value: string) => storage.set(key, value)},
window: {siyuan: {config, notebooks: []}},
document: {activeElement: null, createElement: () => ({
dataset: {}, children: [] as unknown[],
@@ -62,9 +66,38 @@ const loadPanel = (fetchCode = 0) => {
const panel = Object.create(exports.PinnedDocs.prototype) as IPanelHarness;
panel.clearDrop = () => { panel.dropTarget = undefined; };
panel.scheduleRefresh = () => {};
- return {panel, calls, config, docs};
+ return {panel, calls, config, docs, storage};
};
+test("collapse clears descendant expansion and persists the closed section", async () => {
+ const {panel, storage, calls} = loadPanel();
+ panel.generation = 3;
+ panel.expanded = new Set(["root", "root/child"]);
+ const states: unknown[] = [];
+ const row = {
+ nextElementSibling: {replaceChildren: () => states.push("clear"), classList: {add: (name: string) => states.push(name)}},
+ querySelector: () => ({classList: {remove: (name: string) => states.push(name)}}),
+ hasAttribute: () => true,
+ setAttribute: (name: string, value: string) => states.push([name, value]),
+ };
+ panel.list = {querySelectorAll: () => [row]};
+ panel.element = {
+ lastElementChild: {classList: {toggle: (name: string, value: boolean) => states.push([name, value])}},
+ firstElementChild: {setAttribute: (name: string, value: string) => states.push([name, value])},
+ querySelector: () => ({classList: {toggle: () => {}}}),
+ };
+ panel.collapse();
+ assert.equal(panel.generation, 4);
+ assert.equal(panel.expanded.size, 0);
+ assert.equal(storage.get("siyuan-pinned-docs-expanded"), "[]");
+ assert.equal(storage.get("siyuan-pinned-docs-collapsed"), "true");
+ assert.deepEqual(states, [["fn__none", true], ["aria-expanded", "false"], "clear", "fn__none",
+ "b3-list-item__arrow--open", ["aria-expanded", "false"]]);
+ await panel.loadChildren({dataset: {notebook: "notebook", path: "/root.sy", pinRow: "root"}}, {}, 3);
+ assert.equal(calls.length, 1);
+ assert.equal(calls[0].args[0], "/api/filetree/listDocsByPath");
+});
+
test("pinned document more actions open the source document menu", () => {
const {panel, calls} = loadPanel();
const row = {dataset: {nodeId: "document", notebook: "notebook", path: "/document.sy"}};
diff --git a/app/src/layout/dock/PinnedDocs.ts b/app/src/layout/dock/PinnedDocs.ts
index 2fefa58b5f..2d21d7247e 100644
--- a/app/src/layout/dock/PinnedDocs.ts
+++ b/app/src/layout/dock/PinnedDocs.ts
@@ -140,6 +140,21 @@ export class PinnedDocs {
this.refreshTimer = window.setTimeout(() => this.refresh(), 150);
}
+ public collapse() {
+ this.generation++;
+ this.expanded.clear();
+ localStorage.setItem("siyuan-pinned-docs-expanded", "[]");
+ localStorage.setItem("siyuan-pinned-docs-collapsed", "true");
+ this.setCollapsed(true);
+ this.list.querySelectorAll<HTMLElement>("[data-pin-root=true]").forEach(row => {
+ row.nextElementSibling.replaceChildren();
+ row.nextElementSibling.classList.add("fn__none");
+ row.querySelector(".b3-list-item__arrow").classList.remove("b3-list-item__arrow--open");
+ if (row.hasAttribute("aria-expanded")) { row.setAttribute("aria-expanded", "false"); }
+ });
+ this.scheduleRefresh();
+ }
+
private setCollapsed(collapsed: boolean) {
this.element.lastElementChild.classList.toggle("fn__none", collapsed);
this.element.firstElementChild.setAttribute("aria-expanded", String(!collapsed));
@@ -251,6 +266,7 @@ export class PinnedDocs {
await this.appendDoc(children, {...doc, notebook: row.dataset.notebook},
`${row.dataset.pinRow}/${doc.id}`, row.dataset.pinRow.split("/").length, generation);
}
+ if (generation !== this.generation || !this.expanded.has(row.dataset.pinRow)) { return; }
children.classList.remove("fn__none");
row.querySelector(".b3-list-item__arrow").classList.add("b3-list-item__arrow--open");
row.setAttribute("aria-expanded", "true");
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index c2d3ed0fc6..df23ce1ad9 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -151,6 +151,7 @@ export class MobileFiles extends Model {
} else if (type === "newNotebook") {
newNotebook();
} else if (type === "collapse") {
+ this.pinnedDocs.collapse();
Array.from(this.element.children).forEach(item => {
const liElement = item.firstElementChild;
const toggleElement = liElement.querySelector(".b3-list-item__arrow");
diff --git a/docs/PINNED-DOCUMENTS.md b/docs/PINNED-DOCUMENTS.md
index f11093ceac..a4812c33a4 100644
--- a/docs/PINNED-DOCUMENTS.md
+++ b/docs/PINNED-DOCUMENTS.md
@@ -21,6 +21,7 @@ The section appears when it contains pinned documents and hides automatically wh
| Drop between child documents | Reorder actual documents, using the existing sort-conflict confirmation |
| Drop in the middle of a document row | Move into the actual document, using existing move validation |
| Unpin a parent document | Remove only that root entry; independently pinned children remain |
+| Collapse the document panel | Collapse the pinned section and clear its descendant expansion state; keep the pinned entries and their order |
| Rename or move | Reload the source document for every entry; cross-notebook moves maintain the notebook identifier |
| Close an ordinary notebook | Retain unavailable entries and allow unpinning; keep titles already read in the current session, or show the document ID when the title is unavailable |
| Delete a document or notebook | Remove related entries; list reads also filter missing source documents |
diff --git a/docs/PINNED-DOCUMENTS.zh-CN.md b/docs/PINNED-DOCUMENTS.zh-CN.md
index fed43bc335..406a0f6bab 100644
--- a/docs/PINNED-DOCUMENTS.zh-CN.md
+++ b/docs/PINNED-DOCUMENTS.zh-CN.md
@@ -21,6 +21,7 @@
| 子文档之间插入线拖放 | 调整真实文档顺序,沿用现有排序冲突确认 |
| 拖放到文档行中部 | 移入真实文档,沿用现有移动校验 |
| 取消父文档置顶 | 只移除该根入口,单独置顶的子文档保留 |
+| 文档面板折叠 | 同时折叠置顶区域并清除其子文档展开状态,保留置顶文档及顺序 |
| 重命名或移动 | 所有入口重新读取源文档,跨笔记本移动维护笔记本标识 |
| 关闭普通笔记本 | 保留不可用入口,允许取消置顶;当前会话保留已读取的标题,无法读取标题时显示文档 ID |
| 删除文档或笔记本 | 清理相关入口;读取列表时也过滤不存在的源文档 |