Skip to content

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

:art: Improve heading2doc/li2doc sorting Fix https://github.com/siyuan-note/siyuan/issues/9668

Daniel509154262921
diff --git a/kernel/model/file.go b/kernel/model/file.go
index d5d637a155..dae860526d 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1777,3 +1777,48 @@ func (box *Box) removeSort(ids []string) {
return
}
}
+
+func (box *Box) addMinSort(parentPath, id string) {
+ docs, _, err := ListDocTree(box.ID, parentPath, util.SortModeUnassigned, false, false, 1)
+ if nil != err {
+ logging.LogErrorf("list doc tree failed: %s", err)
+ return
+ }
+
+ sortVal := 0
+ if 0 < len(docs) {
+ sortVal = docs[0].Sort - 1
+ }
+
+ confDir := filepath.Join(util.DataDir, box.ID, ".siyuan")
+ if err = os.MkdirAll(confDir, 0755); nil != err {
+ logging.LogErrorf("create conf dir failed: %s", err)
+ return
+ }
+ confPath := filepath.Join(confDir, "sort.json")
+ fullSortIDs := map[string]int{}
+ var data []byte
+ if filelock.IsExist(confPath) {
+ data, err = filelock.ReadFile(confPath)
+ if nil != err {
+ logging.LogErrorf("read sort conf failed: %s", err)
+ return
+ }
+
+ if err = gulu.JSON.UnmarshalJSON(data, &fullSortIDs); nil != err {
+ logging.LogErrorf("unmarshal sort conf failed: %s", err)
+ }
+ }
+
+ fullSortIDs[id] = sortVal
+
+ data, err = gulu.JSON.MarshalJSON(fullSortIDs)
+ if nil != err {
+ logging.LogErrorf("marshal sort conf failed: %s", err)
+ return
+ }
+ if err = filelock.WriteFile(confPath, data); nil != err {
+ logging.LogErrorf("write sort conf failed: %s", err)
+ return
+ }
+}
diff --git a/kernel/model/heading.go b/kernel/model/heading.go
index 38a7528ca1..2d11fdc712 100644
--- a/kernel/model/heading.go
+++ b/kernel/model/heading.go
@@ -362,6 +362,7 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath string) (srcRootBlockID,
newTree.Box, newTree.Path = targetBoxID, newTargetPath
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
newTree.Root.Spec = "1"
+ box.addMinSort(path.Dir(newTargetPath), newTree.ID)
if err = indexWriteJSONQueue(newTree); nil != err {
return "", "", err
}
diff --git a/kernel/model/listitem.go b/kernel/model/listitem.go
index 26ad924422..ef45ccd6e6 100644
--- a/kernel/model/listitem.go
+++ b/kernel/model/listitem.go
@@ -104,6 +104,7 @@ func ListItem2Doc(srcListItemID, targetBoxID, targetPath string) (srcRootBlockID
newTree.Box, newTree.Path = targetBoxID, newTargetPath
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
newTree.Root.Spec = "1"
+ box.addMinSort(path.Dir(newTargetPath), newTree.ID)
if err = indexWriteJSONQueue(newTree); nil != err {
return "", "", err
}