No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

getCharacterRemovalRange.js 4.3 KiB

hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 DraftEntitySegments = require("./DraftEntitySegments");
  13. var getRangesForDraftEntity = require("./getRangesForDraftEntity");
  14. var invariant = require("fbjs/lib/invariant");
  15. /**
  16. * Given a SelectionState and a removal direction, determine the entire range
  17. * that should be removed from a ContentState. This is based on any entities
  18. * within the target, with their `mutability` values taken into account.
  19. *
  20. * For instance, if we are attempting to remove part of an "immutable" entity
  21. * range, the entire entity must be removed. The returned `SelectionState`
  22. * will be adjusted accordingly.
  23. */
  24. function getCharacterRemovalRange(entityMap, startBlock, endBlock, selectionState, direction) {
  25. var start = selectionState.getStartOffset();
  26. var end = selectionState.getEndOffset();
  27. var startEntityKey = startBlock.getEntityAt(start);
  28. var endEntityKey = endBlock.getEntityAt(end - 1);
  29. if (!startEntityKey && !endEntityKey) {
  30. return selectionState;
  31. }
  32. var newSelectionState = selectionState;
  33. if (startEntityKey && startEntityKey === endEntityKey) {
  34. newSelectionState = getEntityRemovalRange(entityMap, startBlock, newSelectionState, direction, startEntityKey, true, true);
  35. } else if (startEntityKey && endEntityKey) {
  36. var startSelectionState = getEntityRemovalRange(entityMap, startBlock, newSelectionState, direction, startEntityKey, false, true);
  37. var endSelectionState = getEntityRemovalRange(entityMap, endBlock, newSelectionState, direction, endEntityKey, false, false);
  38. newSelectionState = newSelectionState.merge({
  39. anchorOffset: startSelectionState.getAnchorOffset(),
  40. focusOffset: endSelectionState.getFocusOffset(),
  41. isBackward: false
  42. });
  43. } else if (startEntityKey) {
  44. var _startSelectionState = getEntityRemovalRange(entityMap, startBlock, newSelectionState, direction, startEntityKey, false, true);
  45. newSelectionState = newSelectionState.merge({
  46. anchorOffset: _startSelectionState.getStartOffset(),
  47. isBackward: false
  48. });
  49. } else if (endEntityKey) {
  50. var _endSelectionState = getEntityRemovalRange(entityMap, endBlock, newSelectionState, direction, endEntityKey, false, false);
  51. newSelectionState = newSelectionState.merge({
  52. focusOffset: _endSelectionState.getEndOffset(),
  53. isBackward: false
  54. });
  55. }
  56. return newSelectionState;
  57. }
  58. function getEntityRemovalRange(entityMap, block, selectionState, direction, entityKey, isEntireSelectionWithinEntity, isEntityAtStart) {
  59. var start = selectionState.getStartOffset();
  60. var end = selectionState.getEndOffset();
  61. var entity = entityMap.__get(entityKey);
  62. var mutability = entity.getMutability();
  63. var sideToConsider = isEntityAtStart ? start : end; // `MUTABLE` entities can just have the specified range of text removed
  64. // directly. No adjustments are needed.
  65. if (mutability === 'MUTABLE') {
  66. return selectionState;
  67. } // Find the entity range that overlaps with our removal range.
  68. var entityRanges = getRangesForDraftEntity(block, entityKey).filter(function (range) {
  69. return sideToConsider <= range.end && sideToConsider >= range.start;
  70. });
  71. !(entityRanges.length == 1) ? process.env.NODE_ENV !== "production" ? invariant(false, 'There should only be one entity range within this removal range.') : invariant(false) : void 0;
  72. var entityRange = entityRanges[0]; // For `IMMUTABLE` entity types, we will remove the entire entity range.
  73. if (mutability === 'IMMUTABLE') {
  74. return selectionState.merge({
  75. anchorOffset: entityRange.start,
  76. focusOffset: entityRange.end,
  77. isBackward: false
  78. });
  79. } // For `SEGMENTED` entity types, determine the appropriate segment to
  80. // remove.
  81. if (!isEntireSelectionWithinEntity) {
  82. if (isEntityAtStart) {
  83. end = entityRange.end;
  84. } else {
  85. start = entityRange.start;
  86. }
  87. }
  88. var removalRange = DraftEntitySegments.getRemovalRange(start, end, block.getText().slice(entityRange.start, entityRange.end), entityRange.start, direction);
  89. return selectionState.merge({
  90. anchorOffset: removalRange.start,
  91. focusOffset: removalRange.end,
  92. isBackward: false
  93. });
  94. }
  95. module.exports = getCharacterRemovalRange;