🎨 Supports setting newly created sub-documents to be listed last https://github.com/siyuan-note/siyuan/issues/16327

Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
Daniel 2025-11-11 10:54:54 +08:00
parent 72acce2c86
commit 6196173898
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
23 changed files with 90 additions and 9 deletions

View file

@ -34,8 +34,8 @@ type FileTree struct {
CloseTabsOnStart bool `json:"closeTabsOnStart"` // 启动时关闭所有页签
UseSingleLineSave bool `json:"useSingleLineSave"` // 使用单行保存文档 .sy 和属性视图 .json
LargeFileWarningSize int `json:"largeFileWarningSize"` // 大文件警告大小单位MB
Sort int `json:"sort"` // 排序方式
CreateDocAtTop *bool `json:"createDocAtTop"` // 在顶部创建新文档 https://github.com/siyuan-note/siyuan/issues/16327
Sort int `json:"sort"` // 排序方式
}
func NewFileTree() *FileTree {
@ -49,5 +49,6 @@ func NewFileTree() *FileTree {
CloseTabsOnStart: false,
UseSingleLineSave: util.UseSingleLineSave,
LargeFileWarningSize: util.LargeFileWarningSize,
CreateDocAtTop: func() *bool { b := false; return &b }(),
}
}

View file

@ -222,6 +222,9 @@ func InitConf() {
Conf.FileTree.LargeFileWarningSize = 8
}
util.LargeFileWarningSize = Conf.FileTree.LargeFileWarningSize
if nil == Conf.FileTree.CreateDocAtTop { // v3.4.0 之前的版本没有该字段,设置默认值为 true即在顶部创建新文档不改变用户习惯
Conf.FileTree.CreateDocAtTop = func() *bool { b := true; return &b }()
}
util.CurrentCloudRegion = Conf.CloudRegion

View file

@ -1032,7 +1032,7 @@ func CreateDocByMd(boxID, p, title, md string, sorts []string) (tree *parse.Tree
if 0 < len(sorts) {
ChangeFileTreeSort(box.ID, sorts)
} else {
box.addMinSort(path.Dir(tree.Path), tree.ID)
box.setSortByConf(path.Dir(tree.Path), tree.ID)
}
return
}
@ -1079,7 +1079,7 @@ func CreateWithMarkdown(tags, boxID, hPath, md, parentID, id string, withMath bo
logging.LogWarnf("get block tree by id [%s] failed after create", retID)
return
}
box.addMinSort(path.Dir(bt.Path), retID)
box.setSortByConf(path.Dir(bt.Path), retID)
return
}
@ -1994,6 +1994,29 @@ func (box *Box) removeSort(ids []string) {
}
}
func (box *Box) setSortByConf(parentPath, id string) {
if *Conf.FileTree.CreateDocAtTop {
box.addMinSort(parentPath, id)
} else {
box.addMaxSort(parentPath, id)
}
}
func (box *Box) addMaxSort(parentPath, id string) {
docs, _, err := ListDocTree(box.ID, parentPath, util.SortModeUnassigned, false, false, 102400)
if err != nil {
logging.LogErrorf("list doc tree failed: %s", err)
return
}
sortVal := 0
if 0 < len(docs) {
sortVal = docs[len(docs)-1].Sort + 1
}
box.setSortVal(id, sortVal)
}
func (box *Box) addMinSort(parentPath, id string) {
docs, _, err := ListDocTree(box.ID, parentPath, util.SortModeUnassigned, false, false, 1)
if err != nil {
@ -2006,6 +2029,11 @@ func (box *Box) addMinSort(parentPath, id string) {
sortVal = docs[0].Sort - 1
}
box.setSortVal(id, sortVal)
}
func (box *Box) setSortVal(id string, sortVal int) {
var err error
confDir := filepath.Join(util.DataDir, box.ID, ".siyuan")
if err = os.MkdirAll(confDir, 0755); err != nil {
logging.LogErrorf("create conf dir failed: %s", err)
@ -2027,7 +2055,6 @@ func (box *Box) addMinSort(parentPath, id string) {
}
fullSortIDs[id] = sortVal
data, err = gulu.JSON.MarshalJSON(fullSortIDs)
if err != nil {
logging.LogErrorf("marshal sort conf failed: %s", err)
@ -2037,6 +2064,7 @@ func (box *Box) addMinSort(parentPath, id string) {
logging.LogErrorf("write sort conf failed: %s", err)
return
}
return
}
func (box *Box) addSort(previousPath, id string) {

View file

@ -431,7 +431,7 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath, previousPath string) (sr
if "" != previousPath {
box.addSort(previousPath, newTree.ID)
} else {
box.addMinSort(path.Dir(newTargetPath), newTree.ID)
box.setSortByConf(path.Dir(newTargetPath), newTree.ID)
}
if err = indexWriteTreeUpsertQueue(newTree); err != nil {
return "", "", err

View file

@ -132,7 +132,7 @@ func ListItem2Doc(srcListItemID, targetBoxID, targetPath, previousPath string) (
if "" != previousPath {
box.addSort(previousPath, newTree.ID)
} else {
box.addMinSort(path.Dir(newTargetPath), newTree.ID)
box.setSortByConf(path.Dir(newTargetPath), newTree.ID)
}
if err = indexWriteTreeUpsertQueue(newTree); err != nil {
return "", "", err

View file

@ -106,7 +106,7 @@ func MoveLocalShorthands(boxID, hPath, parentID string) (retIDs []string, err er
retIDs = append(retIDs, retID)
toRemoves = append(toRemoves, p)
box.addMinSort("/", retID)
box.setSortByConf("/", retID)
}
} else { // 不为空的话将所有速记合并到指定路径的文档中
if !strings.HasPrefix(hPath, "/") {