-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
190 lines (177 loc) · 5.85 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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useViewportMatch } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { __, _x } from '@wordpress/i18n';
import {
NavigableToolbar,
ToolSelector,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Button, ToolbarItem } from '@wordpress/components';
import { listView, plus } from '@wordpress/icons';
import { useCallback } from '@wordpress/element';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { store as preferencesStore } from '@wordpress/preferences';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editorStore } from '../../store';
import EditorHistoryRedo from '../editor-history/redo';
import EditorHistoryUndo from '../editor-history/undo';
function DocumentTools( { className, disableBlockTools = false } ) {
const { setIsInserterOpened, setIsListViewOpened } =
useDispatch( editorStore );
const {
isDistractionFree,
isInserterOpened,
isListViewOpen,
listViewShortcut,
inserterSidebarToggleRef,
listViewToggleRef,
hasFixedToolbar,
showIconLabels,
} = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const { get } = select( preferencesStore );
const {
isListViewOpened,
getEditorMode,
getInserterSidebarToggleRef,
getListViewToggleRef,
} = unlock( select( editorStore ) );
const { getShortcutRepresentation } = select( keyboardShortcutsStore );
const { __unstableGetEditorMode } = select( blockEditorStore );
return {
isInserterOpened: select( editorStore ).isInserterOpened(),
isListViewOpen: isListViewOpened(),
listViewShortcut: getShortcutRepresentation(
'core/editor/toggle-list-view'
),
inserterSidebarToggleRef: getInserterSidebarToggleRef(),
listViewToggleRef: getListViewToggleRef(),
hasFixedToolbar: getSettings().hasFixedToolbar,
showIconLabels: get( 'core', 'showIconLabels' ),
isDistractionFree: get( 'core', 'distractionFree' ),
isVisualMode: getEditorMode() === 'visual',
isZoomedOutView: __unstableGetEditorMode() === 'zoom-out',
};
}, [] );
const preventDefault = ( event ) => {
// Because the inserter behaves like a dialog,
// if the inserter is opened already then when we click on the toggle button
// then the initial click event will close the inserter and then be propagated
// to the inserter toggle and it will open it again.
// To prevent this we need to stop the propagation of the event.
// This won't be necessary when the inserter no longer behaves like a dialog.
if ( isInserterOpened ) {
event.preventDefault();
}
};
const isLargeViewport = useViewportMatch( 'medium' );
const isWideViewport = useViewportMatch( 'wide' );
/* translators: accessibility text for the editor toolbar */
const toolbarAriaLabel = __( 'Document tools' );
const toggleListView = useCallback(
() => setIsListViewOpened( ! isListViewOpen ),
[ setIsListViewOpened, isListViewOpen ]
);
const toggleInserter = useCallback(
() => setIsInserterOpened( ! isInserterOpened ),
[ isInserterOpened, setIsInserterOpened ]
);
/* 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 = ! isInserterOpened ? __( 'Add' ) : __( 'Close' );
return (
// Some plugins expect and use the `edit-post-header-toolbar` CSS class to
// find the toolbar and inject UI elements into it. This is not officially
// supported, but we're keeping it in the list of class names for backwards
// compatibility.
<NavigableToolbar
className={ clsx(
'editor-document-tools',
'edit-post-header-toolbar',
className
) }
aria-label={ toolbarAriaLabel }
variant="unstyled"
>
<div className="editor-document-tools__left">
{ ! isDistractionFree && (
<ToolbarItem
ref={ inserterSidebarToggleRef }
as={ Button }
className="editor-document-tools__inserter-toggle"
variant="primary"
isPressed={ isInserterOpened }
onMouseDown={ preventDefault }
onClick={ toggleInserter }
disabled={ disableBlockTools }
icon={ plus }
label={ showIconLabels ? shortLabel : longLabel }
showTooltip={ ! showIconLabels }
aria-expanded={ isInserterOpened }
/>
) }
{ ( isWideViewport || ! showIconLabels ) && (
<>
{ isLargeViewport && ! hasFixedToolbar && (
<ToolbarItem
as={ ToolSelector }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
disabled={ disableBlockTools }
size="compact"
/>
) }
<ToolbarItem
as={ EditorHistoryUndo }
showTooltip={ ! showIconLabels }
variant={ showIconLabels ? 'tertiary' : undefined }
size="compact"
/>
<ToolbarItem
as={ EditorHistoryRedo }
showTooltip={ ! showIconLabels }
variant={ showIconLabels ? 'tertiary' : undefined }
size="compact"
/>
{ ! isDistractionFree && (
<ToolbarItem
as={ Button }
className="editor-document-tools__document-overview-toggle"
icon={ listView }
disabled={ disableBlockTools }
isPressed={ isListViewOpen }
/* translators: button label text should, if possible, be under 16 characters. */
label={ __( 'Document Overview' ) }
onClick={ toggleListView }
shortcut={ listViewShortcut }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
aria-expanded={ isListViewOpen }
ref={ listViewToggleRef }
size="compact"
/>
) }
</>
) }
</div>
</NavigableToolbar>
);
}
export default DocumentTools;