-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathutils.js
124 lines (111 loc) · 3.37 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* WordPress dependencies
*/
import { parse } from '@wordpress/blocks';
import { parse as grammarParse } from '@wordpress/block-serialization-default-parser';
/**
* Internal dependencies
*/
import { selectBlockPatternsKey } from './private-keys';
import { unlock } from '../lock-unlock';
import { STORE_NAME } from './constants';
import { getSectionRootClientId } from './private-selectors';
export const isFiltered = Symbol( 'isFiltered' );
const parsedPatternCache = new WeakMap();
const grammarMapCache = new WeakMap();
function parsePattern( pattern ) {
const blocks = parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} );
if ( blocks.length === 1 ) {
blocks[ 0 ].attributes = {
...blocks[ 0 ].attributes,
metadata: {
...( blocks[ 0 ].attributes.metadata || {} ),
categories: pattern.categories,
patternName: pattern.name,
name: blocks[ 0 ].attributes.metadata?.name || pattern.title,
},
};
}
return {
...pattern,
blocks,
};
}
export function getParsedPattern( pattern ) {
let parsedPattern = parsedPatternCache.get( pattern );
if ( ! parsedPattern ) {
parsedPattern = parsePattern( pattern );
parsedPatternCache.set( pattern, parsedPattern );
}
return parsedPattern;
}
export function getGrammar( pattern ) {
let grammarMap = grammarMapCache.get( pattern );
if ( ! grammarMap ) {
grammarMap = grammarParse( pattern.content );
// Block names are null only at the top level for whitespace.
grammarMap = grammarMap.filter( ( block ) => block.blockName !== null );
grammarMapCache.set( pattern, grammarMap );
}
return grammarMap;
}
export const checkAllowList = ( list, item, defaultResult = null ) => {
if ( typeof list === 'boolean' ) {
return list;
}
if ( Array.isArray( list ) ) {
// TODO: when there is a canonical way to detect that we are editing a post
// the following check should be changed to something like:
// if ( list.includes( 'core/post-content' ) && getEditorMode() === 'post-content' && item === null )
if ( list.includes( 'core/post-content' ) && item === null ) {
return true;
}
return list.includes( item );
}
return defaultResult;
};
export const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => {
if ( typeof allowedBlockTypes === 'boolean' ) {
return allowedBlockTypes;
}
const blocksQueue = [ ...blocks ];
while ( blocksQueue.length > 0 ) {
const block = blocksQueue.shift();
const isAllowed = checkAllowList(
allowedBlockTypes,
block.name || block.blockName,
true
);
if ( ! isAllowed ) {
return false;
}
block.innerBlocks?.forEach( ( innerBlock ) => {
blocksQueue.push( innerBlock );
} );
}
return true;
};
export const getAllPatternsDependants = ( select ) => ( state ) => {
return [
state.settings.__experimentalBlockPatterns,
state.settings.__experimentalUserPatternCategories,
state.settings.__experimentalReusableBlocks,
state.settings[ selectBlockPatternsKey ]?.( select ),
state.blockPatterns,
unlock( select( STORE_NAME ) ).getReusableBlocks(),
];
};
export const getInsertBlockTypeDependants =
( select ) => ( state, rootClientId ) => {
return [
state.blockListSettings[ rootClientId ],
state.blocks.byClientId.get( rootClientId ),
state.settings.allowedBlockTypes,
state.settings.templateLock,
state.blockEditingModes,
select( STORE_NAME ).__unstableGetEditorMode( state ),
getSectionRootClientId( state ),
];
};