From 8e93c30b3a6f0d7d0cf8651373d514354e6959b5 Mon Sep 17 00:00:00 2001 From: Ringish Date: Fri, 7 Feb 2020 04:07:08 +0100 Subject: [PATCH] Add/transform button to buttons (#20063) * Implements transform on buttons * DEV: Makes buttons transform multi block * Improves variable names * Adds some comments & imporoves code readability --- packages/block-library/src/buttons/index.js | 2 ++ .../block-library/src/buttons/transforms.js | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 packages/block-library/src/buttons/transforms.js diff --git a/packages/block-library/src/buttons/index.js b/packages/block-library/src/buttons/index.js index 211c07f4e43ad0..4777968dd1e710 100644 --- a/packages/block-library/src/buttons/index.js +++ b/packages/block-library/src/buttons/index.js @@ -7,6 +7,7 @@ import { button as icon } from '@wordpress/icons'; /** * Internal dependencies */ +import transforms from './transforms'; import edit from './edit'; import metadata from './block.json'; import save from './save'; @@ -26,6 +27,7 @@ export const settings = { align: true, alignWide: false, }, + transforms, edit, save, }; diff --git a/packages/block-library/src/buttons/transforms.js b/packages/block-library/src/buttons/transforms.js new file mode 100644 index 00000000000000..a558b580741d34 --- /dev/null +++ b/packages/block-library/src/buttons/transforms.js @@ -0,0 +1,32 @@ +/** + * WordPress dependencies + */ +import { createBlock } from '@wordpress/blocks'; + +/** + * Internal dependencies + */ +import { name } from './block.json'; + +const transforms = { + from: [ + { + type: 'block', + isMultiBlock: true, + blocks: [ 'core/button' ], + transform: ( buttons ) => + // Creates the buttons block + createBlock( + name, + {}, + // Loop the selected buttons + buttons.map( ( attributes ) => + // Create singular button in the buttons block + createBlock( 'core/button', attributes ) + ) + ), + }, + ], +}; + +export default transforms;