import Editor from "../editor" ; import Block , { BlockType } from "../components/block" ; import getSelection from "./get-selection" ; import focusAt from "./focus-at" ; import getSelectedIdList from "./get-selected-id-list" ; const exchange = ( editor , newBlock , ... args ) => { let selection = getSelection ( editor ) ; let start = selection . range [ 0 ] ; let end = selection . range [ 1 ] ; try { let idList = getSelectedIdList ( editor . article , start . id , end . id ) ; let endToTailSize = editor . storeManage . getBlockById ( idList [ idList . length - 1 ] ) . getSize ( ) - end . offset ; let exchangeList = [ ] ; let idMap = { } ; idList . forEach ( ( id ) => { editor . storeManage . getBlockById ( id ) . exchangeTo ( newBlock , args ) . forEach ( ( each ) => { if ( ! idMap [ each . id ] ) { idMap [ each . id ] = 1 ; exchangeList . push ( each ) ; } } ) ; } ) ; let nowStart = { id : "" , offset : start . offset } ; let nowEnd = { id : "" , offset : endToTailSize } ; let index = 0 ; while ( index < exchangeList . length ) { let component = exchangeList [ index ] ; let size = component . getSize ( ) ; if ( nowStart . offset <= size ) { nowStart . id = component . id ; break ; } nowStart . offset -= size ; index += 1 ; } let tailIndex = exchangeList . length - 1 ; while ( tailIndex >= 0 ) { let component = exchangeList [ tailIndex ] ; let size = component . getSize ( ) ; if ( nowEnd . offset <= size ) { nowEnd . id = component . id ; nowEnd . offset = size - nowEnd . offset ; break ; } nowEnd . offset -= size ; tailIndex -= 1 ; } focusAt ( editor , nowStart , nowEnd ) ; } catch ( err ) { console . warn ( err ) ; } } ; export default exchange ;