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

Commit 7d3d3c8

Browse files
Frank Thompsonfacebook-github-bot
authored andcommitted
use pasted block type if pasting to empty unstyled block
Summary: DraftJS's paste handling is a little bit weird right now. Suppose you have some arbitrary HTML that looks like this ``` <div> <h1>Some h1 content</h1> <span>Some other content</span> </div> ``` or a DraftJS editor with an H1 block and an unstyled block with the same contents. In either case, if you select both blocks of this content and paste into an empty editor, you get two blocks: one H1 and one unstyled. But if you select *only the first block* and paste into an empty editor, you get a single unstyled block. When you are inserting multiple blocks, `insertFragmentIntoContentState` checks whether to use the type of the target block or the pasted block based on whether the target block is empty: ``` return block.merge({ text: headText + appendToHead.getText(), characterList: headCharacters.concat(appendToHead.getCharacterList()), type: headText ? block.getType() : appendToHead.getType(), data: appendToHead.getData(), }); ``` https://our.intern.facebook.com/intern/diffusion/WWW/browse/master/html/shared/draft-js/model/transaction/insertFragmentIntoContentState.js?commit=1001924671&lines=105 But when inserting a single block, this logic is not used. This diff adds a similar check in `updateExistingBlock`, which handles the single block case. The difference is that the single block case **only** uses the inserted text's block type if the target block is **unstyled**. I did this because I thought it would be weird if you tried to paste text into a bulleted list or a block quote and it removed the styling. Reviewed By: mrkev Differential Revision: D20774564 fbshipit-source-id: c1490e17e5ecacea7891f9c7a5880f7eab8831e7
1 parent 8576432 commit 7d3d3c8

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/model/transaction/insertFragmentIntoContentState.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ const updateExistingBlock = (
5656
break;
5757
}
5858

59+
let type = targetBlock.getType();
60+
if (text && type === 'unstyled') {
61+
type = fragmentBlock.getType();
62+
}
63+
5964
const newBlock = targetBlock.merge({
6065
text:
6166
text.slice(0, targetOffset) +
@@ -66,6 +71,7 @@ const updateExistingBlock = (
6671
fragmentBlock.getCharacterList(),
6772
targetOffset,
6873
),
74+
type,
6975
data,
7076
});
7177

0 commit comments

Comments
 (0)