-
Notifications
You must be signed in to change notification settings - Fork 4k
/
maybe-post-format-panel.js
89 lines (79 loc) · 2.17 KB
/
maybe-post-format-panel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* External dependencies
*/
import { find, get, includes } from 'lodash';
/**
* WordPress dependencies
*/
import { Button, PanelBody } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { POST_FORMATS } from '../post-format';
const getSuggestion = ( supportedFormats, suggestedPostFormat ) => {
const formats = POST_FORMATS.filter( ( format ) =>
includes( supportedFormats, format.id )
);
return find( formats, ( format ) => format.id === suggestedPostFormat );
};
const PostFormatSuggestion = ( {
suggestedPostFormat,
suggestionText,
onUpdatePostFormat,
} ) => (
<Button isLink onClick={ () => onUpdatePostFormat( suggestedPostFormat ) }>
{ suggestionText }
</Button>
);
export default function PostFormatPanel() {
const { currentPostFormat, suggestion } = useSelect( ( select ) => {
const { getEditedPostAttribute, getSuggestedPostFormat } = select(
'core/editor'
);
const supportedFormats = get(
select( 'core' ).getThemeSupports(),
[ 'formats' ],
[]
);
return {
currentPostFormat: getEditedPostAttribute( 'format' ),
suggestion: getSuggestion(
supportedFormats,
getSuggestedPostFormat()
),
};
}, [] );
const { editPost } = useDispatch( 'core/editor' );
const onUpdatePostFormat = ( format ) => editPost( { format } );
const panelBodyTitle = [
__( 'Suggestion:' ),
<span className="editor-post-publish-panel__link" key="label">
{ __( 'Use a post format' ) }
</span>,
];
if ( ! suggestion || suggestion.id === currentPostFormat ) {
return null;
}
return (
<PanelBody initialOpen={ false } title={ panelBodyTitle }>
<p>
{ __(
'Your theme uses post formats to highlight different kinds of content, like images or videos. Apply a post format to see this special styling.'
) }
</p>
<p>
<PostFormatSuggestion
onUpdatePostFormat={ onUpdatePostFormat }
suggestedPostFormat={ suggestion.id }
suggestionText={ sprintf(
/* translators: %s: post format */
__( 'Apply the "%1$s" format.' ),
suggestion.caption
) }
/>
</p>
</PanelBody>
);
}