UNPKG

1.5 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 DraftRemovableWord = require("./DraftRemovableWord");
14
15var EditorState = require("./EditorState");
16
17var moveSelectionBackward = require("./moveSelectionBackward");
18
19var removeTextWithStrategy = require("./removeTextWithStrategy");
20/**
21 * Delete the word that is left of the cursor, as well as any spaces or
22 * punctuation after the word.
23 */
24
25
26function keyCommandBackspaceWord(editorState) {
27 var afterRemoval = removeTextWithStrategy(editorState, function (strategyState) {
28 var selection = strategyState.getSelection();
29 var offset = selection.getStartOffset(); // If there are no words before the cursor, remove the preceding newline.
30
31 if (offset === 0) {
32 return moveSelectionBackward(strategyState, 1);
33 }
34
35 var key = selection.getStartKey();
36 var content = strategyState.getCurrentContent();
37 var text = content.getBlockForKey(key).getText().slice(0, offset);
38 var toRemove = DraftRemovableWord.getBackward(text);
39 return moveSelectionBackward(strategyState, toRemove.length || 1);
40 }, 'backward');
41
42 if (afterRemoval === editorState.getCurrentContent()) {
43 return editorState;
44 }
45
46 return EditorState.push(editorState, afterRemoval, 'remove-range');
47}
48
49module.exports = keyCommandBackspaceWord;
\No newline at end of file