選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

keyCommandPlainDelete.js.flow 1.6 KiB

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. * @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 moveSelectionForward = require("./moveSelectionForward");
  15. const 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: 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 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. const selection = editorState.getSelection();
  34. return EditorState.push(editorState, afterRemoval.set('selectionBefore', selection), selection.isCollapsed() ? 'delete-character' : 'remove-range');
  35. }
  36. module.exports = keyCommandPlainDelete;