@@ -4,7 +4,7 @@ import { deepClone } from './utils/index'
44import type { Topic } from './types/dom'
55import type { MindElixirInstance , NodeObj } from './types/index'
66import { insertNodeObj , insertParentNodeObj , moveUpObj , moveDownObj , removeNodeObj , moveNodeObj } from './utils/objectManipulation'
7- import { addChildDom , realAddChild , removeNodeDom } from './utils/domManipulation'
7+ import { addChildDom , removeNodeDom } from './utils/domManipulation'
88
99const typeMap : Record < string , InsertPosition > = {
1010 before : 'beforebegin' ,
@@ -34,6 +34,24 @@ export const reshapeNode = function (this: MindElixirInstance, tpc: Topic, patch
3434 } )
3535}
3636
37+ const addChildFunc = function ( mei : MindElixirInstance , tpc : Topic , node ?: NodeObj ) {
38+ if ( ! tpc ) return null
39+ const nodeObj = tpc . nodeObj
40+ if ( nodeObj . expanded === false ) {
41+ mei . expandNode ( tpc , true )
42+ // dom had resetted
43+ tpc = findEle ( nodeObj . id ) as Topic
44+ }
45+ const newNodeObj = node || mei . generateNewObj ( )
46+ if ( nodeObj . children ) nodeObj . children . push ( newNodeObj )
47+ else nodeObj . children = [ newNodeObj ]
48+ fillParent ( mei . nodeData )
49+
50+ const { grp, top : newTop } = mei . createWrapper ( newNodeObj )
51+ addChildDom ( mei , tpc , grp )
52+ return { newTop, newNodeObj }
53+ }
54+
3755export const insertSibling = function ( this : MindElixirInstance , type : 'before' | 'after' , el ?: Topic , node ?: NodeObj ) {
3856 const nodeEle = el || this . currentNode
3957 if ( ! nodeEle ) return
@@ -108,7 +126,7 @@ export const addChild = function (this: MindElixirInstance, el?: Topic, node?: N
108126 console . time ( 'addChild' )
109127 const nodeEle = el || this . currentNode
110128 if ( ! nodeEle ) return
111- const res = addChildDom ( this , nodeEle , node )
129+ const res = addChildFunc ( this , nodeEle , node )
112130 if ( ! res ) return
113131 const { newTop, newNodeObj } = res
114132 this . bus . fire ( 'operation' , {
@@ -126,7 +144,7 @@ export const copyNode = function (this: MindElixirInstance, node: Topic, to: Top
126144 console . time ( 'copyNode' )
127145 const deepCloneObj = deepClone ( node . nodeObj )
128146 refreshIds ( deepCloneObj )
129- const res = addChildDom ( this , to , deepCloneObj )
147+ const res = addChildFunc ( this , to , deepCloneObj )
130148 if ( ! res ) return
131149 const { newNodeObj } = res
132150 console . timeEnd ( 'copyNode' )
@@ -144,7 +162,7 @@ export const copyNodes = function (this: MindElixirInstance, tpcs: Topic[], to:
144162 const node = tpcs [ i ]
145163 const deepCloneObj = deepClone ( node . nodeObj )
146164 refreshIds ( deepCloneObj )
147- const res = addChildDom ( this , to , deepCloneObj )
165+ const res = addChildFunc ( this , to , deepCloneObj )
148166 if ( ! res ) return
149167 const { newNodeObj } = res
150168 objs . push ( newNodeObj )
@@ -252,7 +270,7 @@ export const moveNodeIn = function (this: MindElixirInstance, from: Topic[], to:
252270 moveNodeObj ( 'in' , obj , toObj )
253271 fillParent ( this . nodeData ) // update parent property
254272 const fromTop = f . parentElement
255- realAddChild ( this , to , fromTop . parentElement )
273+ addChildDom ( this , to , fromTop . parentElement )
256274 }
257275 this . linkDiv ( )
258276 this . bus . fire ( 'operation' , {
0 commit comments