From 6f7e106be49f7602a2136dac54de5e26d4803330 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 29 Nov 2024 08:41:43 +0800 Subject: [PATCH 1/2] :recycle: Extract method util.GetTreeID --- kernel/api/block_op.go | 6 ++---- kernel/model/box.go | 2 +- kernel/model/export.go | 10 +++++----- kernel/model/file.go | 10 +++++----- kernel/model/heading.go | 2 +- kernel/model/history.go | 7 +++---- kernel/model/import.go | 2 +- kernel/model/listitem.go | 2 +- kernel/model/sync.go | 5 ++--- kernel/treenode/tree.go | 3 +-- kernel/util/path.go | 4 ++++ 11 files changed, 26 insertions(+), 27 deletions(-) diff --git a/kernel/api/block_op.go b/kernel/api/block_op.go index 2e8578e53..8e86a36b2 100644 --- a/kernel/api/block_op.go +++ b/kernel/api/block_op.go @@ -19,8 +19,6 @@ package api import ( "errors" "net/http" - "path" - "strings" "github.com/88250/gulu" "github.com/88250/lute" @@ -113,7 +111,7 @@ func appendDailyNoteBlock(c *gin.Context) { return } - parentID := strings.TrimSuffix(path.Base(p), ".sy") + parentID := util.GetTreeID(p) transactions := []*model.Transaction{ { DoOperations: []*model.Operation{ @@ -166,7 +164,7 @@ func prependDailyNoteBlock(c *gin.Context) { return } - parentID := strings.TrimSuffix(path.Base(p), ".sy") + parentID := util.GetTreeID(p) transactions := []*model.Transaction{ { DoOperations: []*model.Operation{ diff --git a/kernel/model/box.go b/kernel/model/box.go index d54912ad3..2425af4e2 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -708,7 +708,7 @@ func getBoxesByPaths(paths []string) (ret map[string]*Box) { ret = map[string]*Box{} var ids []string for _, p := range paths { - ids = append(ids, strings.TrimSuffix(path.Base(p), ".sy")) + ids = append(ids, util.GetTreeID(p)) } bts := treenode.GetBlockTrees(ids) diff --git a/kernel/model/export.go b/kernel/model/export.go index a223d517e..7d36e85cd 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -404,7 +404,7 @@ func ExportSY(id string) (name, zipPath string) { docPaths = append(docPaths, docFile.path) } zipPath = exportSYZip(boxID, path.Dir(rootPath), baseFolderName, docPaths) - name = strings.TrimSuffix(filepath.Base(block.Path), ".sy") + name = util.GetTreeID(block.Path) return } @@ -1409,7 +1409,7 @@ func BatchExportPandocConvertZip(ids []string, pandocTo, ext string) (name, zipP docPaths = util.FilterSelfChildDocs(docPaths) zipPath = exportPandocConvertZip(false, box.ID, baseFolderName, docPaths, "gfm+footnotes+hard_line_breaks", pandocTo, ext) - name = strings.TrimSuffix(filepath.Base(block.Path), ".sy") + name = util.GetTreeID(block.Path) return } @@ -1433,7 +1433,7 @@ func ExportPandocConvertZip(id, pandocTo, ext string) (name, zipPath string) { } zipPath = exportPandocConvertZip(false, boxID, baseFolderName, docPaths, "gfm+footnotes+hard_line_breaks", pandocTo, ext) - name = strings.TrimSuffix(filepath.Base(block.Path), ".sy") + name = util.GetTreeID(block.Path) return } @@ -2627,7 +2627,7 @@ func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, currentTree *parse.Tr continue } defNode := treenode.GetNodeInTree(t, foot.defID) - docID := strings.TrimSuffix(path.Base(defNode.Path), ".sy") + docID := util.GetTreeID(defNode.Path) var nodes []*ast.Node if ast.NodeHeading == defNode.Type { nodes = append(nodes, defNode) @@ -2721,7 +2721,7 @@ func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, currentTree *parse.Tr return ast.WalkContinue } - docID := strings.TrimSuffix(path.Base(n.Path), ".sy") + docID := util.GetTreeID(n.Path) if currentTree.ID == docID { // 同文档块引转脚注缩略定义 https://github.com/siyuan-note/siyuan/issues/3299 if text := sql.GetRefText(n.ID); 64 < utf8.RuneCountInString(text) { diff --git a/kernel/model/file.go b/kernel/model/file.go index a2b6d0cb6..e2ad3c25b 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -310,7 +310,7 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo } if flashcard { - rootID := strings.TrimSuffix(filepath.Base(parentDocPath), ".sy") + rootID := util.GetTreeID(parentDocPath) newFlashcardCount, dueFlashcardCount, flashcardCount := countTreeFlashcard(rootID, deck, deckBlockIDs) if 0 < flashcardCount { doc.NewFlashcardCount = newFlashcardCount @@ -339,7 +339,7 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo doc := box.docFromFileInfo(file, ial) if flashcard { - rootID := strings.TrimSuffix(filepath.Base(file.path), ".sy") + rootID := util.GetTreeID(file.path) newFlashcardCount, dueFlashcardCount, flashcardCount := countTreeFlashcard(rootID, deck, deckBlockIDs) if 0 < flashcardCount { doc.NewFlashcardCount = newFlashcardCount @@ -1833,7 +1833,7 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) { } baseName := strings.TrimSpace(path.Base(p)) - if "" == strings.TrimSuffix(baseName, ".sy") { + if "" == util.GetTreeID(baseName) { err = errors.New(Conf.Language(16)) return } @@ -1849,7 +1849,7 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) { return } - id := strings.TrimSuffix(path.Base(p), ".sy") + id := util.GetTreeID(p) var hPath string folder := path.Dir(p) if "/" != folder { @@ -2006,7 +2006,7 @@ func ChangeFileTreeSort(boxID string, paths []string) { sortIDs := map[string]int{} max := 0 for i, p := range paths { - id := strings.TrimSuffix(path.Base(p), ".sy") + id := util.GetTreeID(p) sortIDs[id] = i + 1 if i == len(paths)-1 { max = i + 2 diff --git a/kernel/model/heading.go b/kernel/model/heading.go index e256adb0b..5c4f8cdda 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(strings.TrimSuffix(path.Base(previousPath), ".sy"), newTree.ID) + box.addSort(util.GetTreeID(previousPath), newTree.ID) } else { box.addMinSort(path.Dir(newTargetPath), newTree.ID) } diff --git a/kernel/model/history.go b/kernel/model/history.go index 700498654..a49c1cc05 100644 --- a/kernel/model/history.go +++ b/kernel/model/history.go @@ -22,6 +22,7 @@ import ( "io/fs" "math" "os" + "path" "path/filepath" "sort" "strconv" @@ -229,9 +230,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) { srcPath := historyPath var destPath, parentHPath string - baseName := filepath.Base(historyPath) - id := strings.TrimSuffix(baseName, ".sy") - + id := util.GetTreeID(historyPath) workingDoc := treenode.GetBlockTree(id) if nil != workingDoc { if err = filelock.Remove(filepath.Join(util.DataDir, boxID, workingDoc.Path)); err != nil { @@ -334,7 +333,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) { func getRollbackDockPath(boxID, historyPath string) (destPath, parentHPath string, err error) { baseName := filepath.Base(historyPath) - parentID := strings.TrimSuffix(filepath.Base(filepath.Dir(historyPath)), ".sy") + parentID := path.Base(filepath.Dir(historyPath)) parentWorkingDoc := treenode.GetBlockTree(parentID) if nil != parentWorkingDoc { // 父路径如果是文档,则恢复到父路径下 diff --git a/kernel/model/import.go b/kernel/model/import.go index d3215cfff..47edb7757 100644 --- a/kernel/model/import.go +++ b/kernel/model/import.go @@ -758,7 +758,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) { targetPaths[curRelPath] = targetPath } else { targetPath = targetPaths[curRelPath] - id = strings.TrimSuffix(path.Base(targetPath), ".sy") + id = util.GetTreeID(targetPath) } if d.IsDir() { diff --git a/kernel/model/listitem.go b/kernel/model/listitem.go index ebf54cbc7..7cc87f34c 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(strings.TrimSuffix(path.Base(previousPath), ".sy"), newTree.ID) + box.addSort(util.GetTreeID(previousPath), newTree.ID) } else { box.addMinSort(path.Dir(newTargetPath), newTree.ID) } diff --git a/kernel/model/sync.go b/kernel/model/sync.go index 625ddebe1..e9892ab6e 100644 --- a/kernel/model/sync.go +++ b/kernel/model/sync.go @@ -21,7 +21,6 @@ import ( "fmt" "net/http" "os" - "path" "path/filepath" "runtime" "strings" @@ -293,7 +292,7 @@ func removeIndexes(removeFilePaths []string) (removeRootIDs []string) { continue } - id := strings.TrimSuffix(filepath.Base(removeFile), ".sy") + id := util.GetTreeID(removeFile) removeRootIDs = append(removeRootIDs, id) block := treenode.GetBlockTree(id) if nil != block { @@ -338,7 +337,7 @@ func upsertIndexes(upsertFilePaths []string) (upsertRootIDs []string) { box := upsertFile[:idx] p := strings.TrimPrefix(upsertFile, box) - msg := fmt.Sprintf(Conf.Language(40), strings.TrimSuffix(path.Base(p), ".sy")) + msg := fmt.Sprintf(Conf.Language(40), util.GetTreeID(p)) util.IncBootProgress(bootProgressPart, msg) util.PushStatusBar(msg) diff --git a/kernel/treenode/tree.go b/kernel/treenode/tree.go index b6f0ccd53..1275cc8c6 100644 --- a/kernel/treenode/tree.go +++ b/kernel/treenode/tree.go @@ -20,7 +20,6 @@ import ( "crypto/sha256" "fmt" "io/fs" - "path" "path/filepath" "sort" "strings" @@ -61,7 +60,7 @@ func TreeRoot(node *ast.Node) *ast.Node { } func NewTree(boxID, p, hp, title string) *parse.Tree { - id := strings.TrimSuffix(path.Base(p), ".sy") + id := util.GetTreeID(p) root := &ast.Node{Type: ast.NodeDocument, ID: id, Spec: "1", Box: boxID, Path: p} root.SetIALAttr("title", title) root.SetIALAttr("id", id) diff --git a/kernel/util/path.go b/kernel/util/path.go index 1c7e9602b..b8cf52d5e 100644 --- a/kernel/util/path.go +++ b/kernel/util/path.go @@ -37,6 +37,10 @@ var ( UserAgent = "SiYuan/" + Ver ) +func GetTreeID(treePath string) string { + return strings.TrimSuffix(path.Base(treePath), ".sy") +} + func ShortPathForBootingDisplay(p string) string { if 25 > len(p) { return p From 11640f777aef6f7419d70781240613317337b18a Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 29 Nov 2024 09:07:17 +0800 Subject: [PATCH 2/2] :art: Improve dragging of headings or list items to the doc tree https://github.com/siyuan-note/siyuan/issues/13170 --- kernel/model/file.go | 26 +++++++++++++++++--------- kernel/model/heading.go | 2 +- kernel/model/listitem.go | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) 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) }