Skip to content

Commit

Permalink
Update heading block behaviour on ENTER key to create empty paragraph…
Browse files Browse the repository at this point in the history
… instead of heading (WordPress#10326)

The onSplit method for the heading block will now replace an empty heading block with an empty paragraph block for the 'before' content.

This allows the ENTER key pressed at the beginning of a heading to create an empty paragraph above it. Also, when the ENTER key is pressed at the end of a heading, an empty paragraph block is created below, instead of another heading block. The RichText component now also calls event.stopImmediatePropagation() before splitContent() to prevent TinyMCE from throwing an error. This avoids a similar error as in WordPress#4580.
  • Loading branch information
andrewserong committed Feb 13, 2019
1 parent 73683aa commit 91daa21
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ export default function HeadingEdit( {
insertBlocksAfter ?
( before, after, ...blocks ) => {
setAttributes( { content: before } );
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
] );
if ( ! before.text && after.text ) {
onReplace( createBlock( 'core/paragraph', { content: before } ) );
}
if ( after.text ) {
insertBlocksAfter( [
...blocks,
createBlock( 'core/heading', { content: after } ),
] );
} else {
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
] );
}
} :
undefined
}
Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,11 @@ export class RichText extends Component {
} else if ( event.shiftKey || ! this.onSplit ) {
this.onChange( insertLineBreak( record ) );
} else {
// Some implementations of onSplit might also call onReplace, and
// calling onReplace() will destroy the editor, so it's
// important that we stop other handlers (e.g. ones
// registered by TinyMCE) from also handling this event.
event.stopImmediatePropagation();
this.splitContent();
}
}
Expand Down

0 comments on commit 91daa21

Please sign in to comment.