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

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