From 894286b3c01a3ba75933cde2a4beff7ef9dc7c85 Mon Sep 17 00:00:00 2001 From: Ella van Durpe Date: Mon, 18 May 2020 21:37:29 +0200 Subject: [PATCH 1/6] Buttons: allow split and merge --- packages/block-library/src/button/edit.js | 23 +++++++++++++++++++++- packages/block-library/src/button/index.js | 3 +++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index 46c1223cfbf6e..0bee117117a6d 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -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 @@ -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, @@ -183,6 +191,19 @@ function ButtonEdit( props ) { : undefined, ...colorProps.style, } } + onSplit={ ( value ) => { + if ( ! value ) { + return createBlock( 'core/button' ); + } + + return createBlock( 'core/button', { + ...attributes, + text: value, + } ); + } } + onReplace={ onReplace } + onMerge={ mergeBlocks } + identifier="text" /> ( { + text: textA + textB, + } ), }; From 88839406936bc08f9720d65a64e43092c53b1053 Mon Sep 17 00:00:00 2001 From: Ella van Durpe Date: Tue, 19 May 2020 14:05:34 +0200 Subject: [PATCH 2/6] Don't add empty block on paste --- .../block-editor/src/components/rich-text/index.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/index.js b/packages/block-editor/src/components/rich-text/index.js index 2d1a7eced0730..4b68923c534c8 100644 --- a/packages/block-editor/src/components/rich-text/index.js +++ b/packages/block-editor/src/components/rich-text/index.js @@ -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 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 + ? ! isEmpty( after ) + : ! onSplitMiddle || ! isEmpty( after ) + ) { blocks.push( onSplit( toHTMLString( { From 1634d31ad823c80719d262a40a34573188cc05ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Tue, 19 May 2020 16:37:02 +0200 Subject: [PATCH 3/6] Update packages/block-editor/src/components/rich-text/index.js Co-authored-by: Miguel Fonseca --- packages/block-editor/src/components/rich-text/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/rich-text/index.js b/packages/block-editor/src/components/rich-text/index.js index 4b68923c534c8..e936b4d77724a 100644 --- a/packages/block-editor/src/components/rich-text/index.js +++ b/packages/block-editor/src/components/rich-text/index.js @@ -304,7 +304,7 @@ function RichTextWrapper( } // If there's pasted blocks, append a block with non empty content - /// after the caret. Otherwise, do append and empty block if there + /// 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 ( From 7962d92f5ea0b8b72c34cf74e7c28b823ad2aa9f Mon Sep 17 00:00:00 2001 From: Ella van Durpe Date: Tue, 19 May 2020 16:48:48 +0200 Subject: [PATCH 4/6] Keep all attrs on split --- packages/block-library/src/button/edit.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index 0bee117117a6d..b6280d0a81f56 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -191,16 +191,12 @@ function ButtonEdit( props ) { : undefined, ...colorProps.style, } } - onSplit={ ( value ) => { - if ( ! value ) { - return createBlock( 'core/button' ); - } - - return createBlock( 'core/button', { + onSplit={ ( value ) => + createBlock( 'core/button', { ...attributes, text: value, - } ); - } } + } ) + } onReplace={ onReplace } onMerge={ mergeBlocks } identifier="text" From 1ec6662dd83846fad5f1762cf3c68e0bd99e31f0 Mon Sep 17 00:00:00 2001 From: Ella van Durpe Date: Tue, 19 May 2020 16:50:35 +0200 Subject: [PATCH 5/6] Keep attrs on merge --- packages/block-library/src/button/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/button/index.js b/packages/block-library/src/button/index.js index b22579acaa326..b32769c1b554d 100644 --- a/packages/block-library/src/button/index.js +++ b/packages/block-library/src/button/index.js @@ -44,7 +44,8 @@ export const settings = { edit, save, deprecated, - merge: ( { text: textA = '' }, { text: textB = '' } ) => ( { - text: textA + textB, + merge: ( a, { text = '' } ) => ( { + ...a, + text: a.text + text, } ), }; From 1f0f7edc2a785ac7a02eb3745b3f951988af9bee Mon Sep 17 00:00:00 2001 From: Ella van Durpe Date: Wed, 20 May 2020 14:12:01 +0200 Subject: [PATCH 6/6] Adjust paste e2e tests --- .../copy-cut-paste-whole-blocks.test.js.snap | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/e2e-tests/specs/editor/various/__snapshots__/copy-cut-paste-whole-blocks.test.js.snap b/packages/e2e-tests/specs/editor/various/__snapshots__/copy-cut-paste-whole-blocks.test.js.snap index 519d50e98f504..d89e3354fc5dd 100644 --- a/packages/e2e-tests/specs/editor/various/__snapshots__/copy-cut-paste-whole-blocks.test.js.snap +++ b/packages/e2e-tests/specs/editor/various/__snapshots__/copy-cut-paste-whole-blocks.test.js.snap @@ -21,10 +21,6 @@ exports[`Multi-block selection should copy and paste individual blocks 2`] = `

Here is a unique string so we can test copying.

- - - -

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

Yet another unique string.

- - - -

" `;