🎨 Improve dragging of headings or list items to the doc tree https://github.com/siyuan-note/siyuan/issues/13170

This commit is contained in:
Daniel 2024-11-22 22:36:19 +08:00
parent 8bc3186976
commit b6cd6930c7
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 121 additions and 19 deletions

View file

@ -214,8 +214,15 @@ func heading2Doc(c *gin.Context) {
srcHeadingID := arg["srcHeadingID"].(string)
targetNotebook := arg["targetNoteBook"].(string)
targetPath := arg["targetPath"].(string)
srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath)
var targetPath string
if arg["targetPath"] != nil {
targetPath = arg["targetPath"].(string)
}
var previousPath string
if arg["previousPath"] != nil {
previousPath = arg["previousPath"].(string)
}
srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath, previousPath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
@ -259,8 +266,15 @@ func li2Doc(c *gin.Context) {
srcListItemID := arg["srcListItemID"].(string)
targetNotebook := arg["targetNoteBook"].(string)
targetPath := arg["targetPath"].(string)
srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath)
var targetPath string
if arg["targetPath"] != nil {
targetPath = arg["targetPath"].(string)
}
var previousPath string
if arg["previousPath"] != nil {
previousPath = arg["previousPath"].(string)
}
srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath, previousPath)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()

View file

@ -2163,3 +2163,45 @@ func (box *Box) addMinSort(parentPath, id string) {
return
}
}
func (box *Box) addSort(previousID, 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)
return
}
confPath := filepath.Join(confDir, "sort.json")
fullSortIDs := map[string]int{}
var data []byte
if filelock.IsExist(confPath) {
data, err := filelock.ReadFile(confPath)
if err != nil {
logging.LogErrorf("read sort conf failed: %s", err)
return
}
if err = gulu.JSON.UnmarshalJSON(data, &fullSortIDs); err != nil {
logging.LogErrorf("unmarshal sort conf failed: %s", err)
}
}
sortVal := 0
previousSortVal, ok := fullSortIDs[previousID]
if !ok {
sortVal++
} else {
sortVal = previousSortVal + 1
}
fullSortIDs[id] = sortVal
data, err := gulu.JSON.MarshalJSON(fullSortIDs)
if err != nil {
logging.LogErrorf("marshal sort conf failed: %s", err)
return
}
if err = filelock.WriteFile(confPath, data); err != nil {
logging.LogErrorf("write sort conf failed: %s", err)
return
}
}

View file

@ -273,7 +273,7 @@ func Doc2Heading(srcID, targetID string, after bool) (srcTreeBox, srcTreePath st
return
}
func Heading2Doc(srcHeadingID, targetBoxID, targetPath string) (srcRootBlockID, newTargetPath string, err error) {
func Heading2Doc(srcHeadingID, targetBoxID, targetPath, previousPath string) (srcRootBlockID, newTargetPath string, err error) {
srcTree, _ := LoadTreeByBlockID(srcHeadingID)
if nil == srcTree {
err = ErrBlockNotFound
@ -305,15 +305,33 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath string) (srcRootBlockID,
moveToRoot := "/" == targetPath
toHP := path.Join("/", headingText)
toFolder := "/"
if !moveToRoot {
toBlock := treenode.GetBlockTreeRootByPath(targetBoxID, targetPath)
if nil == toBlock {
if "" != previousPath {
previousDoc := treenode.GetBlockTreeRootByPath(targetBoxID, previousPath)
if nil == previousDoc {
err = ErrBlockNotFound
return
}
toHP = path.Join(toBlock.HPath, headingText)
toFolder = path.Join(path.Dir(targetPath), toBlock.ID)
parentPath := path.Dir(previousPath)
if "/" != parentPath {
parentPath = strings.TrimSuffix(parentPath, "/") + ".sy"
parentDoc := treenode.GetBlockTreeRootByPath(targetBoxID, parentPath)
if nil == parentDoc {
err = ErrBlockNotFound
return
}
toHP = path.Join(parentDoc.HPath, headingText)
toFolder = path.Join(path.Dir(parentPath), parentDoc.ID)
}
} else {
if !moveToRoot {
parentDoc := treenode.GetBlockTreeRootByPath(targetBoxID, targetPath)
if nil == parentDoc {
err = ErrBlockNotFound
return
}
toHP = path.Join(parentDoc.HPath, headingText)
toFolder = path.Join(path.Dir(targetPath), parentDoc.ID)
}
}
newTargetPath = path.Join(toFolder, srcHeadingID+".sy")
@ -375,7 +393,11 @@ 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 "" != previousPath {
box.addSort(strings.TrimSuffix(path.Base(previousPath), ".sy"), newTree.ID)
} else {
box.addMinSort(path.Dir(newTargetPath), newTree.ID)
}
if err = indexWriteTreeUpsertQueue(newTree); err != nil {
return "", "", err
}

View file

@ -18,6 +18,7 @@ package model
import (
"path"
"strings"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
@ -26,7 +27,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func ListItem2Doc(srcListItemID, targetBoxID, targetPath string) (srcRootBlockID, newTargetPath string, err error) {
func ListItem2Doc(srcListItemID, targetBoxID, targetPath, previousPath string) (srcRootBlockID, newTargetPath string, err error) {
srcTree, _ := LoadTreeByBlockID(srcListItemID)
if nil == srcTree {
err = ErrBlockNotFound
@ -48,14 +49,33 @@ func ListItem2Doc(srcListItemID, targetBoxID, targetPath string) (srcRootBlockID
toHP := path.Join("/", listItemText)
toFolder := "/"
if !moveToRoot {
toBlock := treenode.GetBlockTreeRootByPath(targetBoxID, targetPath)
if nil == toBlock {
if "" != previousPath {
previousDoc := treenode.GetBlockTreeRootByPath(targetBoxID, previousPath)
if nil == previousDoc {
err = ErrBlockNotFound
return
}
toHP = path.Join(toBlock.HPath, listItemText)
toFolder = path.Join(path.Dir(targetPath), toBlock.ID)
parentPath := path.Dir(previousPath)
if "/" != parentPath {
parentPath = strings.TrimSuffix(parentPath, "/") + ".sy"
parentDoc := treenode.GetBlockTreeRootByPath(targetBoxID, parentPath)
if nil == parentDoc {
err = ErrBlockNotFound
return
}
toHP = path.Join(parentDoc.HPath, listItemText)
toFolder = path.Join(path.Dir(parentPath), parentDoc.ID)
}
} else {
if !moveToRoot {
parentDoc := treenode.GetBlockTreeRootByPath(targetBoxID, targetPath)
if nil == parentDoc {
err = ErrBlockNotFound
return
}
toHP = path.Join(parentDoc.HPath, listItemText)
toFolder = path.Join(path.Dir(targetPath), parentDoc.ID)
}
}
newTargetPath = path.Join(toFolder, srcListItemID+".sy")
@ -107,7 +127,11 @@ 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 "" != previousPath {
box.addSort(strings.TrimSuffix(path.Base(previousPath), ".sy"), newTree.ID)
} else {
box.addMinSort(path.Dir(newTargetPath), newTree.ID)
}
if err = indexWriteTreeUpsertQueue(newTree); err != nil {
return "", "", err
}