You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keyCommandBackspaceWord.js 1.5 KiB

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