-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use iframes in legacy widget preview. #14643
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
53651fa
Rebase and refresh
adamziel 0cd8dd9
Don't hardcode sidebar id
adamziel 43ec0cc
Don't hardcode preview URL
adamziel 5db7445
lint
adamziel e262e73
Update package-lock.json
adamziel bb35e29
Merge branch 'master' into update/use-iframes-for-widget-preview
adamziel cdf5ad3
Lint
adamziel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyone aware of any better way of getting site's URL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is available in the REST API index at
wp-json
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually there is another place in navigation editor that could also use this correction - let's address both in a separate PR