From d14f1540655df9438528536e2c3f00d35b4c2078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Tue, 14 Sep 2021 22:53:30 +0300 Subject: [PATCH 1/4] Rewrite FocusableIframe as hook API --- .../src/embed/wp-embed-preview.js | 100 +++++++----------- .../components/src/focusable-iframe/README.md | 2 + .../components/src/focusable-iframe/index.js | 35 ++---- .../src/focusable-iframe/stories/index.js | 19 ---- packages/components/src/sandbox/index.js | 10 +- packages/compose/README.md | 9 ++ .../src/hooks/use-focusable-iframe/README.md | 29 +++++ .../src/hooks/use-focusable-iframe/index.js | 38 +++++++ packages/compose/src/index.js | 1 + 9 files changed, 126 insertions(+), 117 deletions(-) delete mode 100644 packages/components/src/focusable-iframe/stories/index.js create mode 100644 packages/compose/src/hooks/use-focusable-iframe/README.md create mode 100644 packages/compose/src/hooks/use-focusable-iframe/index.js diff --git a/packages/block-library/src/embed/wp-embed-preview.js b/packages/block-library/src/embed/wp-embed-preview.js index f83341853c250f..754853e856e50d 100644 --- a/packages/block-library/src/embed/wp-embed-preview.js +++ b/packages/block-library/src/embed/wp-embed-preview.js @@ -1,96 +1,72 @@ /** * WordPress dependencies */ +import { useMergeRefs, useFocusableIframe } from '@wordpress/compose'; import { useRef, useEffect, useMemo } from '@wordpress/element'; /** @typedef {import('@wordpress/element').WPSyntheticEvent} WPSyntheticEvent */ +const attributeMap = { + class: 'className', + frameborder: 'frameBorder', + marginheight: 'marginHeight', + marginwidth: 'marginWidth', +}; + export default function WpEmbedPreview( { html } ) { const ref = useRef(); + const props = useMemo( () => { + const doc = new window.DOMParser().parseFromString( html, 'text/html' ); + const iframe = doc.querySelector( 'iframe' ); + const iframeProps = {}; + + Array.from( iframe.attributes ).forEach( ( { name, value } ) => { + if ( name === 'style' ) return; + iframeProps[ attributeMap[ name ] || name ] = value; + } ); + + return iframeProps; + }, [ html ] ); useEffect( () => { const { ownerDocument } = ref.current; const { defaultView } = ownerDocument; /** - * Checks for WordPress embed events signaling the height change when iframe - * content loads or iframe's window is resized. The event is sent from - * WordPress core via the window.postMessage API. + * Checks for WordPress embed events signaling the height change when + * iframe content loads or iframe's window is resized. The event is + * sent from WordPress core via the window.postMessage API. * - * References: - * window.postMessage: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage - * WordPress core embed-template on load: https://github.com/WordPress/WordPress/blob/HEAD/wp-includes/js/wp-embed-template.js#L143 - * WordPress core embed-template on resize: https://github.com/WordPress/WordPress/blob/HEAD/wp-includes/js/wp-embed-template.js#L187 + * References: window.postMessage: + * https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage + * WordPress core embed-template on load: + * https://github.com/WordPress/WordPress/blob/HEAD/wp-includes/js/wp-embed-template.js#L143 + * WordPress core embed-template on resize: + * https://github.com/WordPress/WordPress/blob/HEAD/wp-includes/js/wp-embed-template.js#L187 * * @param {WPSyntheticEvent} event Message event. */ function resizeWPembeds( { data: { secret, message, value } = {} } ) { - if ( - [ secret, message, value ].some( - ( attribute ) => ! attribute - ) || - message !== 'height' - ) { + if ( message !== 'height' || secret !== props[ 'data-secret' ] ) { return; } - ownerDocument - .querySelectorAll( `iframe[data-secret="${ secret }"` ) - .forEach( ( iframe ) => { - if ( +iframe.height !== value ) { - iframe.height = value; - } - } ); - } - - /** - * Checks whether the wp embed iframe is the activeElement, - * if it is dispatch a focus event. - */ - function checkFocus() { - const { activeElement } = ownerDocument; - - if ( - activeElement.tagName !== 'IFRAME' || - activeElement.parentNode !== ref.current - ) { - return; - } - - activeElement.focus(); + ref.current.height = value; } defaultView.addEventListener( 'message', resizeWPembeds ); - defaultView.addEventListener( 'blur', checkFocus ); - return () => { defaultView.removeEventListener( 'message', resizeWPembeds ); - defaultView.removeEventListener( 'blur', checkFocus ); }; }, [] ); - const __html = useMemo( () => { - const doc = new window.DOMParser().parseFromString( html, 'text/html' ); - const iframe = doc.querySelector( 'iframe' ); - - if ( iframe ) { - iframe.removeAttribute( 'style' ); - } - - const blockQuote = doc.querySelector( 'blockquote' ); - - if ( blockQuote ) { - blockQuote.style.display = 'none'; - } - - return doc.body.innerHTML; - }, [ html ] ); - return ( -
+
+