import { Entity } from 'draft-js' ; import { OrderedSet } from 'immutable' ; const getSelectionEntities = ( editorState , entityType ) => { const targetSelection = editorState . getSelection ( ) ; const endOffset = targetSelection . getEndOffset ( ) ; const currentContent = editorState . getCurrentContent ( ) ; const block = currentContent . getBlockForKey ( targetSelection . getStartKey ( ) ) ; const selectionEntitySet = OrderedSet ( ) ; block . findEntityRanges ( ( character ) => { const entityKey = character . getEntity ( ) ; return ( entityKey !== null && Entity . get ( entityKey ) . getType ( ) === entityType ) ; } , ( start , end ) => { const isSelected = end >= endOffset && start <= endOffset ; if ( isSelected ) { selectionEntitySet . add ( { blockKey : block . getKey ( ) , entityKey : block . getEntityAt ( start ) , start , end , } ) ; } } ) ; return selectionEntitySet ; } ; export default getSelectionEntities ;