UNPKG

@wordpress/block-library

Version:
133 lines (127 loc) 4.04 kB
import { createElement, Fragment } from "@wordpress/element"; /** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { AlignmentControl, BlockControls, useBlockProps, InspectorControls } from '@wordpress/block-editor'; import { __, _n, sprintf } from '@wordpress/i18n'; import { useEntityProp } from '@wordpress/core-data'; import { PanelBody, ToggleControl } from '@wordpress/components'; import { useState, useEffect } from '@wordpress/element'; import apiFetch from '@wordpress/api-fetch'; import { addQueryArgs } from '@wordpress/url'; /** * Internal dependencies */ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; export default function Edit(_ref) { let { attributes: { textAlign, showPostTitle, showCommentsCount, level }, setAttributes, context: { postType, postId } } = _ref; const TagName = 'h' + level; const [commentsCount, setCommentsCount] = useState(); const [rawTitle] = useEntityProp('postType', postType, 'title', postId); const isSiteEditor = typeof postId === 'undefined'; const blockProps = useBlockProps({ className: classnames({ [`has-text-align-${textAlign}`]: textAlign }) }); useEffect(() => { if (isSiteEditor) { setCommentsCount(3); return; } const currentPostId = postId; apiFetch({ path: addQueryArgs('/wp/v2/comments', { post: postId, _fields: 'id' }), method: 'HEAD', parse: false }).then(res => { // Stale requests will have the `currentPostId` of an older closure. if (currentPostId === postId) { setCommentsCount(parseInt(res.headers.get('X-WP-Total'))); } }).catch(() => { setCommentsCount(0); }); }, [postId]); const blockControls = createElement(BlockControls, { group: "block" }, createElement(AlignmentControl, { value: textAlign, onChange: newAlign => setAttributes({ textAlign: newAlign }) }), createElement(HeadingLevelDropdown, { selectedLevel: level, onChange: newLevel => setAttributes({ level: newLevel }) })); const inspectorControls = createElement(InspectorControls, null, createElement(PanelBody, { title: __('Settings') }, createElement(ToggleControl, { label: __('Show post title'), checked: showPostTitle, onChange: value => setAttributes({ showPostTitle: value }) }), createElement(ToggleControl, { label: __('Show comments count'), checked: showCommentsCount, onChange: value => setAttributes({ showCommentsCount: value }) }))); const postTitle = isSiteEditor ? __('“Post Title”') : `"${rawTitle}"`; let placeholder; if (showCommentsCount && commentsCount !== undefined) { if (showPostTitle) { if (commentsCount === 1) { /* translators: %s: Post title. */ placeholder = sprintf(__('One response to %s'), postTitle); } else { placeholder = sprintf( /* translators: 1: Number of comments, 2: Post title. */ _n('%1$s response to %2$s', '%1$s responses to %2$s', commentsCount), commentsCount, postTitle); } } else if (commentsCount === 1) { placeholder = __('One response'); } else { placeholder = sprintf( /* translators: %s: Number of comments. */ _n('%s response', '%s responses', commentsCount), commentsCount); } } else if (showPostTitle) { if (commentsCount === 1) { /* translators: %s: Post title. */ placeholder = sprintf(__('Response to %s'), postTitle); } else { /* translators: %s: Post title. */ placeholder = sprintf(__('Responses to %s'), postTitle); } } else if (commentsCount === 1) { placeholder = __('Response'); } else { placeholder = __('Responses'); } return createElement(Fragment, null, blockControls, inspectorControls, createElement(TagName, blockProps, placeholder)); } //# sourceMappingURL=edit.js.map