-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
247 lines (233 loc) · 6.06 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
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import {
FormFileUpload,
NavigableMenu,
MenuItem,
ToolbarButton,
Dropdown,
withFilters,
Tooltip,
} from '@wordpress/components';
import { useSelect, withDispatch } from '@wordpress/data';
import { DOWN } from '@wordpress/keycodes';
import {
postFeaturedImage,
upload,
media as mediaIcon,
} from '@wordpress/icons';
import { compose } from '@wordpress/compose';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import MediaUpload from '../media-upload';
import MediaUploadCheck from '../media-upload/check';
import LinkControl from '../link-control';
import { store as blockEditorStore } from '../../store';
const noop = () => {};
let uniqueId = 0;
const MediaReplaceFlow = ( {
mediaURL,
mediaId,
mediaIds,
allowedTypes,
accept,
onError,
onSelect,
onSelectURL,
onToggleFeaturedImage,
useFeaturedImage,
onFilesUpload = noop,
name = __( 'Replace' ),
createNotice,
removeNotice,
children,
multiple = false,
addToGallery,
handleUpload = true,
} ) => {
const mediaUpload = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().mediaUpload;
}, [] );
const editMediaButtonRef = useRef();
const errorNoticeID = `block-editor/media-replace-flow/error-notice/${ ++uniqueId }`;
const onUploadError = ( message ) => {
const safeMessage = stripHTML( message );
if ( onError ) {
onError( safeMessage );
return;
}
// We need to set a timeout for showing the notice
// so that VoiceOver and possibly other screen readers
// can announce the error afer the toolbar button
// regains focus once the upload dialog closes.
// Otherwise VO simply skips over the notice and announces
// the focused element and the open menu.
setTimeout( () => {
createNotice( 'error', safeMessage, {
speak: true,
id: errorNoticeID,
isDismissible: true,
} );
}, 1000 );
};
const selectMedia = ( media, closeMenu ) => {
if ( useFeaturedImage && onToggleFeaturedImage ) {
onToggleFeaturedImage();
}
closeMenu();
// Calling `onSelect` after the state update since it might unmount the component.
onSelect( media );
speak( __( 'The media file has been replaced' ) );
removeNotice( errorNoticeID );
};
const uploadFiles = ( event, closeMenu ) => {
const files = event.target.files;
if ( ! handleUpload ) {
closeMenu();
return onSelect( files );
}
onFilesUpload( files );
mediaUpload( {
allowedTypes,
filesList: files,
onFileChange: ( [ media ] ) => {
selectMedia( media, closeMenu );
},
onError: onUploadError,
} );
};
const openOnArrowDown = ( event ) => {
if ( event.keyCode === DOWN ) {
event.preventDefault();
event.target.click();
}
};
const onlyAllowsImages = () => {
if ( ! allowedTypes || allowedTypes.length === 0 ) {
return false;
}
return allowedTypes.every(
( allowedType ) =>
allowedType === 'image' || allowedType.startsWith( 'image/' )
);
};
const gallery = multiple && onlyAllowsImages();
const POPOVER_PROPS = {
isAlternate: true,
};
return (
<Dropdown
popoverProps={ POPOVER_PROPS }
contentClassName="block-editor-media-replace-flow__options"
renderToggle={ ( { isOpen, onToggle } ) => (
<ToolbarButton
ref={ editMediaButtonRef }
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
onKeyDown={ openOnArrowDown }
>
{ name }
</ToolbarButton>
) }
renderContent={ ( { onClose } ) => (
<>
<NavigableMenu className="block-editor-media-replace-flow__media-upload-menu">
<>
<MediaUpload
gallery={ gallery }
addToGallery={ addToGallery }
multiple={ multiple }
value={ multiple ? mediaIds : mediaId }
onSelect={ ( media ) =>
selectMedia( media, onClose )
}
allowedTypes={ allowedTypes }
render={ ( { open } ) => (
<MenuItem
icon={ mediaIcon }
onClick={ open }
>
{ __( 'Open Media Library' ) }
</MenuItem>
) }
/>
<MediaUploadCheck>
<FormFileUpload
onChange={ ( event ) => {
uploadFiles( event, onClose );
} }
accept={ accept }
multiple={ multiple }
render={ ( { openFileDialog } ) => {
return (
<MenuItem
icon={ upload }
onClick={ () => {
openFileDialog();
} }
>
{ __( 'Upload' ) }
</MenuItem>
);
} }
/>
</MediaUploadCheck>
</>
{ onToggleFeaturedImage && (
<MenuItem
icon={ postFeaturedImage }
onClick={ onToggleFeaturedImage }
isPressed={ useFeaturedImage }
>
{ __( 'Use featured image' ) }
</MenuItem>
) }
{ children }
</NavigableMenu>
{ onSelectURL && (
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
<form className="block-editor-media-flow__url-input">
<span className="block-editor-media-replace-flow__image-url-label">
{ __( 'Current media URL:' ) }
</span>
<Tooltip text={ mediaURL } position="bottom">
<div>
<LinkControl
value={ { url: mediaURL } }
settings={ [] }
showSuggestions={ false }
onChange={ ( { url } ) => {
onSelectURL( url );
editMediaButtonRef.current.focus();
} }
/>
</div>
</Tooltip>
</form>
) }
</>
) }
/>
);
};
/**
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/media-replace-flow/README.md
*/
export default compose( [
withDispatch( ( dispatch ) => {
const { createNotice, removeNotice } = dispatch( noticesStore );
return {
createNotice,
removeNotice,
};
} ),
withFilters( 'editor.MediaReplaceFlow' ),
] )( MediaReplaceFlow );