UNPKG

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