Skip to content
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

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
margin-top: -1px;
}

.components-panel__body-toggle {
.components-panel__body-toggle.components-icon-button {
display: flex;
justify-content: space-between;
align-items: center;
Expand Down
17 changes: 7 additions & 10 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* External dependencies
*/
import { get } from 'lodash';
import { parse, stringify } from 'querystring';

/**
* WordPress dependencies
* Internal dependencies
*/
import { getBlockSettings, switchToBlockType } from 'blocks';
Copy link
Member

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.

import { getGutenbergURL, getWPAdminURL } from './utils/url';
import { __ } from 'i18n';

/**
Expand All @@ -26,6 +26,7 @@ export default {
dispatch( {
type: 'REQUEST_POST_UPDATE_SUCCESS',
post: newPost,
isNew,
} );
} ).fail( ( err ) => {
dispatch( {
Expand All @@ -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( {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistency between Url and URL. I think we'll want to settle on and use consistently a recommendation for acronyms. The core guidelines do not include one. Calypso recommends strict camel-casing, but I've personally grown fonder of upper-case. I can create an issue.

post_id: post.id,
} );
window.history.replaceState( {}, 'Post ' + post.id, newUrl );
Expand All @@ -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,
Expand Down
110 changes: 110 additions & 0 deletions editor/sidebar/last-revision/index.js
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 );
9 changes: 9 additions & 0 deletions editor/sidebar/last-revision/style.scss
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;
}
}
2 changes: 2 additions & 0 deletions editor/sidebar/post-settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import PostStatus from '../post-status';
import PostExcerpt from '../post-excerpt';
import FeaturedImage from '../featured-image';
import DiscussionPanel from '../discussion-panel';
import LastRevision from '../last-revision';

const PostSettings = ( { toggleSidebar } ) => {
return (
Expand All @@ -31,6 +32,7 @@ const PostSettings = ( { toggleSidebar } ) => {
</div>
</PanelHeader>
<PostStatus />
<LastRevision />
<FeaturedImage />
<PostExcerpt />
<DiscussionPanel />
Expand Down
30 changes: 30 additions & 0 deletions editor/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 || '' );
Copy link
Member

Choose a reason for hiding this comment

The 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;
}