This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
EditorState.js
768 lines (680 loc) · 22.2 KB
/
EditorState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @emails oncall+draft_js
*/
'use strict';
import type {BlockMap} from 'BlockMap';
import type {DecoratorRangeRawType} from 'BlockTree';
import type {ContentStateRawType} from 'ContentStateRawType';
import type {DraftDecoratorType} from 'DraftDecoratorType';
import type {DraftInlineStyle} from 'DraftInlineStyle';
import type {EditorChangeType} from 'EditorChangeType';
import type {EntityMap} from 'EntityMap';
const BlockTree = require('BlockTree');
const ContentState = require('ContentState');
const EditorBidiService = require('EditorBidiService');
const SelectionState = require('SelectionState');
const Immutable = require('immutable');
const {OrderedSet, Record, Stack, OrderedMap, List} = Immutable;
// When configuring an editor, the user can chose to provide or not provide
// basically all keys. `currentContent` varies, so this type doesn't include it.
// (See the types defined below.)
type BaseEditorStateConfig = {|
allowUndo?: boolean,
decorator?: ?DraftDecoratorType,
directionMap?: ?OrderedMap<string, string>,
forceSelection?: boolean,
inCompositionMode?: boolean,
inlineStyleOverride?: ?DraftInlineStyle,
lastChangeType?: ?EditorChangeType,
nativelyRenderedContent?: ?ContentState,
redoStack?: Stack<ContentState>,
selection?: ?SelectionState,
treeMap?: ?OrderedMap<string, List<any>>,
undoStack?: Stack<ContentState>,
|};
type BaseEditorStateRawConfig = {|
allowUndo?: boolean,
decorator?: ?DraftDecoratorType,
directionMap?: ?{...},
forceSelection?: boolean,
inCompositionMode?: boolean,
inlineStyleOverride?: ?Array<String>,
lastChangeType?: ?EditorChangeType,
nativelyRenderedContent?: ?ContentStateRawType,
redoStack?: Array<ContentStateRawType>,
selection?: ?{...},
treeMap?: ?Map<string, Array<DecoratorRangeRawType>>,
undoStack?: Array<ContentStateRawType>,
|};
// When crating an editor, we want currentContent to be set.
type EditorStateCreationConfigType = {|
...BaseEditorStateConfig,
currentContent: ContentState,
|};
type EditorStateCreationConfigRawType = {|
...BaseEditorStateRawConfig,
currentContent: ContentStateRawType,
|};
// When using EditorState.set(...), currentContent is optional
type EditorStateChangeConfigType = {|
...BaseEditorStateConfig,
currentContent?: ?ContentState,
|};
type EditorStateRecordType = {
allowUndo: boolean,
currentContent: ?ContentState,
decorator: ?DraftDecoratorType,
directionMap: ?OrderedMap<string, string>,
forceSelection: boolean,
inCompositionMode: boolean,
inlineStyleOverride: ?DraftInlineStyle,
lastChangeType: ?EditorChangeType,
nativelyRenderedContent: ?ContentState,
redoStack: Stack<ContentState>,
selection: ?SelectionState,
treeMap: ?OrderedMap<string, List<any>>,
undoStack: Stack<ContentState>,
...
};
const defaultRecord: EditorStateRecordType = {
allowUndo: true,
currentContent: null,
decorator: null,
directionMap: null,
forceSelection: false,
inCompositionMode: false,
inlineStyleOverride: null,
lastChangeType: null,
nativelyRenderedContent: null,
redoStack: Stack(),
selection: null,
treeMap: null,
undoStack: Stack(),
};
const EditorStateRecord = (Record(defaultRecord): any);
class EditorState {
_immutable: EditorStateRecord;
static createEmpty(decorator?: ?DraftDecoratorType): EditorState {
return this.createWithText('', decorator);
}
static createWithText(
text: string,
decorator?: ?DraftDecoratorType,
): EditorState {
return EditorState.createWithContent(
ContentState.createFromText(text),
decorator,
);
}
static createWithContent(
contentState: ContentState,
decorator?: ?DraftDecoratorType,
): EditorState {
if (contentState.getBlockMap().count() === 0) {
return EditorState.createEmpty(decorator);
}
const firstKey = contentState
.getBlockMap()
.first()
.getKey();
return EditorState.create({
currentContent: contentState,
undoStack: Stack(),
redoStack: Stack(),
decorator: decorator || null,
selection: SelectionState.createEmpty(firstKey),
});
}
static create(config: EditorStateCreationConfigType): EditorState {
const {currentContent, decorator} = config;
const recordConfig = {
...config,
treeMap: generateNewTreeMap(currentContent, decorator),
directionMap: EditorBidiService.getDirectionMap(currentContent),
};
return new EditorState(new EditorStateRecord(recordConfig));
}
static fromJS(config: EditorStateCreationConfigRawType): EditorState {
return new EditorState(
new EditorStateRecord({
...config,
directionMap:
config.directionMap != null
? OrderedMap(config.directionMap)
: config.directionMap,
inlineStyleOverride:
config.inlineStyleOverride != null
? OrderedSet(config.inlineStyleOverride)
: config.inlineStyleOverride,
nativelyRenderedContent:
config.nativelyRenderedContent != null
? ContentState.fromJS(config.nativelyRenderedContent)
: config.nativelyRenderedContent,
redoStack:
config.redoStack != null
? Stack(config.redoStack.map(v => ContentState.fromJS(v)))
: config.redoStack,
selection:
config.selection != null
? new SelectionState(config.selection)
: config.selection,
treeMap:
config.treeMap != null
? OrderedMap(config.treeMap).map(v =>
List(v).map(v => BlockTree.fromJS(v)),
)
: config.treeMap,
undoStack:
config.undoStack != null
? Stack(config.undoStack.map(v => ContentState.fromJS(v)))
: config.undoStack,
currentContent: ContentState.fromJS(config.currentContent),
}),
);
}
static set(
editorState: EditorState,
put: EditorStateChangeConfigType,
): EditorState {
const map = editorState.getImmutable().withMutations(state => {
const existingDecorator = state.get('decorator');
let decorator = existingDecorator;
if (put.decorator === null) {
decorator = null;
} else if (put.decorator) {
decorator = put.decorator;
}
const newContent = put.currentContent || editorState.getCurrentContent();
if (decorator !== existingDecorator) {
const treeMap: OrderedMap<string, any> = state.get('treeMap');
let newTreeMap;
if (decorator && existingDecorator) {
newTreeMap = regenerateTreeForNewDecorator(
newContent,
newContent.getBlockMap(),
treeMap,
decorator,
existingDecorator,
);
} else {
newTreeMap = generateNewTreeMap(newContent, decorator);
}
state.merge({
decorator,
treeMap: newTreeMap,
nativelyRenderedContent: null,
});
return;
}
const existingContent = editorState.getCurrentContent();
if (newContent !== existingContent) {
state.set(
'treeMap',
regenerateTreeForNewBlocks(
editorState,
newContent.getBlockMap(),
newContent.getEntityMap(),
decorator,
),
);
}
state.merge(put);
});
return new EditorState(map);
}
toJS(): Object {
return this.getImmutable().toJS();
}
getAllowUndo(): boolean {
return this.getImmutable().get('allowUndo');
}
getCurrentContent(): ContentState {
return this.getImmutable().get('currentContent');
}
getUndoStack(): Stack<ContentState> {
return this.getImmutable().get('undoStack');
}
getRedoStack(): Stack<ContentState> {
return this.getImmutable().get('redoStack');
}
getSelection(): SelectionState {
return this.getImmutable().get('selection');
}
getDecorator(): ?DraftDecoratorType {
return this.getImmutable().get('decorator');
}
isInCompositionMode(): boolean {
return this.getImmutable().get('inCompositionMode');
}
mustForceSelection(): boolean {
return this.getImmutable().get('forceSelection');
}
getNativelyRenderedContent(): ?ContentState {
return this.getImmutable().get('nativelyRenderedContent');
}
getLastChangeType(): ?EditorChangeType {
return this.getImmutable().get('lastChangeType');
}
/**
* While editing, the user may apply inline style commands with a collapsed
* cursor, intending to type text that adopts the specified style. In this
* case, we track the specified style as an "override" that takes precedence
* over the inline style of the text adjacent to the cursor.
*
* If null, there is no override in place.
*/
getInlineStyleOverride(): ?DraftInlineStyle {
return this.getImmutable().get('inlineStyleOverride');
}
static setInlineStyleOverride(
editorState: EditorState,
inlineStyleOverride: DraftInlineStyle,
): EditorState {
return EditorState.set(editorState, {inlineStyleOverride});
}
/**
* Get the appropriate inline style for the editor state. If an
* override is in place, use it. Otherwise, the current style is
* based on the location of the selection state.
*/
getCurrentInlineStyle(): DraftInlineStyle {
const override = this.getInlineStyleOverride();
if (override != null) {
return override;
}
const content = this.getCurrentContent();
const selection = this.getSelection();
if (selection.isCollapsed()) {
return getInlineStyleForCollapsedSelection(content, selection);
}
return getInlineStyleForNonCollapsedSelection(content, selection);
}
getBlockTree(blockKey: string): List<any> {
return this.getImmutable().getIn(['treeMap', blockKey]);
}
isSelectionAtStartOfContent(): boolean {
const firstKey = this.getCurrentContent()
.getBlockMap()
.first()
.getKey();
return this.getSelection().hasEdgeWithin(firstKey, 0, 0);
}
isSelectionAtEndOfContent(): boolean {
const content = this.getCurrentContent();
const blockMap = content.getBlockMap();
const last = blockMap.last();
const end = last.getLength();
return this.getSelection().hasEdgeWithin(last.getKey(), end, end);
}
getDirectionMap(): ?OrderedMap<any, any> {
return this.getImmutable().get('directionMap');
}
/**
* Incorporate native DOM selection changes into the EditorState. This
* method can be used when we simply want to accept whatever the DOM
* has given us to represent selection, and we do not need to re-render
* the editor.
*
* To forcibly move the DOM selection, see `EditorState.forceSelection`.
*/
static acceptSelection(
editorState: EditorState,
selection: SelectionState,
): EditorState {
return updateSelection(editorState, selection, false);
}
/**
* At times, we need to force the DOM selection to be where we
* need it to be. This can occur when the anchor or focus nodes
* are non-text nodes, for instance. In this case, we want to trigger
* a re-render of the editor, which in turn forces selection into
* the correct place in the DOM. The `forceSelection` method
* accomplishes this.
*
* This method should be used in cases where you need to explicitly
* move the DOM selection from one place to another without a change
* in ContentState.
*/
static forceSelection(
editorState: EditorState,
selection: SelectionState,
): EditorState {
if (!selection.getHasFocus()) {
selection = selection.set('hasFocus', true);
}
return updateSelection(editorState, selection, true);
}
/**
* Move selection to the end of the editor without forcing focus.
*/
static moveSelectionToEnd(editorState: EditorState): EditorState {
const content = editorState.getCurrentContent();
const lastBlock = content.getLastBlock();
const lastKey = lastBlock.getKey();
const length = lastBlock.getLength();
return EditorState.acceptSelection(
editorState,
new SelectionState({
anchorKey: lastKey,
anchorOffset: length,
focusKey: lastKey,
focusOffset: length,
isBackward: false,
}),
);
}
/**
* Force focus to the end of the editor. This is useful in scenarios
* where we want to programmatically focus the input and it makes sense
* to allow the user to continue working seamlessly.
*/
static moveFocusToEnd(editorState: EditorState): EditorState {
const afterSelectionMove = EditorState.moveSelectionToEnd(editorState);
return EditorState.forceSelection(
afterSelectionMove,
afterSelectionMove.getSelection(),
);
}
/**
* Push the current ContentState onto the undo stack if it should be
* considered a boundary state, and set the provided ContentState as the
* new current content.
*/
static push(
editorState: EditorState,
contentState: ContentState,
changeType: EditorChangeType,
forceSelection: boolean = true,
): EditorState {
if (editorState.getCurrentContent() === contentState) {
return editorState;
}
const directionMap = EditorBidiService.getDirectionMap(
contentState,
editorState.getDirectionMap(),
);
if (!editorState.getAllowUndo()) {
return EditorState.set(editorState, {
currentContent: contentState,
directionMap,
lastChangeType: changeType,
selection: contentState.getSelectionAfter(),
forceSelection,
inlineStyleOverride: null,
});
}
const selection = editorState.getSelection();
const currentContent = editorState.getCurrentContent();
let undoStack = editorState.getUndoStack();
let newContent = contentState;
if (
selection !== currentContent.getSelectionAfter() ||
mustBecomeBoundary(editorState, changeType)
) {
undoStack = undoStack.push(currentContent);
newContent = newContent.set('selectionBefore', selection);
} else if (
changeType === 'insert-characters' ||
changeType === 'backspace-character' ||
changeType === 'delete-character'
) {
// Preserve the previous selection.
newContent = newContent.set(
'selectionBefore',
currentContent.getSelectionBefore(),
);
}
let inlineStyleOverride = editorState.getInlineStyleOverride();
// Don't discard inline style overrides for the following change types:
const overrideChangeTypes = [
'adjust-depth',
'change-block-type',
'split-block',
];
if (overrideChangeTypes.indexOf(changeType) === -1) {
inlineStyleOverride = null;
}
const editorStateChanges = {
currentContent: newContent,
directionMap,
undoStack,
redoStack: Stack(),
lastChangeType: changeType,
selection: contentState.getSelectionAfter(),
forceSelection,
inlineStyleOverride,
};
return EditorState.set(editorState, editorStateChanges);
}
/**
* Make the top ContentState in the undo stack the new current content and
* push the current content onto the redo stack.
*/
static undo(editorState: EditorState): EditorState {
if (!editorState.getAllowUndo()) {
return editorState;
}
const undoStack = editorState.getUndoStack();
const newCurrentContent = undoStack.peek();
if (!newCurrentContent) {
return editorState;
}
const currentContent = editorState.getCurrentContent();
const directionMap = EditorBidiService.getDirectionMap(
newCurrentContent,
editorState.getDirectionMap(),
);
return EditorState.set(editorState, {
currentContent: newCurrentContent,
directionMap,
undoStack: undoStack.shift(),
redoStack: editorState.getRedoStack().push(currentContent),
forceSelection: true,
inlineStyleOverride: null,
lastChangeType: 'undo',
nativelyRenderedContent: null,
selection: currentContent.getSelectionBefore(),
});
}
/**
* Make the top ContentState in the redo stack the new current content and
* push the current content onto the undo stack.
*/
static redo(editorState: EditorState): EditorState {
if (!editorState.getAllowUndo()) {
return editorState;
}
const redoStack = editorState.getRedoStack();
const newCurrentContent = redoStack.peek();
if (!newCurrentContent) {
return editorState;
}
const currentContent = editorState.getCurrentContent();
const directionMap = EditorBidiService.getDirectionMap(
newCurrentContent,
editorState.getDirectionMap(),
);
return EditorState.set(editorState, {
currentContent: newCurrentContent,
directionMap,
undoStack: editorState.getUndoStack().push(currentContent),
redoStack: redoStack.shift(),
forceSelection: true,
inlineStyleOverride: null,
lastChangeType: 'redo',
nativelyRenderedContent: null,
selection: newCurrentContent.getSelectionAfter(),
});
}
/**
* Not for public consumption.
*/
constructor(immutable: EditorStateRecord) {
this._immutable = immutable;
}
/**
* Not for public consumption.
*/
getImmutable(): EditorStateRecord {
return this._immutable;
}
}
/**
* Set the supplied SelectionState as the new current selection, and set
* the `force` flag to trigger manual selection placement by the view.
*/
function updateSelection(
editorState: EditorState,
selection: SelectionState,
forceSelection: boolean,
): EditorState {
return EditorState.set(editorState, {
selection,
forceSelection,
nativelyRenderedContent: null,
inlineStyleOverride: null,
});
}
/**
* Regenerate the entire tree map for a given ContentState and decorator.
* Returns an OrderedMap that maps all available ContentBlock objects.
*/
function generateNewTreeMap(
contentState: ContentState,
decorator?: ?DraftDecoratorType,
): OrderedMap<string, List<any>> {
return contentState
.getBlockMap()
.map(block => BlockTree.generate(contentState, block, decorator))
.toOrderedMap();
}
/**
* Regenerate tree map objects for all ContentBlocks that have changed
* between the current editorState and newContent. Returns an OrderedMap
* with only changed regenerated tree map objects.
*/
function regenerateTreeForNewBlocks(
editorState: EditorState,
newBlockMap: BlockMap,
newEntityMap: EntityMap,
decorator?: ?DraftDecoratorType,
): OrderedMap<string, List<any>> {
const contentState = editorState
.getCurrentContent()
.set('entityMap', newEntityMap);
const prevBlockMap = contentState.getBlockMap();
const prevTreeMap = editorState.getImmutable().get('treeMap');
return prevTreeMap.merge(
newBlockMap
.toSeq()
.filter((block, key) => block !== prevBlockMap.get(key))
.map(block => BlockTree.generate(contentState, block, decorator)),
);
}
/**
* Generate tree map objects for a new decorator object, preserving any
* decorations that are unchanged from the previous decorator.
*
* Note that in order for this to perform optimally, decoration Lists for
* decorators should be preserved when possible to allow for direct immutable
* List comparison.
*/
function regenerateTreeForNewDecorator(
content: ContentState,
blockMap: BlockMap,
previousTreeMap: OrderedMap<string, List<any>>,
decorator: DraftDecoratorType,
existingDecorator: DraftDecoratorType,
): OrderedMap<string, List<any>> {
return previousTreeMap.merge(
blockMap
.toSeq()
.filter(block => {
return (
decorator.getDecorations(block, content) !==
existingDecorator.getDecorations(block, content)
);
})
.map(block => BlockTree.generate(content, block, decorator)),
);
}
/**
* Return whether a change should be considered a boundary state, given
* the previous change type. Allows us to discard potential boundary states
* during standard typing or deletion behavior.
*/
function mustBecomeBoundary(
editorState: EditorState,
changeType: EditorChangeType,
): boolean {
const lastChangeType = editorState.getLastChangeType();
return (
changeType !== lastChangeType ||
(changeType !== 'insert-characters' &&
changeType !== 'backspace-character' &&
changeType !== 'delete-character')
);
}
function getInlineStyleForCollapsedSelection(
content: ContentState,
selection: SelectionState,
): DraftInlineStyle {
const startKey = selection.getStartKey();
const startOffset = selection.getStartOffset();
const startBlock = content.getBlockForKey(startKey);
// If the cursor is not at the start of the block, look backward to
// preserve the style of the preceding character.
if (startOffset > 0) {
return startBlock.getInlineStyleAt(startOffset - 1);
}
// The caret is at position zero in this block. If the block has any
// text at all, use the style of the first character.
if (startBlock.getLength()) {
return startBlock.getInlineStyleAt(0);
}
// Otherwise, look upward in the document to find the closest character.
return lookUpwardForInlineStyle(content, startKey);
}
function getInlineStyleForNonCollapsedSelection(
content: ContentState,
selection: SelectionState,
): DraftInlineStyle {
const startKey = selection.getStartKey();
const startOffset = selection.getStartOffset();
const startBlock = content.getBlockForKey(startKey);
// If there is a character just inside the selection, use its style.
if (startOffset < startBlock.getLength()) {
return startBlock.getInlineStyleAt(startOffset);
}
// Check if the selection at the end of a non-empty block. Use the last
// style in the block.
if (startOffset > 0) {
return startBlock.getInlineStyleAt(startOffset - 1);
}
// Otherwise, look upward in the document to find the closest character.
return lookUpwardForInlineStyle(content, startKey);
}
function lookUpwardForInlineStyle(
content: ContentState,
fromKey: string,
): DraftInlineStyle {
const lastNonEmpty = content
.getBlockMap()
.reverse()
.skipUntil((_, k) => k === fromKey)
.skip(1)
.skipUntil((block, _) => block.getLength())
.first();
if (lastNonEmpty) {
return lastNonEmpty.getInlineStyleAt(lastNonEmpty.getLength() - 1);
}
return OrderedSet();
}
module.exports = EditorState;