diff --git a/kernel/model/file.go b/kernel/model/file.go index e2ad3c25b..34b23320e 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -2175,7 +2175,7 @@ func (box *Box) addMinSort(parentPath, id string) { } } -func (box *Box) addSort(previousID, id string) { +func (box *Box) addSort(previousPath, id string) { confDir := filepath.Join(util.DataDir, box.ID, ".siyuan") if err := os.MkdirAll(confDir, 0755); err != nil { logging.LogErrorf("create conf dir failed: %s", err) @@ -2196,17 +2196,25 @@ func (box *Box) addSort(previousID, id string) { } } - sortVal := 0 - previousSortVal, ok := fullSortIDs[previousID] - if !ok { - sortVal++ - } else { - sortVal = previousSortVal + 1 + parentPath := path.Dir(previousPath) + docs, _, err := ListDocTree(box.ID, parentPath, util.SortModeUnassigned, false, false, Conf.FileTree.MaxListCount) + if err != nil { + logging.LogErrorf("list doc tree failed: %s", err) + return } - fullSortIDs[id] = sortVal + previousID := util.GetTreeID(previousPath) + sortVal := 0 + for _, doc := range docs { + fullSortIDs[doc.ID] = sortVal + if doc.ID == previousID { + sortVal++ + fullSortIDs[id] = sortVal + } + sortVal++ + } - data, err := gulu.JSON.MarshalJSON(fullSortIDs) + data, err = gulu.JSON.MarshalJSON(fullSortIDs) if err != nil { logging.LogErrorf("marshal sort conf failed: %s", err) return diff --git a/kernel/model/heading.go b/kernel/model/heading.go index 5c4f8cdda..f0675beab 100644 --- a/kernel/model/heading.go +++ b/kernel/model/heading.go @@ -394,7 +394,7 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath, previousPath string) (sr newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr()) newTree.Root.Spec = "1" if "" != previousPath { - box.addSort(util.GetTreeID(previousPath), newTree.ID) + box.addSort(previousPath, newTree.ID) } else { box.addMinSort(path.Dir(newTargetPath), newTree.ID) } diff --git a/kernel/model/listitem.go b/kernel/model/listitem.go index 7cc87f34c..5c23ed43d 100644 --- a/kernel/model/listitem.go +++ b/kernel/model/listitem.go @@ -128,7 +128,7 @@ func ListItem2Doc(srcListItemID, targetBoxID, targetPath, previousPath string) ( newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr()) newTree.Root.Spec = "1" if "" != previousPath { - box.addSort(util.GetTreeID(previousPath), newTree.ID) + box.addSort(previousPath, newTree.ID) } else { box.addMinSort(path.Dir(newTargetPath), newTree.ID) }