1+ import React from 'react' ;
12import {
23 Editor ,
34 Modifier ,
45 ContentState ,
6+ ContentBlock ,
57 convertFromHTML ,
68 DefaultDraftBlockRenderMap ,
79 DefaultDraftInlineStyle ,
810 CompositeDecorator ,
9- SelectionState
11+ SelectionState ,
1012} from 'draft-js' ;
1113import * as sdk from './index' ;
1214import * as emojione from 'emojione' ;
@@ -25,7 +27,7 @@ const STYLES = {
2527 CODE : 'code' ,
2628 ITALIC : 'em' ,
2729 STRIKETHROUGH : 's' ,
28- UNDERLINE : 'u'
30+ UNDERLINE : 'u' ,
2931} ;
3032
3133const MARKDOWN_REGEX = {
@@ -168,7 +170,7 @@ export function modifyText(contentState: ContentState, rangeToReplace: Selection
168170 text = "" ;
169171
170172
171- for ( let currentKey = startKey ;
173+ for ( let currentKey = startKey ;
172174 currentKey && currentKey !== endKey ;
173175 currentKey = contentState . getKeyAfter ( currentKey ) ) {
174176 let blockText = getText ( currentKey ) ;
@@ -189,14 +191,14 @@ export function modifyText(contentState: ContentState, rangeToReplace: Selection
189191 * Note that this inherently means we make assumptions about what that means (no separator between ContentBlocks, etc)
190192 * Used by autocomplete to show completions when the current selection lies within, or at the edges of a command.
191193 */
192- export function getTextSelectionOffsets ( selectionState : SelectionState ,
193- contentBlocks : Array < ContentBlock > ) : { start : number , end : number } {
194+ export function selectionStateToTextOffsets ( selectionState : SelectionState ,
195+ contentBlocks : Array < ContentBlock > ) : { start : number , end : number } {
194196 let offset = 0 , start = 0 , end = 0 ;
195197 for ( let block of contentBlocks ) {
196- if ( selectionState . getStartKey ( ) == block . getKey ( ) ) {
198+ if ( selectionState . getStartKey ( ) === block . getKey ( ) ) {
197199 start = offset + selectionState . getStartOffset ( ) ;
198200 }
199- if ( selectionState . getEndKey ( ) == block . getKey ( ) ) {
201+ if ( selectionState . getEndKey ( ) === block . getKey ( ) ) {
200202 end = offset + selectionState . getEndOffset ( ) ;
201203 break ;
202204 }
@@ -205,6 +207,37 @@ export function getTextSelectionOffsets(selectionState: SelectionState,
205207
206208 return {
207209 start,
208- end
210+ end,
211+ } ;
212+ }
213+
214+ export function textOffsetsToSelectionState ( { start, end} : { start : number , end : number } ,
215+ contentBlocks : Array < ContentBlock > ) : SelectionState {
216+ let selectionState = SelectionState . createEmpty ( ) ;
217+
218+ for ( let block of contentBlocks ) {
219+ let blockLength = block . getLength ( ) ;
220+
221+ if ( start !== - 1 && start < blockLength ) {
222+ selectionState = selectionState . merge ( {
223+ anchorKey : block . getKey ( ) ,
224+ anchorOffset : start ,
225+ } ) ;
226+ start = - 1 ;
227+ } else {
228+ start - = blockLength ;
229+ }
230+
231+ if ( end !== - 1 && end <= blockLength ) {
232+ selectionState = selectionState . merge ( {
233+ focusKey : block . getKey ( ) ,
234+ focusOffset : end ,
235+ } ) ;
236+ end = - 1 ;
237+ } else {
238+ end - = blockLength ;
239+ }
209240 }
241+
242+ return selectionState ;
210243}
0 commit comments