@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
77 lines (67 loc) • 2.31 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* 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
}) => createElement(Button, {
isLink: true,
onClick: () => onUpdatePostFormat(suggestedPostFormat)
}, suggestionText);
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:'), createElement("span", {
className: "editor-post-publish-panel__link",
key: "label"
}, __('Use a post format'))];
if (!suggestion || suggestion.id === currentPostFormat) {
return null;
}
return createElement(PanelBody, {
initialOpen: false,
title: panelBodyTitle
}, createElement("p", null, __('Your theme uses post formats to highlight different kinds of content, like images or videos. Apply a post format to see this special styling.')), createElement("p", null, createElement(PostFormatSuggestion, {
onUpdatePostFormat: onUpdatePostFormat,
suggestedPostFormat: suggestion.id,
suggestionText: sprintf(
/* translators: %s: post format */
__('Apply the "%1$s" format.'), suggestion.caption)
})));
}
//# sourceMappingURL=maybe-post-format-panel.js.map