Skip to content

Commit

Permalink
Writing flow: split heading into default block (#61891)
Browse files Browse the repository at this point in the history
Unlinked contributors: erikiva.

Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
  • Loading branch information
3 people committed Jun 18, 2024
1 parent 780b94f commit 580c1fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 21 additions & 1 deletion packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ export const __unstableSplitSelection =
},
};

const tail = {
let tail = {
...blockB,
// Only preserve the original client ID if the end is different.
clientId:
Expand All @@ -995,6 +995,26 @@ export const __unstableSplitSelection =
},
};

// When splitting a block, attempt to convert the tail block to the
// default block type. For example, when splitting a heading block, the
// tail block will be converted to a paragraph block. Note that for
// blocks such as a list item and button, this will be skipped because
// the default block type cannot be inserted.
const defaultBlockName = getDefaultBlockName();
if (
// A block is only split when the selection is within the same
// block.
blockA.clientId === blockB.clientId &&
defaultBlockName &&
tail.name !== defaultBlockName &&
select.canInsertBlockType( defaultBlockName, anchorRootClientId )
) {
const switched = switchToBlockType( tail, defaultBlockName );
if ( switched?.length === 1 ) {
tail = switched[ 0 ];
}
}

if ( ! blocks.length ) {
dispatch.replaceBlocks( select.getSelectedBlockClientIds(), [
head,
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/specs/editor/various/copy-cut-paste.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,9 @@ test.describe( 'Copy/cut/paste', () => {
},
},
{
name: 'core/heading',
name: 'core/paragraph',
attributes: {
content: 'bB',
level: 2,
},
},
] );
Expand Down

0 comments on commit 580c1fe

Please sign in to comment.