-
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.
- Loading branch information
1 parent
8a20ad3
commit 1fbde54
Showing
5 changed files
with
110 additions
and
8 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,36 @@ | ||
<?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(); ?> | ||
</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
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 { withSelect } from '@wordpress/data'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
function WidgetPreview( { postLink, attributes, ...props } ) { | ||
const DEFAULT_HEIGHT = 300; | ||
const HEIGTH_MARGIN = 20; | ||
const [ height, setHeight ] = useState( DEFAULT_HEIGHT ); | ||
const iframeUrl = addQueryArgs( postLink, { | ||
widgetPreview: attributes, | ||
} ); | ||
return ( | ||
<Disabled> | ||
<FocusableIframe | ||
onLoad={ ( event ) => { | ||
const iframeContentHeight = get( | ||
event, | ||
[ 'currentTarget', 'contentDocument', 'body', 'scrollHeight' ] | ||
); | ||
if ( iframeContentHeight !== height ) { | ||
setHeight( iframeContentHeight ); | ||
} | ||
} } | ||
src={ iframeUrl } | ||
height={ height + HEIGTH_MARGIN } | ||
{ ...props } | ||
/> | ||
</Disabled> | ||
); | ||
} | ||
|
||
export default withSelect( ( select ) => { | ||
const { getCurrentPostAttribute } = select( 'core/editor' ); | ||
return { | ||
postLink: getCurrentPostAttribute( 'link' ), | ||
}; | ||
} )( 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