Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove removeAttributesByRole
  • Loading branch information
stacimc committed Feb 1, 2022
commit 2a1afa4f40475f34c9a3dd0c8494a485d12a377a
10 changes: 5 additions & 5 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isEmpty,
map,
reduce,
omit,
} from 'lodash';

/**
Expand All @@ -33,7 +34,7 @@ import {
} from './registration';
import {
normalizeBlockType,
__experimentalRemoveAttributesByRole,
__experimentalGetBlockAttributesNamesByRole,
} from './utils';

/**
Expand Down Expand Up @@ -142,19 +143,18 @@ export function __experimentalCloneSanitizedBlock(

// Merge in new attributes and strip out attributes with the `internal` role,
// which should not be copied during block duplication.
const retainedAttributes = __experimentalRemoveAttributesByRole(
block.name,
const filteredAttributes = omit(
{
...block.attributes,
...mergeAttributes,
},
'internal'
__experimentalGetBlockAttributesNamesByRole( block.name, 'internal' )
);

return {
...block,
clientId,
attributes: retainedAttributes,
attributes: filteredAttributes,
innerBlocks:
newInnerBlocks ||
block.innerBlocks.map( ( innerBlock ) =>
Expand Down
1 change: 0 additions & 1 deletion packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export {
getBlockLabel as __experimentalGetBlockLabel,
getAccessibleBlockLabel as __experimentalGetAccessibleBlockLabel,
__experimentalGetBlockAttributesNamesByRole,
__experimentalRemoveAttributesByRole,
} from './utils';

// Templates are, in a general sense, a basic collection of block nodes with any
Expand Down
82 changes: 0 additions & 82 deletions packages/blocks/src/api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getAccessibleBlockLabel,
getBlockLabel,
__experimentalGetBlockAttributesNamesByRole,
__experimentalRemoveAttributesByRole,
} from '../utils';

describe( 'block helpers', () => {
Expand Down Expand Up @@ -302,84 +301,3 @@ describe( '__experimentalGetBlockAttributesNamesByRole', () => {
).toEqual( [] );
} );
} );

describe( '__experimentalRemoveAttributesByRole', () => {
beforeAll( () => {
registerBlockType( 'core/test-block-with-internal-attrs', {
attributes: {
align: {
type: 'string',
},
internalData: {
type: 'boolean',
__experimentalRole: 'internal',
},
productId: {
type: 'number',
__experimentalRole: 'internal',
},
color: {
type: 'string',
__experimentalRole: 'other',
},
},
save: noop,
category: 'text',
title: 'test block with internal attrs',
} );
registerBlockType( 'core/test-block-with-no-internal-attrs', {
attributes: {
align: { type: 'string' },
color: { type: 'string' },
},
save: noop,
category: 'text',
title: 'test block with no internal attrs',
} );
registerBlockType( 'core/test-block-with-no-attrs', {
save: noop,
category: 'text',
title: 'test block with no attrs',
} );
} );
afterAll( () => {
[
'core/test-block-with-internal-attrs',
'core/test-block-with-no-internal-attrs',
'core/test-block-with-no-attrs',
].forEach( unregisterBlockType );
} );

it( 'should return empty object if no attributes are passed', () => {
expect(
__experimentalRemoveAttributesByRole(
'core/test-block-with-internal-attrs',
{},
'internal'
)
).toEqual( {} );
} );
it( 'should return all attributes when block has no attributes with given role', () => {
expect(
__experimentalRemoveAttributesByRole(
'core/test-block-with-no-internal-attrs',
{ align: 'left', color: 'blue' },
'internal'
)
).toEqual( { align: 'left', color: 'blue' } );
} );
it( 'should remove attributes with given role and return all others', () => {
expect(
__experimentalRemoveAttributesByRole(
'core/test-block-with-internal-attrs',
{
align: 'left',
internalData: true,
productId: 12345,
color: 'blue',
},
'internal'
)
).toEqual( { align: 'left', color: 'blue' } );
} );
} );
26 changes: 0 additions & 26 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,29 +257,3 @@ export function __experimentalGetBlockAttributesNamesByRole( name, role ) {
attributes[ attributeName ]?.__experimentalRole === role
);
}

/**
* Given attributes, remove any attributes with the given `role`.
*
* @param {string} name The block's name.
* @param {Object} attributes The block's attributes.
* @param {string} role The role of a block attribute.
*
* @return {Object} The attributes, with those matching the `role` removed.
*/
export function __experimentalRemoveAttributesByRole( name, attributes, role ) {
const attributesByRole = __experimentalGetBlockAttributesNamesByRole(
name,
role
);
if ( ! attributesByRole?.length ) {
return attributes;
}

return Object.keys( attributes ).reduce( ( _accumulator, attribute ) => {
if ( ! attributesByRole.includes( attribute ) ) {
_accumulator[ attribute ] = attributes[ attribute ];
}
return _accumulator;
}, {} );
}