From 48d2ff578099279d0da9093f1f82ca984bf19e9c Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 24 Mar 2021 13:35:03 +0200 Subject: [PATCH] try with `role: content` in block attributes --- .../pattern-transformations-menu.js | 27 ++++++++++--------- packages/block-library/src/heading/block.json | 3 ++- .../block-library/src/heading/transforms.js | 1 - .../block-library/src/paragraph/block.json | 3 ++- .../block-library/src/paragraph/transforms.js | 1 - packages/blocks/src/api/index.js | 1 + packages/blocks/src/api/utils.js | 21 +++++++++++++++ 7 files changed, 40 insertions(+), 17 deletions(-) diff --git a/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js b/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js index f2579cc69f3cc5..f7f5a7c745ff17 100644 --- a/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js +++ b/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js @@ -5,7 +5,10 @@ import { __ } from '@wordpress/i18n'; import { useState, useMemo } from '@wordpress/element'; import { useInstanceId } from '@wordpress/compose'; import { chevronRight } from '@wordpress/icons'; -import { cloneBlock, getBlockTransforms } from '@wordpress/blocks'; +import { + cloneBlock, + __experimentalGetBlockAttributesNamesByRole as getBlockAttributesNamesByRole, +} from '@wordpress/blocks'; import { MenuGroup, MenuItem, @@ -105,19 +108,17 @@ function PatternTransformationsMenu( { transformedBlocksSet ); if ( ! match ) return; - // Found a match, so get the `retainAttributes` from - // block type's transforms to retain these and keep - // everything else from the pattern's block. - // If `retainAttributes` are not set, update the match - // with all the selected block's attributes. - const retainAttributes = getBlockTransforms( - 'to', - block.name - ).find( ( { type } ) => type === 'pattern' ) - ?.retainAttributes; + // Found a match, so find and retain block attributes + // with `content` role. Everything else comes from the + // pattern's block. If no `content` attributes found, + // update the match with all the selected block's attributes. + const contentAttributes = getBlockAttributesNamesByRole( + block.name, + 'content' + ); let retainedBlockAttributes = block.attributes; - if ( retainAttributes?.length ) { - retainedBlockAttributes = retainAttributes.reduce( + if ( contentAttributes?.length ) { + retainedBlockAttributes = contentAttributes.reduce( ( _accumulator, attribute ) => { if ( block.attributes[ attribute ] ) _accumulator[ attribute ] = diff --git a/packages/block-library/src/heading/block.json b/packages/block-library/src/heading/block.json index 6cd496431f799d..b54ad9860ade11 100644 --- a/packages/block-library/src/heading/block.json +++ b/packages/block-library/src/heading/block.json @@ -10,7 +10,8 @@ "type": "string", "source": "html", "selector": "h1,h2,h3,h4,h5,h6", - "default": "" + "default": "", + "role": "content" }, "level": { "type": "number", diff --git a/packages/block-library/src/heading/transforms.js b/packages/block-library/src/heading/transforms.js index 17fcb2719e6055..8e6768c0d19d8a 100644 --- a/packages/block-library/src/heading/transforms.js +++ b/packages/block-library/src/heading/transforms.js @@ -91,7 +91,6 @@ const transforms = { } ) ), }, - { type: 'pattern', retainAttributes: [ 'content' ] }, ], }; diff --git a/packages/block-library/src/paragraph/block.json b/packages/block-library/src/paragraph/block.json index a29e22c01d5af3..f8c60a50df3901 100644 --- a/packages/block-library/src/paragraph/block.json +++ b/packages/block-library/src/paragraph/block.json @@ -10,7 +10,8 @@ "type": "string", "source": "html", "selector": "p", - "default": "" + "default": "", + "role": "content" }, "dropCap": { "type": "boolean", diff --git a/packages/block-library/src/paragraph/transforms.js b/packages/block-library/src/paragraph/transforms.js index e59b01ad14e3b9..5179a7f05c22fa 100644 --- a/packages/block-library/src/paragraph/transforms.js +++ b/packages/block-library/src/paragraph/transforms.js @@ -37,7 +37,6 @@ const transforms = { }, }, ], - to: [ { type: 'pattern', retainAttributes: [ 'content' ] } ], }; export default transforms; diff --git a/packages/blocks/src/api/index.js b/packages/blocks/src/api/index.js index 625377c0df3c0f..5fdd8aad97bdf6 100644 --- a/packages/blocks/src/api/index.js +++ b/packages/blocks/src/api/index.js @@ -141,6 +141,7 @@ export { getBlockLabel as __experimentalGetBlockLabel, getAccessibleBlockLabel as __experimentalGetAccessibleBlockLabel, __experimentalSanitizeBlockAttributes, + __experimentalGetBlockAttributesNamesByRole, } from './utils'; // Templates are, in a general sense, a basic collection of block nodes with any diff --git a/packages/blocks/src/api/utils.js b/packages/blocks/src/api/utils.js index c82515c4e5cd2a..98ddcf819bfc8c 100644 --- a/packages/blocks/src/api/utils.js +++ b/packages/blocks/src/api/utils.js @@ -275,3 +275,24 @@ export function __experimentalSanitizeBlockAttributes( name, attributes ) { {} ); } + +/** + * I created this wrapper to hide the complexity for the consumer.. + * + * @param {*} name + * @param {*} role + */ +// TODO jsdoc +// TODO tests +export function __experimentalGetBlockAttributesNamesByRole( name, role ) { + const attributes = getBlockType( name )?.attributes; + if ( ! attributes ) return; + if ( ! role ) return Object.keys( attributes ); + return Object.entries( attributes ).reduce( + ( accumulator, [ attributeName, schema ] ) => { + if ( schema?.role === role ) accumulator.push( attributeName ); + return accumulator; + }, + [] + ); +}