Skip to content

Commit

Permalink
try with role: content in block attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Mar 24, 2021
1 parent 0f1cc33 commit 48d2ff5
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ] =
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"type": "string",
"source": "html",
"selector": "h1,h2,h3,h4,h5,h6",
"default": ""
"default": "",
"role": "content"
},
"level": {
"type": "number",
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ const transforms = {
} )
),
},
{ type: 'pattern', retainAttributes: [ 'content' ] },
],
};

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"type": "string",
"source": "html",
"selector": "p",
"default": ""
"default": "",
"role": "content"
},
"dropCap": {
"type": "boolean",
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/paragraph/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const transforms = {
},
},
],
to: [ { type: 'pattern', retainAttributes: [ 'content' ] } ],
};

export default transforms;
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
[]
);
}

0 comments on commit 48d2ff5

Please sign in to comment.