-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
310 lines (295 loc) · 8.15 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
/**
* 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,
} 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';
/**
* 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 LeftSidebar from '../left-sidebar';
const interfaceLabels = {
leftSidebar: __( 'Block Library' ),
};
function Editor() {
const [ leftSidebarContent, setLeftSidebarContent ] = useState( null );
const {
isFullscreenActive,
deviceType,
sidebarIsOpened,
settings,
entityId,
templateType,
page,
template,
select,
} = useSelect( ( _select ) => {
const {
isFeatureActive,
__experimentalGetPreviewDeviceType,
getSettings,
getTemplateId,
getTemplatePartId,
getTemplateType,
getPage,
} = _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 {
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,
};
}, [] );
const { editEntityRecord } = useDispatch( 'core' );
const { setPage } = useDispatch( 'core/edit-site' );
const inlineStyles = useResizeCanvas( deviceType );
const [
isEntitiesSavedStatesOpen,
setIsEntitiesSavedStatesOpen,
] = useState( false );
const openEntitiesSavedStates = useCallback(
() => setIsEntitiesSavedStatesOpen( true ),
[]
);
const closeEntitiesSavedStates = useCallback(
( entitiesToSave ) => {
if ( entitiesToSave ) {
const { getEditedEntityRecord } = select( 'core' );
entitiesToSave.forEach( ( { kind, name, key } ) => {
const record = getEditedEntityRecord( kind, name, key );
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 ]
);
const isNavigationOpen = leftSidebarContent === 'navigation';
useEffect( () => {
if ( isNavigationOpen ) {
document.body.classList.add( 'is-navigation-sidebar-open' );
} else {
document.body.classList.remove( 'is-navigation-sidebar-open' );
}
}, [ isNavigationOpen ] );
const toggleLeftSidebarContent = ( value ) =>
setLeftSidebarContent( leftSidebarContent === value ? null : value );
return (
<>
<EditorStyles styles={ settings.styles } />
<FullscreenMode isActive={ isFullscreenActive } />
<UnsavedChangesWarning />
<SlotFillProvider>
<DropZoneProvider>
<EntityProvider kind="root" type="site">
<EntityProvider
kind="postType"
type={ templateType }
id={ entityId }
>
<EntityProvider
kind="postType"
type="wp_global_styles"
id={
settings.__experimentalGlobalStylesUserEntityId
}
>
<BlockContextProvider value={ blockContext }>
<FocusReturnProvider>
<GlobalStylesProvider
baseStyles={
settings.__experimentalGlobalStylesBaseStyles
}
contexts={
settings.__experimentalGlobalStylesContexts
}
>
<KeyboardShortcuts.Register />
<SidebarComplementaryAreaFills />
<InterfaceSkeleton
labels={ interfaceLabels }
leftSidebar={
<LeftSidebar
content={
leftSidebarContent
}
setContent={
setLeftSidebarContent
}
/>
}
sidebar={
sidebarIsOpened && (
<ComplementaryArea.Slot scope="core/edit-site" />
)
}
header={
<Header
openEntitiesSavedStates={
openEntitiesSavedStates
}
isInserterOpen={
leftSidebarContent ===
'inserter'
}
onToggleInserter={ () =>
toggleLeftSidebarContent(
'inserter'
)
}
isNavigationOpen={
isNavigationOpen
}
onToggleNavigation={ () =>
toggleLeftSidebarContent(
'navigation'
)
}
/>
}
content={
<BlockSelectionClearer
className="edit-site-visual-editor"
style={ inlineStyles }
>
<Notices />
<Popover.Slot name="block-toolbar" />
{ template && (
<BlockEditor
setIsInserterOpen={ (
isInserterOpen
) =>
setLeftSidebarContent(
isInserterOpen
? 'inserter'
: null
)
}
/>
) }
<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>
</DropZoneProvider>
</SlotFillProvider>
</>
);
}
export default Editor;