-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
207 lines (195 loc) · 5.51 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
/**
* WordPress dependencies
*/
import {
BlockEditorKeyboardShortcuts,
BlockEditorProvider,
BlockTools,
__unstableUseBlockSelectionClearer as useBlockSelectionClearer,
} from '@wordpress/block-editor';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { Popover, SlotFillProvider, Spinner } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect, useMemo, useState } from '@wordpress/element';
import {
InterfaceSkeleton,
ComplementaryArea,
store as interfaceStore,
} from '@wordpress/interface';
import { __ } from '@wordpress/i18n';
import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
/**
* Internal dependencies
*/
import UnselectedMenuState from './unselected-menu-state';
import {
IsMenuNameControlFocusedContext,
useNavigationEditor,
useMenuNotifications,
} from '../../hooks';
import ErrorBoundary from '../error-boundary';
import NavigationEditorShortcuts from './shortcuts';
import Sidebar from '../sidebar';
import Header from '../header';
import Notices from '../notices';
import Editor from '../editor';
import InserterSidebar from '../inserter-sidebar';
import UnsavedChangesWarning from './unsaved-changes-warning';
import { store as editNavigationStore } from '../../store';
import {
NAVIGATION_POST_KIND,
NAVIGATION_POST_POST_TYPE,
} from '../../constants';
const interfaceLabels = {
/* translators: accessibility text for the navigation screen top bar landmark region. */
header: __( 'Navigation top bar' ),
/* translators: accessibility text for the navigation screen content landmark region. */
body: __( 'Navigation menu blocks' ),
/* translators: accessibility text for the navigation screen settings landmark region. */
sidebar: __( 'Navigation settings' ),
secondarySidebar: __( 'Block library' ),
};
export default function Layout( { blockEditorSettings } ) {
const contentAreaRef = useBlockSelectionClearer();
const [ isMenuNameControlFocused, setIsMenuNameControlFocused ] = useState(
false
);
const { saveNavigationPost } = useDispatch( editNavigationStore );
const savePost = () => saveNavigationPost( navigationPost );
const {
menus,
hasLoadedMenus,
hasFinishedInitialLoad,
selectedMenuId,
navigationPost,
isMenuBeingDeleted,
selectMenu,
deleteMenu,
isMenuSelected,
} = useNavigationEditor();
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
NAVIGATION_POST_KIND,
NAVIGATION_POST_POST_TYPE,
{
id: navigationPost?.id,
}
);
const { hasSidebarEnabled, isInserterOpened } = useSelect(
( select ) => ( {
hasSidebarEnabled: !! select(
interfaceStore
).getActiveComplementaryArea( 'core/edit-navigation' ),
isInserterOpened: select( editNavigationStore ).isInserterOpened(),
} ),
[]
);
useEffect( () => {
if ( ! selectedMenuId && menus?.length ) {
selectMenu( menus[ 0 ].id );
}
}, [ selectedMenuId, menus ] );
useMenuNotifications( selectedMenuId );
const hasMenus = !! menus?.length;
const isBlockEditorReady = !! (
hasMenus &&
navigationPost &&
isMenuSelected
);
return (
<ErrorBoundary>
<ShortcutProvider>
<div
hidden={ ! isMenuBeingDeleted }
className={ 'edit-navigation-layout__overlay' }
/>
<SlotFillProvider>
<BlockEditorKeyboardShortcuts.Register />
<NavigationEditorShortcuts.Register />
<NavigationEditorShortcuts saveBlocks={ savePost } />
<BlockEditorProvider
value={ blocks }
onInput={ onInput }
onChange={ onChange }
settings={ {
...blockEditorSettings,
templateLock: 'all',
} }
useSubRegistry={ false }
>
<IsMenuNameControlFocusedContext.Provider
value={ useMemo(
() => [
isMenuNameControlFocused,
setIsMenuNameControlFocused,
],
[ isMenuNameControlFocused ]
) }
>
<InterfaceSkeleton
className="edit-navigation-layout"
labels={ interfaceLabels }
header={
<Header
isMenuSelected={ isMenuSelected }
isPending={ ! hasLoadedMenus }
menus={ menus }
navigationPost={ navigationPost }
/>
}
content={
<>
<Notices />
{ ! hasFinishedInitialLoad && (
<Spinner />
) }
{ ! isMenuSelected &&
hasFinishedInitialLoad && (
<UnselectedMenuState
onSelectMenu={ selectMenu }
onCreate={ selectMenu }
menus={ menus }
/>
) }
{ isBlockEditorReady && (
<div
className="edit-navigation-layout__content-area"
ref={ contentAreaRef }
>
<BlockTools>
<Editor
isPending={
! hasLoadedMenus
}
/>
</BlockTools>
</div>
) }
</>
}
sidebar={
hasSidebarEnabled && (
<ComplementaryArea.Slot scope="core/edit-navigation" />
)
}
secondarySidebar={
isInserterOpened && <InserterSidebar />
}
/>
{ isMenuSelected && (
<Sidebar
menus={ menus }
menuId={ selectedMenuId }
onSelectMenu={ selectMenu }
onDeleteMenu={ deleteMenu }
isMenuBeingDeleted={ isMenuBeingDeleted }
/>
) }
</IsMenuNameControlFocusedContext.Provider>
<UnsavedChangesWarning />
</BlockEditorProvider>
<Popover.Slot />
</SlotFillProvider>
</ShortcutProvider>
</ErrorBoundary>
);
}