-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
57 lines (51 loc) · 1.76 KB
/
index.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
import { select } from '@wordpress/data';
/**
* For the given block name, return the first block found in the current post content.
*
* @param {string} blockName - Block name.
* @returns {?object} Block, if found.
*/
export function findBlockByName( blockName ) {
const { getBlocks } = select( 'core/block-editor' );
return getBlocks().find( ( { name } ) => name === blockName );
}
/**
* From the given blocks, return the first one that does not satisfy the given validation callback.
*
* @param {object[]} blocks - Blocks.
* @param {Function} isValid - Validation callback.
* @returns {?object} Invalid block, if found.
*/
export function findInvalidBlock( blocks, isValid ) {
return blocks.find( ( block ) => ! isValid( block ) );
}
/**
* From the given blocks, return the ones that don't satisfy the given validation callback.
*
* @param {object[]} blocks - Blocks.
* @param {Function} isValid - Validation callback.
* @returns {object[]} Invalid blocks, if found.
*/
export function findInvalidBlocks( blocks, isValid ) {
return blocks.filter( ( block ) => ! isValid( block ) );
}
/**
* From the given blocks, return the first one that satisfies the given validation callback.
*
* @param {object[]} blocks - Blocks.
* @param {Function} isValid - Validation callback.
* @returns {?object} Valid block, if found.
*/
export function findValidBlock( blocks, isValid ) {
return blocks.find( ( block ) => isValid( block ) );
}
/**
* From the given blocks, return the ones that satisfy the given validation callback.
*
* @param {object[]} blocks - Blocks.
* @param {Function} isValid - Validation callback.
* @returns {object[]} Valid blocks, if found.
*/
export function findValidBlocks( blocks, isValid ) {
return blocks.filter( ( block ) => isValid( block ) );
}