Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: API `/api/filetree/duplicateDoc` 返回新文档的 id/box/path/hpath Fix https://github.com/siyuan-note/siyuan/issues/5669

Liang Ding6c652fab6b16
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go
index 40325fc462..9aefe207e9 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 194ccd4acc..f1d66f11a9 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()