Add a featured video mode to the front page hero
A fourth hero mode beside brand, photos and livestream: a recorded video next to the words, under a "Featured video · <title>" badge. Unlike the livestream it never autoplays: an embed waits for play, and a file shows the browser's own player with preload="metadata". video_url takes a YouTube, Vimeo or Facebook link (turned into an embed, as the livestream's is) or a video file: a filename in public/front-page/, a /path, or an https URL ending in .mp4, .webm, .m4v, .mov or .ogv. Anything else that can't be framed still gets a "Watch the video" button. Files follow the embeds' https-only rule. Its own video_url and video_title columns rather than the livestream's, so both can be set up ahead of time and the mode switches between them. Migration 024 rebuilds front_page for the wider hero_mode CHECK; its children, keys and touch trigger survive. embeds.ts gains an autoplay switch shared by both modes, and videoSource() to tell a file from an embed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b10a263a90
commit
8c26ec4906
8 changed files with 243 additions and 22 deletions
|
|
@ -9,10 +9,21 @@
|
|||
through; anything that isn't https is refused, so the hero never
|
||||
frames a plain-http or javascript: URL.
|
||||
|
||||
Streams start muted: browsers only autoplay muted video, and a
|
||||
hero that starts shouting is worse than one that asks.
|
||||
Streams start playing, muted: browsers only autoplay muted
|
||||
video, and a hero that starts shouting is worse than one that
|
||||
asks. A featured video doesn't start at all — it's a recording,
|
||||
so the visitor presses play.
|
||||
|
||||
A featured video can also be a file rather than a link: a
|
||||
filename in public/front-page/, a /path, or an https URL ending
|
||||
in a video extension. videoSource() says which, so the hero can
|
||||
hand a file to <video> and a link to an <iframe>.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { heroVideo } from './media.ts'
|
||||
|
||||
const VIDEO_FILE = /\.(mp4|webm|m4v|mov|ogv)$/i
|
||||
|
||||
function youtubeId(url: URL): string | null {
|
||||
const host = url.hostname.replace(/^www\.|^m\./, '')
|
||||
if (host === 'youtu.be') return url.pathname.slice(1) || null
|
||||
|
|
@ -26,8 +37,8 @@ function youtubeId(url: URL): string | null {
|
|||
return match?.[1] ?? null
|
||||
}
|
||||
|
||||
/** An iframe src for a pasted stream link, or null if it can't be framed. */
|
||||
export function livestreamEmbedUrl(raw?: string | null): string | null {
|
||||
/** An iframe src for a pasted link, or null if it can't be framed. */
|
||||
function embedUrl(raw: string | null | undefined, autoplay: boolean): string | null {
|
||||
if (!raw) return null
|
||||
|
||||
let url: URL
|
||||
|
|
@ -40,7 +51,8 @@ export function livestreamEmbedUrl(raw?: string | null): string | null {
|
|||
|
||||
const yt = youtubeId(url)
|
||||
if (yt) {
|
||||
return `https://www.youtube-nocookie.com/embed/${encodeURIComponent(yt)}?autoplay=1&mute=1&playsinline=1`
|
||||
const play = autoplay ? '?autoplay=1&mute=1&playsinline=1' : '?playsinline=1'
|
||||
return `https://www.youtube-nocookie.com/embed/${encodeURIComponent(yt)}${play}`
|
||||
}
|
||||
|
||||
const host = url.hostname.replace(/^www\./, '')
|
||||
|
|
@ -48,15 +60,43 @@ export function livestreamEmbedUrl(raw?: string | null): string | null {
|
|||
if (host === 'vimeo.com') {
|
||||
const id = url.pathname.match(/^\/(?:event\/)?(\d+)/)?.[1]
|
||||
if (id) {
|
||||
const play = autoplay ? '?autoplay=1&muted=1' : ''
|
||||
return url.pathname.startsWith('/event/')
|
||||
? `https://vimeo.com/event/${id}/embed?autoplay=1&muted=1`
|
||||
: `https://player.vimeo.com/video/${id}?autoplay=1&muted=1`
|
||||
? `https://vimeo.com/event/${id}/embed${play}`
|
||||
: `https://player.vimeo.com/video/${id}${play}`
|
||||
}
|
||||
}
|
||||
|
||||
if ((host === 'facebook.com' || host === 'fb.watch') && !url.pathname.startsWith('/plugins/')) {
|
||||
return `https://www.facebook.com/plugins/video.php?href=${encodeURIComponent(url.href)}&autoplay=1&mute=1`
|
||||
const play = autoplay ? '&autoplay=1&mute=1' : ''
|
||||
return `https://www.facebook.com/plugins/video.php?href=${encodeURIComponent(url.href)}${play}`
|
||||
}
|
||||
|
||||
return url.href
|
||||
}
|
||||
|
||||
/** An iframe src for a pasted stream link, or null if it can't be framed. */
|
||||
export function livestreamEmbedUrl(raw?: string | null): string | null {
|
||||
return embedUrl(raw, true)
|
||||
}
|
||||
|
||||
export type VideoSource = { kind: 'file' | 'embed'; src: string }
|
||||
|
||||
/** How to play a featured video: a file for <video>, an embed for
|
||||
* an <iframe>, or null when it's neither (the hero then links to
|
||||
* it instead). Files follow the same https-only rule as embeds. */
|
||||
export function videoSource(raw?: string | null): VideoSource | null {
|
||||
const value = raw?.trim()
|
||||
if (!value) return null
|
||||
|
||||
if (VIDEO_FILE.test(value.split(/[?#]/)[0])) {
|
||||
if (/^https:\/\//i.test(value) || !/^[a-z][a-z0-9+.-]*:/i.test(value)) {
|
||||
const src = heroVideo(value)
|
||||
if (src) return { kind: 'file', src }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const src = embedUrl(value, false)
|
||||
return src ? { kind: 'embed', src } : null
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue