mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 15:10:12 +01:00
🎨 API /api/filetree/duplicateDoc 返回新文档的 id/box/path/hpath Fix https://github.com/siyuan-note/siyuan/issues/5669
This commit is contained in:
parent
a7d3e8a0c8
commit
6c652fab6b
2 changed files with 22 additions and 15 deletions
|
|
@ -307,7 +307,7 @@ func duplicateDoc(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
id := arg["id"].(string)
|
id := arg["id"].(string)
|
||||||
err := model.DuplicateDoc(id)
|
newTree, err := model.DuplicateDoc(id)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
|
|
@ -327,6 +327,13 @@ func duplicateDoc(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pushCreate(box, p, tree.Root.ID, arg)
|
pushCreate(box, p, tree.Root.ID, arg)
|
||||||
|
|
||||||
|
ret.Data = map[string]interface{}{
|
||||||
|
"id": newTree.Root.ID,
|
||||||
|
"notebook": notebook,
|
||||||
|
"path": newTree.Path,
|
||||||
|
"hPath": newTree.HPath,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createDoc(c *gin.Context) {
|
func createDoc(c *gin.Context) {
|
||||||
|
|
|
||||||
|
|
@ -923,31 +923,31 @@ func renameWriteJSONQueue(tree *parse.Tree, oldHPath string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func DuplicateDoc(rootID string) (err error) {
|
func DuplicateDoc(rootID string) (ret *parse.Tree, err error) {
|
||||||
msgId := util.PushMsg(Conf.Language(116), 30000)
|
msgId := util.PushMsg(Conf.Language(116), 30000)
|
||||||
defer util.PushClearMsg(msgId)
|
defer util.PushClearMsg(msgId)
|
||||||
|
|
||||||
WaitForWritingFiles()
|
WaitForWritingFiles()
|
||||||
tree, err := loadTreeByBlockID(rootID)
|
ret, err = loadTreeByBlockID(rootID)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tree.ID = ast.NewNodeID()
|
ret.ID = ast.NewNodeID()
|
||||||
tree.Root.ID = tree.ID
|
ret.Root.ID = ret.ID
|
||||||
titleSuffix := "Duplicated"
|
titleSuffix := "Duplicated"
|
||||||
if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr {
|
if t, parseErr := time.Parse("20060102150405", util.TimeFromID(ret.ID)); nil == parseErr {
|
||||||
titleSuffix = t.Format("2006-01-02 15:04:05")
|
titleSuffix = t.Format("2006-01-02 15:04:05")
|
||||||
}
|
}
|
||||||
tree.Root.SetIALAttr("id", tree.ID)
|
ret.Root.SetIALAttr("id", ret.ID)
|
||||||
tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+" "+titleSuffix)
|
ret.Root.SetIALAttr("title", ret.Root.IALAttr("title")+" "+titleSuffix)
|
||||||
p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"
|
p := path.Join(path.Dir(ret.Path), ret.ID) + ".sy"
|
||||||
tree.Path = p
|
ret.Path = p
|
||||||
tree.HPath = tree.HPath + " " + titleSuffix
|
ret.HPath = ret.HPath + " " + titleSuffix
|
||||||
|
|
||||||
// 收集所有引用
|
// 收集所有引用
|
||||||
refIDs := map[string]string{}
|
refIDs := map[string]string{}
|
||||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
if !entering || ast.NodeBlockRefID != n.Type {
|
if !entering || ast.NodeBlockRefID != n.Type {
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
|
@ -956,7 +956,7 @@ func DuplicateDoc(rootID string) (err error) {
|
||||||
})
|
})
|
||||||
|
|
||||||
// 重置块 ID
|
// 重置块 ID
|
||||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
if !entering || ast.NodeDocument == n.Type {
|
if !entering || ast.NodeDocument == n.Type {
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
|
@ -973,7 +973,7 @@ func DuplicateDoc(rootID string) (err error) {
|
||||||
})
|
})
|
||||||
|
|
||||||
// 重置内部引用
|
// 重置内部引用
|
||||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
if !entering || ast.NodeBlockRefID != n.Type {
|
if !entering || ast.NodeBlockRefID != n.Type {
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
|
@ -983,7 +983,7 @@ func DuplicateDoc(rootID string) (err error) {
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
})
|
})
|
||||||
|
|
||||||
transaction := &Transaction{DoOperations: []*Operation{{Action: "create", Data: tree}}}
|
transaction := &Transaction{DoOperations: []*Operation{{Action: "create", Data: ret}}}
|
||||||
err = PerformTransactions(&[]*Transaction{transaction})
|
err = PerformTransactions(&[]*Transaction{transaction})
|
||||||
if nil != err {
|
if nil != err {
|
||||||
tx, txErr := sql.BeginTx()
|
tx, txErr := sql.BeginTx()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue