Skip to content

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

:technologist: Improve kernel API `/api/block/updateBlock` and `/api/block/batchUpdateBlock` https://github.com/siyuan-note/siyuan/issues/15301

Daniel70e31c71cc93
diff --git a/kernel/api/block_op.go b/kernel/api/block_op.go
index c88bdb50ec..9c6ebe2f25 100644
--- a/kernel/api/block_op.go
+++ b/kernel/api/block_op.go
@@ -575,7 +575,6 @@ func updateBlock(c *gin.Context) {
return
}
- var transactions []*model.Transaction
if "NodeDocument" == block.Type {
oldTree, err := filesys.LoadTree(block.Box, block.Path, luteEngine)
if err != nil {
@@ -584,39 +583,46 @@ func updateBlock(c *gin.Context) {
return
}
var toRemoves []*ast.Node
- var ops []*model.Operation
for n := oldTree.Root.FirstChild; nil != n; n = n.Next {
toRemoves = append(toRemoves, n)
- ops = append(ops, &model.Operation{Action: "delete", ID: n.ID})
}
for _, n := range toRemoves {
n.Unlink()
}
- ops = append(ops, &model.Operation{Action: "appendInsert", Data: data, ParentID: id})
- transactions = append(transactions, &model.Transaction{
- DoOperations: ops,
- })
- } else {
- if "NodeListItem" == block.Type && ast.NodeList == tree.Root.FirstChild.Type {
- // 使用 API `api/block/updateBlock` 更新列表项时渲染错误 https://github.com/siyuan-note/siyuan/issues/4658
- tree.Root.AppendChild(tree.Root.FirstChild.FirstChild) // 将列表下的第一个列表项移到文档结尾,移动以后根下面直接挂列表项,渲染器可以正常工作
- tree.Root.FirstChild.Unlink() // 删除列表
- tree.Root.FirstChild.Unlink() // 继续删除列表 IAL
+
+ var toAppends []*ast.Node
+ for n := tree.Root.FirstChild; nil != n; n = n.Next {
+ toAppends = append(toAppends, n)
+ }
+ for _, n := range toAppends {
+ oldTree.Root.AppendChild(n)
}
- tree.Root.FirstChild.SetIALAttr("id", id)
- data = luteEngine.Tree2BlockDOM(tree, luteEngine.RenderOptions)
- transactions = []*model.Transaction{
- {
- DoOperations: []*model.Operation{
- {
- Action: "update",
- ID: id,
- Data: data,
- },
+ model.WriteTreeUpsertQueue(oldTree)
+ model.ReloadProtyle(id)
+ return
+ }
+
+ var transactions []*model.Transaction
+ if "NodeListItem" == block.Type && ast.NodeList == tree.Root.FirstChild.Type {
+ // 使用 API `api/block/updateBlock` 更新列表项时渲染错误 https://github.com/siyuan-note/siyuan/issues/4658
+ tree.Root.AppendChild(tree.Root.FirstChild.FirstChild) // 将列表下的第一个列表项移到文档结尾,移动以后根下面直接挂列表项,渲染器可以正常工作
+ tree.Root.FirstChild.Unlink() // 删除列表
+ tree.Root.FirstChild.Unlink() // 继续删除列表 IAL
+ }
+ tree.Root.FirstChild.SetIALAttr("id", id)
+
+ data = luteEngine.Tree2BlockDOM(tree, luteEngine.RenderOptions)
+ transactions = []*model.Transaction{
+ {
+ DoOperations: []*model.Operation{
+ {
+ Action: "update",
+ ID: id,
+ Data: data,
},
},
- }
+ },
}
model.PerformTransactions(&transactions)
@@ -704,15 +710,22 @@ func batchUpdateBlock(c *gin.Context) {
return
}
var toRemoves []*ast.Node
-
for n := oldTree.Root.FirstChild; nil != n; n = n.Next {
toRemoves = append(toRemoves, n)
- ops = append(ops, &model.Operation{Action: "delete", ID: n.ID})
}
for _, n := range toRemoves {
n.Unlink()
}
- ops = append(ops, &model.Operation{Action: "appendInsert", Data: data, ParentID: id})
+ var toAppends []*ast.Node
+ for n := tree.Root.FirstChild; nil != n; n = n.Next {
+ toAppends = append(toAppends, n)
+ }
+ for _, n := range toAppends {
+ oldTree.Root.AppendChild(n)
+ }
+
+ model.WriteTreeUpsertQueue(oldTree)
+ model.ReloadProtyle(id)
} else {
if "NodeListItem" == block.Type && ast.NodeList == tree.Root.FirstChild.Type {
// 使用 API `api/block/updateBlock` 更新列表项时渲染错误 https://github.com/siyuan-note/siyuan/issues/4658
@@ -731,13 +744,14 @@ func batchUpdateBlock(c *gin.Context) {
}
}
- tx.DoOperations = ops
- model.PerformTransactions(&transactions)
- model.FlushTxQueue()
-
- ret.Data = transactions
- broadcastTransactions(transactions)
+ if 0 < len(ops) {
+ tx.DoOperations = ops
+ model.PerformTransactions(&transactions)
+ model.FlushTxQueue()
+ ret.Data = transactions
+ broadcastTransactions(transactions)
+ }
}
func deleteBlock(c *gin.Context) {
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 6dd1eb21a6..207dc48e5b 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -321,7 +321,7 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
util.PushClearMsg(msgId)
if 0 < files {
msgId = util.PushMsg(Conf.Language(113), 7000)
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
return
}
util.PushUpdateMsg(msgId, fmt.Sprintf(Conf.Language(120), files), 5000)
diff --git a/kernel/model/bookmark.go b/kernel/model/bookmark.go
index 80ef3e1363..aa38bfe3e3 100644
--- a/kernel/model/bookmark.go
+++ b/kernel/model/bookmark.go
@@ -66,7 +66,7 @@ func RemoveBookmark(bookmark string) (err error) {
}
util.PushEndlessProgress(fmt.Sprintf(Conf.Language(111), util.EscapeHTML(tree.Root.IALAttr("title"))))
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
util.ClearPushProgress(100)
return
}
@@ -124,7 +124,7 @@ func RenameBookmark(oldBookmark, newBookmark string) (err error) {
}
util.PushEndlessProgress(fmt.Sprintf(Conf.Language(111), util.EscapeHTML(tree.Root.IALAttr("title"))))
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
util.ClearPushProgress(100)
return
}
diff --git a/kernel/model/export.go b/kernel/model/export.go
index ccf92e8ba9..2b2b237779 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -310,7 +310,7 @@ func Export2Liandi(id string) (err error) {
articleId = result.Data.(string)
tree, _ = LoadTreeByBlockID(id) // 这里必须重新加载,因为前面导出时已经修改了树结构
tree.Root.SetIALAttr(liandiArticleIdAttrName, articleId)
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
return
}
}
diff --git a/kernel/model/file.go b/kernel/model/file.go
index bd3c51ddf8..c0fcf4e3ee 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -922,7 +922,7 @@ func loadNodesByMode(node *ast.Node, inputIndex, mode, size int, isDoc, isHeadin
return
}
-func writeTreeUpsertQueue(tree *parse.Tree) (err error) {
+func WriteTreeUpsertQueue(tree *parse.Tree) (err error) {
size, err := filesys.WriteTree(tree)
if err != nil {
return
@@ -944,7 +944,7 @@ func indexWriteTreeIndexQueue(tree *parse.Tree) (err error) {
func indexWriteTreeUpsertQueue(tree *parse.Tree) (err error) {
treenode.UpsertBlockTree(tree)
- return writeTreeUpsertQueue(tree)
+ return WriteTreeUpsertQueue(tree)
}
func renameWriteJSONQueue(tree *parse.Tree) (err error) {
diff --git a/kernel/model/format.go b/kernel/model/format.go
index 2a324f47c1..35e99d0bbc 100644
--- a/kernel/model/format.go
+++ b/kernel/model/format.go
@@ -68,7 +68,7 @@ func AutoSpace(rootID string) (err error) {
newTree.Path = tree.Path
newTree.HPath = tree.HPath
newTree.Box = tree.Box
- err = writeTreeUpsertQueue(newTree)
+ err = WriteTreeUpsertQueue(newTree)
if err != nil {
return
}
diff --git a/kernel/model/search.go b/kernel/model/search.go
index e9168a2736..19d60e623b 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -932,7 +932,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
}
}
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
return
}
updateNodes[id] = node
diff --git a/kernel/model/tag.go b/kernel/model/tag.go
index d0d652ce6e..ba453ed403 100644
--- a/kernel/model/tag.go
+++ b/kernel/model/tag.go
@@ -96,7 +96,7 @@ func RemoveTag(label string) (err error) {
n.Unlink()
}
util.PushEndlessProgress(fmt.Sprintf(Conf.Language(111), util.EscapeHTML(tree.Root.IALAttr("title"))))
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
util.ClearPushProgress(100)
return
}
@@ -195,7 +195,7 @@ func RenameTag(oldLabel, newLabel string) (err error) {
updateNodes[node.ID] = node
}
util.PushEndlessProgress(fmt.Sprintf(Conf.Language(111), util.EscapeHTML(tree.Root.IALAttr("title"))))
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
util.ClearPushProgress(100)
return
}
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 0f943951d1..5b827807cc 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -1553,7 +1553,7 @@ func (tx *Transaction) begin() (err error) {
func (tx *Transaction) commit() (err error) {
for _, tree := range tx.trees {
- if err = writeTreeUpsertQueue(tree); err != nil {
+ if err = WriteTreeUpsertQueue(tree); err != nil {
return
}