Skip to content

Commit

Permalink
Merge pull request #2771 from owid/video-autoplay
Browse files Browse the repository at this point in the history
Video autoplay
  • Loading branch information
ikesau authored Oct 16, 2023
2 parents db8f0c3 + 77209cd commit b5ccd01
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions db/model/Gdoc/exampleEnrichedBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export const enrichedBlockExamples: Record<
filename: "https://ourworldindata.org/assets/images/example-poster.jpg",
caption: boldLinkExampleText,
shouldLoop: true,
shouldAutoplay: false,
parseErrors: [],
},
list: {
Expand Down
15 changes: 15 additions & 0 deletions db/model/Gdoc/rawToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,14 @@ const parseVideo = (raw: RawBlockVideo): EnrichedBlockVideo => {
url: string = "",
filename: string = "",
shouldLoop: boolean = false,
shouldAutoplay: boolean = false,
caption?: Span[]
): EnrichedBlockVideo => ({
type: "video",
url,
filename,
shouldLoop,
shouldAutoplay,
caption,
parseErrors: [error],
})
Expand Down Expand Up @@ -594,6 +596,18 @@ const parseVideo = (raw: RawBlockVideo): EnrichedBlockVideo => {
})
}

const shouldAutoplay = raw.value.shouldAutoplay
if (
!!shouldAutoplay &&
shouldAutoplay !== "true" &&
shouldAutoplay !== "false"
) {
return createError({
message:
"If specified, shouldAutoplay property must be true or false",
})
}

const caption = raw.value.caption
? htmlToSpans(raw.value.caption)
: undefined
Expand All @@ -604,6 +618,7 @@ const parseVideo = (raw: RawBlockVideo): EnrichedBlockVideo => {
filename,
caption,
shouldLoop: shouldLoop === "true",
shouldAutoplay: shouldAutoplay === "true",
parseErrors: [],
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/utils/src/owidTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ export type RawBlockVideo = {
url?: string
caption?: string
shouldLoop?: string
shouldAutoplay?: string
filename?: string
}
}
Expand All @@ -696,6 +697,7 @@ export type EnrichedBlockVideo = {
type: "video"
url: string
shouldLoop: boolean
shouldAutoplay: boolean
filename: string
caption?: Span[]
} & EnrichedBlockWithParseErrors
Expand Down
1 change: 1 addition & 0 deletions site/gdocs/ArticleBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export default function ArticleBlock({
className={getLayout("video", containerType)}
url={block.url}
shouldLoop={block.shouldLoop}
shouldAutoplay={block.shouldAutoplay}
caption={block.caption}
filename={block.filename}
/>
Expand Down
12 changes: 10 additions & 2 deletions site/gdocs/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ interface VideoProps {
caption?: Span[]
className?: string
shouldLoop?: boolean
shouldAutoplay?: boolean
filename: string
}

export default function Video(props: VideoProps) {
const { url, caption, className, shouldLoop, filename } = props
const { url, caption, className, shouldLoop, shouldAutoplay, filename } =
props
const { isPreviewing } = useContext(DocumentContext)
const posterSrc = isPreviewing
? `${IMAGE_HOSTING_CDN_URL}/${IMAGE_HOSTING_BUCKET_SUBFOLDER_PATH}/${filename}`
: `${IMAGES_DIRECTORY}${filename}`
return (
<figure className={cx(className)}>
<video controls preload="none" loop={shouldLoop} poster={posterSrc}>
<video
controls
autoPlay={shouldAutoplay}
preload={shouldAutoplay ? "auto" : "none"}
loop={shouldLoop}
poster={posterSrc}
>
<source src={url} type="video/mp4" />
</video>
{caption ? <figcaption>{renderSpans(caption)}</figcaption> : null}
Expand Down
4 changes: 4 additions & 0 deletions site/gdocs/centered-article.scss
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ h3.article-block__heading {

.article-block__video {
margin: 24px 0 32px 0;
@include body-3-medium-italic;
color: $blue-60;
text-align: center;
a {
@include owid-link-90;
}
Expand All @@ -150,6 +153,7 @@ h3.article-block__heading {

.article-block__image-caption {
@include body-3-medium-italic;
color: $blue-60;
text-align: center;
}

Expand Down

0 comments on commit b5ccd01

Please sign in to comment.