-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathedit.js
More file actions
313 lines (294 loc) · 8.13 KB
/
Copy pathedit.js
File metadata and controls
313 lines (294 loc) · 8.13 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { isBlobURL } from '@wordpress/blob';
import {
Disabled,
Spinner,
Placeholder,
__experimentalToolsPanel as ToolsPanel,
} from '@wordpress/components';
import {
BlockControls,
BlockIcon,
InspectorControls,
MediaPlaceholder,
MediaReplaceFlow,
useBlockProps,
useBlockEditingMode,
} from '@wordpress/block-editor';
import { useRef, useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { video as icon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { prependHTTPS } from '@wordpress/url';
/**
* Internal dependencies
*/
import { createUpgradedEmbedBlock } from '../embed/util';
import {
useUploadMediaFromBlobURL,
useToolsPanelDropdownMenuProps,
} from '../utils/hooks';
import VideoCommonSettings from './edit-common-settings';
import TracksEditor from './tracks-editor';
import Tracks from './tracks';
import { Caption } from '../utils/caption';
import PosterImage from '../utils/poster-image';
import { isGifVariation } from './variations';
import GifRestoreControl from './gif-restore-control';
import VideoOriginalControl from './video-original-control';
const ALLOWED_MEDIA_TYPES = [ 'video' ];
function VideoEdit( {
isSelected: isSingleSelected,
attributes,
className,
setAttributes,
insertBlocksAfter,
onReplace,
clientId,
} ) {
const videoPlayer = useRef();
const { id, controls, poster, src, tracks } = attributes;
const isGif = isGifVariation( attributes );
const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const blockEditingMode = useBlockEditingMode();
const hasNonContentControls = blockEditingMode === 'default';
useUploadMediaFromBlobURL( {
url: temporaryURL,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectVideo,
onError: onUploadError,
} );
useEffect( () => {
// Placeholder may be rendered.
if ( videoPlayer.current ) {
videoPlayer.current.load();
}
}, [ poster ] );
// The GIF variation plays like an animated GIF in the editor (the playback
// attributes are applied to the preview <video> below). Regular videos do
// not autoplay in the editor, so only nudge GIFs into playing after a
// source change in case the muted autoplay did not start on its own.
useEffect( () => {
if ( isGif ) {
// Browsers allow muted videos to be played programmatically.
videoPlayer.current?.play().catch( () => {} );
}
}, [ isGif, src, poster ] );
// TODO: Whether the video was obtained from the media library or was provided by URL, obtain the `videoWidth` and `videoHeight` of the video once its metadata has loaded and persist in the block attributes.
function onSelectVideo( media ) {
if ( ! media || ! media.url ) {
// In this case there was an error
// previous attributes should be removed
// because they may be temporary blob urls.
setAttributes( {
src: undefined,
id: undefined,
poster: undefined,
caption: undefined,
blob: undefined,
} );
setTemporaryURL();
return;
}
if ( isBlobURL( media.url ) ) {
setTemporaryURL( media.url );
return;
}
// Prefer the web-safe transcoded companion when available: the
// original video stays the attachment (media.id) but the block plays
// the optimized version. `media_details.optimized_video` is only set
// on transcoded video attachments, so its presence is a sufficient
// signal to swap the playback source. The author can switch back to
// the original from the toolbar (see VideoOriginalControl).
let nextSrc = media.url;
if ( media.media_details?.optimized_video ) {
const dir = media.url.slice( 0, media.url.lastIndexOf( '/' ) + 1 );
nextSrc = dir + media.media_details.optimized_video;
}
// Sets the block's attribute and updates the edit component from the
// selected media.
setAttributes( {
blob: undefined,
src: nextSrc,
id: media.id,
poster:
media.image?.src !== media.icon ? media.image?.src : undefined,
caption: media.caption,
} );
setTemporaryURL();
}
function onSelectURL( newSrc ) {
if ( newSrc !== src ) {
const url = prependHTTPS( newSrc );
// Check if there's an embed block that handles this URL.
const embedBlock = createUpgradedEmbedBlock( {
attributes: { url },
} );
if ( undefined !== embedBlock && onReplace ) {
onReplace( embedBlock );
return;
}
setAttributes( {
blob: undefined,
src: url,
id: undefined,
poster: undefined,
} );
setTemporaryURL();
}
}
const { createErrorNotice } = useDispatch( noticesStore );
function onUploadError( message ) {
createErrorNotice( message, { type: 'snackbar' } );
}
// Much of this description is duplicated from MediaPlaceholder.
const placeholder = ( content ) => {
return (
<Placeholder
className="block-editor-media-placeholder"
withIllustration={ ! isSingleSelected }
icon={ icon }
label={ __( 'Video' ) }
instructions={ __(
'Drag and drop a video, upload, or choose from your library.'
) }
>
{ content }
</Placeholder>
);
};
const classes = clsx( className, {
'is-transient': !! temporaryURL,
} );
const blockProps = useBlockProps( {
className: classes,
} );
if ( ! src && ! temporaryURL ) {
return (
<div { ...blockProps }>
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
onSelect={ onSelectVideo }
onSelectURL={ onSelectURL }
accept="video/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ attributes }
onError={ onUploadError }
placeholder={ placeholder }
/>
</div>
);
}
return (
<>
{ isSingleSelected && (
<>
<BlockControls>
<TracksEditor
tracks={ tracks }
onChange={ ( newTracks ) => {
setAttributes( { tracks: newTracks } );
} }
/>
</BlockControls>
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ id }
mediaURL={ src }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept="video/*"
onSelect={ onSelectVideo }
onSelectURL={ onSelectURL }
onError={ onUploadError }
onReset={ () => onSelectVideo( undefined ) }
variant="toolbar"
/>
</BlockControls>
{ isGif && (
<GifRestoreControl
attributes={ attributes }
clientId={ clientId }
/>
) }
{ ! isGif && (
<VideoOriginalControl
attributes={ attributes }
setAttributes={ setAttributes }
/>
) }
</>
) }
<InspectorControls>
<ToolsPanel
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
autoplay: false,
controls: true,
loop: false,
muted: false,
playsInline: false,
preload: 'metadata',
poster: undefined,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
>
<VideoCommonSettings
setAttributes={ setAttributes }
attributes={ attributes }
/>
<PosterImage
poster={ poster }
onChange={ ( posterImage ) =>
setAttributes( {
poster: posterImage?.url,
} )
}
/>
</ToolsPanel>
</InspectorControls>
<figure { ...blockProps }>
{ /*
Disable the video tag if the block is not selected
so the user clicking on it won't play the
video when the controls are enabled.
*/ }
<Disabled isDisabled={ ! isSingleSelected }>
<video
controls={ controls }
poster={ poster }
src={ src || temporaryURL }
ref={ videoPlayer }
autoPlay={ isGif }
loop={ isGif }
muted={ isGif }
playsInline={ isGif }
>
<Tracks tracks={ tracks } />
</video>
</Disabled>
{ !! temporaryURL && <Spinner /> }
<Caption
attributes={ attributes }
setAttributes={ setAttributes }
isSelected={ isSingleSelected }
insertBlocksAfter={ insertBlocksAfter }
label={ __( 'Video caption text' ) }
showToolbarButton={
isSingleSelected && hasNonContentControls
}
/>
</figure>
</>
);
}
export default VideoEdit;