diff --git a/projects/plugins/jetpack/extensions/blocks/podcast-player/edit.js b/projects/plugins/jetpack/extensions/blocks/podcast-player/edit.js index 2b27e22288402..0d229dec4f7a8 100644 --- a/projects/plugins/jetpack/extensions/blocks/podcast-player/edit.js +++ b/projects/plugins/jetpack/extensions/blocks/podcast-player/edit.js @@ -7,7 +7,7 @@ import { debounce, noop } from 'lodash'; /** * WordPress dependencies */ -import { useCallback, useEffect, useRef, useReducer } from '@wordpress/element'; +import { useCallback, useEffect, useState, useRef, useReducer } from '@wordpress/element'; import { Button, ExternalLink, @@ -45,11 +45,12 @@ import { queueMusic } from './icons/'; import { isAtomicSite, isSimpleSite } from '../../shared/site-type-utils'; import attributesValidation from './attributes'; import PodcastPlayer from './components/podcast-player'; -import { makeCancellable, __maybeInjectStylesIntoSiteEditor } from './utils'; +import { makeCancellable } from './utils'; import { fetchPodcastFeed } from './api'; import { podcastPlayerReducer, actions } from './state'; import { applyFallbackStyles } from '../../shared/apply-fallback-styles'; import { PODCAST_FEED, EMBED_BLOCK } from './constants'; +import { maybeCopyElementsToSiteEditorContext } from '../../shared/block-editor-asset-loader'; const DEFAULT_MIN_ITEMS = 1; const DEFAULT_MAX_ITEMS = 10; @@ -94,6 +95,9 @@ const PodcastPlayerEdit = ( { const playerId = `jetpack-podcast-player-block-${ instanceId }`; + const podCastPlayerRef = useRef(); + const [ hasMigratedStyles, setHasMigratedStyles ] = useState( false ); + // State. const cancellableFetch = useRef(); const [ { selectedGuid, checkUrl, ...state }, dispatch ] = useReducer( podcastPlayerReducer, { @@ -156,13 +160,25 @@ const PodcastPlayerEdit = ( { [ replaceWithEmbedBlock, setAttributes ] ); + // Call once on mount or unmount (the return callback). useEffect( () => { - __maybeInjectStylesIntoSiteEditor(); return () => { cancellableFetch?.current?.cancel?.(); }; }, [] ); + // The Podcast player audio element requires wpmedialement styles. + // These aren't available in the Site Editor context, so we have to copy them in. + useEffect( () => { + if ( ! hasMigratedStyles && podCastPlayerRef.current ) { + maybeCopyElementsToSiteEditorContext( + [ 'link#mediaelement-css', 'link#wp-mediaelement-css' ], + podCastPlayerRef.current + ); + setHasMigratedStyles( true ); + } + }, [ hasMigratedStyles ] ); + // Load RSS feed initially and when the feed or selected episode changes. useEffect( () => { // Don't do anything if no url is set. @@ -375,7 +391,7 @@ const PodcastPlayerEdit = ( { -
+
{ + const parentElementToCopy = parentDoc.querySelector( selector ); + const isElementAlreadyPresentInCurrentWindow = !! currentDoc.querySelector( selector ); + if ( parentElementToCopy && ! isElementAlreadyPresentInCurrentWindow ) { + currentDoc.head.appendChild( parentElementToCopy.cloneNode() ); + parentElementToCopy.remove(); + return true; + } + return false; + } ); + + return results; + } +}