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.

keyCommandDeleteWord.js 1.4 KiB

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 moveSelectionForward = require("./moveSelectionForward");
  15. var 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) {
  21. var afterRemoval = removeTextWithStrategy(editorState, function (strategyState) {
  22. var selection = strategyState.getSelection();
  23. var offset = selection.getStartOffset();
  24. var key = selection.getStartKey();
  25. var content = strategyState.getCurrentContent();
  26. var text = content.getBlockForKey(key).getText().slice(offset);
  27. var 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;