-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
331 lines (312 loc) · 9.65 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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
__unstableMotion as motion,
__unstableAnimatePresence as AnimatePresence,
__unstableUseNavigateRegions as useNavigateRegions,
} from '@wordpress/components';
import {
useReducedMotion,
useViewportMatch,
useResizeObserver,
} from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import {
CommandMenu,
privateApis as commandsPrivateApis,
} from '@wordpress/commands';
import { store as preferencesStore } from '@wordpress/preferences';
import {
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { privateApis as coreCommandsPrivateApis } from '@wordpress/core-commands';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
/**
* Internal dependencies
*/
import ErrorBoundary from '../error-boundary';
import { store as editSiteStore } from '../../store';
import useInitEditedEntityFromURL from '../sync-state-with-url/use-init-edited-entity-from-url';
import SiteHub from '../site-hub';
import ResizableFrame from '../resizable-frame';
import useSyncCanvasModeWithURL from '../sync-state-with-url/use-sync-canvas-mode-with-url';
import { unlock } from '../../lock-unlock';
import SavePanel from '../save-panel';
import KeyboardShortcutsRegister from '../keyboard-shortcuts/register';
import KeyboardShortcutsGlobal from '../keyboard-shortcuts/global';
import { useCommonCommands } from '../../hooks/commands/use-common-commands';
import { useEditModeCommands } from '../../hooks/commands/use-edit-mode-commands';
import { useIsSiteEditorLoading } from './hooks';
import useLayoutAreas from './router';
import useMovingAnimation from './animation';
import SidebarContent from '../sidebar';
import SaveHub from '../save-hub';
const { useCommands } = unlock( coreCommandsPrivateApis );
const { useCommandContext } = unlock( commandsPrivateApis );
const { useGlobalStyle } = unlock( blockEditorPrivateApis );
const { NavigableRegion } = unlock( editorPrivateApis );
const ANIMATION_DURATION = 0.3;
export default function Layout() {
// This ensures the edited entity id and type are initialized properly.
useInitEditedEntityFromURL();
useSyncCanvasModeWithURL();
useCommands();
useEditModeCommands();
useCommonCommands();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const {
isDistractionFree,
isZoomOutMode,
hasFixedToolbar,
hasBlockSelected,
canvasMode,
previousShortcut,
nextShortcut,
} = useSelect( ( select ) => {
const { getAllShortcutKeyCombinations } = select(
keyboardShortcutsStore
);
const { getCanvasMode } = unlock( select( editSiteStore ) );
return {
canvasMode: getCanvasMode(),
previousShortcut: getAllShortcutKeyCombinations(
'core/edit-site/previous-region'
),
nextShortcut: getAllShortcutKeyCombinations(
'core/edit-site/next-region'
),
hasFixedToolbar: select( preferencesStore ).get(
'core',
'fixedToolbar'
),
isDistractionFree: select( preferencesStore ).get(
'core',
'distractionFree'
),
isZoomOutMode:
select( blockEditorStore ).__unstableGetEditorMode() ===
'zoom-out',
hasBlockSelected:
select( blockEditorStore ).getBlockSelectionStart(),
};
}, [] );
const navigateRegionsProps = useNavigateRegions( {
previous: previousShortcut,
next: nextShortcut,
} );
const disableMotion = useReducedMotion();
const [ canvasResizer, canvasSize ] = useResizeObserver();
const [ fullResizer ] = useResizeObserver();
const isEditorLoading = useIsSiteEditorLoading();
const [ isResizableFrameOversized, setIsResizableFrameOversized ] =
useState( false );
const { key: routeKey, areas, widths } = useLayoutAreas();
const animationRef = useMovingAnimation( {
triggerAnimationOnChange: canvasMode + '__' + routeKey,
} );
// This determines which animation variant should apply to the header.
// There is also a `isDistractionFreeHovering` state that gets priority
// when hovering the `edit-site-layout__header-container` in distraction
// free mode. It's set via framer and trickles down to all the children
// so they can use this variant state too.
//
// TODO: The issue with this is we want to have the hover state stick when hovering
// a popover opened via the header. We'll probably need to lift this state to
// handle it ourselves. Also, focusWithin the header needs to be handled.
let headerAnimationState;
if ( canvasMode === 'view' ) {
// We need 'view' to always take priority so 'isDistractionFree'
// doesn't bleed over into the view (sidebar) state
headerAnimationState = 'view';
} else if ( isDistractionFree ) {
headerAnimationState = 'isDistractionFree';
} else {
headerAnimationState = canvasMode; // edit, view, init
}
// Sets the right context for the command palette
let commandContext = 'site-editor';
if ( canvasMode === 'edit' ) {
commandContext = 'site-editor-edit';
}
if ( hasBlockSelected ) {
commandContext = 'block-selection-edit';
}
useCommandContext( commandContext );
const [ backgroundColor ] = useGlobalStyle( 'color.background' );
const [ gradientValue ] = useGlobalStyle( 'color.gradient' );
// Synchronizing the URL with the store value of canvasMode happens in an effect
// This condition ensures the component is only rendered after the synchronization happens
// which prevents any animations due to potential canvasMode value change.
if ( canvasMode === 'init' ) {
return null;
}
return (
<>
<CommandMenu />
<KeyboardShortcutsRegister />
<KeyboardShortcutsGlobal />
{ fullResizer }
<div
{ ...navigateRegionsProps }
ref={ navigateRegionsProps.ref }
className={ classnames(
'edit-site-layout',
navigateRegionsProps.className,
{
'is-distraction-free':
isDistractionFree && canvasMode === 'edit',
'is-full-canvas': canvasMode === 'edit',
'has-fixed-toolbar': hasFixedToolbar,
'is-block-toolbar-visible': hasBlockSelected,
'is-zoom-out': isZoomOutMode,
}
) }
>
<motion.div
className="edit-site-layout__header-container"
variants={ {
isDistractionFree: {
opacity: 0,
transition: {
type: 'tween',
delay: 0.8,
delayChildren: 0.8,
}, // How long to wait before the header exits
},
isDistractionFreeHovering: {
opacity: 1,
transition: {
type: 'tween',
delay: 0.2,
delayChildren: 0.2,
}, // How long to wait before the header shows
},
view: { opacity: 1 },
edit: { opacity: 1 },
} }
whileHover={
isDistractionFree
? 'isDistractionFreeHovering'
: undefined
}
animate={ headerAnimationState }
>
<SiteHub
isTransparent={ isResizableFrameOversized }
className="edit-site-layout__hub"
/>
</motion.div>
<div className="edit-site-layout__content">
{ /*
The NavigableRegion must always be rendered and not use
`inert` otherwise `useNavigateRegions` will fail.
*/ }
{ ( ! isMobileViewport || ! areas.mobile ) && (
<NavigableRegion
ariaLabel={ __( 'Navigation' ) }
className="edit-site-layout__sidebar-region"
>
<AnimatePresence>
{ canvasMode === '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"
>
<SidebarContent routeKey={ routeKey }>
{ areas.sidebar }
</SidebarContent>
<SaveHub />
</motion.div>
) }
</AnimatePresence>
</NavigableRegion>
) }
{ isMobileViewport && areas.mobile && (
<div className="edit-site-layout__mobile">
{ areas.mobile }
</div>
) }
{ ! isMobileViewport &&
areas.content &&
canvasMode !== 'edit' && (
<div
className="edit-site-layout__area"
style={ {
maxWidth: widths?.content,
} }
>
{ areas.content }
</div>
) }
{ ! isMobileViewport && areas.preview && (
<div className="edit-site-layout__canvas-container">
{ canvasResizer }
{ !! canvasSize.width && (
<div
className={ classnames(
'edit-site-layout__canvas',
{
'is-right-aligned':
isResizableFrameOversized,
}
) }
ref={ animationRef }
>
<ErrorBoundary>
<ResizableFrame
isReady={ ! isEditorLoading }
isFullWidth={
canvasMode === 'edit'
}
defaultSize={ {
width:
canvasSize.width -
24 /* $canvas-padding */,
height: canvasSize.height,
} }
isOversized={
isResizableFrameOversized
}
setIsOversized={
setIsResizableFrameOversized
}
innerContentStyle={ {
background:
gradientValue ??
backgroundColor,
} }
>
{ areas.preview }
</ResizableFrame>
</ErrorBoundary>
</div>
) }
</div>
) }
</div>
<SavePanel />
</div>
</>
);
}