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.6 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 EditorState = require("./EditorState");
  13. const UnicodeUtils = require("fbjs/lib/UnicodeUtils");
  14. const moveSelectionBackward = require("./moveSelectionBackward");
  15. const removeTextWithStrategy = require("./removeTextWithStrategy");
  16. /**
  17. * Remove the selected range. If the cursor is collapsed, remove the preceding
  18. * character. This operation is Unicode-aware, so removing a single character
  19. * will remove a surrogate pair properly as well.
  20. */
  21. function keyCommandPlainBackspace(editorState: EditorState): EditorState {
  22. const afterRemoval = removeTextWithStrategy(editorState, strategyState => {
  23. const selection = strategyState.getSelection();
  24. const content = strategyState.getCurrentContent();
  25. const key = selection.getAnchorKey();
  26. const offset = selection.getAnchorOffset();
  27. const charBehind = content.getBlockForKey(key).getText()[offset - 1];
  28. return moveSelectionBackward(strategyState, charBehind ? UnicodeUtils.getUTF16Length(charBehind, 0) : 1);
  29. }, 'backward');
  30. if (afterRemoval === editorState.getCurrentContent()) {
  31. return editorState;
  32. }
  33. const selection = editorState.getSelection();
  34. return EditorState.push(editorState, afterRemoval.set('selectionBefore', selection), selection.isCollapsed() ? 'backspace-character' : 'remove-range');
  35. }
  36. module.exports = keyCommandPlainBackspace;