diff --git a/kernel/model/file.go b/kernel/model/file.go index d5d637a15..dae860526 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 38a7528ca..2d11fdc71 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 26ad92442..ef45ccd6e 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 }