-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
212 lines (196 loc) · 5.49 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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useViewportMatch, useReducedMotion } from '@wordpress/compose';
import {
BlockToolbar,
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useEffect, useRef, useState } from '@wordpress/element';
import { PinnedItems } from '@wordpress/interface';
import { __ } from '@wordpress/i18n';
import { next, previous } from '@wordpress/icons';
import {
Button,
__unstableMotion as motion,
Popover,
} from '@wordpress/components';
import { store as preferencesStore } from '@wordpress/preferences';
import {
DocumentBar,
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
/**
* Internal dependencies
*/
import MoreMenu from './more-menu';
import SaveButton from '../save-button';
import DocumentTools from './document-tools';
import { store as editSiteStore } from '../../store';
import {
getEditorCanvasContainerTitle,
useHasEditorCanvasContainer,
} from '../editor-canvas-container';
import { unlock } from '../../lock-unlock';
import { FOCUSABLE_ENTITIES } from '../../utils/constants';
const { useShowBlockTools } = unlock( blockEditorPrivateApis );
const { PostViewLink, PreviewDropdown } = unlock( editorPrivateApis );
export default function HeaderEditMode() {
const {
templateType,
isDistractionFree,
blockEditorMode,
blockSelectionStart,
showIconLabels,
editorCanvasView,
} = useSelect( ( select ) => {
const { getEditedPostType } = select( editSiteStore );
const { getBlockSelectionStart, __unstableGetEditorMode } =
select( blockEditorStore );
const { get: getPreference } = select( preferencesStore );
const { getDeviceType } = select( editorStore );
return {
deviceType: getDeviceType(),
templateType: getEditedPostType(),
blockEditorMode: __unstableGetEditorMode(),
blockSelectionStart: getBlockSelectionStart(),
showIconLabels: getPreference( 'core', 'showIconLabels' ),
editorCanvasView: unlock(
select( editSiteStore )
).getEditorCanvasContainerView(),
isDistractionFree: getPreference( 'core', 'distractionFree' ),
};
}, [] );
const isLargeViewport = useViewportMatch( 'medium' );
const { showFixedToolbar } = useShowBlockTools();
const showTopToolbar = isLargeViewport && showFixedToolbar;
const blockToolbarRef = useRef();
const disableMotion = useReducedMotion();
const hasDefaultEditorCanvasView = ! useHasEditorCanvasContainer();
const isFocusMode = FOCUSABLE_ENTITIES.includes( templateType );
const isZoomedOutView = blockEditorMode === 'zoom-out';
const [ isBlockToolsCollapsed, setIsBlockToolsCollapsed ] =
useState( true );
useEffect( () => {
// If we have a new block selection, show the block tools
if ( blockSelectionStart ) {
setIsBlockToolsCollapsed( false );
}
}, [ blockSelectionStart ] );
const toolbarVariants = {
isDistractionFree: { y: '-50px' },
isDistractionFreeHovering: { y: 0 },
view: { y: 0 },
edit: { y: 0 },
};
const toolbarTransition = {
type: 'tween',
duration: disableMotion ? 0 : 0.2,
ease: 'easeOut',
};
return (
<div
className={ classnames( 'edit-site-header-edit-mode', {
'show-icon-labels': showIconLabels,
} ) }
>
{ hasDefaultEditorCanvasView && (
<motion.div
className="edit-site-header-edit-mode__start"
variants={ toolbarVariants }
transition={ toolbarTransition }
>
<DocumentTools
blockEditorMode={ blockEditorMode }
isDistractionFree={ isDistractionFree }
/>
{ showTopToolbar && (
<>
<div
className={ classnames(
'selected-block-tools-wrapper',
{
'is-collapsed': isBlockToolsCollapsed,
}
) }
>
<BlockToolbar hideDragHandle />
</div>
<Popover.Slot
ref={ blockToolbarRef }
name="block-toolbar"
/>
<Button
className="edit-site-header-edit-mode__block-tools-toggle"
icon={ isBlockToolsCollapsed ? next : previous }
onClick={ () => {
setIsBlockToolsCollapsed(
( collapsed ) => ! collapsed
);
} }
label={
isBlockToolsCollapsed
? __( 'Show block tools' )
: __( 'Hide block tools' )
}
size="compact"
/>
</>
) }
</motion.div>
) }
{ ! isDistractionFree && (
<div
className={ classnames(
'edit-site-header-edit-mode__center',
{
'is-collapsed':
! isBlockToolsCollapsed && showTopToolbar,
}
) }
>
{ ! hasDefaultEditorCanvasView ? (
getEditorCanvasContainerTitle( editorCanvasView )
) : (
<DocumentBar />
) }
</div>
) }
<div className="edit-site-header-edit-mode__end">
<motion.div
className="edit-site-header-edit-mode__actions"
variants={ toolbarVariants }
transition={ toolbarTransition }
>
{ isLargeViewport && (
<div
className={ classnames(
'edit-site-header-edit-mode__preview-options',
{ 'is-zoomed-out': isZoomedOutView }
) }
>
<PreviewDropdown
disabled={
isFocusMode || ! hasDefaultEditorCanvasView
}
/>
</div>
) }
<PostViewLink />
<SaveButton size="compact" />
{ ! isDistractionFree && (
<PinnedItems.Slot scope="core/edit-site" />
) }
<MoreMenu showIconLabels={ showIconLabels } />
</motion.div>
</div>
</div>
);
}