-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
369 lines (344 loc) · 11.7 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
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
366
367
368
369
/**
* WordPress dependencies
*/
import { useEffect, useLayoutEffect, useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { EntityProvider, useEntityBlockEditor } from '@wordpress/core-data';
import {
BlockEditorProvider,
BlockContextProvider,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { store as noticesStore } from '@wordpress/notices';
import { privateApis as editPatternsPrivateApis } from '@wordpress/patterns';
import { createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import withRegistryProvider from './with-registry-provider';
import { store as editorStore } from '../../store';
import useBlockEditorSettings from './use-block-editor-settings';
import { unlock } from '../../lock-unlock';
import DisableNonPageContentBlocks from './disable-non-page-content-blocks';
import NavigationBlockEditingMode from './navigation-block-editing-mode';
import { useHideBlocksFromInserter } from './use-hide-blocks-from-inserter';
import useCommands from '../commands';
import BlockRemovalWarnings from '../block-removal-warnings';
import StartPageOptions from '../start-page-options';
import KeyboardShortcutHelpModal from '../keyboard-shortcut-help-modal';
import ContentOnlySettingsMenu from '../block-settings-menu/content-only-settings-menu';
import StartTemplateOptions from '../start-template-options';
import EditorKeyboardShortcuts from '../global-keyboard-shortcuts';
import PatternRenameModal from '../pattern-rename-modal';
import PatternDuplicateModal from '../pattern-duplicate-modal';
import TemplatePartMenuItems from '../template-part-menu-items';
const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );
const { PatternsMenuItems } = unlock( editPatternsPrivateApis );
const noop = () => {};
/**
* These are global entities that are only there to split blocks into logical units
* They don't provide a "context" for the current post/page being rendered.
* So we should not use their ids as post context. This is important to allow post blocks
* (post content, post title) to be used within them without issues.
*/
const NON_CONTEXTUAL_POST_TYPES = [
'wp_block',
'wp_template',
'wp_navigation',
'wp_template_part',
];
/**
* Depending on the post, template and template mode,
* returns the appropriate blocks and change handlers for the block editor provider.
*
* @param {Array} post Block list.
* @param {boolean} template Whether the page content has focus (and the surrounding template is inert). If `true` return page content blocks. Default `false`.
* @param {string} mode Rendering mode.
*
* @example
* ```jsx
* const [ blocks, onInput, onChange ] = useBlockEditorProps( post, template, mode );
* ```
*
* @return {Array} Block editor props.
*/
function useBlockEditorProps( post, template, mode ) {
const rootLevelPost =
mode === 'post-only' || ! template ? 'post' : 'template';
const [ postBlocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
post.type,
{ id: post.id }
);
const [ templateBlocks, onInputTemplate, onChangeTemplate ] =
useEntityBlockEditor( 'postType', template?.type, {
id: template?.id,
} );
const maybeNavigationBlocks = useMemo( () => {
if ( post.type === 'wp_navigation' ) {
return [
createBlock( 'core/navigation', {
ref: post.id,
// As the parent editor is locked with `templateLock`, the template locking
// must be explicitly "unset" on the block itself to allow the user to modify
// the block's content.
templateLock: false,
} ),
];
}
}, [ post.type, post.id ] );
// It is important that we don't create a new instance of blocks on every change
// We should only create a new instance if the blocks them selves change, not a dependency of them.
const blocks = useMemo( () => {
if ( maybeNavigationBlocks ) {
return maybeNavigationBlocks;
}
if ( rootLevelPost === 'template' ) {
return templateBlocks;
}
return postBlocks;
}, [ maybeNavigationBlocks, rootLevelPost, templateBlocks, postBlocks ] );
// Handle fallback to postBlocks outside of the above useMemo, to ensure
// that constructed block templates that call `createBlock` are not generated
// too frequently. This ensures that clientIds are stable.
const disableRootLevelChanges =
( !! template && mode === 'template-locked' ) ||
post.type === 'wp_navigation';
if ( disableRootLevelChanges ) {
return [ blocks, noop, noop ];
}
return [
blocks,
rootLevelPost === 'post' ? onInput : onInputTemplate,
rootLevelPost === 'post' ? onChange : onChangeTemplate,
];
}
/**
* This component provides the editor context and manages the state of the block editor.
*
* @param {Object} props The component props.
* @param {Object} props.post The post object.
* @param {Object} props.settings The editor settings.
* @param {boolean} props.recovery Indicates if the editor is in recovery mode.
* @param {Array} props.initialEdits The initial edits for the editor.
* @param {Object} props.children The child components.
* @param {Object} [props.BlockEditorProviderComponent] The block editor provider component to use. Defaults to ExperimentalBlockEditorProvider.
* @param {Object} [props.__unstableTemplate] The template object.
*
* @example
* ```jsx
* <ExperimentalEditorProvider
* post={ post }
* settings={ settings }
* recovery={ recovery }
* initialEdits={ initialEdits }
* __unstableTemplate={ template }
* >
* { children }
* </ExperimentalEditorProvider>
*
* @return {Object} The rendered ExperimentalEditorProvider component.
*/
export const ExperimentalEditorProvider = withRegistryProvider(
( {
post,
settings,
recovery,
initialEdits,
children,
BlockEditorProviderComponent = ExperimentalBlockEditorProvider,
__unstableTemplate: template,
} ) => {
const { editorSettings, selection, isReady, mode } = useSelect(
( select ) => {
const {
getEditorSettings,
getEditorSelection,
getRenderingMode,
__unstableIsEditorReady,
} = select( editorStore );
return {
editorSettings: getEditorSettings(),
isReady: __unstableIsEditorReady(),
mode: getRenderingMode(),
selection: getEditorSelection(),
};
},
[]
);
const shouldRenderTemplate = !! template && mode !== 'post-only';
const rootLevelPost = shouldRenderTemplate ? template : post;
const defaultBlockContext = useMemo( () => {
const postContext =
! NON_CONTEXTUAL_POST_TYPES.includes( rootLevelPost.type ) ||
shouldRenderTemplate
? { postId: post.id, postType: post.type }
: {};
return {
...postContext,
templateSlug:
rootLevelPost.type === 'wp_template'
? rootLevelPost.slug
: undefined,
};
}, [
shouldRenderTemplate,
post.id,
post.type,
rootLevelPost.type,
rootLevelPost.slug,
] );
const { id, type } = rootLevelPost;
const blockEditorSettings = useBlockEditorSettings(
editorSettings,
type,
id,
mode
);
const [ blocks, onInput, onChange ] = useBlockEditorProps(
post,
template,
mode
);
const {
updatePostLock,
setupEditor,
updateEditorSettings,
setCurrentTemplateId,
setEditedPost,
setRenderingMode,
} = unlock( useDispatch( editorStore ) );
const { createWarningNotice } = useDispatch( noticesStore );
// Ideally this should be synced on each change and not just something you do once.
useLayoutEffect( () => {
// Assume that we don't need to initialize in the case of an error recovery.
if ( recovery ) {
return;
}
updatePostLock( settings.postLock );
setupEditor( post, initialEdits, settings.template );
if ( settings.autosave ) {
createWarningNotice(
__(
'There is an autosave of this post that is more recent than the version below.'
),
{
id: 'autosave-exists',
actions: [
{
label: __( 'View the autosave' ),
url: settings.autosave.editLink,
},
],
}
);
}
}, [] );
// Synchronizes the active post with the state
useEffect( () => {
setEditedPost( post.type, post.id );
}, [ post.type, post.id, setEditedPost ] );
// Synchronize the editor settings as they change.
useEffect( () => {
updateEditorSettings( settings );
}, [ settings, updateEditorSettings ] );
// Synchronizes the active template with the state.
useEffect( () => {
setCurrentTemplateId( template?.id );
}, [ template?.id, setCurrentTemplateId ] );
// Sets the right rendering mode when loading the editor.
useEffect( () => {
setRenderingMode( settings.defaultRenderingMode ?? 'post-only' );
}, [ settings.defaultRenderingMode, setRenderingMode ] );
useHideBlocksFromInserter( post.type, mode );
// Register the editor commands.
useCommands();
if ( ! isReady ) {
return null;
}
return (
<EntityProvider kind="root" type="site">
<EntityProvider
kind="postType"
type={ post.type }
id={ post.id }
>
<BlockContextProvider value={ defaultBlockContext }>
<BlockEditorProviderComponent
value={ blocks }
onChange={ onChange }
onInput={ onInput }
selection={ selection }
settings={ blockEditorSettings }
useSubRegistry={ false }
>
{ children }
{ ! settings.__unstableIsPreviewMode && (
<>
<PatternsMenuItems />
<TemplatePartMenuItems />
<ContentOnlySettingsMenu />
{ mode === 'template-locked' && (
<DisableNonPageContentBlocks />
) }
{ type === 'wp_navigation' && (
<NavigationBlockEditingMode />
) }
<EditorKeyboardShortcuts />
<KeyboardShortcutHelpModal />
<BlockRemovalWarnings />
<StartPageOptions />
<StartTemplateOptions />
<PatternRenameModal />
<PatternDuplicateModal />
</>
) }
</BlockEditorProviderComponent>
</BlockContextProvider>
</EntityProvider>
</EntityProvider>
);
}
);
/**
* This component establishes a new post editing context, and serves as the entry point for a new post editor (or post with template editor).
*
* It supports a large number of post types, including post, page, templates,
* custom post types, patterns, template parts.
*
* All modification and changes are performed to the `@wordpress/core-data` store.
*
* @param {Object} props The component props.
* @param {Object} [props.post] The post object to edit. This is required.
* @param {Object} [props.__unstableTemplate] The template object wrapper the edited post.
* This is optional and can only be used when the post type supports templates (like posts and pages).
* @param {Object} [props.settings] The settings object to use for the editor.
* This is optional and can be used to override the default settings.
* @param {Element} [props.children] Children elements for which the BlockEditorProvider context should apply.
* This is optional.
*
* @example
* ```jsx
* <EditorProvider
* post={ post }
* settings={ settings }
* __unstableTemplate={ template }
* >
* { children }
* </EditorProvider>
* ```
*
* @return {JSX.Element} The rendered EditorProvider component.
*/
export function EditorProvider( props ) {
return (
<ExperimentalEditorProvider
{ ...props }
BlockEditorProviderComponent={ BlockEditorProvider }
>
{ props.children }
</ExperimentalEditorProvider>
);
}
export default EditorProvider;