Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 CharacterMetadata = require("./CharacterMetadata");
  13. var ContentStateInlineStyle = require("./ContentStateInlineStyle");
  14. var applyEntityToContentState = require("./applyEntityToContentState");
  15. var getCharacterRemovalRange = require("./getCharacterRemovalRange");
  16. var getContentStateFragment = require("./getContentStateFragment");
  17. var Immutable = require("immutable");
  18. var insertFragmentIntoContentState = require("./insertFragmentIntoContentState");
  19. var insertTextIntoContentState = require("./insertTextIntoContentState");
  20. var invariant = require("fbjs/lib/invariant");
  21. var modifyBlockForContentState = require("./modifyBlockForContentState");
  22. var removeEntitiesAtEdges = require("./removeEntitiesAtEdges");
  23. var removeRangeFromContentState = require("./removeRangeFromContentState");
  24. var splitBlockInContentState = require("./splitBlockInContentState");
  25. var OrderedSet = Immutable.OrderedSet;
  26. /**
  27. * `DraftModifier` provides a set of convenience methods that apply
  28. * modifications to a `ContentState` object based on a target `SelectionState`.
  29. *
  30. * Any change to a `ContentState` should be decomposable into a series of
  31. * transaction functions that apply the required changes and return output
  32. * `ContentState` objects.
  33. *
  34. * These functions encapsulate some of the most common transaction sequences.
  35. */
  36. var DraftModifier = {
  37. replaceText: function replaceText(contentState, rangeToReplace, text, inlineStyle, entityKey) {
  38. var withoutEntities = removeEntitiesAtEdges(contentState, rangeToReplace);
  39. var withoutText = removeRangeFromContentState(withoutEntities, rangeToReplace);
  40. var character = CharacterMetadata.create({
  41. style: inlineStyle || OrderedSet(),
  42. entity: entityKey || null
  43. });
  44. return insertTextIntoContentState(withoutText, withoutText.getSelectionAfter(), text, character);
  45. },
  46. insertText: function insertText(contentState, targetRange, text, inlineStyle, entityKey) {
  47. !targetRange.isCollapsed() ? process.env.NODE_ENV !== "production" ? invariant(false, 'Target range must be collapsed for `insertText`.') : invariant(false) : void 0;
  48. return DraftModifier.replaceText(contentState, targetRange, text, inlineStyle, entityKey);
  49. },
  50. moveText: function moveText(contentState, removalRange, targetRange) {
  51. var movedFragment = getContentStateFragment(contentState, removalRange);
  52. var afterRemoval = DraftModifier.removeRange(contentState, removalRange, 'backward');
  53. return DraftModifier.replaceWithFragment(afterRemoval, targetRange, movedFragment);
  54. },
  55. replaceWithFragment: function replaceWithFragment(contentState, targetRange, fragment) {
  56. var mergeBlockData = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'REPLACE_WITH_NEW_DATA';
  57. var withoutEntities = removeEntitiesAtEdges(contentState, targetRange);
  58. var withoutText = removeRangeFromContentState(withoutEntities, targetRange);
  59. return insertFragmentIntoContentState(withoutText, withoutText.getSelectionAfter(), fragment, mergeBlockData);
  60. },
  61. removeRange: function removeRange(contentState, rangeToRemove, removalDirection) {
  62. var startKey, endKey, startBlock, endBlock;
  63. if (rangeToRemove.getIsBackward()) {
  64. rangeToRemove = rangeToRemove.merge({
  65. anchorKey: rangeToRemove.getFocusKey(),
  66. anchorOffset: rangeToRemove.getFocusOffset(),
  67. focusKey: rangeToRemove.getAnchorKey(),
  68. focusOffset: rangeToRemove.getAnchorOffset(),
  69. isBackward: false
  70. });
  71. }
  72. startKey = rangeToRemove.getAnchorKey();
  73. endKey = rangeToRemove.getFocusKey();
  74. startBlock = contentState.getBlockForKey(startKey);
  75. endBlock = contentState.getBlockForKey(endKey);
  76. var startOffset = rangeToRemove.getStartOffset();
  77. var endOffset = rangeToRemove.getEndOffset();
  78. var startEntityKey = startBlock.getEntityAt(startOffset);
  79. var endEntityKey = endBlock.getEntityAt(endOffset - 1); // Check whether the selection state overlaps with a single entity.
  80. // If so, try to remove the appropriate substring of the entity text.
  81. if (startKey === endKey) {
  82. if (startEntityKey && startEntityKey === endEntityKey) {
  83. var adjustedRemovalRange = getCharacterRemovalRange(contentState.getEntityMap(), startBlock, endBlock, rangeToRemove, removalDirection);
  84. return removeRangeFromContentState(contentState, adjustedRemovalRange);
  85. }
  86. }
  87. var withoutEntities = removeEntitiesAtEdges(contentState, rangeToRemove);
  88. return removeRangeFromContentState(withoutEntities, rangeToRemove);
  89. },
  90. splitBlock: function splitBlock(contentState, selectionState) {
  91. var withoutEntities = removeEntitiesAtEdges(contentState, selectionState);
  92. var withoutText = removeRangeFromContentState(withoutEntities, selectionState);
  93. return splitBlockInContentState(withoutText, withoutText.getSelectionAfter());
  94. },
  95. applyInlineStyle: function applyInlineStyle(contentState, selectionState, inlineStyle) {
  96. return ContentStateInlineStyle.add(contentState, selectionState, inlineStyle);
  97. },
  98. removeInlineStyle: function removeInlineStyle(contentState, selectionState, inlineStyle) {
  99. return ContentStateInlineStyle.remove(contentState, selectionState, inlineStyle);
  100. },
  101. setBlockType: function setBlockType(contentState, selectionState, blockType) {
  102. return modifyBlockForContentState(contentState, selectionState, function (block) {
  103. return block.merge({
  104. type: blockType,
  105. depth: 0
  106. });
  107. });
  108. },
  109. setBlockData: function setBlockData(contentState, selectionState, blockData) {
  110. return modifyBlockForContentState(contentState, selectionState, function (block) {
  111. return block.merge({
  112. data: blockData
  113. });
  114. });
  115. },
  116. mergeBlockData: function mergeBlockData(contentState, selectionState, blockData) {
  117. return modifyBlockForContentState(contentState, selectionState, function (block) {
  118. return block.merge({
  119. data: block.getData().merge(blockData)
  120. });
  121. });
  122. },
  123. applyEntity: function applyEntity(contentState, selectionState, entityKey) {
  124. var withoutEntities = removeEntitiesAtEdges(contentState, selectionState);
  125. return applyEntityToContentState(withoutEntities, selectionState, entityKey);
  126. }
  127. };
  128. module.exports = DraftModifier;