-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathuse-block-editor-settings.js
365 lines (342 loc) · 10.7 KB
/
use-block-editor-settings.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* WordPress dependencies
*/
import { useMemo, useCallback } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import {
store as coreStore,
__experimentalFetchLinkSuggestions as fetchLinkSuggestions,
__experimentalFetchUrlData as fetchUrlData,
} from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { store as preferencesStore } from '@wordpress/preferences';
import { useViewportMatch } from '@wordpress/compose';
import { store as blocksStore } from '@wordpress/blocks';
import {
privateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import inserterMediaCategories from '../media-categories';
import { mediaUpload } from '../../utils';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { useGlobalStylesContext } from '../global-styles-provider';
const EMPTY_OBJECT = {};
function __experimentalReusableBlocksSelect( select ) {
const { getEntityRecords, hasFinishedResolution } = select( coreStore );
const reusableBlocks = getEntityRecords( 'postType', 'wp_block', {
per_page: -1,
} );
return hasFinishedResolution( 'getEntityRecords', [
'postType',
'wp_block',
{ per_page: -1 },
] )
? reusableBlocks
: undefined;
}
const BLOCK_EDITOR_SETTINGS = [
'__experimentalBlockDirectory',
'__experimentalDiscussionSettings',
'__experimentalFeatures',
'__experimentalGlobalStylesBaseStyles',
'alignWide',
'blockInspectorTabs',
'allowedMimeTypes',
'bodyPlaceholder',
'canLockBlocks',
'canUpdateBlockBindings',
'capabilities',
'clearBlockSelection',
'codeEditingEnabled',
'colors',
'disableCustomColors',
'disableCustomFontSizes',
'disableCustomSpacingSizes',
'disableCustomGradients',
'disableLayoutStyles',
'enableCustomLineHeight',
'enableCustomSpacing',
'enableCustomUnits',
'enableOpenverseMediaCategory',
'fontSizes',
'gradients',
'generateAnchors',
'onNavigateToEntityRecord',
'imageDefaultSize',
'imageDimensions',
'imageEditing',
'imageSizes',
'isRTL',
'locale',
'maxWidth',
'postContentAttributes',
'postsPerPage',
'readOnly',
'styles',
'titlePlaceholder',
'supportsLayout',
'widgetTypesToHideFromLegacyWidgetBlock',
'__unstableHasCustomAppender',
'__unstableIsPreviewMode',
'__unstableResolvedAssets',
'__unstableIsBlockBasedTheme',
];
const {
globalStylesDataKey,
globalStylesLinksDataKey,
selectBlockPatternsKey,
reusableBlocksSelectKey,
sectionRootClientIdKey,
} = unlock( privateApis );
/**
* React hook used to compute the block editor settings to use for the post editor.
*
* @param {Object} settings EditorProvider settings prop.
* @param {string} postType Editor root level post type.
* @param {string} postId Editor root level post ID.
* @param {string} renderingMode Editor rendering mode.
*
* @return {Object} Block Editor Settings.
*/
function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
const isLargeViewport = useViewportMatch( 'medium' );
const {
allowRightClickOverrides,
blockTypes,
focusMode,
hasFixedToolbar,
isDistractionFree,
keepCaretInsideBlock,
hasUploadPermissions,
hiddenBlockTypes,
canUseUnfilteredHTML,
userCanCreatePages,
pageOnFront,
pageForPosts,
userPatternCategories,
restBlockPatternCategories,
sectionRootClientId,
} = useSelect(
( select ) => {
const {
canUser,
getRawEntityRecord,
getEntityRecord,
getUserPatternCategories,
getBlockPatternCategories,
} = select( coreStore );
const { get } = select( preferencesStore );
const { getBlockTypes } = select( blocksStore );
const { getBlocksByName, getBlockAttributes } =
select( blockEditorStore );
const siteSettings = canUser( 'read', {
kind: 'root',
name: 'site',
} )
? getEntityRecord( 'root', 'site' )
: undefined;
function getSectionRootBlock() {
if ( renderingMode === 'template-locked' ) {
return getBlocksByName( 'core/post-content' )?.[ 0 ] ?? '';
}
return (
getBlocksByName( 'core/group' ).find(
( clientId ) =>
getBlockAttributes( clientId )?.tagName === 'main'
) ?? ''
);
}
return {
allowRightClickOverrides: get(
'core',
'allowRightClickOverrides'
),
blockTypes: getBlockTypes(),
canUseUnfilteredHTML: getRawEntityRecord(
'postType',
postType,
postId
)?._links?.hasOwnProperty( 'wp:action-unfiltered-html' ),
focusMode: get( 'core', 'focusMode' ),
hasFixedToolbar:
get( 'core', 'fixedToolbar' ) || ! isLargeViewport,
hiddenBlockTypes: get( 'core', 'hiddenBlockTypes' ),
isDistractionFree: get( 'core', 'distractionFree' ),
keepCaretInsideBlock: get( 'core', 'keepCaretInsideBlock' ),
hasUploadPermissions:
canUser( 'create', {
kind: 'root',
name: 'media',
} ) ?? true,
userCanCreatePages: canUser( 'create', {
kind: 'postType',
name: 'page',
} ),
pageOnFront: siteSettings?.page_on_front,
pageForPosts: siteSettings?.page_for_posts,
userPatternCategories: getUserPatternCategories(),
restBlockPatternCategories: getBlockPatternCategories(),
sectionRootClientId: getSectionRootBlock(),
};
},
[ postType, postId, isLargeViewport, renderingMode ]
);
const { merged: mergedGlobalStyles } = useGlobalStylesContext();
const globalStylesData = mergedGlobalStyles.styles ?? EMPTY_OBJECT;
const globalStylesLinksData = mergedGlobalStyles._links ?? EMPTY_OBJECT;
const settingsBlockPatterns =
settings.__experimentalAdditionalBlockPatterns ?? // WP 6.0
settings.__experimentalBlockPatterns; // WP 5.9
const settingsBlockPatternCategories =
settings.__experimentalAdditionalBlockPatternCategories ?? // WP 6.0
settings.__experimentalBlockPatternCategories; // WP 5.9
const blockPatterns = useMemo(
() =>
[ ...( settingsBlockPatterns || [] ) ].filter(
( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) &&
postTypes.includes( postType ) )
);
}
),
[ settingsBlockPatterns, postType ]
);
const blockPatternCategories = useMemo(
() =>
[
...( settingsBlockPatternCategories || [] ),
...( restBlockPatternCategories || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
[ settingsBlockPatternCategories, restBlockPatternCategories ]
);
const { undo, setIsInserterOpened } = useDispatch( editorStore );
const { saveEntityRecord } = useDispatch( coreStore );
/**
* Creates a Post entity.
* This is utilised by the Link UI to allow for on-the-fly creation of Posts/Pages.
*
* @param {Object} options parameters for the post being created. These mirror those used on 3rd param of saveEntityRecord.
* @return {Object} the post type object that was created.
*/
const createPageEntity = useCallback(
( options ) => {
if ( ! userCanCreatePages ) {
return Promise.reject( {
message: __(
'You do not have permission to create Pages.'
),
} );
}
return saveEntityRecord( 'postType', 'page', options );
},
[ saveEntityRecord, userCanCreatePages ]
);
const allowedBlockTypes = useMemo( () => {
// Omit hidden block types if exists and non-empty.
if ( hiddenBlockTypes && hiddenBlockTypes.length > 0 ) {
// Defer to passed setting for `allowedBlockTypes` if provided as
// anything other than `true` (where `true` is equivalent to allow
// all block types).
const defaultAllowedBlockTypes =
true === settings.allowedBlockTypes
? blockTypes.map( ( { name } ) => name )
: settings.allowedBlockTypes || [];
return defaultAllowedBlockTypes.filter(
( type ) => ! hiddenBlockTypes.includes( type )
);
}
return settings.allowedBlockTypes;
}, [ settings.allowedBlockTypes, hiddenBlockTypes, blockTypes ] );
const forceDisableFocusMode = settings.focusMode === false;
return useMemo( () => {
const blockEditorSettings = {
...Object.fromEntries(
Object.entries( settings ).filter( ( [ key ] ) =>
BLOCK_EDITOR_SETTINGS.includes( key )
)
),
[ globalStylesDataKey ]: globalStylesData,
[ globalStylesLinksDataKey ]: globalStylesLinksData,
allowedBlockTypes,
allowRightClickOverrides,
focusMode: focusMode && ! forceDisableFocusMode,
hasFixedToolbar,
isDistractionFree,
keepCaretInsideBlock,
mediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalBlockPatterns: blockPatterns,
[ selectBlockPatternsKey ]: ( select ) => {
const { hasFinishedResolution, getBlockPatternsForPostType } =
unlock( select( coreStore ) );
const patterns = getBlockPatternsForPostType( postType );
return hasFinishedResolution( 'getBlockPatterns' )
? patterns
: undefined;
},
[ reusableBlocksSelectKey ]: __experimentalReusableBlocksSelect,
__experimentalBlockPatternCategories: blockPatternCategories,
__experimentalUserPatternCategories: userPatternCategories,
__experimentalFetchLinkSuggestions: ( search, searchOptions ) =>
fetchLinkSuggestions( search, searchOptions, settings ),
inserterMediaCategories,
__experimentalFetchRichUrlData: fetchUrlData,
// Todo: This only checks the top level post, not the post within a template or any other entity that can be edited.
// This might be better as a generic "canUser" selector.
__experimentalCanUserUseUnfilteredHTML: canUseUnfilteredHTML,
//Todo: this is only needed for native and should probably be removed.
__experimentalUndo: undo,
// Check whether we want all site editor frames to have outlines
// including the navigation / pattern / parts editors.
outlineMode: ! isDistractionFree && postType === 'wp_template',
// Check these two properties: they were not present in the site editor.
__experimentalCreatePageEntity: createPageEntity,
__experimentalUserCanCreatePages: userCanCreatePages,
pageOnFront,
pageForPosts,
__experimentalPreferPatternsOnRoot: postType === 'wp_template',
templateLock:
postType === 'wp_navigation' ? 'insert' : settings.templateLock,
template:
postType === 'wp_navigation'
? [ [ 'core/navigation', {}, [] ] ]
: settings.template,
__experimentalSetIsInserterOpened: setIsInserterOpened,
[ sectionRootClientIdKey ]: sectionRootClientId,
};
return blockEditorSettings;
}, [
allowedBlockTypes,
allowRightClickOverrides,
focusMode,
forceDisableFocusMode,
hasFixedToolbar,
isDistractionFree,
keepCaretInsideBlock,
settings,
hasUploadPermissions,
userPatternCategories,
blockPatterns,
blockPatternCategories,
canUseUnfilteredHTML,
undo,
createPageEntity,
userCanCreatePages,
pageOnFront,
pageForPosts,
postType,
setIsInserterOpened,
sectionRootClientId,
globalStylesData,
globalStylesLinksData,
] );
}
export default useBlockEditorSettings;