@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
82 lines (79 loc) • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = PostExcerpt;
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _htmlEntities = require("@wordpress/html-entities");
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/**
* Renders an editable textarea for the post excerpt.
* Templates, template parts and patterns use the `excerpt` field as a description semantically.
* Additionally templates and template parts override the `excerpt` field as `description` in
* REST API. So this component handles proper labeling and updating the edited entity.
*
* @param {Object} props - Component props.
* @param {boolean} [props.hideLabelFromVision=false] - Whether to visually hide the textarea's label.
* @param {boolean} [props.updateOnBlur=false] - Whether to update the post on change or use local state and update on blur.
*/function PostExcerpt({
hideLabelFromVision = false,
updateOnBlur = false
}) {
const {
excerpt,
shouldUseDescriptionLabel,
usedAttribute
} = (0, _data.useSelect)(select => {
const {
getCurrentPostType,
getEditedPostAttribute
} = select(_store.store);
const postType = getCurrentPostType();
// This special case is unfortunate, but the REST API of wp_template and wp_template_part
// support the excerpt field throught the "description" field rather than "excerpt".
const _usedAttribute = ['wp_template', 'wp_template_part'].includes(postType) ? 'description' : 'excerpt';
return {
excerpt: getEditedPostAttribute(_usedAttribute),
// There are special cases where we want to label the excerpt as a description.
shouldUseDescriptionLabel: ['wp_template', 'wp_template_part', 'wp_block'].includes(postType),
usedAttribute: _usedAttribute
};
}, []);
const {
editPost
} = (0, _data.useDispatch)(_store.store);
const [localExcerpt, setLocalExcerpt] = (0, _element.useState)((0, _htmlEntities.decodeEntities)(excerpt));
const updatePost = value => {
editPost({
[usedAttribute]: value
});
};
const label = shouldUseDescriptionLabel ? (0, _i18n.__)('Write a description (optional)') : (0, _i18n.__)('Write an excerpt (optional)');
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: "editor-post-excerpt",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextareaControl, {
__nextHasNoMarginBottom: true,
label: label,
hideLabelFromVision: hideLabelFromVision,
className: "editor-post-excerpt__textarea",
onChange: updateOnBlur ? setLocalExcerpt : updatePost,
onBlur: updateOnBlur ? () => updatePost(localExcerpt) : undefined,
value: updateOnBlur ? localExcerpt : excerpt,
help: !shouldUseDescriptionLabel ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ExternalLink, {
href: (0, _i18n.__)('https://wordpress.org/documentation/article/page-post-settings-sidebar/#excerpt'),
children: (0, _i18n.__)('Learn more about manual excerpts')
}) : (0, _i18n.__)('Write a description')
})
});
}
//# sourceMappingURL=index.js.map