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
|
|
@ -950,8 +950,9 @@ const timeline = {
|
|||
|
||||
One record (see the singleton note in server/src/admin-schema.js).
|
||||
The groups are the hero; the collections below are the rest of
|
||||
the page. Photos and the livestream are editable whatever the
|
||||
mode, so either can be ready before the switch is flipped.
|
||||
the page. Photos, the livestream and the featured video are
|
||||
editable whatever the mode, so each can be ready before the
|
||||
switch is flipped.
|
||||
|
||||
Section keys and stat sources are the CHECK lists on
|
||||
front_page_sections and front_page_stats. The labels here are what the admin reads; the values are
|
||||
|
|
@ -1000,6 +1001,7 @@ const frontPage = {
|
|||
["brand", "Brand — animated colour, no media"],
|
||||
["photos", "Photos — cycles through the photos below"],
|
||||
["livestream", "Livestream — embeds the stream with a LIVE badge"],
|
||||
["video", "Featured video — shows the video below, click to play"],
|
||||
],
|
||||
blankLabel: "— brand —",
|
||||
help: "Switch to Livestream when you go live, and back when you're done",
|
||||
|
|
@ -1014,7 +1016,7 @@ const frontPage = {
|
|||
],
|
||||
},
|
||||
{
|
||||
legend: "Photos and livestream",
|
||||
legend: "Photos, livestream and video",
|
||||
fields: [
|
||||
{
|
||||
path: "slide_seconds",
|
||||
|
|
@ -1029,6 +1031,15 @@ const frontPage = {
|
|||
help: "A YouTube, Facebook or Vimeo link, or any embed URL",
|
||||
},
|
||||
{ path: "livestream_title", label: "Livestream title", full: true },
|
||||
{
|
||||
path: "video_url",
|
||||
label: "Featured video",
|
||||
full: true,
|
||||
help:
|
||||
"A YouTube, Vimeo or Facebook link, or a video file: a filename in " +
|
||||
"public/front-page/ or a link ending in .mp4 or .webm",
|
||||
},
|
||||
{ path: "video_title", label: "Video title", full: true, help: "Shown after \"Featured video\" above the headline" },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,10 @@ export const awardLogo = inDir('/award-logos') // ⚠ guess
|
|||
the Front page editor, which names the directory in its help. */
|
||||
export const heroPhoto = inDir('/front-page')
|
||||
|
||||
/* front_page.video_url when it names a file rather than a link:
|
||||
same directory as the photos. */
|
||||
export const heroVideo = inDir('/front-page')
|
||||
|
||||
/* content_blocks.media, which can be an image on any owner's page,
|
||||
so it can't share a per-entity directory. */
|
||||
export const blockMedia = inDir('/media') // ⚠ guess
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
import { useRecord, type Resource } from './useRecord.ts'
|
||||
import type { EventSeries } from './eventSeries.ts'
|
||||
|
||||
export type HeroMode = 'brand' | 'photos' | 'livestream'
|
||||
export type HeroMode = 'brand' | 'photos' | 'livestream' | 'video'
|
||||
|
||||
/** The CHECK list on front_page_sections.section. */
|
||||
export type FrontPageSectionKey =
|
||||
|
|
@ -43,6 +43,8 @@ export type Hero = {
|
|||
slide_seconds: number
|
||||
slides: HeroSlide[]
|
||||
livestream: { url: string; title?: string | null } | null
|
||||
/** A link to embed or a video file; see videoSource in embeds.ts. */
|
||||
video: { url: string; title?: string | null } | null
|
||||
}
|
||||
|
||||
export type FrontPageSection = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue