Skip to content

Commit 817802d

Browse files
committed
Switch blocks before merging head/tail after paste
1 parent 0219951 commit 817802d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

packages/block-editor/src/store/actions.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,15 +1062,16 @@ export const __unstableSplitSelection =
10621062
const clonedBlocks = [ ...blocks ];
10631063
const firstBlock = clonedBlocks.shift();
10641064
const headType = getBlockType( head.name );
1065+
const firstBlocks =
1066+
headType.merge && switchToBlockType( firstBlock, headType.name );
10651067

1066-
if ( head.name === firstBlock.name && headType.merge ) {
1068+
if ( firstBlocks?.length ) {
1069+
const first = firstBlocks.shift();
10671070
output.push( {
10681071
...head,
1069-
attributes: headType.merge(
1070-
head.attributes,
1071-
firstBlock.attributes
1072-
),
1072+
attributes: headType.merge( head.attributes, first.attributes ),
10731073
} );
1074+
clonedBlocks.unshift( ...firstBlocks );
10741075
} else {
10751076
if ( ! isUnmodifiedBlock( head ) ) {
10761077
output.push( head );
@@ -1086,14 +1087,19 @@ export const __unstableSplitSelection =
10861087
}
10871088

10881089
if ( lastBlock ) {
1089-
if ( lastBlock.name === tail.name && tailType.merge ) {
1090+
const lastBlocks =
1091+
tailType.merge && switchToBlockType( lastBlock, tailType.name );
1092+
1093+
if ( lastBlocks?.length ) {
1094+
const last = lastBlocks.pop();
10901095
output.push( {
10911096
...tail,
10921097
attributes: tailType.merge(
1093-
lastBlock.attributes,
1098+
last.attributes,
10941099
tail.attributes
10951100
),
10961101
} );
1102+
output.push( ...lastBlocks );
10971103
offset = create( {
10981104
html: lastBlock.attributes[ selectionB.attributeKey ],
10991105
} ).text.length;

packages/blocks/src/api/factory.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,12 @@ function maybeCheckTransformIsMatch( transform, blocks ) {
457457
*/
458458
export function switchToBlockType( blocks, name ) {
459459
const blocksArray = Array.isArray( blocks ) ? blocks : [ blocks ];
460+
461+
// Return early if all blocks are already of the correct block type.
462+
if ( blocksArray.every( ( block ) => block.name === name ) ) {
463+
return blocksArray;
464+
}
465+
460466
const isMultiBlock = blocksArray.length > 1;
461467
const firstBlock = blocksArray[ 0 ];
462468
const sourceName = firstBlock.name;

0 commit comments

Comments
 (0)