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.5 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. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. var EditorState = require("./EditorState");
  13. var UnicodeUtils = require("fbjs/lib/UnicodeUtils");
  14. var moveSelectionForward = require("./moveSelectionForward");
  15. var removeTextWithStrategy = require("./removeTextWithStrategy");
  16. /**
  17. * Remove the selected range. If the cursor is collapsed, remove the following
  18. * character. This operation is Unicode-aware, so removing a single character
  19. * will remove a surrogate pair properly as well.
  20. */
  21. function keyCommandPlainDelete(editorState) {
  22. var afterRemoval = removeTextWithStrategy(editorState, function (strategyState) {
  23. var selection = strategyState.getSelection();
  24. var content = strategyState.getCurrentContent();
  25. var key = selection.getAnchorKey();
  26. var offset = selection.getAnchorOffset();
  27. var charAhead = content.getBlockForKey(key).getText()[offset];
  28. return moveSelectionForward(strategyState, charAhead ? UnicodeUtils.getUTF16Length(charAhead, 0) : 1);
  29. }, 'forward');
  30. if (afterRemoval === editorState.getCurrentContent()) {
  31. return editorState;
  32. }
  33. var selection = editorState.getSelection();
  34. return EditorState.push(editorState, afterRemoval.set('selectionBefore', selection), selection.isCollapsed() ? 'delete-character' : 'remove-range');
  35. }
  36. module.exports = keyCommandPlainDelete;