-
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: Remove dirty-checking metaboxes state #4184
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
09eba85
MetaBoxes: Remove dirty-checking metaboxes state
youknowriad 25f598f
MetaBoxes: compare meta boxes HTML while leaving the editor to warn a…
youknowriad 95a541f
Meta Boxes: Changing the way we save metaboxes
youknowriad 64c06f2
Rename bindNode and guard against empty nodes
youknowriad 6f84a93
Rename setMetaBoxSavedData action
youknowriad 5adcef6
Improve Meta Boxes reducer docs
youknowriad 453cde2
Use foreach and endforeach instead of curlybraces
youknowriad 7a7d61b
Fix dirty checking
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
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 |
---|---|---|
@@ -1,153 +1,89 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEqual } from 'lodash'; | ||
import classnames from 'classnames'; | ||
import { connect } from 'react-redux'; | ||
import jQuery from 'jquery'; | ||
|
||
/** | ||
* 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 '../../../store/actions'; | ||
import { getMetaBox, isSavingPost } from '../../../store/selectors'; | ||
import { isSavingMetaBoxes } from '../../../store/selectors'; | ||
|
||
class MetaBoxesArea extends Component { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.state = { | ||
loading: false, | ||
}; | ||
this.originalFormData = ''; | ||
this.bindNode = this.bindNode.bind( this ); | ||
this.checkState = this.checkState.bind( this ); | ||
} | ||
|
||
bindNode( node ) { | ||
this.node = node; | ||
this.bindContainerNode = this.bindContainerNode.bind( this ); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
componentDidMount() { | ||
this.mounted = true; | ||
this.fetchMetaboxes(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.mounted = false; | ||
this.unbindFormEvents(); | ||
document.querySelector( '#metaboxes' ).appendChild( this.form ); | ||
} | ||
|
||
unbindFormEvents() { | ||
this.form = document.querySelector( '.metabox-location-' + this.props.location ); | ||
if ( this.form ) { | ||
this.form.removeEventListener( 'change', this.checkState ); | ||
this.form.removeEventListener( 'input', this.checkState ); | ||
this.container.appendChild( this.form ); | ||
} | ||
} | ||
|
||
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', | ||
}; | ||
|
||
// Save the metaboxes | ||
window.fetch( addQueryArgs( window._wpMetaBoxUrl, { meta_box: location } ), fetchOptions ) | ||
.then( () => { | ||
if ( ! this.mounted ) { | ||
return false; | ||
} | ||
this.setState( { loading: false } ); | ||
this.props.metaBoxReloaded( location ); | ||
} ); | ||
/** | ||
* Get the meta box location form from the original location. | ||
*/ | ||
componentWillUnmount() { | ||
if ( this.form ) { | ||
document.querySelector( '#metaboxes' ).appendChild( this.form ); | ||
} | ||
} | ||
|
||
fetchMetaboxes() { | ||
const { location } = this.props; | ||
this.form = document.querySelector( '.metabox-location-' + location ); | ||
this.node.appendChild( this.form ); | ||
this.form.onSubmit = ( event ) => event.preventDefault(); | ||
this.originalFormData = this.getFormData(); | ||
this.form.addEventListener( 'change', this.checkState ); | ||
this.form.addEventListener( 'input', this.checkState ); | ||
this.props.metaBoxLoaded( location ); | ||
} | ||
|
||
getFormData() { | ||
return jQuery( this.form ).serialize(); | ||
} | ||
|
||
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 ); | ||
} | ||
/** | ||
* Binds the metabox area container node. | ||
* | ||
* @param {Element} node DOM Node. | ||
*/ | ||
bindContainerNode( node ) { | ||
this.container = node; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
render() { | ||
const { location } = this.props; | ||
const { loading } = this.state; | ||
const { location, isSaving } = this.props; | ||
|
||
const classes = classnames( | ||
'editor-meta-boxes-area', | ||
`is-${ location }`, | ||
{ | ||
'is-loading': loading, | ||
'is-loading': isSaving, | ||
} | ||
); | ||
|
||
return ( | ||
<div className={ classes }> | ||
{ loading && <Spinner /> } | ||
<div ref={ this.bindNode } /> | ||
{ isSaving && <Spinner /> } | ||
<div className="editor-meta-boxes-area__container" ref={ this.bindContainerNode } /> | ||
<div className="editor-meta-boxes-area__clear" /> | ||
</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 ) { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
function mapStateToProps( state ) { | ||
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 ) ), | ||
isSaving: isSavingMetaBoxes( state ), | ||
}; | ||
} | ||
|
||
export default connect( mapStateToProps, mapDispatchToProps )( MetaBoxesArea ); | ||
export default connect( mapStateToProps )( MetaBoxesArea ); |
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,16 @@ | ||
/** | ||
* Function returning the current Meta Boxes DOM Node in the editor | ||
* whether the meta box area is opened or not. | ||
* If the MetaBox Area is visible returns it, and returns the original container instead. | ||
* | ||
* @param {string} location Meta Box location. | ||
* @returns {string} HTML content. | ||
*/ | ||
export const getMetaBoxContainer = ( location ) => { | ||
const area = document.querySelector( `.editor-meta-boxes-area.is-${ location } .metabox-location-${ location }` ); | ||
if ( area ) { | ||
return area; | ||
} | ||
|
||
return document.querySelector( '#metaboxes .metabox-location-' + location ); | ||
}; |
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.
Seems
this.originalFormData
is defunct now and can be removed, along withgetFormData
.