Skip to content

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

:bug: Hide expand controls for empty notebook documents https://github.com/siyuan-note/siyuan/issues/18266

Daniel095fa8a3934e
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index ec44ab4292..bbc1d44029 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -207,7 +207,7 @@ export class Files extends Model {
const isFile = liElement.getAttribute("data-type") === "navigation-file";
const isBoxDoc = liElement.getAttribute("data-type") === "navigation-root" && liElement.getAttribute("data-node-id");
if ((isFile || isBoxDoc) && window.siyuan.config.fileTree.docIconClickExpand) {
- if (isBoxDoc || Number(liElement.getAttribute("data-count")) > 0) {
+ if (Number(liElement.getAttribute("data-count")) > 0) {
this.getLeaf(liElement, notebookId);
} else {
needFocus = false;
@@ -297,8 +297,7 @@ export class Files extends Model {
(target.parentElement.getAttribute("data-type") === "navigation-file" ||
(target.parentElement.getAttribute("data-type") === "navigation-root" && target.parentElement.getAttribute("data-node-id"))) &&
window.siyuan.config.fileTree.parentDocClickExpand &&
- (target.parentElement.getAttribute("data-type") === "navigation-root" ||
- Number(target.parentElement.getAttribute("data-count")) > 0)) {
+ Number(target.parentElement.getAttribute("data-count")) > 0) {
this.getLeaf(target.parentElement, notebookId);
event.preventDefault();
event.stopPropagation();
@@ -929,12 +928,21 @@ export class Files extends Model {
}
private updateDocInfo(data: IWebSocketData) {
- const liElement = this.element.querySelector(`li[data-type="navigation-file"][data-node-id="${data.data.rootID}"]`);
+ 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);
- liElement.querySelector(".b3-list-item__text.ariaLabel")?.setAttribute("aria-label", this.genDocAriaLabel(data.data, escapeLessThans));
+ 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");
}
@@ -950,7 +958,7 @@ export class Files extends Model {
const isFile = liElement.getAttribute("data-type") === "navigation-file";
const isBoxDoc = liElement.getAttribute("data-type") === "navigation-root" &&
Boolean(liElement.getAttribute("data-node-id"));
- const hasChildren = isBoxDoc || (isFile && Number(liElement.getAttribute("data-count")) > 0);
+ const hasChildren = (isFile || isBoxDoc) && Number(liElement.getAttribute("data-count")) > 0;
const iconUsesDocAction = window.siyuan.config.fileTree.docIconClickExpand && (isFile || isBoxDoc);
const editingPublishAccess = this.element.classList.contains("file-tree__publish-access--active");
iconElement.setAttribute("aria-label", iconUsesDocAction ?
@@ -958,7 +966,7 @@ export class Files extends Model {
window.siyuan.languages.changeIcon);
liElement.classList.toggle("file-tree__item--icon-expand", hasChildren && iconUsesDocAction &&
!editingPublishAccess);
- liElement.classList.toggle("file-tree__item--icon-open", isFile && !hasChildren && iconUsesDocAction &&
+ liElement.classList.toggle("file-tree__item--icon-open", (isFile || isBoxDoc) && !hasChildren && iconUsesDocAction &&
!editingPublishAccess);
liElement.classList.toggle("file-tree__item--title-expand", hasChildren &&
window.siyuan.config.fileTree.parentDocClickExpand);
@@ -984,8 +992,14 @@ export class Files extends Model {
if (!liElement) {
const dirname = pathPosix().dirname(currentPath);
if (dirname === "/") {
- if (treeElement.firstElementChild.querySelector(".b3-list-item__arrow--open")) {
- this.getLeaf(treeElement.firstElementChild, notebookId, true);
+ 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);
}
break;
} else {
@@ -1011,12 +1025,15 @@ export class Files extends Model {
const iconContent = (item.encrypted && item.closed)
? "🔒️"
: unicode2Emoji(item.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].note);
- const iconExpands = !item.closed && window.siyuan.config.fileTree.boxDocEnabled && item.boxDocID &&
- window.siyuan.config.fileTree.docIconClickExpand;
- const iconAriaLabel = iconExpands ? window.siyuan.languages.docIconClickExpand : window.siyuan.languages.changeIcon;
- const actionClasses = `${iconExpands && !editingPublishAccess ? " file-tree__item--icon-expand" : ""}${
- !item.closed && window.siyuan.config.fileTree.boxDocEnabled && item.boxDocID &&
- window.siyuan.config.fileTree.parentDocClickExpand ? " file-tree__item--title-expand" : ""}`;
+ const isBoxDoc = !item.closed && window.siyuan.config.fileTree.boxDocEnabled && Boolean(item.boxDocID);
+ const hasChildren = isBoxDoc && item.subFileCount > 0;
+ const iconUsesDocAction = isBoxDoc && window.siyuan.config.fileTree.docIconClickExpand;
+ const iconAriaLabel = iconUsesDocAction ?
+ (hasChildren ? window.siyuan.languages.docIconClickExpand : window.siyuan.languages.openDocument) :
+ window.siyuan.languages.changeIcon;
+ const actionClasses = `${iconUsesDocAction && hasChildren && !editingPublishAccess ? " file-tree__item--icon-expand" : ""}${
+ iconUsesDocAction && !hasChildren && !editingPublishAccess ? " file-tree__item--icon-open" : ""}${
+ hasChildren && window.siyuan.config.fileTree.parentDocClickExpand ? " file-tree__item--title-expand" : ""}`;
const emojiHTML = `<span class="b3-list-item__icon ariaLabel${editingPublishAccess ? " fn__none" : ""}" data-position="parentE" aria-label="${iconAriaLabel}">${iconContent}</span>`;
const switchHTML = `<span class="b3-list-item__switch b3-tooltips b3-tooltips__e${editingPublishAccess ? "" : " fn__none"}" aria-label="${window.siyuan.languages.publishAccess}">${getPublishAccessOptionByLevel("public").iconHTML}</span>`;
if (item.closed) {
@@ -1035,8 +1052,8 @@ export class Files extends Model {
return `<ul class="b3-list b3-list--background" data-url="${item.id}" data-sort="${item.sort}" data-sortmode="${item.sortMode}">
<li class="b3-list-item b3-list-item--hide-action${actionClasses}" ${window.siyuan.config.fileTree.sort === 6 ? 'draggable="true"' : ""}
style="--file-toggle-width:22px;--file-action-offset:22px"
-data-type="navigation-root" data-path="/" data-node-id="${window.siyuan.config.fileTree.boxDocEnabled ? (item.boxDocID || "") : ""}">
- <span class="b3-list-item__toggle b3-list-item__toggle--hl">
+data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}" data-node-id="${window.siyuan.config.fileTree.boxDocEnabled ? (item.boxDocID || "") : ""}">
+ <span class="b3-list-item__toggle b3-list-item__toggle--hl${isBoxDoc && !hasChildren ? " fn__hidden" : ""}">
<svg class="b3-list-item__arrow"><use xlink:href="#iconRight"></use></svg>
</span>
${emojiHTML}
@@ -1143,9 +1160,11 @@ data-type="navigation-root" data-path="/" data-node-id="${window.siyuan.config.f
if (parentElement) {
const iconElement = parentElement.querySelector("svg");
iconElement.classList.remove("b3-list-item__arrow--open");
- if (parentElement.dataset.type !== "navigation-root") {
+ if (parentElement.dataset.type !== "navigation-root" || parentElement.dataset.nodeId) {
iconElement.parentElement.classList.add("fn__hidden");
}
+ parentElement.setAttribute("data-count", "0");
+ this.updateDocActionElement(parentElement);
const emojiElement = iconElement.parentElement.nextElementSibling;
if (emojiElement.innerHTML === unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].folder)) {
emojiElement.innerHTML = unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].file);
@@ -1173,7 +1192,8 @@ data-type="navigation-root" data-path="/" data-node-id="${window.siyuan.config.f
liElement.remove();
}
setNoteBook((notebooks: INotebook[]) => {
- const html = this.genNotebook(data.data.box);
+ const notebook = notebooks.find((item) => item.id === data.data.box.id) || data.data.box;
+ const html = this.genNotebook(notebook);
if (this.element.childElementCount === 0) {
this.element.innerHTML = html;
} else {
@@ -1217,11 +1237,13 @@ data-type="navigation-root" data-path="/" data-node-id="${window.siyuan.config.f
}
if (sourceElement.parentElement.childElementCount === 1) {
if (sourceElement.parentElement.previousElementSibling) {
- const parentLiElement = sourceElement.parentElement.previousElementSibling;
- if (parentLiElement.getAttribute("data-type") !== "navigation-root") {
+ const parentLiElement = sourceElement.parentElement.previousElementSibling as HTMLElement;
+ if (parentLiElement.getAttribute("data-type") !== "navigation-root" || parentLiElement.dataset.nodeId) {
parentLiElement.querySelector(".b3-list-item__toggle").classList.add("fn__hidden");
}
parentLiElement.querySelector(".b3-list-item__arrow").classList.remove("b3-list-item__arrow--open");
+ parentLiElement.setAttribute("data-count", "0");
+ this.updateDocActionElement(parentLiElement);
const emojiElement = parentLiElement.querySelector(".b3-list-item__icon");
if (emojiElement.innerHTML === unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].folder)) {
emojiElement.innerHTML = unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].file);
@@ -1242,6 +1264,10 @@ data-type="navigation-root" data-path="/" data-node-id="${window.siyuan.config.f
// 更新移动到的新文件夹
if (newElement) {
newElement.querySelector(".b3-list-item__toggle").classList.remove("fn__hidden");
+ if (newElement.getAttribute("data-type") === "navigation-root") {
+ newElement.setAttribute("data-count", Math.max(1, Number(newElement.getAttribute("data-count"))).toString());
+ this.updateDocActionElement(newElement);
+ }
const emojiElement = newElement.querySelector(".b3-list-item__icon");
if (emojiElement.innerHTML === unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].file)) {
emojiElement.innerHTML = unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].folder);
diff --git a/app/src/menus/navigation.ts b/app/src/menus/navigation.ts
index 1b84c64294..2dafe3f881 100644
--- a/app/src/menus/navigation.ts
+++ b/app/src/menus/navigation.ts
@@ -225,7 +225,8 @@ export const initNavigationMenu = (app: App, liElement: HTMLElement) => {
const name = getNotebookName(notebookId);
/// #if !MOBILE
const boxDocID = liElement.getAttribute("data-node-id");
- if (boxDocID && window.siyuan.config.fileTree.parentDocClickExpand) {
+ if (boxDocID && window.siyuan.config.fileTree.parentDocClickExpand &&
+ Number(liElement.getAttribute("data-count")) > 0) {
window.siyuan.menus.menu.append(new MenuItem({
id: "openDocument",
label: window.siyuan.languages.openDocument,
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index 8388fa89ab..9e5fa77fb3 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -589,8 +589,13 @@ export class MobileFiles extends Model {
if (!liElement) {
const dirname = pathPosix().dirname(currentPath);
if (dirname === "/") {
- if (treeElement.firstElementChild.querySelector(".b3-list-item__arrow--open")) {
- this.getLeaf(treeElement.firstElementChild, notebookId, true);
+ 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);
}
break;
} else {
@@ -611,11 +616,18 @@ export class MobileFiles extends Model {
}
private updateDocInfo(data: IWebSocketData) {
- const liElement = this.element.querySelector(`li[data-type="navigation-file"][data-node-id="${data.data.rootID}"]`);
+ 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");
}
@@ -628,6 +640,8 @@ export class MobileFiles extends Model {
const iconContent = (item.encrypted && item.closed)
? "🔒️"
: unicode2Emoji(item.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].note);
+ const isBoxDoc = !item.closed && window.siyuan.config.fileTree.boxDocEnabled && Boolean(item.boxDocID);
+ const hasChildren = isBoxDoc && item.subFileCount > 0;
const emojiHTML = `<span class="b3-list-item__icon b3-tooltips b3-tooltips__e${editingPublishAccess ? " fn__none" : ""}" aria-label="${window.siyuan.languages.changeIcon}">${iconContent}</span>`;
const switchHTML = `<span class="b3-list-item__switch b3-tooltips b3-tooltips__e${editingPublishAccess ? "" : " fn__none"}" aria-label="${window.siyuan.languages.publishAccess}">${getPublishAccessOptionByLevel("public").iconHTML}</span>`;
if (item.closed) {
@@ -644,8 +658,8 @@ export class MobileFiles extends Model {
</li>`;
} else {
return `<ul class="b3-list b3-list--background" data-url="${item.id}" data-sortmode="${item.sortMode}">
-<li class="b3-list-item" data-type="navigation-root" data-path="/" data-node-id="${window.siyuan.config.fileTree.boxDocEnabled ? (item.boxDocID || "") : ""}" style="--file-toggle-width:24px">
- <span class="b3-list-item__toggle${item.closed ? " fn__hidden" : ""}">
+<li class="b3-list-item" data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}" data-node-id="${window.siyuan.config.fileTree.boxDocEnabled ? (item.boxDocID || "") : ""}" style="--file-toggle-width:24px">
+ <span class="b3-list-item__toggle${isBoxDoc && !hasChildren ? " fn__hidden" : ""}">
<svg class="b3-list-item__arrow"><use xlink:href="#iconRight"></use></svg>
</span>
${emojiHTML}
@@ -718,9 +732,13 @@ export class MobileFiles extends Model {
}
if (sourceElement.parentElement.childElementCount === 1) {
if (sourceElement.parentElement.previousElementSibling) {
- sourceElement.parentElement.previousElementSibling.querySelector(".b3-list-item__toggle").classList.add("fn__hidden");
- sourceElement.parentElement.previousElementSibling.querySelector(".b3-list-item__arrow").classList.remove("b3-list-item__arrow--open");
- const emojiElement = sourceElement.parentElement.previousElementSibling.querySelector(".b3-list-item__icon");
+ const parentLiElement = sourceElement.parentElement.previousElementSibling as HTMLElement;
+ if (parentLiElement.getAttribute("data-type") !== "navigation-root" || parentLiElement.dataset.nodeId) {
+ parentLiElement.querySelector(".b3-list-item__toggle").classList.add("fn__hidden");
+ }
+ parentLiElement.querySelector(".b3-list-item__arrow").classList.remove("b3-list-item__arrow--open");
+ parentLiElement.setAttribute("data-count", "0");
+ const emojiElement = parentLiElement.querySelector(".b3-list-item__icon");
if (emojiElement.innerHTML === unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].folder)) {
emojiElement.innerHTML = unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].file);
}
@@ -744,6 +762,9 @@ export class MobileFiles extends Model {
emojiElement.innerHTML = unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].folder);
}
newElement.querySelector(".b3-list-item__toggle").classList.remove("fn__hidden");
+ if (newElement.getAttribute("data-type") === "navigation-root") {
+ newElement.setAttribute("data-count", Math.max(1, Number(newElement.getAttribute("data-count"))).toString());
+ }
newElement.querySelector(".b3-list-item__arrow").classList.remove("b3-list-item__arrow--open");
if (newElement.nextElementSibling && newElement.nextElementSibling.tagName === "UL") {
newElement.nextElementSibling.remove();
@@ -799,9 +820,10 @@ export class MobileFiles extends Model {
if (parentElement) {
const iconElement = parentElement.querySelector("svg");
iconElement.classList.remove("b3-list-item__arrow--open");
- if (parentElement.dataset.type !== "navigation-root") {
+ if (parentElement.dataset.type !== "navigation-root" || parentElement.dataset.nodeId) {
iconElement.parentElement.classList.add("fn__hidden");
}
+ parentElement.setAttribute("data-count", "0");
const emojiElement = iconElement.parentElement.nextElementSibling;
if (emojiElement.innerHTML === unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].folder)) {
emojiElement.innerHTML = unicode2Emoji(window.siyuan.storage[Constants.LOCAL_IMAGES].file);
@@ -838,7 +860,8 @@ export class MobileFiles extends Model {
}
}
setNoteBook((notebooks: INotebook[]) => {
- const html = this.genNotebook(data.data.box);
+ const notebook = notebooks.find((item) => item.id === data.data.box.id) || data.data.box;
+ const html = this.genNotebook(notebook);
if (this.element.childElementCount === 0) {
this.element.innerHTML = html;
} else {
diff --git a/kernel/api/notebook.go b/kernel/api/notebook.go
index e9af4d4898..e1db1a24a0 100644
--- a/kernel/api/notebook.go
+++ b/kernel/api/notebook.go
@@ -462,9 +462,18 @@ func lsNotebooks(c *gin.Context) {
}
}
+ boxDocEnabled := model.IsBoxDocEnabled()
+ if !flashcard && boxDocEnabled {
+ for _, notebook := range notebooks {
+ if !notebook.Closed && notebook.BoxDocID != "" {
+ notebook.SubFileCount = model.BoxDocSubFileCount(notebook.ID, notebook.BoxDocID)
+ }
+ }
+ }
+
ret.Data = map[string]any{
"notebooks": notebooks,
- "boxDocEnabled": model.IsBoxDocEnabled(),
+ "boxDocEnabled": boxDocEnabled,
}
}
diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go
index ac441eece2..82228fc88d 100644
--- a/kernel/model/blockinfo.go
+++ b/kernel/model/blockinfo.go
@@ -136,7 +136,7 @@ func GetDocInfoInBox(blockID, boxID string) (ret *BlockInfo, err error) {
var subFileCount int
if IsBoxDoc(tree.Box, tree.ID) {
- subFileCount = boxDocSubFileCount(tree.Box)
+ subFileCount = BoxDocSubFileCount(tree.Box, tree.ID)
} else {
boxLocalPath := filepath.Join(util.DataDir, tree.Box)
subFiles, readErr := os.ReadDir(filepath.Join(boxLocalPath, strings.TrimSuffix(tree.Path, ".sy")))
@@ -244,7 +244,7 @@ func GetDocsInfo(blockIDs []string, queryRefCount bool, queryAv bool) (rets []*B
var subFileCount int
if IsBoxDoc(tree.Box, tree.ID) {
- subFileCount = boxDocSubFileCount(tree.Box)
+ subFileCount = BoxDocSubFileCount(tree.Box, tree.ID)
} else {
boxLocalPath := filepath.Join(util.DataDir, tree.Box)
subFiles, readErr := os.ReadDir(filepath.Join(boxLocalPath, strings.TrimSuffix(tree.Path, ".sy")))
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 0207ed3f4a..ad64c31e51 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -49,13 +49,14 @@ import (
// Box 笔记本。
type Box struct {
- ID string `json:"id"`
- BoxDocID string `json:"boxDocID"`
- Name string `json:"name"`
- Icon string `json:"icon"`
- Sort int `json:"sort"`
- SortMode int `json:"sortMode"`
- Closed bool `json:"closed"`
+ ID string `json:"id"`
+ BoxDocID string `json:"boxDocID"`
+ Name string `json:"name"`
+ Icon string `json:"icon"`
+ Sort int `json:"sort"`
+ SortMode int `json:"sortMode"`
+ Closed bool `json:"closed"`
+ SubFileCount int `json:"subFileCount"`
NewFlashcardCount int `json:"newFlashcardCount"`
DueFlashcardCount int `json:"dueFlashcardCount"`
diff --git a/kernel/model/box_doc.go b/kernel/model/box_doc.go
index ff37788c3d..17173225ae 100644
--- a/kernel/model/box_doc.go
+++ b/kernel/model/box_doc.go
@@ -285,9 +285,9 @@ func reconcileBoxDoc(box *Box, boxDocID string) error {
return nil
}
-func boxDocSubFileCount(boxID string) int {
- box := Conf.Box(boxID)
- if nil == box || "" == box.BoxDocID {
+// BoxDocSubFileCount 返回笔记本顶层文档的可见下级文档数。
+func BoxDocSubFileCount(boxID, boxDocID string) int {
+ if "" == boxDocID {
return 0
}
entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID))
@@ -300,7 +300,7 @@ func boxDocSubFileCount(boxID string) int {
continue
}
id := strings.TrimSuffix(entry.Name(), ".sy")
- if id == box.BoxDocID || !ast.IsNodeIDPattern(id) {
+ if id == boxDocID || !ast.IsNodeIDPattern(id) {
continue
}
ial := filesys.DocIAL(filepath.Join(util.DataDir, boxID, entry.Name()))
diff --git a/kernel/model/box_doc_test.go b/kernel/model/box_doc_test.go
index 3846813137..ae1abe2c9f 100644
--- a/kernel/model/box_doc_test.go
+++ b/kernel/model/box_doc_test.go
@@ -17,9 +17,12 @@
package model
import (
+ "os"
+ "path/filepath"
"testing"
"github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
func TestDeterministicBoxDocID(t *testing.T) {
@@ -35,3 +38,36 @@ func TestDeterministicBoxDocID(t *testing.T) {
t.Fatalf("different boxes generated the same document ID [%s]", boxDocID)
}
}
+
+func TestBoxDocSubFileCount(t *testing.T) {
+ originalDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() {
+ util.DataDir = originalDataDir
+ })
+
+ boxID := "20260716120000-abcdefg"
+ boxDocID := "20260716120001-abcdefg"
+ boxDir := filepath.Join(util.DataDir, boxID)
+ if err := os.MkdirAll(boxDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ writeDoc := func(id, properties string) {
+ t.Helper()
+ data := []byte(`{"Properties":` + properties + `}`)
+ if err := os.WriteFile(filepath.Join(boxDir, id+".sy"), data, 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ writeDoc(boxDocID, `{}`)
+ writeDoc("20260716120002-abcdefg", `{}`)
+ writeDoc("20260716120003-abcdefg", `{"custom-hidden":"true"}`)
+ writeDoc("invalid", `{}`)
+ if err := os.Mkdir(filepath.Join(boxDir, "20260716120004-abcdefg.sy"), 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ if actual := BoxDocSubFileCount(boxID, boxDocID); actual != 1 {
+ t.Fatalf("unexpected box document subfile count [%d]", actual)
+ }
+}
diff --git a/kernel/model/file.go b/kernel/model/file.go
index d5974a4c63..865bd817a7 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1073,12 +1073,16 @@ func loadNodesByMode(node *ast.Node, inputIndex, mode, size int, isDoc, isHeadin
}
func writeTreeUpsertQueue(tree *parse.Tree) (err error) {
+ isNew := !filelock.IsExist(filepath.Join(util.DataDir, tree.Box, tree.Path))
size, err := filesys.WriteTree(tree)
if err != nil {
return
}
sql.UpsertTreeQueue(tree)
refreshDocInfoWithSize(tree, size)
+ if isNew {
+ refreshBoxDocInfo(tree)
+ }
return
}
@@ -1704,6 +1708,13 @@ func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string, luteEngin
util.PushEvent(evt)
refreshDocInfo(fromParentTree)
+ fromRoot := path.Dir(fromPath) == "/"
+ if fromRoot {
+ refreshBoxDocInfoByBoxID(fromBox.ID)
+ }
+ if moveToRoot && (!isSameBox || !fromRoot) {
+ refreshBoxDocInfoByBoxID(toBox.ID)
+ }
return
}
@@ -1725,6 +1736,7 @@ func RemoveDoc(boxID, p string) error {
IncSync()
refreshParentDocInfo(tree)
+ refreshBoxDocInfo(tree)
return nil
}
@@ -1755,15 +1767,22 @@ func RemoveDocs(paths []string) error {
}
parentTrees := map[string]*parse.Tree{}
+ boxDocBoxes := map[string]struct{}{}
for _, tree := range trees {
parentTree := loadParentTree(tree)
if nil != parentTree {
parentTrees[parentTree.ID] = parentTree
}
+ if path.Dir(tree.Path) == "/" && !IsBoxDoc(tree.Box, tree.ID) {
+ boxDocBoxes[tree.Box] = struct{}{}
+ }
}
for _, parentTree := range parentTrees {
refreshDocInfo(parentTree)
}
+ for boxID := range boxDocBoxes {
+ refreshBoxDocInfoByBoxID(boxID)
+ }
return nil
}
diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go
index 3b010b7c55..184c661172 100644
--- a/kernel/model/push_reload.go
+++ b/kernel/model/push_reload.go
@@ -18,6 +18,7 @@ package model
import (
"os"
+ "path"
"path/filepath"
"strings"
"time"
@@ -125,6 +126,28 @@ func refreshParentDocInfo(tree *parse.Tree) {
refreshDocInfo0(parentTree, uint64(len(data)))
}
+func refreshBoxDocInfo(tree *parse.Tree) {
+ if nil == tree || path.Dir(tree.Path) != "/" || IsBoxDoc(tree.Box, tree.ID) {
+ return
+ }
+ refreshBoxDocInfoByBoxID(tree.Box)
+}
+
+func refreshBoxDocInfoByBoxID(boxID string) {
+ if !IsBoxDocEnabled() {
+ return
+ }
+ box := Conf.Box(boxID)
+ if nil == box || "" == box.BoxDocID {
+ return
+ }
+ boxDocTree, err := filesys.LoadTree(boxID, boxDocPath(box.BoxDocID), util.NewLute())
+ if nil != err {
+ return
+ }
+ refreshDocInfo(boxDocTree)
+}
+
func refreshDocInfo0(tree *parse.Tree, size uint64) {
cTime, _ := time.ParseInLocation("20060102150405", tree.ID[:14], time.Local)
mTime := cTime
@@ -136,7 +159,7 @@ func refreshDocInfo0(tree *parse.Tree, size uint64) {
subFileCount := 0
if IsBoxDoc(tree.Box, tree.ID) {
- subFileCount = boxDocSubFileCount(tree.Box)
+ subFileCount = BoxDocSubFileCount(tree.Box, tree.ID)
} else if "true" != tree.Root.IALAttr(DocHiddenAttr) {
subDir := filepath.Join(util.DataDir, tree.Box, strings.TrimSuffix(tree.Path, ".sy"))
subFiles, err := os.ReadDir(subDir)