UNPKG

@wordpress/block-library

Version:
1 lines • 3.55 kB
{"version":3,"file":"wp-embed-preview.cjs","names":["useRef","useMemo","useMergeRefs","useFocusableIframe"],"sources":["../../src/embed/wp-embed-preview.jsx"],"sourcesContent":["import { useMergeRefs, useFocusableIframe } from '@wordpress/compose';\nimport { useRef, useEffect, useMemo } from '@wordpress/element';\n\n/** @typedef {React.SyntheticEvent} SyntheticEvent */\n\nconst attributeMap = {\n\tclass: 'className',\n\tframeborder: 'frameBorder',\n\tmarginheight: 'marginHeight',\n\tmarginwidth: 'marginWidth',\n};\n\nexport default function WpEmbedPreview( { html } ) {\n\tconst ref = useRef();\n\tconst props = useMemo( () => {\n\t\tconst doc = new window.DOMParser().parseFromString( html, 'text/html' );\n\t\tconst iframe = doc.querySelector( 'iframe' );\n\t\tconst iframeProps = {};\n\n\t\tif ( ! iframe ) {\n\t\t\treturn iframeProps;\n\t\t}\n\n\t\tArray.from( iframe.attributes ).forEach( ( { name, value } ) => {\n\t\t\tif ( name === 'style' ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tiframeProps[ attributeMap[ name ] || name ] = value;\n\t\t} );\n\n\t\treturn iframeProps;\n\t}, [ html ] );\n\n\tuseEffect( () => {\n\t\tconst { ownerDocument } = ref.current;\n\t\tconst { defaultView } = ownerDocument;\n\n\t\t/**\n\t\t * Checks for WordPress embed events signaling the height change when\n\t\t * iframe content loads or iframe's window is resized. The event is\n\t\t * sent from WordPress core via the window.postMessage API.\n\t\t *\n\t\t * References:\n\t\t * window.postMessage:\n\t\t * https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage\n\t\t * WordPress core embed-template on load:\n\t\t * https://github.com/WordPress/WordPress/blob/HEAD/wp-includes/js/wp-embed-template.js#L143\n\t\t * WordPress core embed-template on resize:\n\t\t * https://github.com/WordPress/WordPress/blob/HEAD/wp-includes/js/wp-embed-template.js#L187\n\t\t *\n\t\t * @param {MessageEvent} event Message event.\n\t\t */\n\t\tfunction resizeWPembeds( { data: { secret, message, value } = {} } ) {\n\t\t\tif ( message !== 'height' || secret !== props[ 'data-secret' ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tref.current.height = value;\n\t\t}\n\n\t\tdefaultView.addEventListener( 'message', resizeWPembeds );\n\t\treturn () => {\n\t\t\tdefaultView.removeEventListener( 'message', resizeWPembeds );\n\t\t};\n\t}, [] );\n\n\treturn (\n\t\t<div className=\"wp-block-embed__wrapper\">\n\t\t\t<iframe\n\t\t\t\tref={ useMergeRefs( [ ref, useFocusableIframe() ] ) }\n\t\t\t\ttitle={ props.title }\n\t\t\t\t{ ...props }\n\t\t\t/>\n\t\t</div>\n\t);\n}\n"],"mappings":";;;;;;;;;;AAKA,MAAM,eAAe;CACpB,OAAO;CACP,aAAa;CACb,cAAc;CACd,aAAa;AACd;AAEA,SAAwB,eAAgB,EAAE,QAAS;CAClD,MAAM,OAAA,GAAMA,mBAAAA,OAAAA,CAAO;CACnB,MAAM,SAAA,GAAQC,mBAAAA,QAAAA,OAAe;EAE5B,MAAM,SADM,IAAI,OAAO,UAAU,CAAC,CAAC,gBAAiB,MAAM,WACzC,CAAC,CAAC,cAAe,QAAS;EAC3C,MAAM,cAAc,CAAC;EAErB,IAAK,CAAE,QACN,OAAO;EAGR,MAAM,KAAM,OAAO,UAAW,CAAC,CAAC,SAAW,EAAE,MAAM,YAAa;GAC/D,IAAK,SAAS,SACb;GAED,YAAa,aAAc,SAAU,QAAS;EAC/C,CAAE;EAEF,OAAO;CACR,GAAG,CAAE,IAAK,CAAE;CAEZ,CAAA,GAAA,mBAAA,UAAA,OAAiB;EAChB,MAAM,EAAE,kBAAkB,IAAI;EAC9B,MAAM,EAAE,gBAAgB;;;;;;;;;;;;;;;;EAiBxB,SAAS,eAAgB,EAAE,MAAM,EAAE,QAAQ,SAAS,UAAU,CAAC,KAAM;GACpE,IAAK,YAAY,YAAY,WAAW,MAAO,gBAC9C;GAGD,IAAI,QAAQ,SAAS;EACtB;EAEA,YAAY,iBAAkB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