Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 82e2135

Browse files
Tee Xiefacebook-github-bot
Tee Xie
authored andcommitted
Fixed a bug causing block data being over-written when pasted.
Summary: there is a bug in Draft JS replace fragment logic where it replaces block data directly with the fragment's block data. That causes things like notice block loses it's data when pasted from other blocks. The diff introduces a new parameter to allow the data on the block to be merged instead of replaced. That way we could preserve the original data as well as merging from the fragment's data (if available). Note that in our use case, old data take precedence - for exmaple, if you paste from a "note" notice to a "warning" notice, the resulting notice should still be warning instead of "note". Differential Revision: D16167295 fbshipit-source-id: 6e525d2263014e0666928077a71d9d406bd68143
1 parent 0e88544 commit 82e2135

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/model/modifier/DraftModifier.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {DraftInlineStyle} from 'DraftInlineStyle';
1818
import type {DraftRemovalDirection} from 'DraftRemovalDirection';
1919
import type SelectionState from 'SelectionState';
2020
import type {Map} from 'immutable';
21+
import type {BlockDataMergeBehavior} from 'insertFragmentIntoContentState';
2122

2223
const CharacterMetadata = require('CharacterMetadata');
2324
const ContentStateInlineStyle = require('ContentStateInlineStyle');
@@ -118,6 +119,7 @@ const DraftModifier = {
118119
contentState: ContentState,
119120
targetRange: SelectionState,
120121
fragment: BlockMap,
122+
mergeBlockData?: BlockDataMergeBehavior = 'REPLACE_WITH_NEW_DATA',
121123
): ContentState {
122124
const withoutEntities = removeEntitiesAtEdges(contentState, targetRange);
123125
const withoutText = removeRangeFromContentState(
@@ -129,6 +131,7 @@ const DraftModifier = {
129131
withoutText,
130132
withoutText.getSelectionAfter(),
131133
fragment,
134+
mergeBlockData,
132135
);
133136
},
134137

src/model/transaction/insertFragmentIntoContentState.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@ const randomizeBlockMapKeys = require('randomizeBlockMapKeys');
2626

2727
const {List} = Immutable;
2828

29+
export type BlockDataMergeBehavior =
30+
| 'REPLACE_WITH_NEW_DATA'
31+
| 'MERGE_OLD_DATA_TO_NEW_DATA';
32+
2933
const updateExistingBlock = (
3034
contentState: ContentState,
3135
selectionState: SelectionState,
3236
blockMap: BlockMap,
3337
fragmentBlock: BlockNodeRecord,
3438
targetKey: string,
3539
targetOffset: number,
40+
mergeBlockData?: BlockDataMergeBehavior = 'REPLACE_WITH_NEW_DATA',
3641
): ContentState => {
3742
const targetBlock = blockMap.get(targetKey);
3843
const text = targetBlock.getText();
3944
const chars = targetBlock.getCharacterList();
4045
const finalKey = targetKey;
4146
const finalOffset = targetOffset + fragmentBlock.getText().length;
4247

48+
let data = null;
49+
50+
switch (mergeBlockData) {
51+
case 'MERGE_OLD_DATA_TO_NEW_DATA':
52+
data = fragmentBlock.getData().merge(targetBlock.getData());
53+
break;
54+
case 'REPLACE_WITH_NEW_DATA':
55+
data = fragmentBlock.getData();
56+
break;
57+
}
58+
4359
const newBlock = targetBlock.merge({
4460
text:
4561
text.slice(0, targetOffset) +
@@ -50,7 +66,7 @@ const updateExistingBlock = (
5066
fragmentBlock.getCharacterList(),
5167
targetOffset,
5268
),
53-
data: fragmentBlock.getData(),
69+
data,
5470
});
5571

5672
return contentState.merge({
@@ -290,6 +306,7 @@ const insertFragmentIntoContentState = (
290306
contentState: ContentState,
291307
selectionState: SelectionState,
292308
fragmentBlockMap: BlockMap,
309+
mergeBlockData?: BlockDataMergeBehavior = 'REPLACE_WITH_NEW_DATA',
293310
): ContentState => {
294311
invariant(
295312
selectionState.isCollapsed(),
@@ -320,6 +337,7 @@ const insertFragmentIntoContentState = (
320337
fragment.first(),
321338
targetKey,
322339
targetOffset,
340+
mergeBlockData,
323341
);
324342
}
325343

0 commit comments

Comments
 (0)