Skip to content

Commit

Permalink
Rich text: keep block ID on split (#28505)
Browse files Browse the repository at this point in the history
* Rich text: keep block ID on split

* typo
  • Loading branch information
ellatrix authored Feb 19, 2021
1 parent 70b1cde commit 4b9d13f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
10 changes: 8 additions & 2 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ function RichTextWrapper(
const hasPastedBlocks = pastedBlocks.length > 0;
let lastPastedBlockIndex = -1;

// Consider the after value to be the original it is not empty and
// the before value *is* empty.
const isAfterOriginal = isEmpty( before ) && ! isEmpty( after );

// Create a block with the content before the caret if there's no pasted
// blocks, or if there are pasted blocks and the value is not empty.
// We do not want a leading empty block on paste, but we do if split
Expand All @@ -307,7 +311,8 @@ function RichTextWrapper(
toHTMLString( {
value: before,
multilineTag,
} )
} ),
! isAfterOriginal
)
);
lastPastedBlockIndex += 1;
Expand All @@ -334,7 +339,8 @@ function RichTextWrapper(
toHTMLString( {
value: after,
multilineTag,
} )
} ),
isAfterOriginal
)
);
}
Expand Down
23 changes: 16 additions & 7 deletions packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function HeadingEdit( {
mergeBlocks,
onReplace,
mergedStyle,
clientId,
} ) {
const { textAlign, content, level, placeholder } = attributes;
const tagName = 'h' + level;
Expand Down Expand Up @@ -61,15 +62,23 @@ function HeadingEdit( {
value={ content }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( 'core/paragraph' );
onSplit={ ( value, isOriginal ) => {
let block;

if ( isOriginal || value ) {
block = createBlock( 'core/heading', {
...attributes,
content: value,
} );
} else {
block = createBlock( 'core/paragraph' );
}

if ( isOriginal ) {
block.clientId = clientId;
}

return createBlock( 'core/heading', {
...attributes,
content: value,
} );
return block;
} }
onReplace={ onReplace }
onRemove={ () => onReplace( [] ) }
Expand Down
23 changes: 16 additions & 7 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function ParagraphBlock( {
onReplace,
onRemove,
setAttributes,
clientId,
} ) {
const {
align,
Expand Down Expand Up @@ -148,15 +149,23 @@ function ParagraphBlock( {
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( name );
onSplit={ ( value, isOriginal ) => {
let newAttributes;

if ( isOriginal || value ) {
newAttributes = {
...attributes,
content: value,
};
}

const block = createBlock( name, newAttributes );

if ( isOriginal ) {
block.clientId = clientId;
}

return createBlock( name, {
...attributes,
content: value,
} );
return block;
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
Expand Down
23 changes: 16 additions & 7 deletions packages/block-library/src/paragraph/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function ParagraphBlock( {
setAttributes,
mergedStyle,
style,
clientId,
} ) {
const isRTL = useSelect( ( select ) => {
return !! select( blockEditorStore ).getSettings().isRTL;
Expand Down Expand Up @@ -56,15 +57,23 @@ function ParagraphBlock( {
content: nextContent,
} );
} }
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( name );
onSplit={ ( value, isOriginal ) => {
let newAttributes;

if ( isOriginal || value ) {
newAttributes = {
...attributes,
content: value,
};
}

return createBlock( name, {
...attributes,
content: value,
} );
const block = createBlock( name, newAttributes );

if ( isOriginal ) {
block.clientId = clientId;
}

return block;
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
Expand Down

0 comments on commit 4b9d13f

Please sign in to comment.