Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import clsx from 'clsx';
*/
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useEffect, useMemo, useRef } from '@wordpress/element';
import { Placeholder, Spinner } from '@wordpress/components';
import { Placeholder, SandBox, Spinner } from '@wordpress/components';
import { compose, useResizeObserver } from '@wordpress/compose';
import {
withColors,
Expand Down Expand Up @@ -49,7 +49,7 @@ import {
DEFAULT_OVERLAY_COLOR,
} from './color-utils';
import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants';
import { getIframeSrc, getBackgroundVideoSrc } from '../embed-video-utils';
import { getBackgroundEmbedHtml } from '../embed-video-utils';

function getInnerBlocksTemplate( attributes ) {
return [
Expand Down Expand Up @@ -365,23 +365,15 @@ function CoverEdit( {
[ url, backgroundType ]
);

// Compute embedSrc on-the-fly from embed preview for editor display
const embedSrc = useMemo( () => {
// Compute embed HTML for editor display via SandBox
const embedHtml = useMemo( () => {
if (
backgroundType !== EMBED_VIDEO_BACKGROUND_TYPE ||
! embedPreview?.html
) {
return null;
}

// Extract iframe src from embed HTML
const iframeSrc = getIframeSrc( embedPreview.html );
if ( ! iframeSrc ) {
return null;
}

// Modify the src to add background video parameters (provider auto-detected)
return getBackgroundVideoSrc( iframeSrc );
return getBackgroundEmbedHtml( embedPreview.html );
}, [ embedPreview, backgroundType ] );

const isUploadingMedia = isTemporaryMedia( id, url );
Expand Down Expand Up @@ -668,21 +660,22 @@ function CoverEdit( {
style={ mediaStyle }
/>
) }
{ isEmbedVideoBackground && embedSrc && (
{ isEmbedVideoBackground && embedHtml && (
<div
ref={ mediaElement }
className="wp-block-cover__video-background wp-block-cover__embed-background"
style={ mediaStyle }
>
<iframe
src={ embedSrc }
<SandBox
html={ embedHtml }
title="Background video"
frameBorder="0"
allow="autoplay; fullscreen"
styles={ [
'iframe{position:fixed;top:0;left:0;width:100%;height:100%;}',
] }
/>
</div>
) }
{ isEmbedVideoBackground && ! embedSrc && isFetchingEmbed && (
{ isEmbedVideoBackground && ! embedHtml && isFetchingEmbed && (
<Spinner />
) }

Expand Down
16 changes: 9 additions & 7 deletions packages/block-library/src/cover/embed-video-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,20 @@ function findVideoEmbedProvider( url ) {
}

/**
* Extracts iframe src from embed HTML.
* Modifies embed HTML to use background video parameters.
*
* @param {string} html The embed HTML.
* @return {string|null} The iframe src URL or null if not found.
* @param {string} html The original embed HTML.
* @return {string|null} The modified embed HTML, or null if not possible.
*/
export function getIframeSrc( html ) {
if ( ! html ) {
export function getBackgroundEmbedHtml( html ) {
const srcMatch = html?.match( /src=["']([^"']+)["']/ );
if ( ! srcMatch ) {
return null;
}

const srcMatch = html.match( /src=["']([^"']+)["']/ );
return srcMatch ? srcMatch[ 1 ] : null;
const iframeSrc = srcMatch[ 1 ];
const backgroundSrc = getBackgroundVideoSrc( iframeSrc );
return html.replace( iframeSrc, backgroundSrc );
}

/**
Expand Down
Loading