Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

keyCommandDeleteWord.js.flow 1.4 KiB

3 anni fa
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 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;