UNPKG

4.16 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var ContentBlockNode = require("./ContentBlockNode");
14
15var generateRandomKey = require("./generateRandomKey");
16
17var Immutable = require("immutable");
18
19var invariant = require("fbjs/lib/invariant");
20
21var modifyBlockForContentState = require("./modifyBlockForContentState");
22
23var List = Immutable.List,
24 Map = Immutable.Map;
25
26var transformBlock = function transformBlock(key, blockMap, func) {
27 if (!key) {
28 return;
29 }
30
31 var block = blockMap.get(key);
32
33 if (!block) {
34 return;
35 }
36
37 blockMap.set(key, func(block));
38};
39
40var updateBlockMapLinks = function updateBlockMapLinks(blockMap, originalBlock, belowBlock) {
41 return blockMap.withMutations(function (blocks) {
42 var originalBlockKey = originalBlock.getKey();
43 var belowBlockKey = belowBlock.getKey(); // update block parent
44
45 transformBlock(originalBlock.getParentKey(), blocks, function (block) {
46 var parentChildrenList = block.getChildKeys();
47 var insertionIndex = parentChildrenList.indexOf(originalBlockKey) + 1;
48 var newChildrenArray = parentChildrenList.toArray();
49 newChildrenArray.splice(insertionIndex, 0, belowBlockKey);
50 return block.merge({
51 children: List(newChildrenArray)
52 });
53 }); // update original next block
54
55 transformBlock(originalBlock.getNextSiblingKey(), blocks, function (block) {
56 return block.merge({
57 prevSibling: belowBlockKey
58 });
59 }); // update original block
60
61 transformBlock(originalBlockKey, blocks, function (block) {
62 return block.merge({
63 nextSibling: belowBlockKey
64 });
65 }); // update below block
66
67 transformBlock(belowBlockKey, blocks, function (block) {
68 return block.merge({
69 prevSibling: originalBlockKey
70 });
71 });
72 });
73};
74
75var splitBlockInContentState = function splitBlockInContentState(contentState, selectionState) {
76 !selectionState.isCollapsed() ? process.env.NODE_ENV !== "production" ? invariant(false, 'Selection range must be collapsed.') : invariant(false) : void 0;
77 var key = selectionState.getAnchorKey();
78 var blockMap = contentState.getBlockMap();
79 var blockToSplit = blockMap.get(key);
80 var text = blockToSplit.getText();
81
82 if (!text) {
83 var blockType = blockToSplit.getType();
84
85 if (blockType === 'unordered-list-item' || blockType === 'ordered-list-item') {
86 return modifyBlockForContentState(contentState, selectionState, function (block) {
87 return block.merge({
88 type: 'unstyled',
89 depth: 0
90 });
91 });
92 }
93 }
94
95 var offset = selectionState.getAnchorOffset();
96 var chars = blockToSplit.getCharacterList();
97 var keyBelow = generateRandomKey();
98 var isExperimentalTreeBlock = blockToSplit instanceof ContentBlockNode;
99 var blockAbove = blockToSplit.merge({
100 text: text.slice(0, offset),
101 characterList: chars.slice(0, offset)
102 });
103 var blockBelow = blockAbove.merge({
104 key: keyBelow,
105 text: text.slice(offset),
106 characterList: chars.slice(offset),
107 data: Map()
108 });
109 var blocksBefore = blockMap.toSeq().takeUntil(function (v) {
110 return v === blockToSplit;
111 });
112 var blocksAfter = blockMap.toSeq().skipUntil(function (v) {
113 return v === blockToSplit;
114 }).rest();
115 var newBlocks = blocksBefore.concat([[key, blockAbove], [keyBelow, blockBelow]], blocksAfter).toOrderedMap();
116
117 if (isExperimentalTreeBlock) {
118 !blockToSplit.getChildKeys().isEmpty() ? process.env.NODE_ENV !== "production" ? invariant(false, 'ContentBlockNode must not have children') : invariant(false) : void 0;
119 newBlocks = updateBlockMapLinks(newBlocks, blockAbove, blockBelow);
120 }
121
122 return contentState.merge({
123 blockMap: newBlocks,
124 selectionBefore: selectionState,
125 selectionAfter: selectionState.merge({
126 anchorKey: keyBelow,
127 anchorOffset: 0,
128 focusKey: keyBelow,
129 focusOffset: 0,
130 isBackward: false
131 })
132 });
133};
134
135module.exports = splitBlockInContentState;
\No newline at end of file