-
Notifications
You must be signed in to change notification settings - Fork 798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Video Tracks Support to VideoPress Block #21578
Changes from 10 commits
7291421
66d0e38
d73e357
90b4d43
57714b6
3003771
eefd566
5cc245d
84ff4f6
fdceff2
129e285
0e27b4b
9849bd0
c0883c5
bb30f90
09bf973
340028f
62149e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: enhancement | ||
|
||
Added captions/subtitles support to the VideoPress block. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ import Loading from './loading'; | |
import { getVideoPressUrl } from './url'; | ||
import { getClassNames } from './utils'; | ||
import SeekbarColorSettings from './seekbar-color-settings'; | ||
import TracksEditor from './tracks-editor'; | ||
|
||
const VIDEO_POSTER_ALLOWED_MEDIA_TYPES = [ 'image' ]; | ||
|
||
|
@@ -80,6 +81,8 @@ const VideoPressEdit = CoreVideoEdit => | |
const { guid } = this.props.attributes; | ||
if ( ! guid ) { | ||
await this.setGuid(); | ||
} else { | ||
this.setTracks( guid ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so, as I'd like it to still fetch the tracks even if the |
||
} | ||
|
||
this.setRating(); | ||
|
@@ -151,6 +154,7 @@ const VideoPressEdit = CoreVideoEdit => | |
const guid = get( media, 'jetpack_videopress.guid' ); | ||
if ( guid ) { | ||
setAttributes( { guid } ); | ||
this.setTracks( guid ); | ||
} else { | ||
this.fallbackToCore(); | ||
} | ||
|
@@ -192,6 +196,35 @@ const VideoPressEdit = CoreVideoEdit => | |
return media; | ||
}; | ||
|
||
setTracks = async guid => { | ||
const { setAttributes } = this.props; | ||
|
||
if ( ! guid ) { | ||
return; | ||
} | ||
|
||
await apiFetch( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this throw if the request fails? In async/await the catch portion becomes an exception we should probably handle. |
||
url: `https://public-api.wordpress.com/rest/v1.1/videos/${ guid }`, | ||
credentials: 'omit', | ||
global: true, | ||
} ).then( videoInfo => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we use async/await then the On the other hand, since this code seems pretty Promise-based, we can just go all in and: 1. get rid of await 2. add catch/finally to the promise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in bb30f90! I don't think we need to take any action in a |
||
// Convert API object response to an array that works better with the tracks editor component | ||
const tracks = []; | ||
Object.keys( videoInfo.tracks ).forEach( kind => { | ||
for ( const srcLang in videoInfo.tracks[ kind ] ) { | ||
const track = videoInfo.tracks[ kind ][ srcLang ]; | ||
tracks.push( { | ||
src: track.src, | ||
kind, | ||
srcLang, | ||
label: track.label, | ||
} ); | ||
} | ||
} ); | ||
setAttributes( { videoPressTracks: tracks } ); | ||
} ); | ||
}; | ||
|
||
switchToEditing = () => { | ||
this.props.setAttributes( { | ||
id: undefined, | ||
|
@@ -293,12 +326,32 @@ const VideoPressEdit = CoreVideoEdit => | |
setAttributes, | ||
} = this.props; | ||
const { fallback, isFetchingMedia, isUpdatingRating, interactive, rating } = this.state; | ||
const { autoplay, caption, controls, loop, muted, playsinline, poster, preload } = attributes; | ||
const { | ||
autoplay, | ||
caption, | ||
controls, | ||
guid, | ||
loop, | ||
muted, | ||
playsinline, | ||
poster, | ||
preload, | ||
videoPressTracks, | ||
} = attributes; | ||
|
||
const videoPosterDescription = `video-block__poster-image-description-${ instanceId }`; | ||
|
||
const blockSettings = ( | ||
<Fragment> | ||
<BlockControls group="block"> | ||
<TracksEditor | ||
tracks={ videoPressTracks } | ||
onChange={ newTracks => { | ||
setAttributes( { videoPressTracks: newTracks } ); | ||
} } | ||
guid={ guid } | ||
/> | ||
</BlockControls> | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a chance that this call will throw? Should we wrap in a try/catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From looking at
setGuid
, I think it doesn't throw, but it doesfall back
to the core block if something goes wrong. So I think safe to leave as-is.