Skip to content

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

:bug: Keep notebook document child state in sync https://github.com/siyuan-note/siyuan/issues/18266

Daniel4cc5c42f0948
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index bbc1d44029..6fced7eb98 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -40,6 +40,7 @@ export class Files extends Model {
public closeElement: HTMLElement;
public lastSelectedElement: Element = null;
private actionsElement: HTMLElement;
+ private reloadNotebookInfoTimeout: number;
constructor(options: { tab: Tab, app: App }) {
super({app: options.app});
@@ -866,6 +867,21 @@ export class Files extends Model {
this.init(false);
});
break;
+ case "reloadNotebookInfo":
+ window.clearTimeout(this.reloadNotebookInfoTimeout);
+ this.reloadNotebookInfoTimeout = window.setTimeout(() => {
+ setNoteBook((notebooks) => {
+ notebooks.forEach((notebook) => {
+ const liElement = this.element.querySelector<HTMLElement>(
+ `ul[data-url="${notebook.id}"] > li[data-type="navigation-root"]`
+ );
+ if (liElement) {
+ this.updateSubFileCount(liElement, notebook.subFileCount);
+ }
+ });
+ });
+ }, 128);
+ break;
case "mount":
this.onMount(data);
this.app.plugins.forEach((item) => {
@@ -928,26 +944,35 @@ export class Files extends Model {
}
private updateDocInfo(data: IWebSocketData) {
+ const notebook = window.siyuan.notebooks.find((item) => item.boxDocID === data.data.rootID);
+ const subFileCount = notebook && window.siyuan.isPublish ? notebook.subFileCount : data.data.subFileCount;
+ if (notebook) {
+ notebook.subFileCount = subFileCount;
+ }
const liElement = this.element.querySelector(
`li[data-node-id="${data.data.rootID}"][data-type="navigation-file"], ` +
`li[data-node-id="${data.data.rootID}"][data-type="navigation-root"]`
);
if (liElement) {
- liElement.setAttribute("data-count", data.data.subFileCount);
if (liElement.getAttribute("data-type") === "navigation-file") {
liElement.querySelector(".b3-list-item__text.ariaLabel")?.setAttribute("aria-label", this.genDocAriaLabel(data.data, escapeLessThans));
}
- if (data.data.subFileCount === 0) {
- liElement.querySelector(".b3-list-item__toggle")?.classList.add("fn__hidden");
- liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
- if (liElement.nextElementSibling?.tagName === "UL") {
- liElement.nextElementSibling.remove();
- }
- } else {
- liElement.querySelector(".b3-list-item__toggle")?.classList.remove("fn__hidden");
+ this.updateSubFileCount(liElement as HTMLElement, subFileCount);
+ }
+ }
+
+ private updateSubFileCount(liElement: HTMLElement, subFileCount: number) {
+ liElement.setAttribute("data-count", subFileCount.toString());
+ if (subFileCount === 0) {
+ liElement.querySelector(".b3-list-item__toggle")?.classList.add("fn__hidden");
+ liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
+ if (liElement.nextElementSibling?.tagName === "UL") {
+ liElement.nextElementSibling.remove();
}
- this.updateDocActionElement(liElement as HTMLElement);
+ } else {
+ liElement.querySelector(".b3-list-item__toggle")?.classList.remove("fn__hidden");
}
+ this.updateDocActionElement(liElement);
}
private updateDocActionElement(liElement: HTMLElement) {
@@ -993,11 +1018,6 @@ export class Files extends Model {
const dirname = pathPosix().dirname(currentPath);
if (dirname === "/") {
const rootElement = treeElement.firstElementChild as HTMLElement;
- if (rootElement.getAttribute("data-node-id")) {
- rootElement.setAttribute("data-count", Math.max(1, Number(rootElement.getAttribute("data-count"))).toString());
- rootElement.querySelector(".b3-list-item__toggle")?.classList.remove("fn__hidden");
- this.updateDocActionElement(rootElement);
- }
if (rootElement.querySelector(".b3-list-item__arrow--open")) {
this.getLeaf(rootElement, notebookId, true);
}
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index 9e5fa77fb3..72c3cb2d49 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -26,6 +26,7 @@ export class MobileFiles extends Model {
public element: HTMLElement;
private actionsElement: HTMLElement;
private closeElement: HTMLElement;
+ private reloadNotebookInfoTimeout: number;
private touchDragState: {
selectedElement: HTMLElement;
startX: number;
@@ -495,6 +496,21 @@ export class MobileFiles extends Model {
this.init(false);
});
break;
+ case "reloadNotebookInfo":
+ window.clearTimeout(this.reloadNotebookInfoTimeout);
+ this.reloadNotebookInfoTimeout = window.setTimeout(() => {
+ setNoteBook((notebooks) => {
+ notebooks.forEach((notebook) => {
+ const liElement = this.element.querySelector<HTMLElement>(
+ `ul[data-url="${notebook.id}"] > li[data-type="navigation-root"]`
+ );
+ if (liElement) {
+ this.updateSubFileCount(liElement, notebook.subFileCount);
+ }
+ });
+ });
+ }, 128);
+ break;
case "mount":
this.onMount(data);
break;
@@ -590,10 +606,6 @@ export class MobileFiles extends Model {
const dirname = pathPosix().dirname(currentPath);
if (dirname === "/") {
const rootElement = treeElement.firstElementChild as HTMLElement;
- if (rootElement.getAttribute("data-node-id")) {
- rootElement.setAttribute("data-count", Math.max(1, Number(rootElement.getAttribute("data-count"))).toString());
- rootElement.querySelector(".b3-list-item__toggle")?.classList.remove("fn__hidden");
- }
if (rootElement.querySelector(".b3-list-item__arrow--open")) {
this.getLeaf(rootElement, notebookId, true);
}
@@ -616,21 +628,30 @@ export class MobileFiles extends Model {
}
private updateDocInfo(data: IWebSocketData) {
+ const notebook = window.siyuan.notebooks.find((item) => item.boxDocID === data.data.rootID);
+ const subFileCount = notebook && window.siyuan.isPublish ? notebook.subFileCount : data.data.subFileCount;
+ if (notebook) {
+ notebook.subFileCount = subFileCount;
+ }
const liElement = this.element.querySelector(
`li[data-node-id="${data.data.rootID}"][data-type="navigation-file"], ` +
`li[data-node-id="${data.data.rootID}"][data-type="navigation-root"]`
);
if (liElement) {
- liElement.setAttribute("data-count", data.data.subFileCount);
- if (data.data.subFileCount === 0) {
- liElement.querySelector(".b3-list-item__toggle")?.classList.add("fn__hidden");
- liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
- if (liElement.nextElementSibling?.tagName === "UL") {
- liElement.nextElementSibling.remove();
- }
- } else {
- liElement.querySelector(".b3-list-item__toggle")?.classList.remove("fn__hidden");
+ this.updateSubFileCount(liElement as HTMLElement, subFileCount);
+ }
+ }
+
+ private updateSubFileCount(liElement: HTMLElement, subFileCount: number) {
+ liElement.setAttribute("data-count", subFileCount.toString());
+ if (subFileCount === 0) {
+ liElement.querySelector(".b3-list-item__toggle")?.classList.add("fn__hidden");
+ liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
+ if (liElement.nextElementSibling?.tagName === "UL") {
+ liElement.nextElementSibling.remove();
}
+ } else {
+ liElement.querySelector(".b3-list-item__toggle")?.classList.remove("fn__hidden");
}
}
diff --git a/kernel/api/notebook.go b/kernel/api/notebook.go
index e1db1a24a0..809709e62c 100644
--- a/kernel/api/notebook.go
+++ b/kernel/api/notebook.go
@@ -427,6 +427,8 @@ func lsNotebooks(c *gin.Context) {
}
var notebooks []*model.Box
+ var publishAccess model.PublishAccess
+ isReadOnlyRole := model.IsReadOnlyRoleContext(c)
if flashcard {
notebooks = model.GetFlashcardNotebooks()
} else {
@@ -435,8 +437,8 @@ func lsNotebooks(c *gin.Context) {
if err != nil {
return
}
- if model.IsReadOnlyRoleContext(c) {
- publishAccess := model.GetPublishAccess()
+ if isReadOnlyRole {
+ publishAccess = model.GetPublishAccess()
tempNotebooks := []*model.Box{}
for _, notebook := range notebooks {
// 筛除关闭的笔记本
@@ -466,7 +468,11 @@ func lsNotebooks(c *gin.Context) {
if !flashcard && boxDocEnabled {
for _, notebook := range notebooks {
if !notebook.Closed && notebook.BoxDocID != "" {
- notebook.SubFileCount = model.BoxDocSubFileCount(notebook.ID, notebook.BoxDocID)
+ if isReadOnlyRole {
+ notebook.SubFileCount = model.BoxDocSubFileCountForPublish(notebook.ID, notebook.BoxDocID, publishAccess)
+ } else {
+ notebook.SubFileCount = model.BoxDocSubFileCount(notebook.ID, notebook.BoxDocID)
+ }
}
}
}
diff --git a/kernel/model/box_doc.go b/kernel/model/box_doc.go
index 17173225ae..8fabb47d34 100644
--- a/kernel/model/box_doc.go
+++ b/kernel/model/box_doc.go
@@ -287,6 +287,18 @@ func reconcileBoxDoc(box *Box, boxDocID string) error {
// BoxDocSubFileCount 返回笔记本顶层文档的可见下级文档数。
func BoxDocSubFileCount(boxID, boxDocID string) int {
+ return boxDocSubFileCount(boxID, boxDocID, nil)
+}
+
+// BoxDocSubFileCountForPublish 返回发布访问控制下笔记本顶层文档的可见下级文档数。
+func BoxDocSubFileCountForPublish(boxID, boxDocID string, publishAccess PublishAccess) int {
+ publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ return boxDocSubFileCount(boxID, boxDocID, func(p string) bool {
+ return CheckPathAccessableByPublishIgnore(boxID, p, publishIgnore)
+ })
+}
+
+func boxDocSubFileCount(boxID, boxDocID string, include func(string) bool) int {
if "" == boxDocID {
return 0
}
@@ -303,6 +315,10 @@ func BoxDocSubFileCount(boxID, boxDocID string) int {
if id == boxDocID || !ast.IsNodeIDPattern(id) {
continue
}
+ p := "/" + entry.Name()
+ if nil != include && !include(p) {
+ continue
+ }
ial := filesys.DocIAL(filepath.Join(util.DataDir, boxID, entry.Name()))
if "true" == ial[DocHiddenAttr] {
continue
diff --git a/kernel/model/box_doc_test.go b/kernel/model/box_doc_test.go
index ae1abe2c9f..ccc0dc092f 100644
--- a/kernel/model/box_doc_test.go
+++ b/kernel/model/box_doc_test.go
@@ -70,4 +70,12 @@ func TestBoxDocSubFileCount(t *testing.T) {
if actual := BoxDocSubFileCount(boxID, boxDocID); actual != 1 {
t.Fatalf("unexpected box document subfile count [%d]", actual)
}
+ publishAccess := PublishAccess{{ID: "20260716120002-abcdefg", Visible: false}}
+ if actual := BoxDocSubFileCountForPublish(boxID, boxDocID, publishAccess); actual != 0 {
+ t.Fatalf("unexpected published box document subfile count [%d]", actual)
+ }
+ publishAccess[0].Visible = true
+ if actual := BoxDocSubFileCountForPublish(boxID, boxDocID, publishAccess); actual != 1 {
+ t.Fatalf("unexpected visible published box document subfile count [%d]", actual)
+ }
}
diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go
index 184c661172..c25204c0c9 100644
--- a/kernel/model/push_reload.go
+++ b/kernel/model/push_reload.go
@@ -141,11 +141,7 @@ func refreshBoxDocInfoByBoxID(boxID string) {
if nil == box || "" == box.BoxDocID {
return
}
- boxDocTree, err := filesys.LoadTree(boxID, boxDocPath(box.BoxDocID), util.NewLute())
- if nil != err {
- return
- }
- refreshDocInfo(boxDocTree)
+ util.BroadcastByType("filetree", "reloadNotebookInfo", 0, "", boxID)
}
func refreshDocInfo0(tree *parse.Tree, size uint64) {
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 7ebb063982..1fab5cfaaa 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -2152,9 +2152,11 @@ func (tx *Transaction) commit() (err error) {
if nil == box {
return ErrBoxNotFound
}
- if _, err = removeDoc(box, tree.Path, util.NewLute()); nil != err {
+ removedTree, err := removeDoc(box, tree.Path, util.NewLute())
+ if nil != err {
return err
}
+ refreshBoxDocInfo(removedTree)
}
for _, tree := range tx.restoredCreatedDocs {
box := Conf.Box(tree.Box)