-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
235 lines (226 loc) · 6.75 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
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { 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 } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { SidebarComplementaryAreaFills } from '../sidebar-edit-mode';
import BlockEditor from '../block-editor';
import CodeEditor from '../code-editor';
import KeyboardShortcuts from '../keyboard-shortcuts';
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';
import CanvasSpinner from '../canvas-spinner';
import { unlock } from '../../experiments';
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() {
const {
editedPostId,
editedPostType,
editedPost,
context,
hasLoadedPost,
editorMode,
canvasMode,
blockEditorMode,
isRightSidebarOpen,
isInserterOpen,
isListViewOpen,
showIconLabels,
} = useSelect( ( select ) => {
const {
getEditedPostType,
getEditedPostId,
getEditedPostContext,
getEditorMode,
getCanvasMode,
isInserterOpened,
isListViewOpened,
} = unlock( select( editSiteStore ) );
const { hasFinishedResolution, getEntityRecord } = select( coreStore );
const { __unstableGetEditorMode } = select( blockEditorStore );
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,
context: getEditedPostContext(),
hasLoadedPost: postId
? hasFinishedResolution( 'getEntityRecord', [
'postType',
postType,
postId,
] )
: false,
editorMode: getEditorMode(),
canvasMode: getCanvasMode(),
blockEditorMode: __unstableGetEditorMode(),
isInserterOpen: isInserterOpened(),
isListViewOpen: isListViewOpened(),
isRightSidebarOpen: getActiveComplementaryArea(
editSiteStore.name
),
showIconLabels: select( preferencesStore ).get(
'core/edit-site',
'showIconLabels'
),
};
}, [] );
const { setEditedPostContext } = 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(
() => ( {
...context,
queryContext: [
context?.queryContext || { page: 1 },
( newQueryContext ) =>
setEditedPostContext( {
...context,
queryContext: {
...context?.queryContext,
...newQueryContext,
},
} ),
],
} ),
[ context ]
);
const isReady = editedPostType !== undefined && editedPostId !== undefined;
let title;
if ( isReady && editedPost ) {
const type =
editedPostType === 'wp_template'
? __( 'Template' )
: __( 'Template Part' );
title = `${ editedPost.title?.rendered } ‹ ${ type } ‹ ${ __(
'Editor (beta)'
) }`;
}
// Only announce the title once the editor is ready to prevent "Replace"
// action in <URlQueryController> from double-announcing.
useTitle( isReady && title );
if ( ! isReady ) {
return <CanvasSpinner />;
}
return (
<>
{ isEditMode && <WelcomeGuide /> }
<KeyboardShortcuts.Register />
<EntityProvider kind="root" type="site">
<EntityProvider
kind="postType"
type={ editedPostType }
id={ editedPostId }
>
<GlobalStylesProvider>
<BlockContextProvider value={ blockContext }>
<SidebarComplementaryAreaFills />
<InterfaceSkeleton
enableRegionNavigation={ false }
className={
showIconLabels && 'show-icon-labels'
}
notices={ isEditMode && <EditorSnackbars /> }
content={
<>
<GlobalStylesRenderer />
{ isEditMode && <EditorNotices /> }
{ showVisualEditor && editedPost && (
<BlockEditor />
) }
{ editorMode === 'text' &&
editedPost &&
isEditMode && <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" />
)
}
footer={
showBlockBreakcrumb && (
<BlockBreadcrumb
rootLabelText={ __( 'Template' ) }
/>
)
}
labels={ {
...interfaceLabels,
secondarySidebar: secondarySidebarLabel,
} }
/>
</BlockContextProvider>
</GlobalStylesProvider>
</EntityProvider>
</EntityProvider>
</>
);
}