-
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
Metaboxes: Refactor to render inline without iFrame #3345
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3345 +/- ##
=========================================
+ Coverage 33.15% 33.3% +0.15%
=========================================
Files 249 249
Lines 6711 6683 -28
Branches 1206 1202 -4
=========================================
+ Hits 2225 2226 +1
+ Misses 3786 3761 -25
+ Partials 700 696 -4
Continue to review full report at Codecov.
|
I see the following PHP notice when loading the new post page:
When I click on It doesn't load WYSWIG editor: Sometimes Google map is no longer rendered after changes are saved and WYSWIG editor area changes size. WYSWIG editor works properly in the solution that uses |
const head = document.getElementsByTagName( 'head' )[ 0 ] || document.documentElement, | ||
script = document.createElement( 'script' ); | ||
script.type = 'text/javascript'; | ||
script.appendChild( document.createTextNode( data ) ); |
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.
Should it have a different handling for scripts that have src
attribute?
} from './selectors'; | ||
|
||
const SAVE_POST_NOTICE_ID = 'SAVE_POST_NOTICE_ID'; | ||
const TRASH_POST_NOTICE_ID = 'TRASH_POST_NOTICE_ID'; | ||
|
||
// Hold jquery.ready until the metaboxes load | ||
jQuery.holdReady( true ); |
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.
Should we guard it with a check for jQuery
existence to make sure it works in test env, too?
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.
Okey, you mocked it. It's okayish :)
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.
The wp-editor
script does not define a dependency on jQuery. We can't otherwise assume this global exists.
gutenberg/lib/client-assets.php
Lines 603 to 609 in 4da6c0f
wp_enqueue_script( | |
'wp-editor', | |
gutenberg_url( 'editor/build/index.js' ), | |
array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils', 'word-count', 'editor', 'heartbeat' ), | |
filemtime( gutenberg_dir_path() . 'editor/build/index.js' ), | |
true // enqueue in the footer. | |
); |
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.
Is it possible to move this into the effect for one of the initialization actions? Not sure I like the side-effect we're introducing to the top-level scope here. Maybe better to at least consolidate this somewhere, as in editor/index.js
?
As I'm reviewing this, I'm still working through understanding why we need this; can you clarify more in a comment why we need to hold?
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.
I'm still working through understanding why we need this; can you clarify more in a comment why we need to hold?
I believe we need to currently hold due to waiting on the areas to load, so that any scripts can properly add their listeners when jQuery is ready.
I don't think that is a long term need, now that we are on post.php we can render the metaboxes server side, then take those dom nodes and bring them into React.
lib/meta-box-partial-page.php
Outdated
<html class="gutenberg-meta-box-html"> | ||
<head> | ||
</head> | ||
<body class="wp-admin wp-core-ui no-js <?php echo $admin_body_classes . ' ' . $admin_body_class; ?>"> |
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.
Missing escaping
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.
This is the same code as the default admin_header. The idea is to have the same result. We may also remove it, I think it may be useless
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.
As a security measure, esc_attr
should have no effect here if that's the case, unless an attack is in progress. Instead of trusting it's safe, lets eliminate all doubt and move forward with absolute rock solid certainty by escaping properly.
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.
Yeah, it is most likely useless now as we load on post.php
That's a downside for metaboxes relying on the global TinyMCE variables. We'll have to look deeper, but Gutenberg is not a TinyMCE instance and metaboxes relying on these globals break. We can try to mitigate the breakage in the future. Precision: this is true no matter the approach |
Yup, this is pretty much the only unavoidable breakage. |
Not sure if this is just on my end, but the collapse and expanding headers for the sidebar do not appear to be working, the main area looks good though. |
What do you think about server rendering the meta boxes as well then hoist them into the React DOM? Maybe that would be a good next iteration? That way there is no spinning wheel on load. |
Totally on board with this. It will also fix some JS issues I believe. |
@@ -1,47 +0,0 @@ | |||
( function() { |
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.
If this folder no longer exists, it should be removed from .eslintignore
and build-plugin-zip.sh
:
Line 5 in 4da6c0f
/assets/js |
gutenberg/bin/build-plugin-zip.sh
Line 96 in 4da6c0f
assets/js/*.js \ |
@@ -56,15 +48,10 @@ between the `MetaBoxIframe` and the Redux Store. *If no meta boxes are active, | |||
nothing happens. This will be the default behavior, as all core meta boxes have | |||
been stripped.* | |||
|
|||
#### MetaBoxIframe Component |
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.
There are still two lingering references to MetaBoxIframe
components in this README (in section "Redux and React Meta Box Management")
editor/actions.js
Outdated
*/ | ||
export function metaBoxLoaded( location ) { | ||
return { | ||
type: 'METABOX_LOADED', |
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.
If we're treating "meta box" as two distinct words (evidenced by action creator name and other action types in this file), this action type should be META_BOX_LOADED
} from './selectors'; | ||
|
||
const SAVE_POST_NOTICE_ID = 'SAVE_POST_NOTICE_ID'; | ||
const TRASH_POST_NOTICE_ID = 'TRASH_POST_NOTICE_ID'; | ||
|
||
// Hold jquery.ready until the metaboxes load | ||
jQuery.holdReady( true ); |
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.
The wp-editor
script does not define a dependency on jQuery. We can't otherwise assume this global exists.
gutenberg/lib/client-assets.php
Lines 603 to 609 in 4da6c0f
wp_enqueue_script( | |
'wp-editor', | |
gutenberg_url( 'editor/build/index.js' ), | |
array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils', 'word-count', 'editor', 'heartbeat' ), | |
filemtime( gutenberg_dir_path() . 'editor/build/index.js' ), | |
true // enqueue in the footer. | |
); |
} from './selectors'; | ||
|
||
const SAVE_POST_NOTICE_ID = 'SAVE_POST_NOTICE_ID'; | ||
const TRASH_POST_NOTICE_ID = 'TRASH_POST_NOTICE_ID'; | ||
|
||
// Hold jquery.ready until the metaboxes load | ||
jQuery.holdReady( true ); |
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.
Is it possible to move this into the effect for one of the initialization actions? Not sure I like the side-effect we're introducing to the top-level scope here. Maybe better to at least consolidate this somewhere, as in editor/index.js
?
As I'm reviewing this, I'm still working through understanding why we need this; can you clarify more in a comment why we need to hold?
lib/meta-box-partial-page.php
Outdated
wp_enqueue_script( 'utils' ); | ||
wp_enqueue_script( 'common' ); | ||
wp_enqueue_script( 'svg-painter' ); | ||
?> | ||
<!-- Add an html class so that scroll bars can be removed in css and make it appear as though the iframe is one with Gutenberg. --> |
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.
Reference to iframe.
<script type="text/javascript"> | ||
document.body.className = document.body.className.replace('no-js','js'); | ||
</script> | ||
<?php |
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.
Another comment below referencing iframes:
THIS IS SOMEHOW REALLY IMPORTANT FOR IFRAMES TO RESIZE CORRECTLY
lib/meta-box-partial-page.php
Outdated
wp_enqueue_script( 'utils' ); | ||
wp_enqueue_script( 'common' ); | ||
wp_enqueue_script( 'svg-painter' ); | ||
?> | ||
<!-- Add an html class so that scroll bars can be removed in css and make it appear as though the iframe is one with Gutenberg. --> | ||
<html class="gutenberg-meta-box-html"> |
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.
No styles exist anymore for this class name, we can drop it?
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.
Yes
if ( ! this.mounted ) { | ||
return; | ||
} | ||
jQuery( this.node ).html( body ); |
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.
Why do we use jQuery for this? this.node.innerHTML = body
?
(To earlier point that we don't have an explicit dependency on jQuery for the editor)
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.
We need to run the scripts contained in body
. jQuery does this pretty well. I tried recreating the behavior myself, it "mostly" worked but not 100%. Is there a specific lib to do so?
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.
I would think innerHTML
should be running any newly injected scripts.
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.
Can't find official documentation about this, but it's not running the injected scripts. See https://stackoverflow.com/questions/2592092/executing-script-elements-inserted-with-innerhtml
this.form = this.node.querySelector( '.meta-box-form' ); | ||
this.form.onSubmit = ( event ) => event.preventDefault(); | ||
this.originalFormData = this.getFormData(); | ||
this.form.addEventListener( 'change', this.checkState ); |
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.
Need to be removed in componentWillUnmount
?
We need to accommodate core stylesheets though |
948b0af
to
7db33b9
Compare
I'm using It looks like see this issue every time I load Gutenberg. To give you a better context, it works perfectly fine when using the Classic editor. Another issue I observed here is that when I update location on the map, it |
@@ -188,7 +217,6 @@ function gutenberg_meta_box_partial_page_admin_header( $hook_suffix, $current_sc | |||
decimalPoint = '<?php echo addslashes( $wp_locale->number_format['decimal_point'] ); ?>', | |||
isRtl = <?php echo (int) is_rtl(); ?>; | |||
</script> | |||
<meta name="viewport" content="width=device-width,initial-scale=1.0"> |
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.
Shouldn't be viewport
kept?
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.
No this is not rendered at all, it's an AJAX request where we just render the body
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 makes perfect sense :)
Ok, I'm merging this. Acknowledging this doesn't fix all the metaboxes issues. It could also create some new issues. But this gets us closer to where we want to go. Future steps could include:
|
Want just to report custom Metaboxes in right sidebar are near perfect, from design point of view. Under Gutenberg content area is very different thing. To make third party developers more acceptable later when converting Metaboxes to Gutenberg blocks. Less work in the beginning if their old Metaboxes can be full width. They will have to do it twice, change a lot of code. First adapt it to look nice for transition period (compatibility fallback), second to code Metaboxes to be real Gutenberg blocks. Not small amount of coding and work. |
The regular metaboxes do show up. But are the data supposed to be saved as well? The normal function doesn't seem to work if you click the Update button |
@mattiman2 Probably a small bug somewhere, we'll look into it. A drop-in metabox example could help debug ;). Feel free to create an issue. |
@youknowriad OK I'll try to create an issue about this. Thanks. |
Something in this pull request introduced a regression where images from my media library are no longer appearing: If I had to guess, I'd think it might have something to do with the jQuery dependency changes? Aside: This branch was very difficult to bisect effectively to find the start of this issue, because most of the commits in the branch would not compile until the "fix" in 6eec188 . The branch could have used some more squashing, or at least ensuring that each commit could independently compile without errors. |
Listen, if You decided to put Metaboxes inside max-width: 636px then it is ugly as hell as it is now. Give them 100% wrapper width, or do some nice boxed CSS styling. As it is now not so appealing. It is not written in constitution everything in WP backend has to have boring white background. So huge amount of energy and time You devote to smallest Gutenberg CSS and styling. Again it looks like you treat old Metaboxes as second class citizens. |
@mattiman2 I'm having the same problem. Although the meta boxes show up correctly, changes in them are not saved. My custom function hooked to the |
Description
This PR refactors the metaboxes rendering to drop the iFrame.
closes #3243 #3242 #3304 #3169