import clsx from 'clsx'; import { __, _x, sprintf } from '@wordpress/i18n'; import { useState, useEffect } from '@wordpress/element'; import { useDispatch, useSelect } from '@wordpress/data'; import { useBlockProps, store as blockEditorStore, } from '@wordpress/block-editor'; import { store as coreStore } from '@wordpress/core-data'; import { Caption } from '../utils/caption'; import { createUpgradedEmbedBlock, findMoreSuitableBlock, getClassNames, rewriteXToTwitter, removeAspectRatioClasses, fallback, getEmbedInfoByProvider, getMergedAttributesWithPreview, } from './util'; import EmbedControls from './embed-controls'; import { embedContentIcon } from './icons'; import EmbedLoading from './embed-loading'; import EmbedPlaceholder from './embed-placeholder'; import EmbedPreview from './embed-preview'; import type { EmbedEditProps, EmbedPreviewData } from './types'; const EmbedEdit = ( props: EmbedEditProps ) => { const { attributes: { providerNameSlug, previewable, responsive, url: attributesUrl, }, attributes, isSelected, onReplace, setAttributes, insertBlocksAfter, } = props; const defaultEmbedInfo = { title: _x( 'Embed', 'block title' ), icon: embedContentIcon, }; const { icon, title } = getEmbedInfoByProvider( providerNameSlug ) || defaultEmbedInfo; const [ url, setURL ] = useState( attributesUrl ); const [ isEditingURL, setIsEditingURL ] = useState( false ); const { invalidateResolution } = useDispatch( coreStore ); const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); const { preview, fetching, themeSupportsResponsive, cannotEmbed, hasResolved, } = useSelect( ( select ) => { const { getEmbedPreview, isPreviewEmbedFallback, getThemeSupports, hasFinishedResolution, } = select( coreStore ); if ( ! attributesUrl ) { return { fetching: false, cannotEmbed: false }; } // Embed previews arrive untyped from the oEmbed proxy endpoint. const embedPreview = getEmbedPreview( attributesUrl ) as EmbedPreviewData | undefined; const previewIsFallback = isPreviewEmbedFallback( attributesUrl ); // No html to show: the provider or URL can't be embedded, or core // dropped the html, as it does on the front end. const validPreview = !! embedPreview?.html; const resolved = hasFinishedResolution( 'getEmbedPreview', [ attributesUrl, ] ); return { preview: validPreview ? embedPreview : undefined, // Also loading before the request starts, which is a tick // after the URL is set. fetching: ! resolved, // Theme supports arrive untyped from the REST API. themeSupportsResponsive: getThemeSupports()[ 'responsive-embeds' ] as boolean | undefined, cannotEmbed: ! validPreview || previewIsFallback, hasResolved: resolved, }; }, [ attributesUrl ] ); /** * Returns the attributes derived from the preview, merged with the current attributes. * * @return Merged attributes. */ const getMergedAttributes = () => getMergedAttributesWithPreview( attributes, preview, title, responsive ); function toggleResponsive( newAllowResponsive: boolean ) { const { className } = attributes; // The controls calling this only render with a preview. const { html } = preview!; setAttributes( { allowResponsive: newAllowResponsive, className: getClassNames( html, className, responsive && newAllowResponsive ), } ); } // When the preview can't be embedded, retry without any trailing slash. useEffect( () => { if ( ! cannotEmbed || ! hasResolved || ! attributesUrl ) { return; } const newURL = attributesUrl.replace( /\/$/, '' ); if ( newURL === attributesUrl ) { return; } setURL( newURL ); setIsEditingURL( false ); __unstableMarkNextChangeAsNotPersistent(); setAttributes( { url: newURL } ); }, [ attributesUrl, cannotEmbed, hasResolved, setAttributes, __unstableMarkNextChangeAsNotPersistent, ] ); // Apply preview-derived attributes once the preview resolves. useEffect( () => { if ( ! preview || isEditingURL ) { return; } const mergedAttributes = getMergedAttributes(); if ( onReplace ) { const upgradedBlock = createUpgradedEmbedBlock( props, mergedAttributes ); if ( upgradedBlock ) { // Mutate via setAttributes; onReplace would remount the // block and clear the URL textbox on undo. __unstableMarkNextChangeAsNotPersistent(); setAttributes( upgradedBlock.attributes ); return; } } const hasChanges = Object.keys( mergedAttributes ).some( ( key ) => mergedAttributes[ key ] !== attributes[ key ] ); if ( hasChanges ) { // Merge into the URL-submit undo level so a single undo // reverts both the submit and the preview-driven attributes. __unstableMarkNextChangeAsNotPersistent(); setAttributes( mergedAttributes ); } }, [ preview, isEditingURL ] ); const blockProps = useBlockProps(); if ( fetching ) { return (