Skip to content

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

:bug: Allow pinning notebook root documents https://github.com/siyuan-note/siyuan/issues/19401

Danielca736c86d38c
diff --git a/app/src/layout/dock/PinnedDocs.test.ts b/app/src/layout/dock/PinnedDocs.test.ts
index 0cf534b4b3..62061b61b1 100644
--- a/app/src/layout/dock/PinnedDocs.test.ts
+++ b/app/src/layout/dock/PinnedDocs.test.ts
@@ -6,6 +6,7 @@ import {runInNewContext} from "node:vm";
import * as ts from "typescript";
interface IPanelHarness {
+ loadChildren(row: unknown, children: unknown, generation: number): Promise<void>;
drop(ids: string[], x: number, y: number): Promise<void>;
click(event: unknown): void;
previewDrop(): void;
@@ -15,11 +16,11 @@ interface IPanelHarness {
suppressClick?: boolean;
}
-const loadPanel = () => {
+const loadPanel = (fetchCode = 0) => {
const calls: {kind: string, args: unknown[]}[] = [];
const record = (kind: string) => async (...args: unknown[]) => {
calls.push({kind, args});
- return {code: 0};
+ return {code: kind === "http" ? fetchCode : 0};
};
const exports: {PinnedDocs?: {prototype: object}} = {};
const source = ts.transpileModule(readFileSync(join(__dirname, "PinnedDocs.ts"), "utf8"), {
@@ -81,3 +82,12 @@ test("panel clicks never bubble into the mobile source tree handler", () => {
assert.equal(stopped, 2);
assert.equal(prevented, 1);
});
+
+test("notebook root expansion requests physical root while documents keep their own paths", async () => {
+ const {panel, calls} = loadPanel(-1);
+ for (const id of ["notebook", "document"]) {
+ await panel.loadChildren({dataset: {notebook: "notebook", nodeId: id, path: `/${id}.sy`}}, {}, 0);
+ }
+ assert.equal(JSON.stringify(calls[0].args[1]), JSON.stringify({notebook: "notebook", path: "/", maxListCount: 0}));
+ assert.equal(JSON.stringify(calls[1].args[1]), JSON.stringify({notebook: "notebook", path: "/document.sy", maxListCount: 0}));
+});
diff --git a/app/src/layout/dock/PinnedDocs.ts b/app/src/layout/dock/PinnedDocs.ts
index 7cf90c0bf8..be008c2f18 100644
--- a/app/src/layout/dock/PinnedDocs.ts
+++ b/app/src/layout/dock/PinnedDocs.ts
@@ -230,7 +230,8 @@ export class PinnedDocs {
private async loadChildren(row: HTMLElement, children: HTMLElement, generation: number) {
const response = await fetchSyncPost("/api/filetree/listDocsByPath", {
- notebook: row.dataset.notebook, path: row.dataset.path,
+ notebook: row.dataset.notebook,
+ path: row.dataset.nodeId === row.dataset.notebook ? "/" : row.dataset.path,
maxListCount: 0,
});
if (response.code !== 0 || generation !== this.generation || !this.expanded.has(row.dataset.pinRow)) {
diff --git a/kernel/model/pinned_docs.go b/kernel/model/pinned_docs.go
index a8b75c5ef4..8446e1ab63 100644
--- a/kernel/model/pinned_docs.go
+++ b/kernel/model/pinned_docs.go
@@ -35,6 +35,11 @@ type PinnedDoc struct {
var pinnedDocsLock sync.Mutex
+// 置顶入口允许普通笔记本根文档,不使用同级排序对根文档的排除规则。
+func isPinnableDocument(tree *treenode.BlockTree) bool {
+ return tree != nil && tree.ID == tree.RootID && tree.Type == "d" && !IsEncryptedBox(tree.BoxID)
+}
+
func readPinnedDocs() (ret pinnedDocsStorage, err error) {
ret = pinnedDocsStorage{Version: 1, Docs: []pinnedDocRef{}}
data, err := filelock.ReadFile(filepath.Join(util.DataDir, "storage", "pinned-docs.json"))
@@ -113,7 +118,7 @@ func GetPinnedDocs() (ret []PinnedDoc, err error) {
ret = append(ret, doc)
continue
}
- if !isSortableDocument(bt) {
+ if !isPinnableDocument(bt) {
continue
}
info := box.Stat(bt.Path)
@@ -133,12 +138,16 @@ func GetPinnedDocs() (ret []PinnedDoc, err error) {
if file.TitleEmpty {
doc.Name = Conf.Language(16)
}
- entries, readErr := os.ReadDir(filepath.Join(util.DataDir, boxID, strings.TrimSuffix(bt.Path, ".sy")))
+ childrenPath := strings.TrimSuffix(bt.Path, ".sy")
+ if ref.ID == boxID {
+ childrenPath = "/"
+ }
+ entries, readErr := os.ReadDir(filepath.Join(util.DataDir, boxID, childrenPath))
if readErr != nil && !os.IsNotExist(readErr) {
return nil, readErr
}
for _, entry := range entries {
- if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".sy") && ast.IsNodeIDPattern(strings.TrimSuffix(entry.Name(), ".sy")) {
+ if !entry.IsDir() && entry.Name() != ref.ID+".sy" && strings.HasSuffix(entry.Name(), ".sy") && ast.IsNodeIDPattern(strings.TrimSuffix(entry.Name(), ".sy")) {
doc.SubFileCount++
}
}
@@ -175,7 +184,7 @@ func UpdatePinnedDocs(ids []string, action, targetID string, after bool) error {
continue
}
bt := treenode.GetBlockTree(id)
- if !isSortableDocument(bt) || IsEncryptedBox(bt.BoxID) {
+ if !isPinnableDocument(bt) {
return fmt.Errorf("document [%s] cannot be pinned", id)
}
box := Conf.Box(bt.BoxID)
diff --git a/kernel/model/pinned_docs_test.go b/kernel/model/pinned_docs_test.go
index c655f6d4ef..7537378bce 100644
--- a/kernel/model/pinned_docs_test.go
+++ b/kernel/model/pinned_docs_test.go
@@ -6,6 +6,9 @@ import (
"reflect"
"testing"
+ "github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/filesys"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -129,3 +132,36 @@ func TestPinnedDocsClosedAndEncryptedNotebook(t *testing.T) {
t.Fatalf("encrypted document exposed: %+v, %v", docs, err)
}
}
+
+func TestPinnedDocsSupportNotebookRoot(t *testing.T) {
+ f := setupFileOperationTest(t)
+ root := treenode.NewTree(f.box.ID, "/"+f.box.ID+".sy", "/Notebook", "Notebook")
+ if _, err := filesys.WriteTree(root); err != nil {
+ t.Fatal(err)
+ }
+ treenode.UpsertBlockTree(root)
+ t.Cleanup(func() { cache.RemoveTreeData(root.ID); cache.RemoveDocIAL(root.Path) })
+ if err := UpdatePinnedDocs([]string{root.ID}, "pin", "", false); err != nil {
+ t.Fatal(err)
+ }
+ docs, err := GetPinnedDocs()
+ if err != nil || len(docs) != 1 {
+ t.Fatalf("notebook root missing: %+v, %v", docs, err)
+ }
+ if docs[0].ID != root.ID || docs[0].Path != root.Path || docs[0].SubFileCount != 2 {
+ t.Fatalf("incorrect root document or child count: %+v", docs[0])
+ }
+ if isSortableDocument(treenode.GetBlockTree(root.ID)) {
+ t.Fatal("pinning changed source sorting eligibility")
+ }
+ if err := UpdatePinnedDocs([]string{f.childID}, "pin", "", false); err == nil {
+ t.Fatal("non-document block accepted")
+ }
+ if err := UpdatePinnedDocs([]string{root.ID}, "unpin", "", false); err != nil {
+ t.Fatal(err)
+ }
+ docs, err = GetPinnedDocs()
+ if err != nil || len(docs) != 0 {
+ t.Fatalf("notebook root unpin failed: %+v, %v", docs, err)
+ }
+}