Skip to content

Commit

Permalink
Merge pull request #864 from WordPress/add/suggested-format
Browse files Browse the repository at this point in the history
Editor: add suggestedPostFormat selector.
  • Loading branch information
mtias authored May 22, 2017
2 parents ae8734d + 01e65f5 commit 258e44a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
21 changes: 21 additions & 0 deletions editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,24 @@ export function didPostSaveRequestFail( state ) {
export function isSavingNewPost( state ) {
return state.saving.isNew;
}

export function getSuggestedPostFormat( state ) {
const blocks = state.editor.blockOrder;

let type;
// If there is only one block in the content of the post grab its
// `blockType` name so we can derive a suitable post format from it.
if ( blocks.length === 1 ) {
type = state.editor.blocksByUid[ blocks[ 0 ] ].blockType;
}

// We only convert to default post formats in core.
switch ( type ) {
case 'core/image':
return 'Image';
case 'core/quote':
return 'Quote';
default:
return false;
}
}
14 changes: 11 additions & 3 deletions editor/sidebar/post-status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ import FormToggle from 'components/form-toggle';
*/
import './style.scss';
import PostVisibility from '../post-visibility';
import { getEditedPostStatus } from '../../selectors';
import { getEditedPostStatus, getSuggestedPostFormat } from '../../selectors';
import { editPost } from '../../actions';

function PostStatus( { status, onUpdateStatus } ) {
function PostStatus( { status, onUpdateStatus, suggestedFormat } ) {
const onToggle = () => {
const updatedStatus = status === 'pending' ? 'draft' : 'pending';
onUpdateStatus( updatedStatus );
};

// Use the suggested post format based on the blocks content of the post
// or the default post format setting for the site.
const format = suggestedFormat || __( 'Standard' );

// Disable Reason: The input is inside the label, we shouldn't need the htmlFor
/* eslint-disable jsx-a11y/label-has-for */
return (
Expand All @@ -35,10 +39,13 @@ function PostStatus( { status, onUpdateStatus } ) {
onChange={ onToggle }
/>
</label>

<div className="editor-post-status__row">
<PostVisibility />
</div>
<div className="editor-post-status__row">
<span>{ __( 'Post Format' ) }</span>
<span>{ format }</span>
</div>
</PanelBody>
);
/* eslint-enable jsx-a11y/label-has-for */
Expand All @@ -47,6 +54,7 @@ function PostStatus( { status, onUpdateStatus } ) {
export default connect(
( state ) => ( {
status: getEditedPostStatus( state ),
suggestedFormat: getSuggestedPostFormat( state ),
} ),
( dispatch ) => {
return {
Expand Down

0 comments on commit 258e44a

Please sign in to comment.