1
+ import React from 'react' ;
1
2
import {
2
3
Editor ,
3
4
Modifier ,
4
5
ContentState ,
6
+ ContentBlock ,
5
7
convertFromHTML ,
6
8
DefaultDraftBlockRenderMap ,
7
9
DefaultDraftInlineStyle ,
8
10
CompositeDecorator ,
9
- SelectionState
11
+ SelectionState ,
10
12
} from 'draft-js' ;
11
13
import * as sdk from './index' ;
12
14
import * as emojione from 'emojione' ;
@@ -25,7 +27,7 @@ const STYLES = {
25
27
CODE : 'code' ,
26
28
ITALIC : 'em' ,
27
29
STRIKETHROUGH : 's' ,
28
- UNDERLINE : 'u'
30
+ UNDERLINE : 'u' ,
29
31
} ;
30
32
31
33
const MARKDOWN_REGEX = {
@@ -168,7 +170,7 @@ export function modifyText(contentState: ContentState, rangeToReplace: Selection
168
170
text = "" ;
169
171
170
172
171
- for ( let currentKey = startKey ;
173
+ for ( let currentKey = startKey ;
172
174
currentKey && currentKey !== endKey ;
173
175
currentKey = contentState . getKeyAfter ( currentKey ) ) {
174
176
let blockText = getText ( currentKey ) ;
@@ -189,14 +191,14 @@ export function modifyText(contentState: ContentState, rangeToReplace: Selection
189
191
* Note that this inherently means we make assumptions about what that means (no separator between ContentBlocks, etc)
190
192
* Used by autocomplete to show completions when the current selection lies within, or at the edges of a command.
191
193
*/
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 } {
194
196
let offset = 0 , start = 0 , end = 0 ;
195
197
for ( let block of contentBlocks ) {
196
- if ( selectionState . getStartKey ( ) == block . getKey ( ) ) {
198
+ if ( selectionState . getStartKey ( ) === block . getKey ( ) ) {
197
199
start = offset + selectionState . getStartOffset ( ) ;
198
200
}
199
- if ( selectionState . getEndKey ( ) == block . getKey ( ) ) {
201
+ if ( selectionState . getEndKey ( ) === block . getKey ( ) ) {
200
202
end = offset + selectionState . getEndOffset ( ) ;
201
203
break ;
202
204
}
@@ -205,6 +207,37 @@ export function getTextSelectionOffsets(selectionState: SelectionState,
205
207
206
208
return {
207
209
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
+ }
209
240
}
241
+
242
+ return selectionState ;
210
243
}
0 commit comments