-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
utils.js
66 lines (63 loc) · 1.99 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* WordPress dependencies
*/
import { getBlockAttributesNamesByRole } from '@wordpress/blocks';
/**
* Try to find a matching block by a block's name in a provided
* block. We recurse through InnerBlocks and return the reference
* of the matched block (it could be an InnerBlock).
* If no match is found return nothing.
*
* @param {WPBlock} block The block to try to find a match.
* @param {string} selectedBlockName The block's name to use for matching condition.
* @param {Set} consumedBlocks A set holding the previously matched/consumed blocks.
*
* @return {WPBlock | undefined} The matched block if found or nothing(`undefined`).
*/
export const getMatchingBlockByName = (
block,
selectedBlockName,
consumedBlocks = new Set()
) => {
const { clientId, name, innerBlocks = [] } = block;
// Check if block has been consumed already.
if ( consumedBlocks.has( clientId ) ) {
return;
}
if ( name === selectedBlockName ) {
return block;
}
// Try to find a matching block from InnerBlocks recursively.
for ( const innerBlock of innerBlocks ) {
const match = getMatchingBlockByName(
innerBlock,
selectedBlockName,
consumedBlocks
);
if ( match ) {
return match;
}
}
};
/**
* Find and return the block attributes to retain through
* the transformation, based on Block Type's `role:content`
* attributes. If no `role:content` attributes exist,
* return selected block's attributes.
*
* @param {string} name Block type's namespaced name.
* @param {Object} attributes Selected block's attributes.
* @return {Object} The block's attributes to retain.
*/
export const getRetainedBlockAttributes = ( name, attributes ) => {
const contentAttributes = getBlockAttributesNamesByRole( name, 'content' );
if ( ! contentAttributes?.length ) {
return attributes;
}
return contentAttributes.reduce( ( _accumulator, attribute ) => {
if ( attributes[ attribute ] ) {
_accumulator[ attribute ] = attributes[ attribute ];
}
return _accumulator;
}, {} );
};