Skip to content

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

:bug: Preserve supplied folded content in container DOM updates https://github.com/siyuan-note/siyuan/issues/21005

Daniel16d6a239cee6
diff --git a/kernel/model/transaction_fold_fts5_test.go b/kernel/model/transaction_fold_fts5_test.go
index e62ce08845..7dbe62b338 100644
--- a/kernel/model/transaction_fold_fts5_test.go
+++ b/kernel/model/transaction_fold_fts5_test.go
@@ -33,6 +33,61 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+func TestCompleteContainerDOMUpdatePreservesFoldedContentOnce(t *testing.T) {
+ fixture := setupStructureTransactionTest(t)
+ setupFoldTransactionDatabase(t, fixture)
+ tree, err := LoadTreeByBlockID(fixture.sourceID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for tree.Root.FirstChild != nil {
+ tree.Root.FirstChild.Unlink()
+ }
+ quote := &ast.Node{Type: ast.NodeBlockquote, ID: ast.NewNodeID()}
+ quote.SetIALAttr("id", quote.ID)
+ heading := newHeadingLevelTestNode(ast.NewNodeID(), 1)
+ heading.SetIALAttr("id", heading.ID)
+ treenode.SetSelfFolded(heading, true)
+ paragraph := treenode.NewParagraph(ast.NewNodeID())
+ paragraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("hidden original")})
+ quote.AppendChild(heading)
+ quote.AppendChild(paragraph)
+ tree.Root.AppendChild(quote)
+ if _, err = filesys.WriteTree(tree); err != nil {
+ t.Fatal(err)
+ }
+ treenode.UpsertBlockTree(tree)
+ sql.IndexTreeQueue(tree)
+ sql.FlushQueue()
+ dom := strings.ReplaceAll(GetBlockDOM(quote.ID), "hidden original", "hidden edited")
+ if !strings.Contains(dom, "hidden edited") {
+ t.Fatal("complete DOM omitted hidden content")
+ }
+ if _, _, err = PerformBlockUpdates([]BlockUpdateInput{{ID: quote.ID, DataType: "dom", Data: dom}}); err != nil {
+ t.Fatal(err)
+ }
+ actual, err := filesys.LoadTree(tree.Box, tree.Path, util.NewLute())
+ if err != nil {
+ t.Fatal(err)
+ }
+ counts := map[string]int{}
+ ast.Walk(actual.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering && n.IsBlock() && n.Type != ast.NodeKramdownBlockIAL {
+ counts[n.ID]++
+ }
+ return ast.WalkContinue
+ })
+ for _, id := range []string{quote.ID, heading.ID, paragraph.ID} {
+ if counts[id] != 1 {
+ t.Fatalf("block %s persisted %d times", id, counts[id])
+ }
+ }
+ node := treenode.GetNodeInTree(actual, paragraph.ID)
+ if node == nil || node.FirstChild == nil || string(node.FirstChild.Tokens) != "hidden edited" {
+ t.Fatal("incoming hidden content was overwritten")
+ }
+}
+
func TestCancelFoldedHeadingSuperBlockKeepsUndoBoundary(t *testing.T) {
const (
superBlockID = "20260817000000-sblock1"
diff --git a/kernel/treenode/heading.go b/kernel/treenode/heading.go
index de840df261..45b38e7792 100644
--- a/kernel/treenode/heading.go
+++ b/kernel/treenode/heading.go
@@ -70,11 +70,15 @@ func MoveFoldHeading(updateNode, oldNode *ast.Node) {
// 将原来所有折叠标题对应的下方节点移动到新节点下
var updateFoldHeadings []*ast.Node
+ updatedBlocks := map[string]*ast.Node{}
ast.Walk(updateNode, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
}
+ if n.IsBlock() && n.ID != "" {
+ updatedBlocks[n.ID] = n
+ }
if ast.NodeHeading == n.Type && IsSelfFolded(n) {
updateFoldHeadings = append(updateFoldHeadings, n)
}
@@ -82,8 +86,35 @@ func MoveFoldHeading(updateNode, oldNode *ast.Node) {
})
for _, h := range updateFoldHeadings {
children := foldHeadings[h.ID]
- for i := len(children) - 1; 0 <= i; i-- {
- h.Next.InsertAfter(children[i]) // Next 是 Block IAL
+ anchor := h
+ if h.Next != nil && h.Next.Type == ast.NodeKramdownBlockIAL {
+ anchor = h.Next
+ }
+ skipIAL := false
+ for _, child := range children {
+ if child.Type == ast.NodeKramdownBlockIAL && skipIAL {
+ continue
+ }
+ skipIAL = false
+ if existing := updatedBlocks[child.ID]; child.ID != "" && existing != nil {
+ // Complete DOM already carries this block, including edits to hidden content.
+ skipIAL = true
+ if existing.Parent == h.Parent {
+ anchor = existing
+ if existing.Next != nil && existing.Next.Type == ast.NodeKramdownBlockIAL {
+ anchor = existing.Next
+ }
+ }
+ continue
+ }
+ anchor.InsertAfter(child)
+ anchor = child
+ ast.Walk(child, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering && n.IsBlock() && n.ID != "" {
+ updatedBlocks[n.ID] = n
+ }
+ return ast.WalkContinue
+ })
}
}
}