Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buttons: allow split and merge #22436

Merged
merged 6 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,15 @@ function RichTextWrapper(
blocks.push( onSplitMiddle() );
}

// If there's pasted blocks, append a block with the content after the
// caret. Otherwise, do append and empty block if there is no
// `onSplitMiddle` prop, but if there is and the content is empty, the
// middle block is enough to set focus in.
if ( hasPastedBlocks || ! onSplitMiddle || ! isEmpty( after ) ) {
// If there's pasted blocks, append a block with non empty content
/// after the caret. Otherwise, do append an empty block if there
// is no `onSplitMiddle` prop, but if there is and the content is
// empty, the middle block is enough to set focus in.
if (
hasPastedBlocks
? ! isEmpty( after )
: ! onSplitMiddle || ! isEmpty( after )
) {
blocks.push(
onSplit(
toHTMLString( {
Expand Down
19 changes: 18 additions & 1 deletion packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '@wordpress/block-editor';
import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { link } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -123,7 +124,14 @@ function URLPicker( {
}

function ButtonEdit( props ) {
const { attributes, setAttributes, className, isSelected } = props;
const {
attributes,
setAttributes,
className,
isSelected,
onReplace,
mergeBlocks,
} = props;
const {
borderRadius,
linkTarget,
Expand Down Expand Up @@ -183,6 +191,15 @@ function ButtonEdit( props ) {
: undefined,
...colorProps.style,
} }
onSplit={ ( value ) =>
createBlock( 'core/button', {
...attributes,
text: value,
} )
}
onReplace={ onReplace }
onMerge={ mergeBlocks }
identifier="text"
/>
</Block.div>
<URLPicker
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ export const settings = {
edit,
save,
deprecated,
merge: ( a, { text = '' } ) => ( {
...a,
text: a.text + text,
} ),
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ exports[`Multi-block selection should copy and paste individual blocks 2`] = `

<!-- wp:paragraph -->
<p>Here is a unique string so we can test copying.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->"
`;

Expand All @@ -41,10 +37,6 @@ exports[`Multi-block selection should cut and paste individual blocks 2`] = `

<!-- wp:paragraph -->
<p>Yet another unique string.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->"
`;

Expand Down