@@ -748,13 +748,16 @@ const WorkflowContent = React.memo(() => {
748748 } , [ contextMenuBlocks , collaborativeBatchToggleBlockHandles ] )
749749
750750 const handleContextRemoveFromSubflow = useCallback ( ( ) => {
751- contextMenuBlocks . forEach ( ( block ) => {
752- if ( block . parentId && ( block . parentType === 'loop' || block . parentType === 'parallel' ) ) {
753- window . dispatchEvent (
754- new CustomEvent ( 'remove-from-subflow' , { detail : { blockId : block . id } } )
755- )
756- }
757- } )
751+ const blocksToRemove = contextMenuBlocks . filter (
752+ ( block ) => block . parentId && ( block . parentType === 'loop' || block . parentType === 'parallel' )
753+ )
754+ if ( blocksToRemove . length > 0 ) {
755+ window . dispatchEvent (
756+ new CustomEvent ( 'remove-from-subflow' , {
757+ detail : { blockIds : blocksToRemove . map ( ( b ) => b . id ) } ,
758+ } )
759+ )
760+ }
758761 } , [ contextMenuBlocks ] )
759762
760763 const handleContextOpenEditor = useCallback ( ( ) => {
@@ -921,20 +924,32 @@ const WorkflowContent = React.memo(() => {
921924 /** Handles ActionBar remove-from-subflow events. */
922925 useEffect ( ( ) => {
923926 const handleRemoveFromSubflow = ( event : Event ) => {
924- const customEvent = event as CustomEvent < { blockId : string } >
925- const blockId = customEvent . detail ?. blockId
926- if ( ! blockId ) return
927+ const customEvent = event as CustomEvent < { blockIds : string [ ] } >
928+ const blockIds = customEvent . detail ?. blockIds
929+ if ( ! blockIds || blockIds . length === 0 ) return
927930
928931 try {
929- const currentBlock = blocks [ blockId ]
930- const parentId = currentBlock ?. data ?. parentId
931- if ( ! parentId ) return
932+ const validBlockIds = blockIds . filter ( ( id ) => {
933+ const block = blocks [ id ]
934+ return block ?. data ?. parentId
935+ } )
936+ if ( validBlockIds . length === 0 ) return
932937
933- const edgesToRemove = edgesForDisplay . filter (
934- ( e ) => e . source === blockId || e . target === blockId
935- )
936- removeEdgesForNode ( blockId , edgesToRemove )
937- updateNodeParent ( blockId , null , edgesToRemove )
938+ const movingNodeIds = new Set ( validBlockIds )
939+
940+ const boundaryEdges = edgesForDisplay . filter ( ( e ) => {
941+ const sourceInSelection = movingNodeIds . has ( e . source )
942+ const targetInSelection = movingNodeIds . has ( e . target )
943+ return sourceInSelection !== targetInSelection
944+ } )
945+
946+ for ( const blockId of validBlockIds ) {
947+ const edgesForThisNode = boundaryEdges . filter (
948+ ( e ) => e . source === blockId || e . target === blockId
949+ )
950+ removeEdgesForNode ( blockId , edgesForThisNode )
951+ updateNodeParent ( blockId , null , edgesForThisNode )
952+ }
938953 } catch ( err ) {
939954 logger . error ( 'Failed to remove from subflow' , { err } )
940955 }
0 commit comments