Skip to content

Commit

Permalink
retainAttributes api in block's transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Mar 24, 2021
1 parent f8e8e76 commit 5f64abb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n';
import { useState, useMemo } from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose';
import { chevronRight } from '@wordpress/icons';
import { cloneBlock } from '@wordpress/blocks';
import { cloneBlock, getBlockType } from '@wordpress/blocks';
import {
MenuGroup,
MenuItem,
Expand Down Expand Up @@ -105,10 +105,29 @@ function PatternTransformationsMenu( {
transformedBlocksSet
);
if ( ! match ) return;
// Found a match so update it with the selected block's attributes.
// 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 blockType = getBlockType( block.name );
const retainAttributes =
blockType.transforms?.retainAttributes;
let retainedBlockAttributes = block.attributes;
if ( retainAttributes?.length ) {
retainedBlockAttributes = retainAttributes.reduce(
( _accumulator, attribute ) => {
if ( block.attributes[ attribute ] )
_accumulator[ attribute ] =
block.attributes[ attribute ];
return _accumulator;
},
{}
);
}
match.attributes = {
...match.attributes,
...block.attributes,
...retainedBlockAttributes,
};
// When we have a match with inner blocks keep only the
// blocks from the selected block and skip the inner blocks
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getLevelFromHeadingNodeName } from './shared';
import { name } from './block.json';

const transforms = {
retainAttributes: [ 'content' ],
from: [
{
type: 'block',
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/paragraph/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createBlock, getBlockAttributes } from '@wordpress/blocks';
import { name } from './block.json';

const transforms = {
retainAttributes: [ 'content' ],
from: [
{
type: 'raw',
Expand Down

0 comments on commit 5f64abb

Please sign in to comment.