mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-03 04:01:23 +08:00
🎨 Improve dragging of headings or list items to the doc tree https://github.com/siyuan-note/siyuan/issues/13170
This commit is contained in:
parent
8bc3186976
commit
b6cd6930c7
@ -214,8 +214,15 @@ func heading2Doc(c *gin.Context) {
|
|||||||
|
|
||||||
srcHeadingID := arg["srcHeadingID"].(string)
|
srcHeadingID := arg["srcHeadingID"].(string)
|
||||||
targetNotebook := arg["targetNoteBook"].(string)
|
targetNotebook := arg["targetNoteBook"].(string)
|
||||||
targetPath := arg["targetPath"].(string)
|
var targetPath string
|
||||||
srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath)
|
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 {
|
if err != nil {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
@ -259,8 +266,15 @@ func li2Doc(c *gin.Context) {
|
|||||||
|
|
||||||
srcListItemID := arg["srcListItemID"].(string)
|
srcListItemID := arg["srcListItemID"].(string)
|
||||||
targetNotebook := arg["targetNoteBook"].(string)
|
targetNotebook := arg["targetNoteBook"].(string)
|
||||||
targetPath := arg["targetPath"].(string)
|
var targetPath string
|
||||||
srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath)
|
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 {
|
if err != nil {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
|
@ -2163,3 +2163,45 @@ func (box *Box) addMinSort(parentPath, id string) {
|
|||||||
return
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -273,7 +273,7 @@ func Doc2Heading(srcID, targetID string, after bool) (srcTreeBox, srcTreePath st
|
|||||||
return
|
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)
|
srcTree, _ := LoadTreeByBlockID(srcHeadingID)
|
||||||
if nil == srcTree {
|
if nil == srcTree {
|
||||||
err = ErrBlockNotFound
|
err = ErrBlockNotFound
|
||||||
@ -305,15 +305,33 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath string) (srcRootBlockID,
|
|||||||
moveToRoot := "/" == targetPath
|
moveToRoot := "/" == targetPath
|
||||||
toHP := path.Join("/", headingText)
|
toHP := path.Join("/", headingText)
|
||||||
toFolder := "/"
|
toFolder := "/"
|
||||||
|
if "" != previousPath {
|
||||||
if !moveToRoot {
|
previousDoc := treenode.GetBlockTreeRootByPath(targetBoxID, previousPath)
|
||||||
toBlock := treenode.GetBlockTreeRootByPath(targetBoxID, targetPath)
|
if nil == previousDoc {
|
||||||
if nil == toBlock {
|
|
||||||
err = ErrBlockNotFound
|
err = ErrBlockNotFound
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
toHP = path.Join(toBlock.HPath, headingText)
|
parentPath := path.Dir(previousPath)
|
||||||
toFolder = path.Join(path.Dir(targetPath), toBlock.ID)
|
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")
|
newTargetPath = path.Join(toFolder, srcHeadingID+".sy")
|
||||||
@ -375,7 +393,11 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath string) (srcRootBlockID,
|
|||||||
newTree.Box, newTree.Path = targetBoxID, newTargetPath
|
newTree.Box, newTree.Path = targetBoxID, newTargetPath
|
||||||
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
|
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
|
||||||
newTree.Root.Spec = "1"
|
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 {
|
if err = indexWriteTreeUpsertQueue(newTree); err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
"github.com/88250/lute/parse"
|
"github.com/88250/lute/parse"
|
||||||
@ -26,7 +27,7 @@ import (
|
|||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"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)
|
srcTree, _ := LoadTreeByBlockID(srcListItemID)
|
||||||
if nil == srcTree {
|
if nil == srcTree {
|
||||||
err = ErrBlockNotFound
|
err = ErrBlockNotFound
|
||||||
@ -48,14 +49,33 @@ func ListItem2Doc(srcListItemID, targetBoxID, targetPath string) (srcRootBlockID
|
|||||||
toHP := path.Join("/", listItemText)
|
toHP := path.Join("/", listItemText)
|
||||||
toFolder := "/"
|
toFolder := "/"
|
||||||
|
|
||||||
if !moveToRoot {
|
if "" != previousPath {
|
||||||
toBlock := treenode.GetBlockTreeRootByPath(targetBoxID, targetPath)
|
previousDoc := treenode.GetBlockTreeRootByPath(targetBoxID, previousPath)
|
||||||
if nil == toBlock {
|
if nil == previousDoc {
|
||||||
err = ErrBlockNotFound
|
err = ErrBlockNotFound
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
toHP = path.Join(toBlock.HPath, listItemText)
|
parentPath := path.Dir(previousPath)
|
||||||
toFolder = path.Join(path.Dir(targetPath), toBlock.ID)
|
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")
|
newTargetPath = path.Join(toFolder, srcListItemID+".sy")
|
||||||
@ -107,7 +127,11 @@ func ListItem2Doc(srcListItemID, targetBoxID, targetPath string) (srcRootBlockID
|
|||||||
newTree.Box, newTree.Path = targetBoxID, newTargetPath
|
newTree.Box, newTree.Path = targetBoxID, newTargetPath
|
||||||
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
|
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
|
||||||
newTree.Root.Spec = "1"
|
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 {
|
if err = indexWriteTreeUpsertQueue(newTree); err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user