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: Add a Post Publish Panel #4158

Merged
merged 2 commits into from
Jan 5, 2018
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
110 changes: 62 additions & 48 deletions editor/components/post-publish-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,89 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { get } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/element';
import { withAPIData, PanelBody, IconButton } from '@wordpress/components';
import { compose, Component } from '@wordpress/element';
import { withAPIData, IconButton, Spinner } from '@wordpress/components';

/**
* Internal Dependencies
*/
import './style.scss';
import PostVisibility from '../post-visibility';
import PostVisibilityLabel from '../post-visibility/label';
import PostSchedule from '../post-schedule';
import PostScheduleLabel from '../post-schedule/label';
import PostPublishButton from '../post-publish-button';
import { getCurrentPostType } from '../../store/selectors';
import PostPublishPanelPrepublish from './prepublish';
import PostPublishPanelPostpublish from './postpublish';
import { getCurrentPostType, isCurrentPostPublished, isSavingPost } from '../../store/selectors';

function PostPublishPanel( { onClose, user } ) {
const canPublish = user.data && user.data.capabilities.publish_posts;
class PostPublishPanel extends Component {
constructor() {
super( ...arguments );
this.onPublish = this.onPublish.bind( this );
this.state = {
loading: false,
published: false,
};
}

return (
<div className="editor-post-publish-panel">
<div className="editor-post-publish-panel__header">
<div className="editor-post-publish-panel__header-publish-button">
<PostPublishButton onSubmit={ onClose } />
</div>
<IconButton
onClick={ onClose }
icon="no-alt"
label={ __( 'Close Publish Panel' ) }
/>
</div>
componentWillReceiveProps( newProps ) {
if (
newProps.isPublished &&
! this.state.published &&
! newProps.isSaving
) {
this.setState( {
published: true,
loading: false,
} );
}
}

onPublish() {
this.setState( { loading: true } );
}

<div className="editor-post-publish-panel__content">
<div><strong>{ __( 'Are you ready to publish?' ) }</strong></div>
<p>{ __( 'Here, you can do a last-minute check up of your settings below, before you publish.' ) }</p>
{ ! canPublish &&
<div>
<span>{ __( 'Visibility' ) }</span>
<span><PostVisibilityLabel /></span>
</div>
}
{ canPublish &&
<PanelBody initialOpen={ false } title={ [
__( 'Visibility: ' ),
<span className="editor-post-publish-panel__link" key="label"><PostVisibilityLabel /></span>,
] }>
<PostVisibility />
</PanelBody>
}
{ canPublish &&
<PanelBody initialOpen={ false } title={ [
__( 'Publish: ' ),
<span className="editor-post-publish-panel__link" key="label"><PostScheduleLabel /></span>,
] }>
<PostSchedule />
</PanelBody>
}
render() {
const { onClose, user } = this.props;
const { loading, published } = this.state;
const canPublish = get( user.data, [ 'post_type_capabilities', 'publish_posts' ], false );

return (
<div className="editor-post-publish-panel">
<div className="editor-post-publish-panel__header">
{ ! published && (
<div className="editor-post-publish-panel__header-publish-button">
<PostPublishButton onSubmit={ this.onPublish } />
</div>
) }
{ published && (
<div className="editor-post-publish-panel__header-published">{ __( 'Published' ) }</div>
) }
<IconButton
onClick={ onClose }
icon="no-alt"
label={ __( 'Close Publish Panel' ) }
/>
</div>
<div className="editor-post-publish-panel__content">
{ canPublish && ! loading && ! published && <PostPublishPanelPrepublish /> }
{ loading && ! published && <Spinner /> }
{ published && <PostPublishPanelPostpublish /> }
</div>
</div>
</div>
);
);
}
}

const applyConnect = connect(
( state ) => {
return {
postType: getCurrentPostType( state ),
isPublished: isCurrentPostPublished( state ),
isSaving: isSavingPost( state ),
};
},
);
Expand Down
102 changes: 102 additions & 0 deletions editor/components/post-publish-panel/postpublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* External Dependencies
*/
import { get } from 'lodash';
import { connect } from 'react-redux';

/**
* WordPress Dependencies
*/
import { PanelBody, Button, ClipboardButton, withAPIData } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { Component, compose } from '@wordpress/element';

/**
* Internal Dependencies
*/
import { getCurrentPost, getCurrentPostType } from '../../store/selectors';

class PostPublishPanelPostpublish extends Component {
constructor() {
super( ...arguments );
this.state = {
showCopyConfirmation: false,
};
this.onCopy = this.onCopy.bind( this );
this.onSelectInput = this.onSelectInput.bind( this );
}

componentWillUnmount() {
clearTimeout( this.dismissCopyConfirmation );
}

onCopy() {
this.setState( {
showCopyConfirmation: true,
} );

clearTimeout( this.dismissCopyConfirmation );
this.dismissCopyConfirmation = setTimeout( () => {
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 no issue here, but this pattern is so easy to get wrong, makes me wonder if we ought to explore a higher-order component like https://www.npmjs.com/package/@hocs/safe-timers or our own.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mmm! sounds like a good idea to me.

this.setState( {
showCopyConfirmation: false,
} );
}, 4000 );
}

onSelectInput( event ) {
event.target.select();
}

render() {
const { post, postType } = this.props;
const viewPostLabel = get( postType, [ 'data', 'labels', 'view_item' ] );

return (
<div className="post-publish-panel__postpublish">
<PanelBody className="post-publish-panel__postpublish-header">
<a href={ post.link }>{ post.title || __( '(no title)' ) }</a>{ __( ' is now live.' ) }
</PanelBody>
<PanelBody>
<div><strong>{ __( 'What\'s next?' ) }</strong></div>
Copy link
Member

Choose a reason for hiding this comment

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

Prefer to use "What's next?" in these cases instead of escaping.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We may have to tweak our ESlint settings to do so.

Copy link
Member

Choose a reason for hiding this comment

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

I prefer as currently proposed 😉

<input
className="post-publish-panel__postpublish-link-input"
readOnly
value={ post.link }
onFocus={ this.onSelectInput }
/>
<div className="post-publish-panel__postpublish-buttons">
<Button className="button" href={ post.link }>
{ viewPostLabel }
</Button>

<ClipboardButton className="button" text={ post.link } onCopy={ this.onCopy }>
{ this.state.showCopyConfirmation ? __( 'Copied!' ) : __( 'Copy Link' ) }
</ClipboardButton>
</div>
</PanelBody>
</div>
);
}
}

const applyConnect = connect(
( state ) => {
return {
post: getCurrentPost( state ),
postTypeSlug: getCurrentPostType( state ),
};
}
);

const applyWithAPIData = withAPIData( ( props ) => {
const { postTypeSlug } = props;

return {
postType: `/wp/v2/types/${ postTypeSlug }?context=edit`,
};
} );

export default compose( [
applyConnect,
applyWithAPIData,
] )( PostPublishPanelPostpublish );
36 changes: 36 additions & 0 deletions editor/components/post-publish-panel/prepublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PanelBody } from '@wordpress/components';

/**
* Internal Dependencies
*/
import PostVisibility from '../post-visibility';
import PostVisibilityLabel from '../post-visibility/label';
import PostSchedule from '../post-schedule';
import PostScheduleLabel from '../post-schedule/label';

function PostPublishPanelPrepublish() {
return (
<div className="editor-post-publish-panel__prepublish">
<div><strong>{ __( 'Are you ready to publish?' ) }</strong></div>
<p>{ __( 'Here, you can do a last-minute check up of your settings below, before you publish.' ) }</p>
<PanelBody initialOpen={ false } title={ [
__( 'Visibility: ' ),
<span className="editor-post-publish-panel__link" key="label"><PostVisibilityLabel /></span>,
] }>
<PostVisibility />
</PanelBody>
<PanelBody initialOpen={ false } title={ [
__( 'Publish: ' ),
<span className="editor-post-publish-panel__link" key="label"><PostScheduleLabel /></span>,
] }>
<PostSchedule />
</PanelBody>
</div>
);
}

export default PostPublishPanelPrepublish;
86 changes: 70 additions & 16 deletions editor/components/post-publish-panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@
}

.editor-post-publish-panel__content {
padding: 16px;

.components-panel__body {
margin-left: -16px;
margin-right: -16px;
margin-bottom: -16px;
margin-top: 10px;
border: none;

.components-panel__body-toggle {
font-weight: normal;
}
}

.editor-post-visibility__dialog-legend {
display: none;
.spinner {
display: block;
float: none;
margin: 100px auto 0;
}
}

Expand All @@ -37,6 +25,10 @@
text-align: right;
}

.editor-post-publish-panel__header-published {
flex-grow: 1;
}

.wp-core-ui .editor-post-publish-panel__toggle.button-primary {
display: inline-flex;
align-items: center;
Expand All @@ -54,3 +46,65 @@
color: $blue-medium-700;
text-decoration: underline;
}

.editor-post-publish-panel__prepublish {
padding: 16px;

.components-panel__body {
margin-left: -16px;
margin-right: -16px;
margin-bottom: -16px;
margin-top: 10px;
border: none;

.components-panel__body-toggle {
font-weight: normal;
}
}

.editor-post-visibility__dialog-legend {
display: none;
}
}

.post-publish-panel__postpublish .components-panel__body {
border-bottom: 1px solid $light-gray-500;
border-top: none;
}

.wp-core-ui .post-publish-panel__postpublish-buttons {
display: flex;
align-content: space-between;

> * {
Copy link
Member

Choose a reason for hiding this comment

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

Why need this selector? It generally degrades performance a tiny bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the "ClipboardButton" needs a wrapper, we can't target children buttons using a selector. I can replace it with something like > .components__button, > span, what do you think? I like the genericity of > * though?

flex-grow: 1;
margin-right: 10px;

&:last-child {
margin-right: 0;
}
}

.components-button {
text-align: center;
}

.components-clipboard-button {
width: 100%;
}
}

.post-publish-panel__postpublish-link-input[readonly] {
width: 100%;
border: 1px solid $light-gray-500;
padding: 10px;
border-radius: $button-style__radius-roundrect;
margin: 15px 0;
background: $white;
overflow: hidden;
text-overflow: ellipsis;
}

.post-publish-panel__postpublish-header {
font-weight: 500;
}