From 6c652fab6b1682ad6dd6974cd25db3ce6cf86f0c Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 19 Aug 2022 16:26:57 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20API=20`/api/filetree/duplicateDoc`=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=96=B0=E6=96=87=E6=A1=A3=E7=9A=84=20id/box?= =?UTF-8?q?/path/hpath=20Fix=20https://github.com/siyuan-note/siyuan/issue?= =?UTF-8?q?s/5669?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/filetree.go | 9 ++++++++- kernel/model/file.go | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go index 40325fc46..9aefe207e 100644 --- a/kernel/api/filetree.go +++ b/kernel/api/filetree.go @@ -307,7 +307,7 @@ func duplicateDoc(c *gin.Context) { } id := arg["id"].(string) - err := model.DuplicateDoc(id) + newTree, err := model.DuplicateDoc(id) if nil != err { ret.Code = -1 ret.Msg = err.Error() @@ -327,6 +327,13 @@ func duplicateDoc(c *gin.Context) { } 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) { diff --git a/kernel/model/file.go b/kernel/model/file.go index 194ccd4ac..f1d66f11a 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -923,31 +923,31 @@ func renameWriteJSONQueue(tree *parse.Tree, oldHPath string) (err error) { return } -func DuplicateDoc(rootID string) (err error) { +func DuplicateDoc(rootID string) (ret *parse.Tree, err error) { msgId := util.PushMsg(Conf.Language(116), 30000) defer util.PushClearMsg(msgId) WaitForWritingFiles() - tree, err := loadTreeByBlockID(rootID) + ret, err = loadTreeByBlockID(rootID) if nil != err { return } - tree.ID = ast.NewNodeID() - tree.Root.ID = tree.ID + ret.ID = ast.NewNodeID() + ret.Root.ID = ret.ID 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") } - tree.Root.SetIALAttr("id", tree.ID) - tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+" "+titleSuffix) - p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy" - tree.Path = p - tree.HPath = tree.HPath + " " + titleSuffix + ret.Root.SetIALAttr("id", ret.ID) + ret.Root.SetIALAttr("title", ret.Root.IALAttr("title")+" "+titleSuffix) + p := path.Join(path.Dir(ret.Path), ret.ID) + ".sy" + ret.Path = p + ret.HPath = ret.HPath + " " + titleSuffix // 收集所有引用 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 { return ast.WalkContinue } @@ -956,7 +956,7 @@ func DuplicateDoc(rootID string) (err error) { }) // 重置块 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 { 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 { return ast.WalkContinue } @@ -983,7 +983,7 @@ func DuplicateDoc(rootID string) (err error) { return ast.WalkContinue }) - transaction := &Transaction{DoOperations: []*Operation{{Action: "create", Data: tree}}} + transaction := &Transaction{DoOperations: []*Operation{{Action: "create", Data: ret}}} err = PerformTransactions(&[]*Transaction{transaction}) if nil != err { tx, txErr := sql.BeginTx()