Skip to content

Commit

Permalink
Fix cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 25, 2023
1 parent e9f7eb8 commit 02955d6
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ function Iframe( {
forwardedRef: ref,
...props
} ) {
const assets = useSelect(
const { styles = '', scripts = '' } = useSelect(
( select ) =>
select( blockEditorStore ).getSettings().__unstableResolvedAssets,
select( blockEditorStore ).getSettings().__unstableResolvedAssets ??
{},
[]
);
const [ iframeDocument, setIframeDocument ] = useState();
Expand Down Expand Up @@ -181,24 +182,23 @@ function Iframe( {
};
}, [] );

const html =
'<!doctype html>' +
'<style>html{height:auto!important;}body{margin:0}</style>' +
styles +
scripts;

// Correct doctype is required to enable rendering in standards
// mode. Also preload the styles to avoid a flash of unstyled
// content.
const src = useMemo( () => {
const html =
'<!doctype html><style>html{height:auto!important;}body{margin:0}</style>' +
assets?.styles +
assets?.scripts;
const blob = new window.Blob( [ html ], { type: 'text/html' } );
return URL.createObjectURL( blob );
}, [] );
const [ src, cleanup ] = useMemo( () => {
const _src = URL.createObjectURL(
new window.Blob( [ html ], { type: 'text/html' } )
);
return [ _src, () => URL.revokeObjectURL( _src ) ];
}, [ html ] );

useEffect(
() => () => {
URL.revokeObjectURL( src );
},
[]
);
useEffect( () => cleanup, [ cleanup ] );

// We need to counter the margin created by scaling the iframe. If the scale
// is e.g. 0.45, then the top + bottom margin is 0.55 (1 - scale). Just the
Expand Down

0 comments on commit 02955d6

Please sign in to comment.