1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var CharacterMetadata = require("./CharacterMetadata");
|
14 |
|
15 | var findRangesImmutable = require("./findRangesImmutable");
|
16 |
|
17 | var invariant = require("fbjs/lib/invariant");
|
18 |
|
19 | function removeEntitiesAtEdges(contentState, selectionState) {
|
20 | var blockMap = contentState.getBlockMap();
|
21 | var entityMap = contentState.getEntityMap();
|
22 | var updatedBlocks = {};
|
23 | var startKey = selectionState.getStartKey();
|
24 | var startOffset = selectionState.getStartOffset();
|
25 | var startBlock = blockMap.get(startKey);
|
26 | var updatedStart = removeForBlock(entityMap, startBlock, startOffset);
|
27 |
|
28 | if (updatedStart !== startBlock) {
|
29 | updatedBlocks[startKey] = updatedStart;
|
30 | }
|
31 |
|
32 | var endKey = selectionState.getEndKey();
|
33 | var endOffset = selectionState.getEndOffset();
|
34 | var endBlock = blockMap.get(endKey);
|
35 |
|
36 | if (startKey === endKey) {
|
37 | endBlock = updatedStart;
|
38 | }
|
39 |
|
40 | var updatedEnd = removeForBlock(entityMap, endBlock, endOffset);
|
41 |
|
42 | if (updatedEnd !== endBlock) {
|
43 | updatedBlocks[endKey] = updatedEnd;
|
44 | }
|
45 |
|
46 | if (!Object.keys(updatedBlocks).length) {
|
47 | return contentState.set('selectionAfter', selectionState);
|
48 | }
|
49 |
|
50 | return contentState.merge({
|
51 | blockMap: blockMap.merge(updatedBlocks),
|
52 | selectionAfter: selectionState
|
53 | });
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function getRemovalRange(characters, entityKey, offset) {
|
63 | var removalRange;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | findRangesImmutable(characters,
|
72 | function (a, b) {
|
73 | return a.getEntity() === b.getEntity();
|
74 | },
|
75 | function (element) {
|
76 | return element.getEntity() === entityKey;
|
77 | },
|
78 | function (start, end) {
|
79 |
|
80 | if (start <= offset && end >= offset) {
|
81 |
|
82 | removalRange = {
|
83 | start: start,
|
84 | end: end
|
85 | };
|
86 | }
|
87 | });
|
88 | !(typeof removalRange === 'object') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Removal range must exist within character list.') : invariant(false) : void 0;
|
89 | return removalRange;
|
90 | }
|
91 |
|
92 | function removeForBlock(entityMap, block, offset) {
|
93 | var chars = block.getCharacterList();
|
94 | var charBefore = offset > 0 ? chars.get(offset - 1) : undefined;
|
95 | var charAfter = offset < chars.count() ? chars.get(offset) : undefined;
|
96 | var entityBeforeCursor = charBefore ? charBefore.getEntity() : undefined;
|
97 | var entityAfterCursor = charAfter ? charAfter.getEntity() : undefined;
|
98 |
|
99 | if (entityAfterCursor && entityAfterCursor === entityBeforeCursor) {
|
100 | var entity = entityMap.__get(entityAfterCursor);
|
101 |
|
102 | if (entity.getMutability() !== 'MUTABLE') {
|
103 | var _getRemovalRange = getRemovalRange(chars, entityAfterCursor, offset),
|
104 | start = _getRemovalRange.start,
|
105 | end = _getRemovalRange.end;
|
106 |
|
107 | var current;
|
108 |
|
109 | while (start < end) {
|
110 | current = chars.get(start);
|
111 | chars = chars.set(start, CharacterMetadata.applyEntity(current, null));
|
112 | start++;
|
113 | }
|
114 |
|
115 | return block.set('characterList', chars);
|
116 | }
|
117 | }
|
118 |
|
119 | return block;
|
120 | }
|
121 |
|
122 | module.exports = removeEntitiesAtEdges; |
\ | No newline at end of file |