-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
353 lines (338 loc) · 9.6 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
/**
* WordPress dependencies
*/
import { useEffect, useState, useMemo, useCallback } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import {
SlotFillProvider,
DropZoneProvider,
Popover,
FocusReturnProvider,
Button,
} from '@wordpress/components';
import { EntityProvider } from '@wordpress/core-data';
import {
BlockContextProvider,
BlockSelectionClearer,
BlockBreadcrumb,
__unstableEditorStyles as EditorStyles,
__experimentalUseResizeCanvas as useResizeCanvas,
__experimentalLibrary as Library,
} from '@wordpress/block-editor';
import {
FullscreenMode,
InterfaceSkeleton,
ComplementaryArea,
} from '@wordpress/interface';
import { EntitiesSavedStates, UnsavedChangesWarning } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { PluginArea } from '@wordpress/plugins';
import { close } from '@wordpress/icons';
import { useViewportMatch } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Notices from '../notices';
import Header from '../header';
import { SidebarComplementaryAreaFills } from '../sidebar';
import BlockEditor from '../block-editor';
import KeyboardShortcuts from '../keyboard-shortcuts';
import GlobalStylesProvider from './global-styles-provider';
import NavigationSidebar from '../navigation-sidebar';
const interfaceLabels = {
secondarySidebar: __( 'Block Library' ),
drawer: __( 'Navigation Sidebar' ),
};
function Editor() {
const {
isFullscreenActive,
isInserterOpen,
deviceType,
sidebarIsOpened,
settings,
entityId,
templateType,
page,
template,
select,
isNavigationOpen,
} = useSelect( ( _select ) => {
const {
isFeatureActive,
isInserterOpened,
__experimentalGetPreviewDeviceType,
getSettings,
getTemplateId,
getTemplatePartId,
getTemplateType,
getPage,
isNavigationOpened,
} = _select( 'core/edit-site' );
const _templateId = getTemplateId();
const _templatePartId = getTemplatePartId();
const _templateType = getTemplateType();
// The currently selected entity to display. Typically template or template part.
let _entityId;
if ( _templateType ) {
_entityId =
_templateType === 'wp_template' ? _templateId : _templatePartId;
}
return {
isInserterOpen: isInserterOpened(),
isFullscreenActive: isFeatureActive( 'fullscreenMode' ),
deviceType: __experimentalGetPreviewDeviceType(),
sidebarIsOpened: !! _select(
'core/interface'
).getActiveComplementaryArea( 'core/edit-site' ),
settings: getSettings(),
templateType: _templateType,
page: getPage(),
template: _templateType
? _select( 'core' ).getEntityRecord(
'postType',
_templateType,
_entityId
)
: null,
select: _select,
entityId: _entityId,
isNavigationOpen: isNavigationOpened(),
};
}, [] );
const { editEntityRecord } = useDispatch( 'core' );
const { updateEditorSettings } = useDispatch( 'core/editor' );
const { setPage, setIsInserterOpened } = useDispatch( 'core/edit-site' );
// Keep the defaultTemplateTypes in the core/editor settings too,
// so that they can be selected with core/editor selectors in any editor.
// This is needed because edit-site doesn't initialize with EditorProvider,
// which internally uses updateEditorSettings as well.
const { defaultTemplateTypes } = settings;
useEffect( () => {
updateEditorSettings( { defaultTemplateTypes } );
}, [ defaultTemplateTypes ] );
const inlineStyles = useResizeCanvas( deviceType );
const [
isEntitiesSavedStatesOpen,
setIsEntitiesSavedStatesOpen,
] = useState( false );
const openEntitiesSavedStates = useCallback(
() => setIsEntitiesSavedStatesOpen( true ),
[]
);
const closeEntitiesSavedStates = useCallback(
( entitiesToSave ) => {
if ( entitiesToSave ) {
const { getEditedEntityRecord } = select( 'core' );
const {
__experimentalGetTemplateInfo: getTemplateInfo,
} = select( 'core/editor' );
entitiesToSave.forEach( ( { kind, name, key } ) => {
const record = getEditedEntityRecord( kind, name, key );
if ( 'postType' === kind && name === 'wp_template' ) {
const { title } = getTemplateInfo( record );
return editEntityRecord( kind, name, key, {
status: 'publish',
title,
} );
}
const edits = record.slug
? { status: 'publish', title: record.slug }
: { status: 'publish' };
editEntityRecord( kind, name, key, edits );
} );
}
setIsEntitiesSavedStatesOpen( false );
},
[ select ]
);
// Set default query for misplaced Query Loop blocks, and
// provide the root `queryContext` for top-level Query Loop
// and Query Pagination blocks.
const blockContext = useMemo(
() => ( {
...page?.context,
query: page?.context.query || { categoryIds: [], tagIds: [] },
queryContext: [
page?.context.queryContext || { page: 1 },
( newQueryContext ) =>
setPage( {
...page,
context: {
...page?.context,
queryContext: {
...page?.context.queryContext,
...newQueryContext,
},
},
} ),
],
} ),
[ page?.context ]
);
useEffect( () => {
if ( isNavigationOpen ) {
document.body.classList.add( 'is-navigation-sidebar-open' );
} else {
document.body.classList.remove( 'is-navigation-sidebar-open' );
}
}, [ isNavigationOpen ] );
const isMobile = useViewportMatch( 'medium', '<' );
return (
<>
<EditorStyles styles={ settings.styles } />
<FullscreenMode isActive={ isFullscreenActive } />
<UnsavedChangesWarning />
<SlotFillProvider>
<DropZoneProvider>
<EntityProvider kind="root" type="site">
<EntityProvider
kind="postType"
type={ 'wp_template' }
id={
templateType === 'wp_template' ? entityId : null
}
>
<EntityProvider
kind="postType"
type="wp_template_part"
id={
templateType === 'wp_template_part'
? entityId
: null
}
>
<EntityProvider
kind="postType"
type="wp_global_styles"
id={
settings.__experimentalGlobalStylesUserEntityId
}
>
<BlockContextProvider
value={ blockContext }
>
<FocusReturnProvider>
<GlobalStylesProvider
baseStyles={
settings.__experimentalGlobalStylesBaseStyles
}
>
<KeyboardShortcuts.Register />
<SidebarComplementaryAreaFills />
<InterfaceSkeleton
labels={ interfaceLabels }
drawer={
<NavigationSidebar />
}
secondarySidebar={
isInserterOpen ? (
<div className="edit-site-editor__inserter-panel">
<div className="edit-site-editor__inserter-panel-header">
<Button
icon={
close
}
onClick={ () =>
setIsInserterOpened(
false
)
}
/>
</div>
<div className="edit-site-editor__inserter-panel-content">
<Library
showInserterHelpPanel
onSelect={ () => {
if (
isMobile
) {
setIsInserterOpened(
false
);
}
} }
/>
</div>
</div>
) : null
}
sidebar={
sidebarIsOpened && (
<ComplementaryArea.Slot scope="core/edit-site" />
)
}
header={
<Header
openEntitiesSavedStates={
openEntitiesSavedStates
}
/>
}
content={
<BlockSelectionClearer
className="edit-site-visual-editor"
style={
inlineStyles
}
>
<Notices />
<Popover.Slot name="block-toolbar" />
{ template && (
<BlockEditor
setIsInserterOpen={
setIsInserterOpened
}
/>
) }
<KeyboardShortcuts />
</BlockSelectionClearer>
}
actions={
<>
<EntitiesSavedStates
isOpen={
isEntitiesSavedStatesOpen
}
close={
closeEntitiesSavedStates
}
/>
{ ! isEntitiesSavedStatesOpen && (
<div className="edit-site-editor__toggle-save-panel">
<Button
isSecondary
className="edit-site-editor__toggle-save-panel-button"
onClick={
openEntitiesSavedStates
}
aria-expanded={
false
}
>
{ __(
'Open save panel'
) }
</Button>
</div>
) }
</>
}
footer={
<BlockBreadcrumb />
}
/>
<Popover.Slot />
<PluginArea />
</GlobalStylesProvider>
</FocusReturnProvider>
</BlockContextProvider>
</EntityProvider>
</EntityProvider>
</EntityProvider>
</EntityProvider>
</DropZoneProvider>
</SlotFillProvider>
</>
);
}
export default Editor;