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

Suggest more post formats based on block presence. #3156

Merged
merged 3 commits into from
Oct 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
21 changes: 20 additions & 1 deletion editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,18 +951,37 @@ export function getSuggestedPostFormat( state ) {
const blocks = state.editor.blockOrder;

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

// If there are two blocks in the content and the last one is a text blocks
Copy link
Member

Choose a reason for hiding this comment

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

What is the intended logic here? Should it really be restricted to posts with two blocks specifically, or is the idea if the block is entirely text except for a first non-text block, it should inherit format based on that first block?

let name;
// If there is only one block in the content of the post grab its name
// so we can derive a suitable post format from it.
if ( uids.length === 1 ) {
	name = getBlock( uids[ 0 ] ).name;
} else if ( uids.length > 1 ) {
	const isMostlyText = uids.slice( 1 ).every( ( uid ) => {
		return getBlock( uid ).name === 'core/paragraph';
	} );

	if ( isMostlyText ) {
		name = getBlock( uids[ 0 ] ).name;
	}
}

This is non-trivial logic, so might be worth memoizing, and definitely worth testing.

Copy link
Member Author

Choose a reason for hiding this comment

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

The latter. If there are only two blocks, and the second one is text, the first one determines the format suggestion if it matches one of the post formats.

Example: quote + comment like https://ma.tt/2017/10/gauguin-to-olive-garden/

Copy link
Member Author

Choose a reason for hiding this comment

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

If there are more than two we don't suggest anymore as we consider it a standard post.

// grab the name of the first one to also suggest a post format from it.
if ( blocks.length === 2 ) {
if ( state.editor.blocksByUid[ blocks[ 1 ] ].name === 'core/paragraph' ) {
name = state.editor.blocksByUid[ blocks[ 0 ] ].name;
}
}

// We only convert to default post formats in core.
switch ( name ) {
case 'core/image':
return 'image';
case 'core/quote':
case 'core/pullquote':
return 'quote';
case 'core/gallery':
return 'gallery';
case 'core/video':
case 'core-embed/youtube':
case 'core-embed/vimeo':
return 'video';
case 'core/audio':
case 'core-embed/spotify':
case 'core-embed/soundcloud':
return 'audio';
}

return null;
Expand Down
27 changes: 27 additions & 0 deletions editor/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,33 @@ describe( 'selectors', () => {

expect( getSuggestedPostFormat( state ) ).toBe( 'quote' );
} );

it( 'returns Video if the first block is of type `core-embed/youtube`', () => {
const state = {
editor: {
blockOrder: [ 567 ],
blocksByUid: {
567: { uid: 567, name: 'core-embed/youtube' },
},
},
};

expect( getSuggestedPostFormat( state ) ).toBe( 'video' );
} );

it( 'returns Quote if the first block is of type `core/quote` and second is of type `core/paragraph`', () => {
const state = {
editor: {
blockOrder: [ 456, 789 ],
blocksByUid: {
456: { uid: 456, name: 'core/quote' },
789: { uid: 789, name: 'core/paragraph' },
},
},
};

expect( getSuggestedPostFormat( state ) ).toBe( 'quote' );
} );
} );

describe( 'getNotices', () => {
Expand Down