-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
use-block-editor-settings.js
213 lines (199 loc) · 5.51 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
/**
* External dependencies
*/
import { map, pick, defaultTo, flatten, partialRight } from 'lodash';
/**
* WordPress dependencies
*/
import { Platform, useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import { mediaUpload } from '../../utils';
import { store as editorStore } from '../../store';
/**
* Fetches link suggestions from the API. This function is an exact copy of a function found at:
*
* packages/edit-navigation/src/index.js
*
* It seems like there is no suitable package to import this from. Ideally it would be either part of core-data.
* Until we refactor it, just copying the code is the simplest solution.
*
* @param {string} search
* @param {Object} [searchArguments]
* @param {number} [searchArguments.isInitialSuggestions]
* @param {number} [searchArguments.type]
* @param {number} [searchArguments.subtype]
* @param {number} [searchArguments.page]
* @param {Object} [editorSettings]
* @param {boolean} [editorSettings.disablePostFormats=false]
* @return {Promise<Object[]>} List of suggestions
*/
const fetchLinkSuggestions = async (
search,
{ isInitialSuggestions, type, subtype, page, perPage: perPageArg } = {},
{ disablePostFormats = false } = {}
) => {
const perPage = perPageArg || isInitialSuggestions ? 3 : 20;
const queries = [];
if ( ! type || type === 'post' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'post',
subtype,
} ),
} ).catch( () => [] ) // fail by returning no results
);
}
if ( ! type || type === 'term' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'term',
subtype,
} ),
} ).catch( () => [] )
);
}
if ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'post-format',
subtype,
} ),
} ).catch( () => [] )
);
}
return Promise.all( queries ).then( ( results ) => {
return map(
flatten( results )
.filter( ( result ) => !! result.id )
.slice( 0, perPage ),
( result ) => ( {
id: result.id,
url: result.url,
title: decodeEntities( result.title ) || __( '(no title)' ),
type: result.subtype || result.type,
} )
);
} );
};
/**
* 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,
isTitleSelected,
} = useSelect( ( select ) => {
const { canUserUseUnfilteredHTML, isPostTitleSelected } = select(
editorStore
);
const { canUser } = select( coreStore );
return {
canUseUnfilteredHTML: canUserUseUnfilteredHTML(),
reusableBlocks: select( coreStore ).getEntityRecords(
'postType',
'wp_block',
/**
* Unbounded queries are not supported on native so as a workaround, we set per_page with the maximum value that native version can handle.
* Related issue: https://github.com/wordpress-mobile/gutenberg-mobile/issues/2661
*/
{ per_page: Platform.select( { web: -1, native: 10 } ) }
),
hasUploadPermissions: defaultTo(
canUser( 'create', 'media' ),
true
),
// This selector is only defined on mobile.
isTitleSelected: isPostTitleSelected && isPostTitleSelected(),
};
}, [] );
const { undo } = useDispatch( editorStore );
return useMemo(
() => ( {
...pick( settings, [
'__experimentalBlockDirectory',
'__experimentalBlockPatternCategories',
'__experimentalBlockPatterns',
'__experimentalFeatures',
'__experimentalGlobalStylesBaseStyles',
'__experimentalGlobalStylesUserEntityId',
'__experimentalPreferredStyleVariations',
'__experimentalSetIsInserterOpened',
'alignWide',
'allowedBlockTypes',
'availableLegacyWidgets',
'bodyPlaceholder',
'codeEditingEnabled',
'colors',
'disableCustomColors',
'disableCustomFontSizes',
'disableCustomGradients',
'enableCustomLineHeight',
'enableCustomSpacing',
'enableCustomUnits',
'focusMode',
'fontSizes',
'gradients',
'hasFixedToolbar',
'hasReducedUI',
'imageDimensions',
'imageEditing',
'imageSizes',
'isRTL',
'keepCaretInsideBlock',
'maxWidth',
'onUpdateDefaultBlockStyles',
'styles',
'template',
'templateLock',
'titlePlaceholder',
] ),
mediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalReusableBlocks: reusableBlocks,
__experimentalFetchLinkSuggestions: partialRight(
fetchLinkSuggestions,
settings
),
__experimentalCanUserUseUnfilteredHTML: canUseUnfilteredHTML,
__experimentalUndo: undo,
__experimentalShouldInsertAtTheTop: isTitleSelected,
outlineMode: hasTemplate,
} ),
[
settings,
hasUploadPermissions,
reusableBlocks,
canUseUnfilteredHTML,
undo,
isTitleSelected,
hasTemplate,
]
);
}
export default useBlockEditorSettings;