UNPKG

@wordpress/components

Version:
8 lines (7 loc) 11.9 kB
{ "version": 3, "sources": ["../../src/sandbox/index.tsx"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { renderToString, useRef, useState, useEffect } from '@wordpress/element';\nimport { useFocusableIframe, useMergeRefs } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport { jsx as _jsx, jsxs as _jsxs } from \"react/jsx-runtime\";\nconst observeAndResizeJS = function () {\n const {\n MutationObserver\n } = window;\n if (!MutationObserver || !document.body || !window.parent) {\n return;\n }\n function sendResize() {\n const clientBoundingRect = document.body.getBoundingClientRect();\n window.parent.postMessage({\n action: 'resize',\n width: clientBoundingRect.width,\n height: clientBoundingRect.height\n }, '*');\n }\n const observer = new MutationObserver(sendResize);\n observer.observe(document.body, {\n attributes: true,\n attributeOldValue: false,\n characterData: true,\n characterDataOldValue: false,\n childList: true,\n subtree: true\n });\n window.addEventListener('load', sendResize, true);\n\n // Hack: Remove viewport unit styles, as these are relative\n // the iframe root and interfere with our mechanism for\n // determining the unconstrained page bounds.\n function removeViewportStyles(ruleOrNode) {\n if (ruleOrNode.style) {\n ['width', 'height', 'minHeight', 'maxHeight'].forEach(function (style) {\n if (/^\\\\d+(vw|vh|svw|lvw|dvw|svh|lvh|dvh|vi|svi|lvi|dvi|vb|svb|lvb|dvb|vmin|svmin|lvmin|dvmin|vmax|svmax|lvmax|dvmax)$/.test(ruleOrNode.style[style])) {\n ruleOrNode.style[style] = '';\n }\n });\n }\n }\n Array.prototype.forEach.call(document.querySelectorAll('[style]'), removeViewportStyles);\n Array.prototype.forEach.call(document.styleSheets, function (stylesheet) {\n Array.prototype.forEach.call(stylesheet.cssRules || stylesheet.rules, removeViewportStyles);\n });\n document.body.style.position = 'absolute';\n document.body.style.width = '100%';\n document.body.setAttribute('data-resizable-iframe-connected', '');\n sendResize();\n\n // Resize events can change the width of elements with 100% width, but we don't\n // get an DOM mutations for that, so do the resize when the window is resized, too.\n window.addEventListener('resize', sendResize, true);\n};\n\n// TODO: These styles shouldn't be coupled with WordPress.\nconst style = `\n\tbody {\n\t\tmargin: 0;\n\t}\n\thtml,\n\tbody,\n\tbody > div {\n\t\twidth: 100%;\n\t}\n\thtml.wp-has-aspect-ratio,\n\tbody.wp-has-aspect-ratio,\n\tbody.wp-has-aspect-ratio > div,\n\tbody.wp-has-aspect-ratio > div iframe {\n\t\twidth: 100%;\n\t\theight: 100%;\n\t\toverflow: hidden; /* If it has an aspect ratio, it shouldn't scroll. */\n\t}\n\tbody > div > * {\n\t\tmargin-top: 0 !important; /* Has to have !important to override inline styles. */\n\t\tmargin-bottom: 0 !important;\n\t}\n`;\n\n/**\n * This component provides an isolated environment for arbitrary HTML via iframes.\n *\n * ```jsx\n * import { SandBox } from '@wordpress/components';\n *\n * const MySandBox = () => (\n * \t<SandBox html=\"<p>Content</p>\" title=\"SandBox\" type=\"embed\" />\n * );\n * ```\n */\nfunction SandBox({\n html = '',\n title = '',\n type,\n styles = [],\n scripts = [],\n onFocus,\n tabIndex\n}) {\n const ref = useRef(null);\n const [width, setWidth] = useState(0);\n const [height, setHeight] = useState(0);\n function isFrameAccessible() {\n try {\n return !!ref.current?.contentDocument?.body;\n } catch (e) {\n return false;\n }\n }\n function trySandBox(forceRerender = false) {\n if (!isFrameAccessible()) {\n return;\n }\n const {\n contentDocument,\n ownerDocument\n } = ref.current;\n if (!forceRerender && null !== contentDocument?.body.getAttribute('data-resizable-iframe-connected')) {\n return;\n }\n\n // Put the html snippet into a html document, and then write it to the iframe's document\n // we can use this in the future to inject custom styles or scripts.\n // Scripts go into the body rather than the head, to support embedded content such as Instagram\n // that expect the scripts to be part of the body.\n const htmlDoc = /*#__PURE__*/_jsxs(\"html\", {\n lang: ownerDocument.documentElement.lang,\n className: type,\n children: [/*#__PURE__*/_jsxs(\"head\", {\n children: [/*#__PURE__*/_jsx(\"title\", {\n children: title\n }), /*#__PURE__*/_jsx(\"style\", {\n dangerouslySetInnerHTML: {\n __html: style\n }\n }), styles.map((rules, i) => /*#__PURE__*/_jsx(\"style\", {\n dangerouslySetInnerHTML: {\n __html: rules\n }\n }, i))]\n }), /*#__PURE__*/_jsxs(\"body\", {\n \"data-resizable-iframe-connected\": \"data-resizable-iframe-connected\",\n className: type,\n children: [/*#__PURE__*/_jsx(\"div\", {\n dangerouslySetInnerHTML: {\n __html: html\n }\n }), /*#__PURE__*/_jsx(\"script\", {\n type: \"text/javascript\",\n dangerouslySetInnerHTML: {\n __html: `(${observeAndResizeJS.toString()})();`\n }\n }), scripts.map(src => /*#__PURE__*/_jsx(\"script\", {\n src: src\n }, src))]\n })]\n });\n\n // Writing the document like this makes it act in the same way as if it was\n // loaded over the network, so DOM creation and mutation, script execution, etc.\n // all work as expected.\n contentDocument.open();\n contentDocument.write('<!DOCTYPE html>' + renderToString(htmlDoc));\n contentDocument.close();\n }\n useEffect(() => {\n trySandBox();\n function tryNoForceSandBox() {\n trySandBox(false);\n }\n function checkMessageForResize(event) {\n const iframe = ref.current;\n\n // Verify that the mounted element is the source of the message.\n if (!iframe || iframe.contentWindow !== event.source) {\n return;\n }\n\n // Attempt to parse the message data as JSON if passed as string.\n let data = event.data || {};\n if ('string' === typeof data) {\n try {\n data = JSON.parse(data);\n } catch (e) {}\n }\n\n // Update the state only if the message is formatted as we expect,\n // i.e. as an object with a 'resize' action.\n if ('resize' !== data.action) {\n return;\n }\n setWidth(data.width);\n setHeight(data.height);\n }\n const iframe = ref.current;\n const defaultView = iframe?.ownerDocument?.defaultView;\n\n // This used to be registered using <iframe onLoad={} />, but it made the iframe blank\n // after reordering the containing block. See these two issues for more details:\n // https://github.com/WordPress/gutenberg/issues/6146\n // https://github.com/facebook/react/issues/18752\n iframe?.addEventListener('load', tryNoForceSandBox, false);\n defaultView?.addEventListener('message', checkMessageForResize);\n return () => {\n iframe?.removeEventListener('load', tryNoForceSandBox, false);\n defaultView?.removeEventListener('message', checkMessageForResize);\n };\n // Passing `exhaustive-deps` will likely involve a more detailed refactor.\n // See https://github.com/WordPress/gutenberg/pull/44378\n }, []);\n useEffect(() => {\n trySandBox();\n // Passing `exhaustive-deps` will likely involve a more detailed refactor.\n // See https://github.com/WordPress/gutenberg/pull/44378\n }, [title, styles, scripts]);\n useEffect(() => {\n trySandBox(true);\n // Passing `exhaustive-deps` will likely involve a more detailed refactor.\n // See https://github.com/WordPress/gutenberg/pull/44378\n }, [html, type]);\n return /*#__PURE__*/_jsx(\"iframe\", {\n ref: useMergeRefs([ref, useFocusableIframe()]),\n title: title,\n tabIndex: tabIndex,\n className: \"components-sandbox\",\n sandbox: \"allow-scripts allow-same-origin allow-presentation\",\n onFocus: onFocus,\n width: Math.ceil(width),\n height: Math.ceil(height)\n });\n}\nexport default SandBox;"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA4D;AAC5D,qBAAiD;AAKjD,yBAA2C;AAC3C,IAAM,qBAAqB,WAAY;AACrC,QAAM;AAAA,IACJ;AAAA,EACF,IAAI;AACJ,MAAI,CAAC,oBAAoB,CAAC,SAAS,QAAQ,CAAC,OAAO,QAAQ;AACzD;AAAA,EACF;AACA,WAAS,aAAa;AACpB,UAAM,qBAAqB,SAAS,KAAK,sBAAsB;AAC/D,WAAO,OAAO,YAAY;AAAA,MACxB,QAAQ;AAAA,MACR,OAAO,mBAAmB;AAAA,MAC1B,QAAQ,mBAAmB;AAAA,IAC7B,GAAG,GAAG;AAAA,EACR;AACA,QAAM,WAAW,IAAI,iBAAiB,UAAU;AAChD,WAAS,QAAQ,SAAS,MAAM;AAAA,IAC9B,YAAY;AAAA,IACZ,mBAAmB;AAAA,IACnB,eAAe;AAAA,IACf,uBAAuB;AAAA,IACvB,WAAW;AAAA,IACX,SAAS;AAAA,EACX,CAAC;AACD,SAAO,iBAAiB,QAAQ,YAAY,IAAI;AAKhD,WAAS,qBAAqB,YAAY;AACxC,QAAI,WAAW,OAAO;AACpB,OAAC,SAAS,UAAU,aAAa,WAAW,EAAE,QAAQ,SAAU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