-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d8d5eba
Metaboxes: Refactor to render inline without iFrame
youknowriad 0ac886a
Metaboxes: No flickering on save
youknowriad 7e439d1
Metaboxes: Fix URL when saving new posts
youknowriad 76ea2e1
Metaboxes: Hold initialization until the metaboxes load
youknowriad 478e781
Metaboxes: Rely on jQuery to insert content and scripts
youknowriad 6bc0b75
Metaboxes: Drop the body classname as useless in this approach
youknowriad 1e0207e
Metaboxes: Fix publish button style when using metaboxes
youknowriad 4a44cca
Metaboxes: Add jquery dependency to `wp-editor`
youknowriad 6961ba1
Metaboxes: Remove lingering useless code or mentions of iframe
youknowriad c8cc737
Metaboxes: unbind form events on unmount
youknowriad 7a65b3e
Metaboxes: Rename META_BOX_LOADED action
youknowriad 7db33b9
Metaboxes: Fix MetaBoxesArea component after rebase
youknowriad 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 |
---|---|---|
|
@@ -2,4 +2,3 @@ build | |
coverage | ||
vendor | ||
node_modules | ||
/assets/js |
This file was deleted.
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
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
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,161 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEqual } from 'lodash'; | ||
import classnames from 'classnames'; | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { Component } from '@wordpress/element'; | ||
import { Spinner } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import { handleMetaBoxReload, metaBoxStateChanged, metaBoxLoaded } from '../../../actions'; | ||
import { getMetaBox, isSavingPost } from '../../../selectors'; | ||
|
||
class MetaBoxesArea extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.state = { | ||
loading: true, | ||
}; | ||
this.originalFormData = []; | ||
this.bindNode = this.bindNode.bind( this ); | ||
this.checkState = this.checkState.bind( this ); | ||
} | ||
|
||
bindNode( node ) { | ||
this.node = node; | ||
} | ||
|
||
componentDidMount() { | ||
this.mounted = true; | ||
this.fetchMetaboxes(); | ||
} | ||
|
||
componentWillUnmout() { | ||
this.mounted = false; | ||
this.unbindFormEvents(); | ||
} | ||
|
||
unbindFormEvents() { | ||
if ( this.form ) { | ||
this.form.removeEventListener( 'change', this.checkState ); | ||
this.form.removeEventListener( 'input', this.checkState ); | ||
} | ||
} | ||
|
||
componentWillReceiveProps( nextProps ) { | ||
if ( nextProps.isUpdating && ! this.props.isUpdating ) { | ||
this.setState( { loading: true } ); | ||
const { location } = nextProps; | ||
const headers = new window.Headers(); | ||
const fetchOptions = { | ||
method: 'POST', | ||
headers, | ||
body: new window.FormData( this.form ), | ||
credentials: 'include', | ||
}; | ||
const request = window.fetch( addQueryArgs( window._wpMetaBoxUrl, { meta_box: location } ), fetchOptions ); | ||
this.onMetaboxResponse( request, false ); | ||
this.unbindFormEvents(); | ||
} | ||
} | ||
|
||
fetchMetaboxes() { | ||
const { location } = this.props; | ||
const request = window.fetch( addQueryArgs( window._wpMetaBoxUrl, { meta_box: location } ), { credentials: 'include' } ); | ||
this.onMetaboxResponse( request ); | ||
} | ||
|
||
onMetaboxResponse( request, initial = true ) { | ||
request.then( ( response ) => response.text() ) | ||
.then( ( body ) => { | ||
if ( ! this.mounted ) { | ||
return; | ||
} | ||
jQuery( this.node ).html( body ); | ||
this.form = this.node.querySelector( '.meta-box-form' ); | ||
this.form.onSubmit = ( event ) => event.preventDefault(); | ||
this.originalFormData = this.getFormData(); | ||
this.form.addEventListener( 'change', this.checkState ); | ||
this.form.addEventListener( 'input', this.checkState ); | ||
this.setState( { loading: false } ); | ||
if ( ! initial ) { | ||
this.props.metaBoxReloaded( this.props.location ); | ||
} else { | ||
this.props.metaBoxLoaded( this.props.location ); | ||
} | ||
} ); | ||
} | ||
|
||
getFormData() { | ||
const data = new window.FormData( this.form ); | ||
const entries = Array.from( data.entries() ); | ||
return entries; | ||
} | ||
|
||
checkState() { | ||
const { loading } = this.state; | ||
const { isDirty, changedMetaBoxState, location } = this.props; | ||
|
||
const newIsDirty = ! isEqual( this.originalFormData, this.getFormData() ); | ||
|
||
/** | ||
* If we are not updating, then if dirty and equal to original, then set not dirty. | ||
* If we are not updating, then if not dirty and not equal to original, set as dirty. | ||
*/ | ||
if ( ! loading && isDirty !== newIsDirty ) { | ||
changedMetaBoxState( location, newIsDirty ); | ||
} | ||
} | ||
|
||
render() { | ||
const { location } = this.props; | ||
const { loading } = this.state; | ||
|
||
const classes = classnames( | ||
'editor-meta-boxes-area', | ||
`is-${ location }`, | ||
{ | ||
'is-loading': loading, | ||
} | ||
); | ||
|
||
return ( | ||
<div className={ classes }> | ||
{ loading && <Spinner /> } | ||
<div ref={ this.bindNode } /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
function mapStateToProps( state, ownProps ) { | ||
const metaBox = getMetaBox( state, ownProps.location ); | ||
const { isDirty, isUpdating } = metaBox; | ||
|
||
return { | ||
isDirty, | ||
isUpdating, | ||
isPostSaving: isSavingPost( state ) ? true : false, | ||
}; | ||
} | ||
|
||
function mapDispatchToProps( dispatch ) { | ||
return { | ||
// Used to set the reference to the MetaBox in redux, fired when the component mounts. | ||
metaBoxReloaded: ( location ) => dispatch( handleMetaBoxReload( location ) ), | ||
changedMetaBoxState: ( location, hasChanged ) => dispatch( metaBoxStateChanged( location, hasChanged ) ), | ||
metaBoxLoaded: ( location ) => dispatch( metaBoxLoaded( location ) ), | ||
}; | ||
} | ||
|
||
export default connect( mapStateToProps, mapDispatchToProps )( MetaBoxesArea ); |
Oops, something went wrong.
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.
There are still two lingering references to
MetaBoxIframe
components in this README (in section "Redux and React Meta Box Management")