-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
use-block-editor-settings.js
210 lines (196 loc) · 5.78 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
/**
* External dependencies
*/
import { pick } from 'lodash';
/**
* WordPress dependencies
*/
import { Platform, useMemo } 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';
/**
* Internal dependencies
*/
import { mediaUpload } from '../../utils';
import { store as editorStore } from '../../store';
/**
* React hook used to compute the block editor settings to use for the post editor.
*
* @param {Object} settings EditorProvider settings prop.
* @param {boolean} hasTemplate Whether template mode is enabled.
*
* @return {Object} Block Editor Settings.
*/
function useBlockEditorSettings( settings, hasTemplate ) {
const {
reusableBlocks,
hasUploadPermissions,
canUseUnfilteredHTML,
userCanCreatePages,
pageOnFront,
postType,
} = useSelect( ( select ) => {
const { canUserUseUnfilteredHTML, getCurrentPostType } =
select( editorStore );
const isWeb = Platform.OS === 'web';
const { canUser, getEntityRecord } = select( coreStore );
const siteSettings = canUser( 'read', 'settings' )
? getEntityRecord( 'root', 'site' )
: undefined;
return {
canUseUnfilteredHTML: canUserUseUnfilteredHTML(),
reusableBlocks: isWeb
? select( coreStore ).getEntityRecords(
'postType',
'wp_block',
{ per_page: -1 }
)
: [], // Reusable blocks are fetched in the native version of this hook.
hasUploadPermissions: canUser( 'create', 'media' ) ?? true,
userCanCreatePages: canUser( 'create', 'pages' ),
pageOnFront: siteSettings?.page_on_front,
postType: getCurrentPostType(),
};
}, [] );
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 { restBlockPatterns, restBlockPatternCategories } = useSelect(
( select ) => ( {
restBlockPatterns: select( coreStore ).getBlockPatterns(),
restBlockPatternCategories:
select( coreStore ).getBlockPatternCategories(),
} ),
[]
);
const blockPatterns = useMemo(
() =>
[
...( settingsBlockPatterns || [] ),
...( restBlockPatterns || [] ),
]
.filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
)
.filter( ( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) &&
postTypes.includes( postType ) )
);
} ),
[ settingsBlockPatterns, restBlockPatterns, postType ]
);
const blockPatternCategories = useMemo(
() =>
[
...( settingsBlockPatternCategories || [] ),
...( restBlockPatternCategories || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
[ settingsBlockPatternCategories, restBlockPatternCategories ]
);
const { undo } = 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 = ( options ) => {
if ( ! userCanCreatePages ) {
return Promise.reject( {
message: __( 'You do not have permission to create Pages.' ),
} );
}
return saveEntityRecord( 'postType', 'page', options );
};
return useMemo(
() => ( {
...pick( settings, [
'__experimentalBlockDirectory',
'__experimentalDiscussionSettings',
'__experimentalFeatures',
'__experimentalPreferredStyleVariations',
'__experimentalSetIsInserterOpened',
'__unstableGalleryWithImageBlocks',
'alignWide',
'allowedBlockTypes',
'bodyPlaceholder',
'canLockBlocks',
'codeEditingEnabled',
'colors',
'disableCustomColors',
'disableCustomFontSizes',
'disableCustomSpacingSizes',
'disableCustomGradients',
'disableLayoutStyles',
'enableCustomLineHeight',
'enableCustomSpacing',
'enableCustomUnits',
'focusMode',
'fontSizes',
'gradients',
'generateAnchors',
'hasFixedToolbar',
'isDistractionFree',
'hasInlineToolbar',
'imageDefaultSize',
'imageDimensions',
'imageEditing',
'imageSizes',
'isRTL',
'keepCaretInsideBlock',
'maxWidth',
'onUpdateDefaultBlockStyles',
'styles',
'template',
'templateLock',
'titlePlaceholder',
'supportsLayout',
'widgetTypesToHideFromLegacyWidgetBlock',
'__unstableResolvedAssets',
] ),
mediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalReusableBlocks: reusableBlocks,
__experimentalBlockPatterns: blockPatterns,
__experimentalBlockPatternCategories: blockPatternCategories,
__experimentalFetchLinkSuggestions: ( search, searchOptions ) =>
fetchLinkSuggestions( search, searchOptions, settings ),
__experimentalFetchRichUrlData: fetchUrlData,
__experimentalCanUserUseUnfilteredHTML: canUseUnfilteredHTML,
__experimentalUndo: undo,
outlineMode: hasTemplate,
__experimentalCreatePageEntity: createPageEntity,
__experimentalUserCanCreatePages: userCanCreatePages,
pageOnFront,
__experimentalPreferPatternsOnRoot: hasTemplate,
} ),
[
settings,
hasUploadPermissions,
reusableBlocks,
blockPatterns,
blockPatternCategories,
canUseUnfilteredHTML,
undo,
hasTemplate,
userCanCreatePages,
pageOnFront,
]
);
}
export default useBlockEditorSettings;