import { EditorState } from '@craftjs/core' ; export const getSplitOperations = ( state , slateNodeId , rootType , acceptableChildrenType ) => { const slateNode = state . nodes [ slateNodeId ] ; let currentTree = { } ; let hasSplitted = false ; const getOperations = ( nodeId , parentId , parentTree = { } ) => { let hasCurrentNodeBeenExpelled = false ; const operations = [ ] ; const node = state . nodes [ nodeId ] ; let parentIdForChildren = nodeId ; if ( nodeId === slateNodeId ) { const nodeInfo = { id : nodeId , nodes : [ ] , expelled : hasSplitted , } ; currentTree [ nodeId ] = { ... nodeInfo } ; parentTree [ nodeId ] = { ... nodeInfo } ; } else if ( rootType === node . data . type ) { parentIdForChildren = parentId ; currentTree = { ... parentTree } ; operations . push ( { type : 'expand' , id : node . id , } ) ; } else if ( acceptableChildrenType . includes ( node . data . type ) === false ) { operations . push ( { type : 'expel' , id : node . id , } ) ; currentTree = null ; hasSplitted = true ; hasCurrentNodeBeenExpelled = true ; } else { if ( ! currentTree ) { currentTree = { ... parentTree } ; operations . push ( { type : 'insert_tree' , tree : currentTree , } ) ; } const nodeInfo = { id : nodeId , nodes : [ ] , expelled : hasSplitted , } ; if ( currentTree [ parentId ] ) { currentTree [ parentId ] = { ... currentTree [ parentId ] , nodes : [ ... currentTree [ parentId ] . nodes , nodeId ] , } ; } if ( parentTree [ parentId ] ) { parentTree [ parentId ] = { ... parentTree [ parentId ] , nodes : [ nodeId ] , } ; } currentTree [ nodeId ] = { ... nodeInfo } ; parentTree [ nodeId ] = { ... nodeInfo } ; } if ( hasCurrentNodeBeenExpelled ) { return operations ; } return [ ... operations , ... node . data . nodes . map ( ( childNodeId ) => getOperations ( childNodeId , parentIdForChildren , { ... parentTree } ) ) . flat ( ) , ] ; } ; return getOperations ( slateNode . id , slateNode . data . parent ) ; } ;