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
/
splitBlockInContentState.js
166 lines (140 loc) · 4.15 KB
/
splitBlockInContentState.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
/**
* 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.
*
* @flow
* @format
* @oncall draft_js
*/
'use strict';
import type {BlockMap} from 'BlockMap';
import type ContentState from 'ContentState';
import type SelectionState from 'SelectionState';
const ContentBlockNode = require('ContentBlockNode');
const generateRandomKey = require('generateRandomKey');
const Immutable = require('immutable');
const invariant = require('invariant');
const modifyBlockForContentState = require('modifyBlockForContentState');
const {List, Map} = Immutable;
const transformBlock = (
key: ?string,
blockMap: BlockMap,
func: (block: ContentBlockNode) => ContentBlockNode,
): void => {
if (!key) {
return;
}
const block = blockMap.get(key);
if (!block) {
return;
}
blockMap.set(key, func(block));
};
const updateBlockMapLinks = (
blockMap: BlockMap,
originalBlock: ContentBlockNode,
belowBlock: ContentBlockNode,
): BlockMap => {
return blockMap.withMutations(blocks => {
const originalBlockKey = originalBlock.getKey();
const belowBlockKey = belowBlock.getKey();
// update block parent
transformBlock(originalBlock.getParentKey(), blocks, block => {
const parentChildrenList = block.getChildKeys();
const insertionIndex = parentChildrenList.indexOf(originalBlockKey) + 1;
const newChildrenArray = parentChildrenList.toArray();
newChildrenArray.splice(insertionIndex, 0, belowBlockKey);
return block.merge({
children: List(newChildrenArray),
});
});
// update original next block
transformBlock(originalBlock.getNextSiblingKey(), blocks, block =>
block.merge({
prevSibling: belowBlockKey,
}),
);
// update original block
transformBlock(originalBlockKey, blocks, block =>
block.merge({
nextSibling: belowBlockKey,
}),
);
// update below block
transformBlock(belowBlockKey, blocks, block =>
block.merge({
prevSibling: originalBlockKey,
}),
);
});
};
const splitBlockInContentState = (
contentState: ContentState,
selectionState: SelectionState,
): ContentState => {
invariant(selectionState.isCollapsed(), 'Selection range must be collapsed.');
const key = selectionState.getAnchorKey();
const blockMap = contentState.getBlockMap();
const blockToSplit = blockMap.get(key);
const text = blockToSplit.getText();
if (!text) {
const blockType = blockToSplit.getType();
if (
blockType === 'unordered-list-item' ||
blockType === 'ordered-list-item'
) {
return modifyBlockForContentState(contentState, selectionState, block =>
block.merge({type: 'unstyled', depth: 0}),
);
}
}
const offset = selectionState.getAnchorOffset();
const chars = blockToSplit.getCharacterList();
const keyBelow = generateRandomKey();
const isExperimentalTreeBlock = blockToSplit instanceof ContentBlockNode;
const blockAbove = blockToSplit.merge({
text: text.slice(0, offset),
characterList: chars.slice(0, offset),
});
const blockBelow = blockAbove.merge({
key: keyBelow,
text: text.slice(offset),
characterList: chars.slice(offset),
data: Map(),
});
const blocksBefore = blockMap.toSeq().takeUntil(v => v === blockToSplit);
const blocksAfter = blockMap
.toSeq()
.skipUntil(v => v === blockToSplit)
.rest();
let newBlocks = blocksBefore
.concat(
[
[key, blockAbove],
[keyBelow, blockBelow],
],
blocksAfter,
)
.toOrderedMap();
if (isExperimentalTreeBlock) {
invariant(
blockToSplit.getChildKeys().isEmpty(),
'ContentBlockNode must not have children',
);
newBlocks = updateBlockMapLinks(newBlocks, blockAbove, blockBelow);
}
return contentState.merge({
blockMap: newBlocks,
selectionBefore: selectionState,
selectionAfter: selectionState.merge({
anchorKey: keyBelow,
anchorOffset: 0,
focusKey: keyBelow,
focusOffset: 0,
isBackward: false,
}),
});
};
module.exports = splitBlockInContentState;