@wordpress/block-library
Version:
Block library for the WordPress editor.
143 lines (142 loc) • 5.61 kB
JavaScript
import { getBlockVariations } from "@wordpress/blocks";
import { matchesPatterns } from "../embed/util.mjs";
// packages/block-library/src/cover/embed-video-utils.js
const DEFAULT_EMBED_BLOCK = "core/embed";
const DEFAULT_VIDEO_PROVIDERS = [
"youtube",
"vimeo",
"videopress",
"animoto",
"tiktok",
"wordpress-tv"
];
/**
* Resolves the list of providers that are actually allowed.
*
* @param {string[]} [allowedVideoProviders] The providers configured via the block attribute.
* @return {string[]} The supported providers that remain allowed.
*/
function getAllowedVideoProviders(allowedVideoProviders) {
if (!Array.isArray(allowedVideoProviders)) return [];
return DEFAULT_VIDEO_PROVIDERS.filter((provider) => allowedVideoProviders.includes(provider));
}
/**
* Checks if a URL is a valid video embed URL from allowed providers.
*
* @param {string} url The URL to validate.
* @param {string[]} [allowedProviders] The allowed providers, Defaults to all
* supported providers.
* @return {boolean} True if the URL matches an allowed video provider pattern.
*/
function isValidVideoEmbedUrl(url, allowedProviders) {
if (!url) return false;
return findVideoEmbedProvider(url, allowedProviders) !== null;
}
/**
* Finds the embed provider for a given URL if it's an allowed video provider.
*
* @param {string} url The URL to check.
* @param {string[]} [allowedProviders] The allowed providers, Defaults to all
* supported providers.
* @return {string|null} The provider name slug (e.g., 'youtube') or null if not found.
*/
function getVideoEmbedProvider(url, allowedProviders) {
const embedBlock = findVideoEmbedProvider(url, allowedProviders);
return embedBlock ? embedBlock.name : null;
}
/**
* Finds a matching video embed block variation for the given URL.
*
* @param {string} url The URL to match against provider patterns.
* @param {string[]} [allowedProviders] The allowed providers, Defaults to all
* supported providers.
* @return {Object|null} The matching block variation or null if not found.
*/
function findVideoEmbedProvider(url, allowedProviders = DEFAULT_VIDEO_PROVIDERS) {
const embedVariations = getBlockVariations(DEFAULT_EMBED_BLOCK);
if (!embedVariations) return null;
const matchingVariation = embedVariations.find(({ patterns }) => matchesPatterns(url, patterns));
if (!matchingVariation || !allowedProviders.includes(matchingVariation.name)) return null;
return matchingVariation;
}
/**
* Modifies embed HTML to use background video parameters.
*
* @param {string} html The original embed HTML.
* @param {Object} options Options.
* @param {boolean} options.autoplay Whether the video should start playing on load.
* @return {string|null} The modified embed HTML, or null if not possible.
*/
function getBackgroundEmbedHtml(html, { autoplay = true } = {}) {
const srcMatch = html?.match(/src=["']([^"']+)["']/);
if (!srcMatch) return null;
const iframeSrc = srcMatch[1];
const backgroundSrc = getBackgroundVideoSrc(iframeSrc, { autoplay });
return html.replace(iframeSrc, backgroundSrc);
}
/**
* Detects the video provider from an iframe src URL.
*
* @param {string} src The iframe src URL.
* @return {string|null} The provider name slug or null if not recognized.
*/
function detectProviderFromSrc(src) {
if (!src) return null;
const lowerSrc = src.toLowerCase();
if (lowerSrc.includes("youtube.com") || lowerSrc.includes("youtu.be")) return "youtube";
if (lowerSrc.includes("vimeo.com")) return "vimeo";
if (lowerSrc.includes("videopress.com")) return "videopress";
if (lowerSrc.includes("animoto.com")) return "animoto";
if (lowerSrc.includes("tiktok.com")) return "tiktok";
if (lowerSrc.includes("wordpress.tv")) return "wordpress-tv";
return null;
}
/**
* Modifies an iframe src URL to add background video parameters.
* Automatically detects the provider from the URL.
*
* @param {string} src The iframe src URL.
* @param {Object} options Options.
* @param {boolean} options.autoplay Whether the video should start playing on load.
* @return {string} The modified URL.
*/
function getBackgroundVideoSrc(src, { autoplay = true } = {}) {
if (!src) return src;
try {
const url = new URL(src);
const provider = detectProviderFromSrc(src);
if (autoplay) url.searchParams.set("autoplay", "1");
switch (provider) {
case "youtube":
url.searchParams.set("mute", "1");
url.searchParams.set("loop", "1");
url.searchParams.set("controls", "0");
url.searchParams.set("showinfo", "0");
url.searchParams.set("modestbranding", "1");
url.searchParams.set("playsinline", "1");
url.searchParams.set("rel", "0");
const videoId = url.pathname.split("/").pop();
if (videoId) url.searchParams.set("playlist", videoId);
break;
case "vimeo":
url.searchParams.set("muted", "1");
url.searchParams.set("loop", "1");
if (autoplay) url.searchParams.set("background", "1");
url.searchParams.set("controls", "0");
break;
case "videopress":
case "wordpress-tv":
url.searchParams.set("loop", "1");
url.searchParams.set("muted", "1");
break;
default:
url.searchParams.set("muted", "1");
url.searchParams.set("loop", "1");
}
return url.toString();
} catch {
return src;
}
}
export { detectProviderFromSrc, getAllowedVideoProviders, getBackgroundEmbedHtml, getBackgroundVideoSrc, getVideoEmbedProvider, isValidVideoEmbedUrl };
//# sourceMappingURL=embed-video-utils.mjs.map