-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Chrome: Adding the Revisions Panel #910
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,12 @@ | |
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
import { parse, stringify } from 'querystring'; | ||
|
||
/** | ||
* WordPress dependencies | ||
* Internal dependencies | ||
*/ | ||
import { getBlockSettings, switchToBlockType } from 'blocks'; | ||
import { getGutenbergURL, getWPAdminURL } from './utils/url'; | ||
import { __ } from 'i18n'; | ||
|
||
/** | ||
|
@@ -26,6 +26,7 @@ export default { | |
dispatch( { | ||
type: 'REQUEST_POST_UPDATE_SUCCESS', | ||
post: newPost, | ||
isNew, | ||
} ); | ||
} ).fail( ( err ) => { | ||
dispatch( { | ||
|
@@ -40,15 +41,11 @@ export default { | |
} ); | ||
}, | ||
REQUEST_POST_UPDATE_SUCCESS( action ) { | ||
const { post } = action; | ||
const [ baseUrl, query ] = window.location.href.split( '?' ); | ||
const qs = parse( query || '' ); | ||
if ( qs.post_id === post.id ) { | ||
const { post, isNew } = action; | ||
if ( ! isNew ) { | ||
return; | ||
} | ||
|
||
const newUrl = baseUrl + '?' + stringify( { | ||
...qs, | ||
const newUrl = getGutenbergURL( { | ||
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. Inconsistency between |
||
post_id: post.id, | ||
} ); | ||
window.history.replaceState( {}, 'Post ' + post.id, newUrl ); | ||
|
@@ -65,7 +62,7 @@ export default { | |
}, | ||
TRASH_POST_SUCCESS( action ) { | ||
const { postId, postType } = action; | ||
window.location.href = 'edit.php?' + stringify( { | ||
window.location.href = getWPAdminURL( 'edit.php', { | ||
trashed: 1, | ||
post_type: postType, | ||
ids: postId, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from 'element'; | ||
import { sprintf, _n } from 'i18n'; | ||
import IconButton from 'components/icon-button'; | ||
import PanelBody from 'components/panel/body'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import { getCurrentPost, isSavingPost } from '../../selectors'; | ||
import { getWPAdminURL } from '../../utils/url'; | ||
|
||
class LastRevision extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.state = { | ||
revisions: [], | ||
loading: false, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchRevisions(); | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
if ( prevProps.postId !== this.props.postId ) { | ||
this.setState( { revisions: [] } ); | ||
} | ||
|
||
if ( | ||
( prevProps.postId !== this.props.postId ) || | ||
( prevProps.isSaving && ! this.props.isSaving ) | ||
) { | ||
this.fetchRevisions(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if ( this.fetchMediaRequest ) { | ||
this.fetchRevisionsRequest.abort(); | ||
} | ||
} | ||
|
||
fetchRevisions() { | ||
if ( ! this.props.postId ) { | ||
this.setState( { loading: false } ); | ||
return; | ||
} | ||
this.setState( { loading: true } ); | ||
const postIdToLoad = this.props.postId; | ||
this.fetchRevisionsRequest = new wp.api.collections.PostRevisions( {}, { parent: postIdToLoad } ).fetch() | ||
.done( ( revisions ) => { | ||
if ( this.props.postId !== postIdToLoad ) { | ||
return; | ||
} | ||
this.setState( { | ||
loading: false, | ||
revisions, | ||
} ); | ||
} ) | ||
.fail( () => { | ||
if ( this.props.postId !== postIdToLoad ) { | ||
return; | ||
} | ||
this.setState( { | ||
loading: false, | ||
} ); | ||
} ); | ||
} | ||
|
||
render() { | ||
const { revisions } = this.state; | ||
const lastRevision = revisions.length ? revisions[ 0 ] : null; | ||
|
||
return ( | ||
<PanelBody> | ||
<IconButton | ||
href={ lastRevision ? getWPAdminURL( 'revision.php', { revision: lastRevision.id } ) : undefined } | ||
className="editor-last-revision__title" | ||
icon="backup" | ||
> | ||
{ | ||
sprintf( | ||
_n( '%d Revision', '%d Revisions', revisions.length ), | ||
revisions.length | ||
) | ||
} | ||
</IconButton> | ||
</PanelBody> | ||
); | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
postId: getCurrentPost( state ).id, | ||
isSaving: isSavingPost( state ), | ||
}; | ||
} | ||
)( LastRevision ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.editor-last-revision__title { | ||
padding: 0; | ||
width: 100%; | ||
font-weight: 600; | ||
|
||
.dashicon { | ||
margin-right: 5px; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* External dependencies | ||
*/ | ||
import { parse, format } from 'url'; | ||
import { parse as parseQueryString, stringify } from 'querystring'; | ||
|
||
/** | ||
* Appends arguments to the query string of the url | ||
|
@@ -18,3 +19,32 @@ export function addQueryArgs( url, args ) { | |
|
||
return format( { ...parsedUrl, query } ); | ||
} | ||
|
||
/** | ||
* Returns the Gutenberg page URL with extra query strings | ||
* | ||
* @param {Object} query Query Args | ||
* | ||
* @return {String} URL | ||
*/ | ||
export function getGutenbergURL( query = {} ) { | ||
const [ baseUrl, currentQuery ] = window.location.href.split( '?' ); | ||
const qs = parseQueryString( currentQuery || '' ); | ||
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. Minor: The default can be assigned in the array destructure for clarity/consistency: const [ baseUrl, currentQuery = '' ] = window.location.href.split( '?' ); |
||
return baseUrl + '?' + stringify( { | ||
...qs, | ||
...query, | ||
} ); | ||
} | ||
|
||
/** | ||
* Returns the url of a WPAdmin Page | ||
* | ||
* @param {String} page page to navigate to | ||
* @param {Object} query Query Args | ||
* | ||
* @return {String} URL | ||
*/ | ||
export function getWPAdminURL( page, query ) { | ||
const querystring = query ? '?' + stringify( query ) : ''; | ||
return page + querystring; | ||
} |
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's both WordPress and Internal dependencies here.
Aside: We could do for a better indicator.