1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var Immutable = require("immutable");
|
14 |
|
15 | var insertIntoList = require("./insertIntoList");
|
16 |
|
17 | var invariant = require("fbjs/lib/invariant");
|
18 |
|
19 | var Repeat = Immutable.Repeat;
|
20 |
|
21 | function insertTextIntoContentState(contentState, selectionState, text, characterMetadata) {
|
22 | !selectionState.isCollapsed() ? process.env.NODE_ENV !== "production" ? invariant(false, '`insertText` should only be called with a collapsed range.') : invariant(false) : void 0;
|
23 | var len = null;
|
24 |
|
25 | if (text != null) {
|
26 | len = text.length;
|
27 | }
|
28 |
|
29 | if (len == null || len === 0) {
|
30 | return contentState;
|
31 | }
|
32 |
|
33 | var blockMap = contentState.getBlockMap();
|
34 | var key = selectionState.getStartKey();
|
35 | var offset = selectionState.getStartOffset();
|
36 | var block = blockMap.get(key);
|
37 | var blockText = block.getText();
|
38 | var newBlock = block.merge({
|
39 | text: blockText.slice(0, offset) + text + blockText.slice(offset, block.getLength()),
|
40 | characterList: insertIntoList(block.getCharacterList(), Repeat(characterMetadata, len).toList(), offset)
|
41 | });
|
42 | var newOffset = offset + len;
|
43 | return contentState.merge({
|
44 | blockMap: blockMap.set(key, newBlock),
|
45 | selectionAfter: selectionState.merge({
|
46 | anchorOffset: newOffset,
|
47 | focusOffset: newOffset
|
48 | })
|
49 | });
|
50 | }
|
51 |
|
52 | module.exports = insertTextIntoContentState; |
\ | No newline at end of file |