@@ -587,52 +587,66 @@ func (tx *Transaction) doPrependInsert(operation *Operation) (ret *TxErr) {
587587 if nil == insertedNode {
588588 return & TxErr {code : TxErrCodeBlockNotFound , msg : "invalid data tree" , id : block .ID }
589589 }
590- var remains []* ast.Node
591- for remain := insertedNode .Next ; nil != remain ; remain = remain .Next {
592- if ast .NodeKramdownBlockIAL != remain .Type {
593- if "" == remain .ID {
594- remain .ID = ast .NewNodeID ()
595- remain .SetIALAttr ("id" , remain .ID )
596- }
597- remains = append (remains , remain )
598- }
599- }
600590 if "" == insertedNode .ID {
601591 insertedNode .ID = ast .NewNodeID ()
602592 insertedNode .SetIALAttr ("id" , insertedNode .ID )
603593 }
594+ var toInserts []* ast.Node
595+ for toInsert := insertedNode ; nil != toInsert ; toInsert = toInsert .Next {
596+ if ast .NodeKramdownBlockIAL != toInsert .Type {
597+ if "" == toInsert .ID {
598+ toInsert .ID = ast .NewNodeID ()
599+ toInsert .SetIALAttr ("id" , toInsert .ID )
600+ }
601+ toInserts = append (toInserts , toInsert )
602+ }
603+ }
604604
605605 node := treenode .GetNodeInTree (tree , operation .ParentID )
606606 if nil == node {
607607 logging .LogErrorf ("get node [%s] in tree [%s] failed" , operation .ParentID , tree .Root .ID )
608608 return & TxErr {code : TxErrCodeBlockNotFound , id : operation .ParentID }
609609 }
610610 isContainer := node .IsContainerBlock ()
611- for i := len (remains ) - 1 ; 0 <= i ; i -- {
612- remain := remains [i ]
611+ slices .Reverse (toInserts )
612+
613+ for _ , toInsert := range toInserts {
613614 if isContainer {
614- if ast .NodeListItem == node .Type && 3 == node .ListData .Typ {
615- node .FirstChild .InsertAfter (remain )
615+ if ast .NodeList == node .Type {
616+ // 列表下只能挂列表项,所以这里需要分情况处理
617+ if ast .NodeList == toInsert .Type {
618+ var childLis []* ast.Node
619+ for childLi := toInsert .FirstChild ; nil != childLi ; childLi = childLi .Next {
620+ childLis = append (childLis , childLi )
621+ }
622+ for i := len (childLis ) - 1 ; - 1 < i ; i -- {
623+ node .PrependChild (childLis [i ])
624+ }
625+ } else {
626+ newLiID := ast .NewNodeID ()
627+ newLi := & ast.Node {ID : newLiID , Type : ast .NodeListItem , ListData : & ast.ListData {Typ : node .ListData .Typ }}
628+ newLi .SetIALAttr ("id" , newLiID )
629+ node .PrependChild (newLi )
630+ newLi .AppendChild (toInsert )
631+ }
616632 } else if ast .NodeSuperBlock == node .Type {
617- node .FirstChild .Next .InsertAfter (remain )
633+ layout := node .ChildByType (ast .NodeSuperBlockLayoutMarker )
634+ if nil != layout {
635+ layout .InsertAfter (toInsert )
636+ } else {
637+ node .FirstChild .InsertAfter (toInsert )
638+ }
618639 } else {
619- node .PrependChild (remain )
640+ node .PrependChild (toInsert )
620641 }
621642 } else {
622- node .InsertAfter (remain )
623- }
624- }
625- if isContainer {
626- if ast .NodeListItem == node .Type && 3 == node .ListData .Typ {
627- node .FirstChild .InsertAfter (insertedNode )
628- } else if ast .NodeSuperBlock == node .Type {
629- node .FirstChild .Next .InsertAfter (insertedNode )
630- } else {
631- node .PrependChild (insertedNode )
643+ node .InsertAfter (toInsert )
632644 }
633- } else {
634- node .InsertAfter (insertedNode )
645+
646+ createdUpdated (toInsert )
647+ tx .nodes [toInsert .ID ] = toInsert
635648 }
649+
636650 createdUpdated (insertedNode )
637651 tx .nodes [insertedNode .ID ] = insertedNode
638652 if err = tx .writeTree (tree ); err != nil {
@@ -695,9 +709,14 @@ func (tx *Transaction) doAppendInsert(operation *Operation) (ret *TxErr) {
695709 if ! isContainer {
696710 slices .Reverse (toInserts )
697711 }
712+ var lastChildBelowHeading * ast.Node
713+ if ast .NodeHeading == node .Type {
714+ if children := treenode .HeadingChildren (node ); 0 < len (children ) {
715+ lastChildBelowHeading = children [len (children )- 1 ]
716+ }
717+ }
698718
699- for i := 0 ; i < len (toInserts ); i ++ {
700- toInsert := toInserts [i ]
719+ for _ , toInsert := range toInserts {
701720 if isContainer {
702721 if ast .NodeList == node .Type {
703722 // 列表下只能挂列表项,所以这里需要分情况处理 https://github.com/siyuan-note/siyuan/issues/9955
@@ -722,7 +741,15 @@ func (tx *Transaction) doAppendInsert(operation *Operation) (ret *TxErr) {
722741 node .AppendChild (toInsert )
723742 }
724743 } else {
725- node .InsertAfter (toInsert )
744+ if ast .NodeHeading == node .Type {
745+ if nil != lastChildBelowHeading {
746+ lastChildBelowHeading .InsertAfter (toInsert )
747+ } else {
748+ node .InsertAfter (toInsert )
749+ }
750+ } else {
751+ node .InsertAfter (toInsert )
752+ }
726753 }
727754
728755 createdUpdated (toInsert )
0 commit comments