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.
 
 
 
 

45 lines
1.4 KiB

  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. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. const DraftRemovableWord = require("./DraftRemovableWord");
  13. const EditorState = require("./EditorState");
  14. const moveSelectionForward = require("./moveSelectionForward");
  15. const removeTextWithStrategy = require("./removeTextWithStrategy");
  16. /**
  17. * Delete the word that is right of the cursor, as well as any spaces or
  18. * punctuation before the word.
  19. */
  20. function keyCommandDeleteWord(editorState: EditorState): EditorState {
  21. const afterRemoval = removeTextWithStrategy(editorState, strategyState => {
  22. const selection = strategyState.getSelection();
  23. const offset = selection.getStartOffset();
  24. const key = selection.getStartKey();
  25. const content = strategyState.getCurrentContent();
  26. const text = content.getBlockForKey(key).getText().slice(offset);
  27. const toRemove = DraftRemovableWord.getForward(text); // If there are no words in front of the cursor, remove the newline.
  28. return moveSelectionForward(strategyState, toRemove.length || 1);
  29. }, 'forward');
  30. if (afterRemoval === editorState.getCurrentContent()) {
  31. return editorState;
  32. }
  33. return EditorState.push(editorState, afterRemoval, 'remove-range');
  34. }
  35. module.exports = keyCommandDeleteWord;