import { EditorState , Node } from '@craftjs/core' ; import forIn from 'lodash/forIn' ; import deleteNodes from './deleteNodes' ; export default function mergeElements ( rootType , state , onDeleteCallback ? ) { const nodesToDelete = [ ] ; forIn ( state . nodes , ( node ) => { if ( ! node . data . nodes ) { return node ; } let lastNode = null ; const childNodesIds = new Set < string > ( ) ; node . data . nodes . forEach ( ( id ) => { const childNode = state . nodes [ id ] ; if ( ! childNode ) { return ; } childNodesIds . add ( id ) ; if ( childNode . data . type !== rootType ) { lastNode = null ; return ; } if ( lastNode ) { if ( lastNode . data . nodes ) { lastNode . data . nodes . reverse ( ) . forEach ( ( childNodeId ) => { state . nodes [ childNodeId ] . data . parent = childNode . id ; childNode . data . nodes . unshift ( childNodeId ) ; } ) ; } childNodesIds . delete ( lastNode . id ) ; nodesToDelete . push ( [ lastNode . id , childNode . id ] ) ; } lastNode = childNode ; if ( ! childNode . data . nodes ) { return ; } const deletedIds = nodesToDelete . map ( ( [ deletedId ] ) => deletedId ) ; if ( childNode . data . nodes . length === 0 || childNode . data . nodes . every ( ( childNodeId ) => deletedIds . includes ( childNodeId ) ) ) { nodesToDelete . push ( [ id ] ) ; if ( onDeleteCallback ) { onDeleteCallback ( childNode , nodesToDelete ) ; } } } ) ; node . data . nodes = Array . from ( childNodesIds ) ; return node ; } ) ; deleteNodes ( state , nodesToDelete ) ; }