Skip to content

Commit

Permalink
Suggest more post formats based on block presence. (#3156)
Browse files Browse the repository at this point in the history
* Suggest more post formats based on block presence.

- Add audio block suggestions.
- Add galleries.
- Add video embeds as videos.
- Also consider pullquotes a kind of quote.

* Improve the heuristics to account for a block + paragraph.
* Add more tests to suggested post format.
  • Loading branch information
mtias authored Oct 31, 2017
1 parent 082f6cd commit 82575f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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
// 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

0 comments on commit 82575f5

Please sign in to comment.