UNPKG

2.15 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 DraftModifier = require("./DraftModifier");
14
15var gkx = require("./gkx");
16
17var experimentalTreeDataSupport = gkx('draft_tree_data_support');
18/**
19 * For a collapsed selection state, remove text based on the specified strategy.
20 * If the selection state is not collapsed, remove the entire selected range.
21 */
22
23function removeTextWithStrategy(editorState, strategy, direction) {
24 var selection = editorState.getSelection();
25 var content = editorState.getCurrentContent();
26 var target = selection;
27 var anchorKey = selection.getAnchorKey();
28 var focusKey = selection.getFocusKey();
29 var anchorBlock = content.getBlockForKey(anchorKey);
30
31 if (experimentalTreeDataSupport) {
32 if (direction === 'forward') {
33 if (anchorKey !== focusKey) {
34 // For now we ignore forward delete across blocks,
35 // if there is demand for this we will implement it.
36 return content;
37 }
38 }
39 }
40
41 if (selection.isCollapsed()) {
42 if (direction === 'forward') {
43 if (editorState.isSelectionAtEndOfContent()) {
44 return content;
45 }
46
47 if (experimentalTreeDataSupport) {
48 var isAtEndOfBlock = selection.getAnchorOffset() === content.getBlockForKey(anchorKey).getLength();
49
50 if (isAtEndOfBlock) {
51 var anchorBlockSibling = content.getBlockForKey(anchorBlock.nextSibling);
52
53 if (!anchorBlockSibling || anchorBlockSibling.getLength() === 0) {
54 // For now we ignore forward delete at the end of a block,
55 // if there is demand for this we will implement it.
56 return content;
57 }
58 }
59 }
60 } else if (editorState.isSelectionAtStartOfContent()) {
61 return content;
62 }
63
64 target = strategy(editorState);
65
66 if (target === selection) {
67 return content;
68 }
69 }
70
71 return DraftModifier.removeRange(content, target, direction);
72}
73
74module.exports = removeTextWithStrategy;
\No newline at end of file