Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

79 wiersze
2.4 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. import type ContentState from "./ContentState";
  13. import type { DraftRemovalDirection } from "./DraftRemovalDirection";
  14. import type EditorState from "./EditorState";
  15. import type SelectionState from "./SelectionState";
  16. const DraftModifier = require("./DraftModifier");
  17. const gkx = require("./gkx");
  18. const experimentalTreeDataSupport = gkx('draft_tree_data_support');
  19. /**
  20. * For a collapsed selection state, remove text based on the specified strategy.
  21. * If the selection state is not collapsed, remove the entire selected range.
  22. */
  23. function removeTextWithStrategy(editorState: EditorState, strategy: (editorState: EditorState) => SelectionState, direction: DraftRemovalDirection): ContentState {
  24. const selection = editorState.getSelection();
  25. const content = editorState.getCurrentContent();
  26. let target = selection;
  27. const anchorKey = selection.getAnchorKey();
  28. const focusKey = selection.getFocusKey();
  29. const anchorBlock = content.getBlockForKey(anchorKey);
  30. if (experimentalTreeDataSupport) {
  31. if (direction === 'forward') {
  32. if (anchorKey !== focusKey) {
  33. // For now we ignore forward delete across blocks,
  34. // if there is demand for this we will implement it.
  35. return content;
  36. }
  37. }
  38. }
  39. if (selection.isCollapsed()) {
  40. if (direction === 'forward') {
  41. if (editorState.isSelectionAtEndOfContent()) {
  42. return content;
  43. }
  44. if (experimentalTreeDataSupport) {
  45. const isAtEndOfBlock = selection.getAnchorOffset() === content.getBlockForKey(anchorKey).getLength();
  46. if (isAtEndOfBlock) {
  47. const anchorBlockSibling = content.getBlockForKey(anchorBlock.nextSibling);
  48. if (!anchorBlockSibling || anchorBlockSibling.getLength() === 0) {
  49. // For now we ignore forward delete at the end of a block,
  50. // if there is demand for this we will implement it.
  51. return content;
  52. }
  53. }
  54. }
  55. } else if (editorState.isSelectionAtStartOfContent()) {
  56. return content;
  57. }
  58. target = strategy(editorState);
  59. if (target === selection) {
  60. return content;
  61. }
  62. }
  63. return DraftModifier.removeRange(content, target, direction);
  64. }
  65. module.exports = removeTextWithStrategy;