-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
ui.js
443 lines (401 loc) · 12.3 KB
/
ui.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* WordPress dependencies
*/
import {
Navigator,
useNavigator,
createSlotFill,
DropdownMenu,
MenuGroup,
MenuItem,
} from '@wordpress/components';
import { getBlockTypes, store as blocksStore } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
import {
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { store as preferencesStore } from '@wordpress/preferences';
import { moreVertical } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect, Fragment } from '@wordpress/element';
import { usePrevious } from '@wordpress/compose';
/**
* Internal dependencies
*/
import ScreenRoot from './screen-root';
import {
useBlockHasGlobalStyles,
default as ScreenBlockList,
} from './screen-block-list';
import ScreenBlock from './screen-block';
import ScreenTypography from './screen-typography';
import ScreenTypographyElement from './screen-typography-element';
import FontSize from './font-sizes/font-size';
import FontSizes from './font-sizes/font-sizes';
import ScreenColors from './screen-colors';
import ScreenColorPalette from './screen-color-palette';
import ScreenBackground from './screen-background';
import { ScreenShadows, ScreenShadowsEdit } from './screen-shadows';
import ScreenLayout from './screen-layout';
import ScreenStyleVariations from './screen-style-variations';
import StyleBook from '../style-book';
import ScreenCSS from './screen-css';
import ScreenRevisions from './screen-revisions';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import { STYLE_BOOK_COLOR_GROUPS } from '../style-book/constants';
const SLOT_FILL_NAME = 'GlobalStylesMenu';
const { useGlobalStylesReset } = unlock( blockEditorPrivateApis );
const { Slot: GlobalStylesMenuSlot, Fill: GlobalStylesMenuFill } =
createSlotFill( SLOT_FILL_NAME );
function GlobalStylesActionMenu() {
const [ canReset, onReset ] = useGlobalStylesReset();
const { toggle } = useDispatch( preferencesStore );
const { canEditCSS } = useSelect( ( select ) => {
const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } =
select( coreStore );
const globalStylesId = __experimentalGetCurrentGlobalStylesId();
const globalStyles = globalStylesId
? getEntityRecord( 'root', 'globalStyles', globalStylesId )
: undefined;
return {
canEditCSS: !! globalStyles?._links?.[ 'wp:action-edit-css' ],
};
}, [] );
const { setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);
const loadCustomCSS = () => {
setEditorCanvasContainerView( 'global-styles-css' );
};
return (
<GlobalStylesMenuFill>
<DropdownMenu
icon={ moreVertical }
label={ __( 'More' ) }
toggleProps={ { size: 'compact' } }
>
{ ( { onClose } ) => (
<>
<MenuGroup>
{ canEditCSS && (
<MenuItem onClick={ loadCustomCSS }>
{ __( 'Additional CSS' ) }
</MenuItem>
) }
<MenuItem
onClick={ () => {
toggle(
'core/edit-site',
'welcomeGuideStyles'
);
onClose();
} }
>
{ __( 'Welcome Guide' ) }
</MenuItem>
</MenuGroup>
<MenuGroup>
<MenuItem
onClick={ () => {
onReset();
onClose();
} }
disabled={ ! canReset }
>
{ __( 'Reset styles' ) }
</MenuItem>
</MenuGroup>
</>
) }
</DropdownMenu>
</GlobalStylesMenuFill>
);
}
function GlobalStylesNavigationScreen( { className, ...props } ) {
return (
<Navigator.Screen
className={ [
'edit-site-global-styles-sidebar__navigator-screen',
className,
]
.filter( Boolean )
.join( ' ' ) }
{ ...props }
/>
);
}
function BlockStylesNavigationScreens( {
parentMenu,
blockStyles,
blockName,
} ) {
return blockStyles.map( ( style, index ) => (
<GlobalStylesNavigationScreen
key={ index }
path={ parentMenu + '/variations/' + style.name }
>
<ScreenBlock name={ blockName } variation={ style.name } />
</GlobalStylesNavigationScreen>
) );
}
function ContextScreens( { name, parentMenu = '' } ) {
const blockStyleVariations = useSelect(
( select ) => {
const { getBlockStyles } = select( blocksStore );
return getBlockStyles( name );
},
[ name ]
);
return (
<>
<GlobalStylesNavigationScreen
path={ parentMenu + '/colors/palette' }
>
<ScreenColorPalette name={ name } />
</GlobalStylesNavigationScreen>
{ !! blockStyleVariations?.length && (
<BlockStylesNavigationScreens
parentMenu={ parentMenu }
blockStyles={ blockStyleVariations }
blockName={ name }
/>
) }
</>
);
}
function GlobalStylesStyleBook() {
const navigator = useNavigator();
const { path } = navigator.location;
return (
<StyleBook
isSelected={ ( blockName ) =>
// Match '/blocks/core%2Fbutton' and
// '/blocks/core%2Fbutton/typography', but not
// '/blocks/core%2Fbuttons'.
path === `/blocks/${ encodeURIComponent( blockName ) }` ||
path.startsWith(
`/blocks/${ encodeURIComponent( blockName ) }/`
)
}
onSelect={ ( blockName ) => {
if (
STYLE_BOOK_COLOR_GROUPS.find(
( group ) => group.slug === blockName
)
) {
// Go to color palettes Global Styles.
navigator.goTo( '/colors/palette' );
return;
}
// Now go to the selected block.
navigator.goTo( '/blocks/' + encodeURIComponent( blockName ) );
} }
/>
);
}
function GlobalStylesBlockLink() {
const navigator = useNavigator();
const { selectedBlockName, selectedBlockClientId } = useSelect(
( select ) => {
const { getSelectedBlockClientId, getBlockName } =
select( blockEditorStore );
const clientId = getSelectedBlockClientId();
return {
selectedBlockName: getBlockName( clientId ),
selectedBlockClientId: clientId,
};
},
[]
);
const blockHasGlobalStyles = useBlockHasGlobalStyles( selectedBlockName );
// When we're in the `Blocks` screen enable deep linking to the selected block.
useEffect( () => {
if ( ! selectedBlockClientId || ! blockHasGlobalStyles ) {
return;
}
const currentPath = navigator.location.path;
if (
currentPath !== '/blocks' &&
! currentPath.startsWith( '/blocks/' )
) {
return;
}
const newPath = '/blocks/' + encodeURIComponent( selectedBlockName );
// Avoid navigating to the same path. This can happen when selecting
// a new block of the same type.
if ( newPath !== currentPath ) {
navigator.goTo( newPath, { skipFocus: true } );
}
}, [ selectedBlockClientId, selectedBlockName, blockHasGlobalStyles ] );
}
function GlobalStylesEditorCanvasContainerLink() {
const { goTo, location } = useNavigator();
const editorCanvasContainerView = useSelect(
( select ) =>
unlock( select( editSiteStore ) ).getEditorCanvasContainerView(),
[]
);
const path = location?.path;
const isRevisionsOpen = path === '/revisions';
// If the user switches the editor canvas container view, redirect
// to the appropriate screen. This effectively allows deep linking to the
// desired screens from outside the global styles navigation provider.
useEffect( () => {
switch ( editorCanvasContainerView ) {
case 'global-styles-revisions':
case 'global-styles-revisions:style-book':
if ( ! isRevisionsOpen ) {
goTo( '/revisions' );
}
break;
case 'global-styles-css':
goTo( '/css' );
break;
// The stand-alone style book is open
// and the revisions panel is open,
// close the revisions panel.
// Otherwise keep the style book open while
// browsing global styles panel.
//
// Falling through as it matches the default scenario.
case 'style-book':
default:
// In general, if the revision screen is in view but the
// `editorCanvasContainerView` is not a revision view, close it.
// This also includes the scenario when the stand-alone style
// book is open, in which case we want the user to close the
// revisions screen and browse global styles.
if ( isRevisionsOpen ) {
goTo( '/', { isBack: true } );
}
break;
}
}, [ editorCanvasContainerView, isRevisionsOpen, goTo ] );
}
function useNavigatorSync( parentPath, onPathChange ) {
const navigator = useNavigator();
const { path: childPath } = navigator.location;
const previousParentPath = usePrevious( parentPath );
const previousChildPath = usePrevious( childPath );
useEffect( () => {
if ( parentPath !== childPath ) {
if ( parentPath !== previousParentPath ) {
navigator.goTo( parentPath );
} else if ( childPath !== previousChildPath ) {
onPathChange( childPath );
}
}
}, [
onPathChange,
parentPath,
previousChildPath,
previousParentPath,
childPath,
navigator,
] );
}
// This component is used to wrap the hook in order to conditionally execute it
// when the parent component is used on controlled mode.
function NavigationSync( { path: parentPath, onPathChange, children } ) {
useNavigatorSync( parentPath, onPathChange );
return children;
}
function GlobalStylesUI( { path, onPathChange } ) {
const blocks = getBlockTypes();
const editorCanvasContainerView = useSelect(
( select ) =>
unlock( select( editSiteStore ) ).getEditorCanvasContainerView(),
[]
);
return (
<Navigator
className="edit-site-global-styles-sidebar__navigator-provider"
initialPath="/"
>
{ path && onPathChange && (
<NavigationSync path={ path } onPathChange={ onPathChange } />
) }
<GlobalStylesNavigationScreen path="/">
<ScreenRoot />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/variations">
<ScreenStyleVariations />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/blocks">
<ScreenBlockList />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography">
<ScreenTypography />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography/font-sizes">
<FontSizes />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography/font-sizes/:origin/:slug">
<FontSize />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography/text">
<ScreenTypographyElement element="text" />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography/link">
<ScreenTypographyElement element="link" />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography/heading">
<ScreenTypographyElement element="heading" />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography/caption">
<ScreenTypographyElement element="caption" />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/typography/button">
<ScreenTypographyElement element="button" />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/colors">
<ScreenColors />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/shadows">
<ScreenShadows />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/shadows/edit/:category/:slug">
<ScreenShadowsEdit />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/layout">
<ScreenLayout />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/css">
<ScreenCSS />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/revisions">
<ScreenRevisions />
</GlobalStylesNavigationScreen>
<GlobalStylesNavigationScreen path="/background">
<ScreenBackground />
</GlobalStylesNavigationScreen>
{ blocks.map( ( block ) => (
<GlobalStylesNavigationScreen
key={ 'menu-block-' + block.name }
path={ '/blocks/' + encodeURIComponent( block.name ) }
>
<ScreenBlock name={ block.name } />
</GlobalStylesNavigationScreen>
) ) }
<ContextScreens />
{ blocks.map( ( block ) => (
<ContextScreens
key={ 'screens-block-' + block.name }
name={ block.name }
parentMenu={ '/blocks/' + encodeURIComponent( block.name ) }
/>
) ) }
{ 'style-book' === editorCanvasContainerView && (
<GlobalStylesStyleBook />
) }
<GlobalStylesActionMenu />
<GlobalStylesBlockLink />
<GlobalStylesEditorCanvasContainerLink />
</Navigator>
);
}
export { GlobalStylesMenuSlot };
export default GlobalStylesUI;