UNPKG

1.55 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 EditorState = require("./EditorState");
14
15var UnicodeUtils = require("fbjs/lib/UnicodeUtils");
16
17var moveSelectionBackward = require("./moveSelectionBackward");
18
19var removeTextWithStrategy = require("./removeTextWithStrategy");
20/**
21 * Remove the selected range. If the cursor is collapsed, remove the preceding
22 * character. This operation is Unicode-aware, so removing a single character
23 * will remove a surrogate pair properly as well.
24 */
25
26
27function keyCommandPlainBackspace(editorState) {
28 var afterRemoval = removeTextWithStrategy(editorState, function (strategyState) {
29 var selection = strategyState.getSelection();
30 var content = strategyState.getCurrentContent();
31 var key = selection.getAnchorKey();
32 var offset = selection.getAnchorOffset();
33 var charBehind = content.getBlockForKey(key).getText()[offset - 1];
34 return moveSelectionBackward(strategyState, charBehind ? UnicodeUtils.getUTF16Length(charBehind, 0) : 1);
35 }, 'backward');
36
37 if (afterRemoval === editorState.getCurrentContent()) {
38 return editorState;
39 }
40
41 var selection = editorState.getSelection();
42 return EditorState.push(editorState, afterRemoval.set('selectionBefore', selection), selection.isCollapsed() ? 'backspace-character' : 'remove-range');
43}
44
45module.exports = keyCommandPlainBackspace;
\No newline at end of file