@@ -362,30 +362,31 @@ export const moveDownNode = function (this: MindElixirInstance, el?: Topic) {
362362export const removeNode = function ( this : MindElixirInstance , el ?: Topic ) {
363363 const nodeEle = el || this . currentNode
364364 if ( ! nodeEle ) return
365- console . log ( 'removeNode' , nodeEle )
366365 const nodeObj = nodeEle . nodeObj
367366 if ( nodeObj . root === true ) {
368367 throw new Error ( 'Can not remove root node' )
369368 }
370369 const siblings = nodeObj . parent ! . children !
371- const index = siblings . findIndex ( node => node === nodeObj )
372- const next : NodeObj | undefined = siblings [ index + 1 ]
373- const originSiblingId = next && next . id
370+ const i = siblings . findIndex ( node => node === nodeObj )
371+ const siblingLength = removeNodeObj ( nodeObj )
374372
375- const childrenLength = removeNodeObj ( nodeObj )
376373 const t = nodeEle . parentNode
377- if ( childrenLength === 0 ) {
374+ if ( siblingLength === 0 ) {
378375 // remove epd when children length === 0
379376 const c = t . parentNode . parentNode
380377 // root doesn't have epd
381378 if ( c . tagName !== 'ME-MAIN' ) {
382379 c . previousSibling . children [ 1 ] . remove ( )
383380 }
384- this . selectParent ( )
381+ }
382+ // automatically select sibling or parent
383+ if ( siblings . length !== 0 ) {
384+ const sibling = siblings [ i ] || siblings [ i - 1 ]
385+ this . selectNode ( findEle ( sibling . id ) )
385386 } else {
386- // select sibling automatically
387- this . selectPrevSibling ( ) || this . selectNextSibling ( )
387+ this . selectNode ( findEle ( nodeObj . parent ! . id ) )
388388 }
389+
389390 for ( const prop in this . linkData ) {
390391 // MAYBEBUG should traverse all children node
391392 const link = this . linkData [ prop ]
@@ -394,13 +395,12 @@ export const removeNode = function (this: MindElixirInstance, el?: Topic) {
394395 this . removeLink ( linkEle )
395396 }
396397 }
397- // remove GRP
398398 t . parentNode . remove ( )
399399 this . linkDiv ( )
400400 this . bus . fire ( 'operation' , {
401401 name : 'removeNode' ,
402402 obj : nodeObj ,
403- originSiblingId ,
403+ originIndex : i ,
404404 originParentId : nodeObj ?. parent ?. id ,
405405 } )
406406}
0 commit comments