diff --git a/src/model/modifier/DraftModifier.js b/src/model/modifier/DraftModifier.js index 1d7b04114f..65d91e9cf7 100644 --- a/src/model/modifier/DraftModifier.js +++ b/src/model/modifier/DraftModifier.js @@ -18,6 +18,7 @@ import type {DraftInlineStyle} from 'DraftInlineStyle'; import type {DraftRemovalDirection} from 'DraftRemovalDirection'; import type SelectionState from 'SelectionState'; import type {Map} from 'immutable'; +import type {BlockDataMergeBehavior} from 'insertFragmentIntoContentState'; const CharacterMetadata = require('CharacterMetadata'); const ContentStateInlineStyle = require('ContentStateInlineStyle'); @@ -118,6 +119,7 @@ const DraftModifier = { contentState: ContentState, targetRange: SelectionState, fragment: BlockMap, + mergeBlockData?: BlockDataMergeBehavior = 'REPLACE_WITH_NEW_DATA', ): ContentState { const withoutEntities = removeEntitiesAtEdges(contentState, targetRange); const withoutText = removeRangeFromContentState( @@ -129,6 +131,7 @@ const DraftModifier = { withoutText, withoutText.getSelectionAfter(), fragment, + mergeBlockData, ); }, diff --git a/src/model/transaction/insertFragmentIntoContentState.js b/src/model/transaction/insertFragmentIntoContentState.js index bc618b2bbe..a96bf05865 100644 --- a/src/model/transaction/insertFragmentIntoContentState.js +++ b/src/model/transaction/insertFragmentIntoContentState.js @@ -26,6 +26,10 @@ const randomizeBlockMapKeys = require('randomizeBlockMapKeys'); const {List} = Immutable; +export type BlockDataMergeBehavior = + | 'REPLACE_WITH_NEW_DATA' + | 'MERGE_OLD_DATA_TO_NEW_DATA'; + const updateExistingBlock = ( contentState: ContentState, selectionState: SelectionState, @@ -33,6 +37,7 @@ const updateExistingBlock = ( fragmentBlock: BlockNodeRecord, targetKey: string, targetOffset: number, + mergeBlockData?: BlockDataMergeBehavior = 'REPLACE_WITH_NEW_DATA', ): ContentState => { const targetBlock = blockMap.get(targetKey); const text = targetBlock.getText(); @@ -40,6 +45,17 @@ const updateExistingBlock = ( const finalKey = targetKey; const finalOffset = targetOffset + fragmentBlock.getText().length; + let data = null; + + switch (mergeBlockData) { + case 'MERGE_OLD_DATA_TO_NEW_DATA': + data = fragmentBlock.getData().merge(targetBlock.getData()); + break; + case 'REPLACE_WITH_NEW_DATA': + data = fragmentBlock.getData(); + break; + } + const newBlock = targetBlock.merge({ text: text.slice(0, targetOffset) + @@ -50,7 +66,7 @@ const updateExistingBlock = ( fragmentBlock.getCharacterList(), targetOffset, ), - data: fragmentBlock.getData(), + data, }); return contentState.merge({ @@ -290,6 +306,7 @@ const insertFragmentIntoContentState = ( contentState: ContentState, selectionState: SelectionState, fragmentBlockMap: BlockMap, + mergeBlockData?: BlockDataMergeBehavior = 'REPLACE_WITH_NEW_DATA', ): ContentState => { invariant( selectionState.isCollapsed(), @@ -320,6 +337,7 @@ const insertFragmentIntoContentState = ( fragment.first(), targetKey, targetOffset, + mergeBlockData, ); }