-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
280 lines (271 loc) · 7.98 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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
AutosaveMonitor,
LocalAutosaveMonitor,
UnsavedChangesWarning,
EditorNotices,
EditorKeyboardShortcutsRegister,
store as editorStore,
} from '@wordpress/editor';
import { AsyncModeProvider, useSelect, useDispatch } from '@wordpress/data';
import { BlockBreadcrumb } from '@wordpress/block-editor';
import { Button, ScrollLock, Popover } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { PluginArea } from '@wordpress/plugins';
import { __ } from '@wordpress/i18n';
import {
ComplementaryArea,
FullscreenMode,
InterfaceSkeleton,
store as interfaceStore,
} from '@wordpress/interface';
import { useState, useEffect, useCallback } from '@wordpress/element';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
/**
* Internal dependencies
*/
import TextEditor from '../text-editor';
import VisualEditor from '../visual-editor';
import EditPostKeyboardShortcuts from '../keyboard-shortcuts';
import KeyboardShortcutHelpModal from '../keyboard-shortcut-help-modal';
import ManageBlocksModal from '../manage-blocks-modal';
import PreferencesModal from '../preferences-modal';
import BrowserURL from '../browser-url';
import Header from '../header';
import InserterSidebar from '../secondary-sidebar/inserter-sidebar';
import ListViewSidebar from '../secondary-sidebar/list-view-sidebar';
import SettingsSidebar from '../sidebar/settings-sidebar';
import MetaBoxes from '../meta-boxes';
import WelcomeGuide from '../welcome-guide';
import ActionsPanel from './actions-panel';
import { store as editPostStore } from '../../store';
const interfaceLabels = {
secondarySidebar: __( 'Block library' ),
/* translators: accessibility text for the editor top bar landmark region. */
header: __( 'Editor top bar' ),
/* 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' ),
};
function Layout( { styles } ) {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isHugeViewport = useViewportMatch( 'huge', '>=' );
const {
openGeneralSidebar,
closeGeneralSidebar,
setIsInserterOpened,
} = useDispatch( editPostStore );
const {
mode,
isFullscreenActive,
isRichEditingEnabled,
sidebarIsOpened,
hasActiveMetaboxes,
hasFixedToolbar,
previousShortcut,
nextShortcut,
hasBlockSelected,
isInserterOpened,
isListViewOpened,
showIconLabels,
hasReducedUI,
showBlockBreadcrumbs,
isTemplateMode,
} = useSelect( ( select ) => {
const editorSettings = select( editorStore ).getEditorSettings();
return {
isTemplateMode: select( editPostStore ).isEditingTemplate(),
hasFixedToolbar: select( editPostStore ).isFeatureActive(
'fixedToolbar'
),
sidebarIsOpened: !! (
select( interfaceStore ).getActiveComplementaryArea(
editPostStore.name
) || select( editPostStore ).isPublishSidebarOpened()
),
isFullscreenActive: select( editPostStore ).isFeatureActive(
'fullscreenMode'
),
isInserterOpened: select( editPostStore ).isInserterOpened(),
isListViewOpened: select( editPostStore ).isListViewOpened(),
mode: select( editPostStore ).getEditorMode(),
isRichEditingEnabled: editorSettings.richEditingEnabled,
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
previousShortcut: select(
keyboardShortcutsStore
).getAllShortcutRawKeyCombinations(
'core/edit-post/previous-region'
),
nextShortcut: select(
keyboardShortcutsStore
).getAllShortcutRawKeyCombinations( 'core/edit-post/next-region' ),
showIconLabels: select( editPostStore ).isFeatureActive(
'showIconLabels'
),
hasReducedUI: select( editPostStore ).isFeatureActive(
'reducedUI'
),
showBlockBreadcrumbs: select( editPostStore ).isFeatureActive(
'showBlockBreadcrumbs'
),
};
}, [] );
const className = classnames( 'edit-post-layout', 'is-mode-' + mode, {
'is-sidebar-opened': sidebarIsOpened,
'has-fixed-toolbar': hasFixedToolbar,
'has-metaboxes': hasActiveMetaboxes,
'show-icon-labels': showIconLabels,
} );
const openSidebarPanel = () =>
openGeneralSidebar(
hasBlockSelected ? 'edit-post/block' : 'edit-post/document'
);
// Inserter and Sidebars are mutually exclusive
useEffect( () => {
if ( sidebarIsOpened && ! isHugeViewport ) {
setIsInserterOpened( false );
}
}, [ sidebarIsOpened, isHugeViewport ] );
useEffect( () => {
if ( isInserterOpened && ! isHugeViewport ) {
closeGeneralSidebar();
}
}, [ isInserterOpened, isHugeViewport ] );
// Local state for save panel.
// Note 'truthy' callback implies an open panel.
const [
entitiesSavedStatesCallback,
setEntitiesSavedStatesCallback,
] = useState( false );
const closeEntitiesSavedStates = useCallback(
( arg ) => {
if ( typeof entitiesSavedStatesCallback === 'function' ) {
entitiesSavedStatesCallback( arg );
}
setEntitiesSavedStatesCallback( false );
},
[ entitiesSavedStatesCallback ]
);
const secondarySidebar = () => {
if ( mode === 'visual' && isInserterOpened ) {
return <InserterSidebar />;
}
if ( mode === 'visual' && isListViewOpened ) {
return (
<AsyncModeProvider value="true">
<ListViewSidebar />
</AsyncModeProvider>
);
}
return null;
};
return (
<>
<FullscreenMode isActive={ isFullscreenActive } />
<BrowserURL />
<UnsavedChangesWarning />
<AutosaveMonitor />
<LocalAutosaveMonitor />
<EditPostKeyboardShortcuts />
<EditorKeyboardShortcutsRegister />
<SettingsSidebar />
<InterfaceSkeleton
className={ className }
labels={ interfaceLabels }
header={
<Header
setEntitiesSavedStatesCallback={
setEntitiesSavedStatesCallback
}
/>
}
secondarySidebar={ secondarySidebar() }
sidebar={
( ! isMobileViewport || sidebarIsOpened ) && (
<>
{ ! isMobileViewport && ! sidebarIsOpened && (
<div className="edit-post-layout__toggle-sidebar-panel">
<Button
variant="secondary"
className="edit-post-layout__toggle-sidebar-panel-button"
onClick={ openSidebarPanel }
aria-expanded={ false }
>
{ hasBlockSelected
? __( 'Open block settings' )
: __( 'Open document settings' ) }
</Button>
</div>
) }
<ComplementaryArea.Slot scope="core/edit-post" />
</>
)
}
content={
<>
<EditorNotices />
{ ( mode === 'text' || ! isRichEditingEnabled ) && (
<TextEditor />
) }
{ isRichEditingEnabled && mode === 'visual' && (
<VisualEditor styles={ styles } />
) }
{ ! isTemplateMode && (
<div className="edit-post-layout__metaboxes">
<MetaBoxes location="normal" />
<MetaBoxes location="advanced" />
</div>
) }
{ isMobileViewport && sidebarIsOpened && (
<ScrollLock />
) }
</>
}
footer={
! hasReducedUI &&
showBlockBreadcrumbs &&
! isMobileViewport &&
isRichEditingEnabled &&
mode === 'visual' && (
<div className="edit-post-layout__footer">
<BlockBreadcrumb />
</div>
)
}
actions={
<ActionsPanel
closeEntitiesSavedStates={ closeEntitiesSavedStates }
isEntitiesSavedStatesOpen={
entitiesSavedStatesCallback
}
setEntitiesSavedStatesCallback={
setEntitiesSavedStatesCallback
}
/>
}
shortcuts={ {
previous: previousShortcut,
next: nextShortcut,
} }
/>
<ManageBlocksModal />
<PreferencesModal />
<KeyboardShortcutHelpModal />
<WelcomeGuide />
<Popover.Slot />
<PluginArea />
</>
);
}
export default Layout;