draft-js
Version:
A React framework for building text editors.
45 lines (35 loc) • 1.39 kB
JavaScript
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*
* @emails oncall+draft_js
*/
;
var DraftRemovableWord = require("./DraftRemovableWord");
var EditorState = require("./EditorState");
var moveSelectionForward = require("./moveSelectionForward");
var removeTextWithStrategy = require("./removeTextWithStrategy");
/**
* Delete the word that is right of the cursor, as well as any spaces or
* punctuation before the word.
*/
function keyCommandDeleteWord(editorState) {
var afterRemoval = removeTextWithStrategy(editorState, function (strategyState) {
var selection = strategyState.getSelection();
var offset = selection.getStartOffset();
var key = selection.getStartKey();
var content = strategyState.getCurrentContent();
var text = content.getBlockForKey(key).getText().slice(offset);
var toRemove = DraftRemovableWord.getForward(text); // If there are no words in front of the cursor, remove the newline.
return moveSelectionForward(strategyState, toRemove.length || 1);
}, 'forward');
if (afterRemoval === editorState.getCurrentContent()) {
return editorState;
}
return EditorState.push(editorState, afterRemoval, 'remove-range');
}
module.exports = keyCommandDeleteWord;