Skip to content

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

:art: Prefer inherited custom sorting after document tree dragging https://github.com/siyuan-note/siyuan/issues/19176

Danielb42a2032f484
diff --git a/kernel/model/file_tree_reorder.go b/kernel/model/file_tree_reorder.go
index 5699e379e2..c17c8b06b4 100644
--- a/kernel/model/file_tree_reorder.go
+++ b/kernel/model/file_tree_reorder.go
@@ -77,6 +77,29 @@ type DocTreeReorderResult struct {
ParentPath string `json:"parentPath"`
}
+// 父级已经使用自定义排序时恢复继承,避免留下多余的排序覆盖。
+func resolveDraggedDocSortMode(boxID, listPath string) (*int, error) {
+ mode := util.SortModeCustom
+ if listPath == "/" {
+ if Conf.FileTree.Sort == util.SortModeCustom {
+ mode = util.SortModeFileTree
+ }
+ return &mode, nil
+ }
+ parentPath := path.Dir(strings.TrimSuffix(listPath, ".sy"))
+ if parentPath != "/" {
+ parentPath += ".sy"
+ }
+ inherited, err := ResolveDocTreeSortMode(boxID, parentPath)
+ if err != nil {
+ return nil, err
+ }
+ if inherited == util.SortModeCustom {
+ return nil, nil
+ }
+ return &mode, nil
+}
+
// ReorderDocTree 在移动前检查完整列表,确认冲突后仅设置目标列表的排序方式。
func ReorderDocTree(sourceIDs []string, targetID, position string, preview, removeSorts bool) (ret *DocTreeReorderResult, err error) {
if err = validateReorderArgs(sourceIDs, targetID, position); err != nil {
@@ -169,6 +192,13 @@ func ReorderDocTree(sourceIDs []string, targetID, position string, preview, remo
if preview || !ret.Changed || ret.Conflict && !removeSorts {
return ret, nil
}
+ var declaredMode *int
+ if ret.Conflict {
+ declaredMode, err = resolveDraggedDocSortMode(box.ID, listPath)
+ if err != nil {
+ return ret, err
+ }
+ }
if len(fromPaths) > 0 {
if err = MoveDocs(fromPaths, box.ID, listPath, nil); err != nil {
return ret, err
@@ -191,15 +221,14 @@ func ReorderDocTree(sourceIDs []string, targetID, position string, preview, remo
return ret, err
}
if ret.Conflict {
- custom := util.SortModeCustom
if listPath == "/" {
boxConf := box.GetConf()
- boxConf.SortMode = custom
+ boxConf.SortMode = *declaredMode
if err = box.SaveConf(boxConf); err != nil {
return ret, err
}
- PushDocSortModeChanged("notebook", box.ID, "", "/", &custom)
- } else if _, err = SetDocSortMode(util.GetTreeID(listPath), &custom); err != nil {
+ PushDocSortModeChanged("notebook", box.ID, "", "/", declaredMode)
+ } else if _, err = SetDocSortMode(util.GetTreeID(listPath), declaredMode); err != nil {
return ret, err
}
}
diff --git a/kernel/model/file_tree_reorder_test.go b/kernel/model/file_tree_reorder_test.go
index 0921713271..0cb743ccd8 100644
--- a/kernel/model/file_tree_reorder_test.go
+++ b/kernel/model/file_tree_reorder_test.go
@@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"reflect"
+ "strconv"
"testing"
"github.com/siyuan-note/siyuan/kernel/cache"
@@ -134,3 +135,61 @@ func TestDocTreeReorderMultipleSourcesKeepSelectionOrder(t *testing.T) {
}
assertSiblingCustomOrder(t, f.box.ID, "/", []string{extra.ID, f.targetID, f.sourceID})
}
+
+func TestDocTreeReorderInheritsCustomPanelSort(t *testing.T) {
+ f := setupFileOperationTest(t)
+ Conf.FileTree.Sort = util.SortModeCustom
+ boxConf := f.box.GetConf()
+ boxConf.SortMode = util.SortModeNameASC
+ if err := f.box.SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+ result, err := ReorderDocTree([]string{f.targetID}, f.sourceID, "before", false, true)
+ if err != nil || !result.Conflict {
+ t.Fatalf("reorder failed: %+v, %v", result, err)
+ }
+ if mode := f.box.GetConf().SortMode; mode != util.SortModeFileTree {
+ t.Fatalf("expected panel inheritance, got %d", mode)
+ }
+ assertSiblingCustomOrder(t, f.box.ID, "/", []string{f.targetID, f.sourceID})
+ Conf.FileTree.Sort = util.SortModeCreatedASC
+ if mode, err := ResolveDocTreeSortMode(f.box.ID, "/"); err != nil || mode != util.SortModeCreatedASC {
+ t.Fatalf("notebook did not follow later panel changes: %d, %v", mode, err)
+ }
+}
+
+func TestDraggedDocSortModeUsesParentInheritance(t *testing.T) {
+ f := setupFileOperationTest(t)
+ Conf.FileTree.Sort = util.SortModeNameASC
+ parent, err := LoadTreeByBlockID(f.sourceID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ childPath := "/" + f.sourceID + "/20260718000003-abcdefg.sy"
+ for _, parentMode := range []int{util.SortModeCustom, util.SortModeUpdatedDESC} {
+ parent.Root.SetIALAttr(DocSortModeAttr, strconv.Itoa(parentMode))
+ if _, err = filesys.WriteTree(parent); err != nil {
+ t.Fatal(err)
+ }
+ cache.RemoveDocIALInBox(parent.Path, parent.Box)
+ mode, err := resolveDraggedDocSortMode(f.box.ID, childPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if parentMode == util.SortModeCustom {
+ if mode != nil {
+ t.Fatalf("expected parent inheritance, got %d", *mode)
+ }
+ } else if mode == nil || *mode != util.SortModeCustom {
+ t.Fatalf("expected explicit custom sort, got %v", mode)
+ }
+ }
+ boxConf := f.box.GetConf()
+ boxConf.SortMode = util.SortModeCustom
+ if err = f.box.SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+ if mode, err := resolveDraggedDocSortMode(f.box.ID, f.sourcePath); err != nil || mode != nil {
+ t.Fatalf("expected notebook inheritance despite the document override: %v, %v", mode, err)
+ }
+}