-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
282 lines (273 loc) · 7.91 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
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { Button, Notice } from '@wordpress/components';
import { EntityProvider, store as coreStore } from '@wordpress/core-data';
import { store as preferencesStore } from '@wordpress/preferences';
import {
BlockContextProvider,
BlockBreadcrumb,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
InterfaceSkeleton,
ComplementaryArea,
store as interfaceStore,
} from '@wordpress/interface';
import {
EditorNotices,
EditorSnackbars,
EntitiesSavedStates,
} from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
/**
* Internal dependencies
*/
import { SidebarComplementaryAreaFills } from '../sidebar-edit-mode';
import BlockEditor from '../block-editor';
import CodeEditor from '../code-editor';
import KeyboardShortcuts from '../keyboard-shortcuts';
import useInitEditedEntityFromURL from '../use-init-edited-entity-from-url';
import InserterSidebar from '../secondary-sidebar/inserter-sidebar';
import ListViewSidebar from '../secondary-sidebar/list-view-sidebar';
import WelcomeGuide from '../welcome-guide';
import { store as editSiteStore } from '../../store';
import { GlobalStylesRenderer } from '../global-styles-renderer';
import { GlobalStylesProvider } from '../global-styles/global-styles-provider';
import useTitle from '../routes/use-title';
const interfaceLabels = {
/* translators: accessibility text for the editor content landmark region. */
body: __( 'Editor content' ),
/* translators: accessibility text for the editor settings landmark region. */
sidebar: __( 'Editor settings' ),
/* translators: accessibility text for the editor publish landmark region. */
actions: __( 'Editor publish' ),
/* translators: accessibility text for the editor footer landmark region. */
footer: __( 'Editor footer' ),
};
export default function Editor() {
// This ensures the edited entity id and type are initialized properly.
useInitEditedEntityFromURL();
const {
editedPostId,
editedPostType,
editedPost,
page,
hasLoadedPost,
editorMode,
canvasMode,
blockEditorMode,
isRightSidebarOpen,
isInserterOpen,
isListViewOpen,
isSaveViewOpen,
previousShortcut,
nextShortcut,
showIconLabels,
} = useSelect( ( select ) => {
const {
getEditedPostType,
getEditedPostId,
getPage,
getEditorMode,
__unstableGetCanvasMode,
isInserterOpened,
isListViewOpened,
isSaveViewOpened,
} = select( editSiteStore );
const { hasFinishedResolution, getEntityRecord } = select( coreStore );
const { __unstableGetEditorMode } = select( blockEditorStore );
const { getAllShortcutKeyCombinations } = select(
keyboardShortcutsStore
);
const { getActiveComplementaryArea } = select( interfaceStore );
const postType = getEditedPostType();
const postId = getEditedPostId();
// The currently selected entity to display.
// Typically template or template part in the site editor.
return {
editedPostId: postId,
editedPostType: postType,
editedPost: postId
? getEntityRecord( 'postType', postType, postId )
: null,
page: getPage(),
hasLoadedPost: postId
? hasFinishedResolution( 'getEntityRecord', [
'postType',
postType,
postId,
] )
: false,
editorMode: getEditorMode(),
canvasMode: __unstableGetCanvasMode(),
blockEditorMode: __unstableGetEditorMode(),
isInserterOpen: isInserterOpened(),
isListViewOpen: isListViewOpened(),
isSaveViewOpen: isSaveViewOpened(),
isRightSidebarOpen: getActiveComplementaryArea(
editSiteStore.name
),
previousShortcut: getAllShortcutKeyCombinations(
'core/edit-site/previous-region'
),
nextShortcut: getAllShortcutKeyCombinations(
'core/edit-site/next-region'
),
showIconLabels: select( preferencesStore ).get(
'core/edit-site',
'showIconLabels'
),
};
}, [] );
const { setIsSaveViewOpened, setPage } = useDispatch( editSiteStore );
const isViewMode = canvasMode === 'view';
const isEditMode = canvasMode === 'edit';
const showVisualEditor = isViewMode || editorMode === 'visual';
const showBlockBreakcrumb =
isEditMode && showVisualEditor && blockEditorMode !== 'zoom-out';
const shouldShowInserter = isEditMode && showVisualEditor && isInserterOpen;
const shouldShowListView = isEditMode && showVisualEditor && isListViewOpen;
const secondarySidebarLabel = isListViewOpen
? __( 'List View' )
: __( 'Block Library' );
const blockContext = useMemo(
() => ( {
...page?.context,
queryContext: [
page?.context.queryContext || { page: 1 },
( newQueryContext ) =>
setPage( {
...page,
context: {
...page?.context,
queryContext: {
...page?.context.queryContext,
...newQueryContext,
},
},
} ),
],
} ),
[ page?.context ]
);
const isReady = editedPostType !== undefined && editedPostId !== undefined;
// Only announce the title once the editor is ready to prevent "Replace"
// action in <URlQueryController> from double-announcing.
useTitle( isReady && __( 'Editor (beta)' ) );
if ( ! isReady ) {
return null;
}
return (
<>
{ isEditMode && <WelcomeGuide /> }
<KeyboardShortcuts.Register />
<SidebarComplementaryAreaFills />
<EntityProvider kind="root" type="site">
<EntityProvider
kind="postType"
type={ editedPostType }
id={ editedPostId }
>
<GlobalStylesProvider>
<BlockContextProvider value={ blockContext }>
<InterfaceSkeleton
className={
showIconLabels && 'show-icon-labels'
}
notices={ isEditMode && <EditorSnackbars /> }
content={
<>
<GlobalStylesRenderer />
<EditorNotices />
{ showVisualEditor && editedPost && (
<BlockEditor />
) }
{ editorMode === 'text' &&
editedPost && <CodeEditor /> }
{ hasLoadedPost && ! editedPost && (
<Notice
status="warning"
isDismissible={ false }
>
{ __(
"You attempted to edit an item that doesn't exist. Perhaps it was deleted?"
) }
</Notice>
) }
{ isEditMode && <KeyboardShortcuts /> }
</>
}
secondarySidebar={
isEditMode &&
( ( shouldShowInserter && (
<InserterSidebar />
) ) ||
( shouldShowListView && (
<ListViewSidebar />
) ) )
}
sidebar={
isEditMode &&
isRightSidebarOpen && (
<ComplementaryArea.Slot scope="core/edit-site" />
)
}
actions={
isEditMode && (
<>
{ isSaveViewOpen ? (
<EntitiesSavedStates
close={ () =>
setIsSaveViewOpened(
false
)
}
/>
) : (
<div className="edit-site-editor__toggle-save-panel">
<Button
variant="secondary"
className="edit-site-editor__toggle-save-panel-button"
onClick={ () =>
setIsSaveViewOpened(
true
)
}
aria-expanded={ false }
>
{ __(
'Open save panel'
) }
</Button>
</div>
) }
</>
)
}
footer={
showBlockBreakcrumb && (
<BlockBreadcrumb
rootLabelText={ __( 'Template' ) }
/>
)
}
shortcuts={ {
previous: previousShortcut,
next: nextShortcut,
} }
labels={ {
...interfaceLabels,
secondarySidebar: secondarySidebarLabel,
} }
/>
</BlockContextProvider>
</GlobalStylesProvider>
</EntityProvider>
</EntityProvider>
</>
);
}