ChangeNameByLabel.js
2 years ago
ControlsSettings.js
2 years ago
appendField.js
2 years ago
blocksRecursiveIterator.js
2 years ago
getAvailableFields.js
2 years ago
getAvailableFieldsString.js
2 years ago
getBlockControls.js
2 years ago
getBlocksByName.js
2 years ago
getCurrentInnerBlocks.js
2 years ago
getFieldsWithoutCurrent.js
2 years ago
getFormFieldsBlocks.js
2 years ago
getInnerBlocks.js
2 years ago
blocksRecursiveIterator.js
69 lines
| 1 | const { select } = wp.data; |
| 2 | |
| 3 | function blocksRecursiveIterator( blockParserFunc ) { |
| 4 | const blocksRecursiveIterator = ( blocks, parent = null ) => { |
| 5 | |
| 6 | blocks = blocks || select( 'core/block-editor' ).getBlocks(); |
| 7 | |
| 8 | blocks.forEach( block => { |
| 9 | blockParserFunc( block, parent ); |
| 10 | |
| 11 | if ( block.innerBlocks.length ) { |
| 12 | const currentParent = 'jet-forms/repeater-field' === block.name |
| 13 | ? block |
| 14 | : parent; |
| 15 | |
| 16 | blocksRecursiveIterator( |
| 17 | block.innerBlocks, |
| 18 | currentParent, |
| 19 | ); |
| 20 | |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | if ( 'core/block' !== block.name ) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Hardcode the receipt of internal blocks "Reusable block". |
| 30 | * This is the only way so far. |
| 31 | * |
| 32 | * Added after major refactoring |
| 33 | * @since 3.0.4 |
| 34 | * |
| 35 | * Introduced |
| 36 | * @since 2.1.1 |
| 37 | * |
| 38 | * @type {{clientId: String, innerBlocks: Array}[]} |
| 39 | */ |
| 40 | let innerReusable = select( 'core/block-editor' )?. |
| 41 | __unstableGetClientIdsTree?.( |
| 42 | block.clientId, |
| 43 | ); |
| 44 | |
| 45 | if ( ! innerReusable?.length ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | const innerReusableIds = innerReusable.map( |
| 50 | ( { clientId } ) => clientId, |
| 51 | ); |
| 52 | |
| 53 | /** |
| 54 | * We update this variable because `__unstableGetClientIdsTree` |
| 55 | * returns insufficient information about the block. |
| 56 | * Namely, the `name` property is missing |
| 57 | */ |
| 58 | innerReusable = select( 'core/block-editor' ).getBlocksByClientId( |
| 59 | innerReusableIds, |
| 60 | ); |
| 61 | |
| 62 | blocksRecursiveIterator( innerReusable ); |
| 63 | } ); |
| 64 | }; |
| 65 | |
| 66 | blocksRecursiveIterator(); |
| 67 | } |
| 68 | |
| 69 | export default blocksRecursiveIterator; |