-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathindex.js
More file actions
325 lines (311 loc) · 9.49 KB
/
Copy pathindex.js
File metadata and controls
325 lines (311 loc) · 9.49 KB
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
import clsx from 'clsx';
import { NavigableRegion, getAdminThemeColors } from '@wordpress/admin-ui';
import {
__unstableMotion as motion,
__unstableAnimatePresence as AnimatePresence,
__unstableUseNavigateRegions as useNavigateRegions,
SlotFillProvider,
} from '@wordpress/components';
import {
useReducedMotion,
useViewportMatch,
useResizeObserver,
usePrevious,
} from '@wordpress/compose';
import { focus } from '@wordpress/dom';
import { __, sprintf } from '@wordpress/i18n';
import { useState, useRef, useEffect, useMemo } from '@wordpress/element';
import {
UnsavedChangesWarning,
ErrorBoundary,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { ThemeProvider } from '@wordpress/theme';
import { PluginArea } from '@wordpress/plugins';
import { SnackbarNotices, store as noticesStore } from '@wordpress/notices';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';
import { Tooltip } from '@wordpress/ui';
import { SiteHubMobile } from '../site-hub';
import ResizableFrame from '../resizable-frame';
import { unlock } from '../../lock-unlock';
import SaveKeyboardShortcut from '../save-keyboard-shortcut';
import { useIsSiteEditorLoading } from './hooks';
import useMovingAnimation from './animation';
import { SidebarContent, SidebarNavigationProvider } from '../sidebar';
import SaveHub from '../save-hub';
import SavePanel from '../save-panel';
const { useLocation } = unlock( routerPrivateApis );
const { useStyle, UploadProgressSnackbar, useSyncAdminBarSiteIcon } =
unlock( editorPrivateApis );
const ANIMATION_DURATION = 0.3;
const CONTENT_COLOR = { background: '#ffffff' };
function Layout() {
useSyncAdminBarSiteIcon();
const { query, name: routeKey, areas, widths } = useLocation();
// Force canvas to 'view' on notfound route to show the error message and allow navigation.
const canvas = routeKey === 'notfound' ? 'view' : query?.canvas ?? 'view';
const showMobileSiteHub = !! areas.mobileContent;
const hasMobileAreas =
areas.mobileSidebar || areas.mobileContent || areas.preview;
const isMobileViewport = useViewportMatch( 'medium', '<' );
const mobileToggleRef = useRef();
const sidebarRegionRef = useRef();
const navigateRegionsProps = useNavigateRegions();
const disableMotion = useReducedMotion();
const [ canvasResizer, canvasSize ] = useResizeObserver();
const isEditorLoading = useIsSiteEditorLoading();
const [ isResizableFrameOversized, setIsResizableFrameOversized ] =
useState( false );
const animationRef = useMovingAnimation( {
triggerAnimationOnChange: routeKey + '-' + canvas,
} );
const { showIconLabels } = useSelect( ( select ) => {
return {
showIconLabels: select( preferencesStore ).get(
'core',
'showIconLabels'
),
};
} );
const backgroundColor = useStyle( 'color.background' );
const gradientValue = useStyle( 'color.gradient' );
const previousCanvaMode = usePrevious( canvas );
useEffect( () => {
if ( previousCanvaMode === 'edit' ) {
const desktopToggle = sidebarRegionRef.current
? // We're typically expecting the `<DashboardBackButton />` component as the first tabbable element.
focus.tabbable.find( sidebarRegionRef.current )[ 0 ]
: undefined;
( desktopToggle ?? mobileToggleRef.current )?.focus();
}
// Should not depend on the previous canvas mode value but the next.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ canvas ] );
return (
<>
<UnsavedChangesWarning />
{ canvas === 'view' && <SaveKeyboardShortcut /> }
<div
{ ...navigateRegionsProps }
className={ clsx(
'edit-site-layout',
navigateRegionsProps.className,
{
'is-full-canvas': canvas === 'edit',
'show-icon-labels': showIconLabels,
}
) }
>
<div className="edit-site-layout__content">
{ /*
The NavigableRegion must always be rendered and not use
`inert` otherwise `useNavigateRegions` will fail.
*/ }
{ ( ! isMobileViewport || ! hasMobileAreas ) && (
<NavigableRegion
ref={ sidebarRegionRef }
ariaLabel={ __( 'Navigation' ) }
className="edit-site-layout__sidebar-region"
>
<AnimatePresence>
{ canvas === 'view' && (
<motion.div
initial={ { opacity: 0 } }
animate={ { opacity: 1 } }
exit={ { opacity: 0 } }
transition={ {
type: 'tween',
duration:
// Disable transition in mobile to emulate a full page transition.
disableMotion ||
isMobileViewport
? 0
: ANIMATION_DURATION,
ease: 'easeOut',
} }
className="edit-site-layout__sidebar"
>
<SidebarNavigationProvider>
<SidebarContent
shouldAnimate={
routeKey !== 'styles' &&
routeKey !== 'identity'
}
routeKey={ routeKey }
>
<ErrorBoundary>
{ areas.sidebar }
</ErrorBoundary>
</SidebarContent>
</SidebarNavigationProvider>
<SaveHub />
<SavePanel />
</motion.div>
) }
</AnimatePresence>
</NavigableRegion>
) }
<SnackbarNotices className="edit-site-layout__snackbar" />
<UploadProgressSnackbar />
{ isMobileViewport && hasMobileAreas && (
<div className="edit-site-layout__mobile">
<SidebarNavigationProvider>
{ canvas !== 'edit' ? (
<>
{ showMobileSiteHub && (
<SiteHubMobile
ref={ mobileToggleRef }
/>
) }
{ areas.mobileContent ? (
/*
* ThemeProvider wraps SidebarContent (rather than
* just the content) so the scroll wrapper it renders
* inherits the content background tokens. See
* `.edit-site-sidebar__screen-wrapper` in style.scss.
*/
<ThemeProvider
color={ CONTENT_COLOR }
>
<SidebarContent
routeKey={ routeKey }
>
<div className="edit-site-layout__mobile-content">
<ErrorBoundary>
{
areas.mobileContent
}
</ErrorBoundary>
</div>
</SidebarContent>
</ThemeProvider>
) : (
<SidebarContent
routeKey={ routeKey }
>
<ErrorBoundary>
{ areas.mobileSidebar }
</ErrorBoundary>
</SidebarContent>
) }
<SaveHub />
<SavePanel />
</>
) : (
<ThemeProvider color={ CONTENT_COLOR }>
<ErrorBoundary>
{ areas.preview }
</ErrorBoundary>
</ThemeProvider>
) }
</SidebarNavigationProvider>
</div>
) }
{ ! isMobileViewport &&
areas.content &&
canvas !== 'edit' && (
<div
className="edit-site-layout__area"
style={ {
maxWidth: widths?.content,
} }
>
<ThemeProvider color={ CONTENT_COLOR }>
<ErrorBoundary>
{ areas.content }
</ErrorBoundary>
</ThemeProvider>
</div>
) }
{ ! isMobileViewport && areas.edit && canvas !== 'edit' && (
<div
className="edit-site-layout__area"
style={ {
maxWidth: widths?.edit,
} }
>
<ThemeProvider color={ CONTENT_COLOR }>
<ErrorBoundary>{ areas.edit }</ErrorBoundary>
</ThemeProvider>
</div>
) }
{ ! isMobileViewport && areas.preview && (
<div className="edit-site-layout__canvas-container">
{ canvasResizer }
{ !! canvasSize.width && (
<div
className={ clsx(
'edit-site-layout__canvas',
{
'is-right-aligned':
isResizableFrameOversized,
}
) }
ref={ animationRef }
>
<ErrorBoundary>
<ResizableFrame
isReady={ ! isEditorLoading }
isFullWidth={ canvas === 'edit' }
defaultSize={ {
width:
canvasSize.width -
24 /* $canvas-padding */,
height: canvasSize.height,
} }
isOversized={
isResizableFrameOversized
}
setIsOversized={
setIsResizableFrameOversized
}
innerContentStyle={ {
background:
gradientValue ??
backgroundColor,
} }
>
<ThemeProvider
color={ CONTENT_COLOR }
>
{ areas.preview }
</ThemeProvider>
</ResizableFrame>
</ErrorBoundary>
</div>
) }
</div>
) }
</div>
</div>
</>
);
}
export default function LayoutWithGlobalStylesProvider( props ) {
const themeColors = useMemo( getAdminThemeColors, [] );
const { createErrorNotice } = useDispatch( noticesStore );
function onPluginAreaError( name ) {
createErrorNotice(
sprintf(
/* translators: %s: plugin name */
__(
'The "%s" plugin has encountered an error and cannot be rendered.'
),
name
)
);
}
return (
<SlotFillProvider>
<Tooltip.Provider>
{ /** This needs to be within the SlotFillProvider */ }
<PluginArea onError={ onPluginAreaError } />
<ThemeProvider color={ themeColors }>
<Layout { ...props } />
</ThemeProvider>
</Tooltip.Provider>
</SlotFillProvider>
);
}