-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use iframes in legacy widget preview. (#14643)
* Rebase and refresh * Don't hardcode sidebar id * Don't hardcode preview URL * lint * Update package-lock.json * Lint Co-authored-by: Adam Zieliński <[email protected]>
- Loading branch information
1 parent
e5b6da9
commit cc003b2
Showing
8 changed files
with
208 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Start: Include for phase 2 | ||
* Template used to render widget previews. | ||
* | ||
* @package gutenberg | ||
* @since 5.4.0 | ||
*/ | ||
|
||
if ( ! function_exists( 'wp_head' ) ) { | ||
exit; | ||
} | ||
?> | ||
<!doctype html> | ||
<html <?php language_attributes(); ?>> | ||
<head> | ||
<meta charset="<?php bloginfo( 'charset' ); ?>" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="profile" href="https://gmpg.org/xfn/11" /> | ||
<?php wp_head(); ?> | ||
<style> | ||
/* Reset theme styles */ | ||
html, body, #page, #content { | ||
background: #FFF !important; | ||
padding: 0 !important; | ||
margin: 0 !important; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body <?php body_class(); ?>> | ||
<div id="page" class="site"> | ||
<div id="content" class="site-content"> | ||
<?php | ||
$registry = WP_Block_Type_Registry::get_instance(); | ||
$block = $registry->get_registered( 'core/legacy-widget' ); | ||
echo $block->render( $_GET['widgetPreview'] ); | ||
?> | ||
</div><!-- #content --> | ||
</div><!-- #page --> | ||
|
||
<?php wp_footer(); ?> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/edit-widgets/src/blocks/legacy-widget/edit/widget-preview.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { Disabled, FocusableIframe } from '@wordpress/components'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
function WidgetPreview( { widgetAreaId, attributes, ...props } ) { | ||
const DEFAULT_HEIGHT = 300; | ||
const HEIGHT_MARGIN = 20; | ||
const [ height, setHeight ] = useState( DEFAULT_HEIGHT ); | ||
const currentUrl = document.location.href; | ||
const siteUrl = currentUrl.substr( 0, currentUrl.indexOf( 'wp-admin/' ) ); | ||
const iframeUrl = addQueryArgs( siteUrl, { | ||
widgetPreview: { | ||
...attributes, | ||
sidebarId: widgetAreaId, | ||
}, | ||
} ); | ||
return ( | ||
<Disabled> | ||
<FocusableIframe | ||
onLoad={ ( event ) => { | ||
const iframeContentHeight = get( event, [ | ||
'currentTarget', | ||
'contentDocument', | ||
'body', | ||
'scrollHeight', | ||
] ); | ||
if ( iframeContentHeight !== height ) { | ||
setHeight( iframeContentHeight ); | ||
} | ||
} } | ||
src={ iframeUrl } | ||
height={ height + HEIGHT_MARGIN } | ||
{ ...props } | ||
/> | ||
</Disabled> | ||
); | ||
} | ||
|
||
export default WidgetPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters