-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
269 lines (251 loc) · 7.64 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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useCallback, useRef } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
import { store as coreStore } from '@wordpress/core-data';
import {
ToolSelector,
__experimentalPreviewOptions as PreviewOptions,
NavigableToolbar,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import { PinnedItems } from '@wordpress/interface';
import { _x, __ } from '@wordpress/i18n';
import { listView, plus, external, chevronUpDown } from '@wordpress/icons';
import {
Button,
ToolbarItem,
MenuGroup,
MenuItem,
VisuallyHidden,
} from '@wordpress/components';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { store as preferencesStore } from '@wordpress/preferences';
/**
* Internal dependencies
*/
import MoreMenu from './more-menu';
import SaveButton from '../save-button';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import DocumentActions from './document-actions';
import { store as editSiteStore } from '../../store';
import { useHasStyleBook } from '../style-book';
const preventDefault = ( event ) => {
event.preventDefault();
};
export default function HeaderEditMode() {
const inserterButton = useRef();
const {
deviceType,
templateType,
isInserterOpen,
isListViewOpen,
listViewShortcut,
isVisualMode,
blockEditorMode,
homeUrl,
showIconLabels,
} = useSelect( ( select ) => {
const {
__experimentalGetPreviewDeviceType,
getEditedPostType,
isInserterOpened,
isListViewOpened,
getEditorMode,
} = select( editSiteStore );
const { getShortcutRepresentation } = select( keyboardShortcutsStore );
const { __unstableGetEditorMode } = select( blockEditorStore );
const postType = getEditedPostType();
const {
getUnstableBase, // Site index.
} = select( coreStore );
return {
deviceType: __experimentalGetPreviewDeviceType(),
templateType: postType,
isInserterOpen: isInserterOpened(),
isListViewOpen: isListViewOpened(),
listViewShortcut: getShortcutRepresentation(
'core/edit-site/toggle-list-view'
),
isVisualMode: getEditorMode() === 'visual',
blockEditorMode: __unstableGetEditorMode(),
homeUrl: getUnstableBase()?.home,
showIconLabels: select( preferencesStore ).get(
'core/edit-site',
'showIconLabels'
),
};
}, [] );
const {
__experimentalSetPreviewDeviceType: setPreviewDeviceType,
setIsInserterOpened,
setIsListViewOpened,
} = useDispatch( editSiteStore );
const { __unstableSetEditorMode } = useDispatch( blockEditorStore );
const isLargeViewport = useViewportMatch( 'medium' );
const toggleInserter = useCallback( () => {
if ( isInserterOpen ) {
// Focusing the inserter button should close the inserter popover.
// However, there are some cases it won't close when the focus is lost.
// See https://github.com/WordPress/gutenberg/issues/43090 for more details.
inserterButton.current.focus();
setIsInserterOpened( false );
} else {
setIsInserterOpened( true );
}
}, [ isInserterOpen, setIsInserterOpened ] );
const toggleListView = useCallback(
() => setIsListViewOpened( ! isListViewOpen ),
[ setIsListViewOpened, isListViewOpen ]
);
const hasStyleBook = useHasStyleBook();
const isFocusMode = templateType === 'wp_template_part';
/* translators: button label text should, if possible, be under 16 characters. */
const longLabel = _x(
'Toggle block inserter',
'Generic label for block inserter button'
);
const shortLabel = ! isInserterOpen ? __( 'Add' ) : __( 'Close' );
const isZoomedOutViewExperimentEnabled =
window?.__experimentalEnableZoomedOutView && isVisualMode;
const isZoomedOutView = blockEditorMode === 'zoom-out';
return (
<div
className={ classnames( 'edit-site-header-edit-mode', {
'show-icon-labels': showIconLabels,
} ) }
>
{ ! hasStyleBook && (
<NavigableToolbar
className="edit-site-header-edit-mode__start"
aria-label={ __( 'Document tools' ) }
>
<div className="edit-site-header-edit-mode__toolbar">
<ToolbarItem
ref={ inserterButton }
as={ Button }
className="edit-site-header-edit-mode__inserter-toggle"
variant="primary"
isPressed={ isInserterOpen }
onMouseDown={ preventDefault }
onClick={ toggleInserter }
disabled={ ! isVisualMode }
icon={ plus }
label={ showIconLabels ? shortLabel : longLabel }
showTooltip={ ! showIconLabels }
/>
{ isLargeViewport && (
<>
<ToolbarItem
as={ ToolSelector }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
disabled={ ! isVisualMode }
/>
<ToolbarItem
as={ UndoButton }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
/>
<ToolbarItem
as={ RedoButton }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
/>
<ToolbarItem
as={ Button }
className="edit-site-header-edit-mode__list-view-toggle"
disabled={
! isVisualMode || isZoomedOutView
}
icon={ listView }
isPressed={ isListViewOpen }
/* translators: button label text should, if possible, be under 16 characters. */
label={ __( 'List View' ) }
onClick={ toggleListView }
shortcut={ listViewShortcut }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
/>
{ isZoomedOutViewExperimentEnabled && (
<ToolbarItem
as={ Button }
className="edit-site-header-edit-mode__zoom-out-view-toggle"
icon={ chevronUpDown }
isPressed={ isZoomedOutView }
/* translators: button label text should, if possible, be under 16 characters. */
label={ __( 'Zoom-out View' ) }
onClick={ () => {
setPreviewDeviceType( 'desktop' );
__unstableSetEditorMode(
isZoomedOutView
? 'edit'
: 'zoom-out'
);
} }
/>
) }
</>
) }
</div>
</NavigableToolbar>
) }
<div className="edit-site-header-edit-mode__center">
{ hasStyleBook ? __( 'Style Book' ) : <DocumentActions /> }
</div>
<div className="edit-site-header-edit-mode__end">
<div className="edit-site-header-edit-mode__actions">
{ ! isFocusMode && ! hasStyleBook && (
<div
className={ classnames(
'edit-site-header-edit-mode__preview-options',
{ 'is-zoomed-out': isZoomedOutView }
) }
>
<PreviewOptions
deviceType={ deviceType }
setDeviceType={ setPreviewDeviceType }
/* translators: button label text should, if possible, be under 16 characters. */
viewLabel={ __( 'View' ) }
>
<MenuGroup>
<MenuItem
href={ homeUrl }
target="_blank"
icon={ external }
>
{ __( 'View site' ) }
<VisuallyHidden as="span">
{
/* translators: accessibility text */
__( '(opens in a new tab)' )
}
</VisuallyHidden>
</MenuItem>
</MenuGroup>
</PreviewOptions>
</div>
) }
<SaveButton />
<PinnedItems.Slot scope="core/edit-site" />
<MoreMenu showIconLabels={ showIconLabels } />
</div>
</div>
</div>
);
}