An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Avoid refreshing the document tree when toggling notebook documents https://github.com/siyuan-note/siyuan/issues/18722
Danieledb633317593
diff --git a/app/src/config/tabs/fileTab.ts b/app/src/config/tabs/fileTab.tsindex 7509a40483..2161a2d7f3 100644--- a/app/src/config/tabs/fileTab.ts+++ b/app/src/config/tabs/fileTab.ts@@ -7,7 +7,6 @@ import type {SettingTabBuilder} from "../setting/builder";import {controlNumber, controlSelect, controlString} from "../setting/control";import {genConfigItemName} from "../render/fragments";import {genButtonHtml, genNumberInputHtml} from "../render/render";-import {setNoteBook} from "../../util/pathName";/// #if !MOBILEimport {getAllModels} from "../../layout/getAll";/// #endif@@ -309,15 +308,6 @@ const registerFileOthersGroup = (tab: SettingTabBuilder) => {group.switch("fileTree.boxDocEnabled", {title: window.siyuan.languages.boxDocEnabled,desc: window.siyuan.languages.boxDocEnabledTip,- save: (value) => fileConfigApi.patch("boxDocEnabled", value, () => {- setNoteBook(() => {- /// #if MOBILE- window.siyuan.mobile.docks.file?.init(false);- /// #else- getAllModels().files.forEach((files) => files.init(false));- /// #endif- });- }),});group.number("fileTree.recentDocsMaxListCount", {title: window.siyuan.languages.recentDocsMaxListCount,diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.tsindex bd7dab376f..4f6ce693aa 100644--- a/app/src/layout/dock/Files.ts+++ b/app/src/layout/dock/Files.ts@@ -38,6 +38,7 @@ import {expandFileTree,toggleFileTree} from "./fileTreeAnimation";+import {updateNotebookRootForBoxDoc} from "../../util/notebookRoot";import {collectExpandedDocIDs,findMovedFileTreeItem,@@ -1048,6 +1049,11 @@ export class Files extends Model {this.init(false);});break;+ case "boxDocFeatureChanged":+ setNoteBook(() => {+ this.updateBoxDocFeature();+ });+ break;case "notebookIconChanged":this.updateNotebookIcon(data.data);break;@@ -1195,6 +1201,31 @@ export class Files extends Model {});}+ private updateBoxDocFeature() {+ const enabled = window.siyuan.config.fileTree.boxDocEnabled;+ window.siyuan.notebooks.forEach((notebook) => {+ if (notebook.closed) {+ return;+ }+ const notebookElement = this.element.querySelector<HTMLElement>(`:scope > ul[data-url="${notebook.id}"]`);+ const rootElement = notebookElement?.querySelector<HTMLElement>(":scope > li[data-type=\"navigation-root\"]");+ if (!notebookElement || !rootElement) {+ return;+ }++ updateNotebookRootForBoxDoc(rootElement, notebook, enabled);+ const iconElement = rootElement.querySelector<HTMLElement>(".b3-list-item__icon");+ iconElement?.classList.toggle("popover__block", enabled);+ if (enabled) {+ iconElement?.setAttribute("data-id", notebook.id);+ } else {+ iconElement?.removeAttribute("data-id");+ }+ this.updateDocActionElement(rootElement);+ this.element.append(notebookElement);+ });+ }+private updateItemArrow(notebookId: string, filePath: string) {const treeElement = this.element.querySelector(`[data-url="${notebookId}"]`);if (!treeElement) {diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.tsindex b91df6a3b1..5fd8c298e9 100644--- a/app/src/mobile/dock/MobileFiles.ts+++ b/app/src/mobile/dock/MobileFiles.ts@@ -27,6 +27,7 @@ import {expandFileTree,isFileTreeCollapsing} from "../../layout/dock/fileTreeAnimation";+import {updateNotebookRootForBoxDoc} from "../../util/notebookRoot";import {bindMousePointerTouchBridge, isMousePointerTouchEvent} from "../util/mousePointerTouchBridge";import {collectExpandedDocIDs,@@ -552,6 +553,11 @@ export class MobileFiles extends Model {this.init(false);});break;+ case "boxDocFeatureChanged":+ setNoteBook(() => {+ this.updateBoxDocFeature();+ });+ break;case "notebookIconChanged":this.updateNotebookIcon(data.data);break;@@ -780,6 +786,26 @@ export class MobileFiles extends Model {window.siyuan.languages.docIconClickExpand : window.siyuan.languages.openDocument);}+ private updateBoxDocFeature() {+ const enabled = window.siyuan.config.fileTree.boxDocEnabled;+ window.siyuan.notebooks.forEach((notebook) => {+ if (notebook.closed) {+ return;+ }+ const notebookElement = this.element.querySelector<HTMLElement>(`:scope > ul[data-url="${notebook.id}"]`);+ const rootElement = notebookElement?.querySelector<HTMLElement>(":scope > li[data-type=\"navigation-root\"]");+ if (!notebookElement || !rootElement) {+ return;+ }++ const subFileCount = updateNotebookRootForBoxDoc(rootElement, notebook, enabled);+ rootElement.querySelector<HTMLElement>(".b3-list-item__icon")?.setAttribute("aria-label", enabled ?+ (subFileCount > 0 ? window.siyuan.languages.docIconClickExpand : window.siyuan.languages.openDocument) :+ window.siyuan.languages.changeIcon);+ this.element.append(notebookElement);+ });+ }+private genNotebook(item: INotebook) {const editingPublishAccess = this.actionsElement.querySelector('[data-type="publish-access"]').classList.contains("block__icon--active");// 加密笔记本关闭(锁定)时用 🔒 提示需解锁;打开(解锁)后恢复正常 emojidiff --git a/app/src/util/notebookRoot.test.ts b/app/src/util/notebookRoot.test.tsnew file mode 100644index 0000000000..0bcecdaed5--- /dev/null+++ b/app/src/util/notebookRoot.test.ts@@ -0,0 +1,91 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {updateNotebookRootForBoxDoc} from "./notebookRoot";++const createRoot = () => {+ const attributes = new Map<string, string>();+ const toggleClasses = new Set<string>();+ const arrowClasses = new Set(["b3-list-item__arrow--open"]);+ let childRemoved = false;+ const arrowElement = {+ classList: {+ remove: (name: string) => arrowClasses.delete(name),+ },+ };+ const toggleElement = {+ classList: {+ toggle(name: string, force: boolean) {+ if (force) {+ toggleClasses.add(name);+ } else {+ toggleClasses.delete(name);+ }+ },+ },+ querySelector: () => arrowElement,+ };+ const childElement = {+ tagName: "UL",+ remove: () => {+ childRemoved = true;+ },+ };+ const rootElement = {+ nextElementSibling: childElement,+ setAttribute: (name: string, value: string) => attributes.set(name, value),+ querySelector: () => toggleElement,+ } as unknown as HTMLElement;+ return {+ rootElement,+ attributes,+ toggleClasses,+ arrowClasses,+ childRemoved: () => childRemoved,+ };+};++describe("updateNotebookRootForBoxDoc", () => {+ it("enables an empty notebook document without rebuilding its root", () => {+ const root = createRoot();++ const count = updateNotebookRootForBoxDoc(root.rootElement, {+ id: "20260814120000-abcdefg",+ subFileCount: 0,+ }, true);++ assert.equal(count, 0);+ assert.equal(root.attributes.get("data-node-id"), "20260814120000-abcdefg");+ assert.equal(root.attributes.get("data-count"), "0");+ assert.equal(root.toggleClasses.has("fn__hidden"), true);+ assert.equal(root.arrowClasses.has("b3-list-item__arrow--open"), false);+ assert.equal(root.childRemoved(), true);+ });++ it("disables a notebook document while preserving its expanded children", () => {+ const root = createRoot();++ updateNotebookRootForBoxDoc(root.rootElement, {+ id: "20260814120000-abcdefg",+ subFileCount: 0,+ }, false);++ assert.equal(root.attributes.get("data-node-id"), "");+ assert.equal(root.toggleClasses.has("fn__hidden"), false);+ assert.equal(root.arrowClasses.has("b3-list-item__arrow--open"), true);+ assert.equal(root.childRemoved(), false);+ });++ it("keeps the expand control for a notebook document with children", () => {+ const root = createRoot();++ const count = updateNotebookRootForBoxDoc(root.rootElement, {+ id: "20260814120000-abcdefg",+ subFileCount: 3,+ }, true);++ assert.equal(count, 3);+ assert.equal(root.attributes.get("data-count"), "3");+ assert.equal(root.toggleClasses.has("fn__hidden"), false);+ assert.equal(root.childRemoved(), false);+ });+});diff --git a/app/src/util/notebookRoot.ts b/app/src/util/notebookRoot.tsnew file mode 100644index 0000000000..e6ec888e5f--- /dev/null+++ b/app/src/util/notebookRoot.ts@@ -0,0 +1,19 @@+export const updateNotebookRootForBoxDoc = (+ rootElement: HTMLElement,+ notebook: Pick<INotebook, "id" | "subFileCount">,+ enabled: boolean,+) => {+ const subFileCount = notebook.subFileCount || 0;+ rootElement.setAttribute("data-node-id", enabled ? notebook.id : "");+ rootElement.setAttribute("data-count", subFileCount.toString());+ const toggleElement = rootElement.querySelector<HTMLElement>(".b3-list-item__toggle");+ const hideToggle = enabled && subFileCount === 0;+ toggleElement?.classList.toggle("fn__hidden", hideToggle);+ if (hideToggle) {+ toggleElement?.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");+ if (rootElement.nextElementSibling?.tagName === "UL") {+ rootElement.nextElementSibling.remove();+ }+ }+ return subFileCount;+};diff --git a/kernel/model/box_doc.go b/kernel/model/box_doc.goindex 91e82797c0..55e597431e 100644--- a/kernel/model/box_doc.go+++ b/kernel/model/box_doc.go@@ -186,7 +186,7 @@ func RefreshBoxDocFeature() {}}}- ReloadFiletree()+ util.PushBoxDocFeatureChanged()}func findBoxDoc(box *Box) (ret string, err error) {diff --git a/kernel/util/websocket.go b/kernel/util/websocket.goindex 2a2c523f2e..8eba819867 100644--- a/kernel/util/websocket.go+++ b/kernel/util/websocket.go@@ -322,6 +322,10 @@ func PushReloadFiletree() {BroadcastByType("filetree", "reloadFiletree", 0, "", nil)}+func PushBoxDocFeatureChanged() {+ BroadcastByType("filetree", "boxDocFeatureChanged", 0, "", nil)+}+func PushReloadTag() {BroadcastByType("main", "reloadTag", 0, "", nil)}