@wordpress/block-library
Version:
Block library for the WordPress editor.
94 lines (93 loc) • 4.23 kB
JavaScript
import { BlockControls, HeadingLevelDropdown, InspectorControls, store, useBlockProps } from "@wordpress/block-editor";
import { __, _n, sprintf } from "@wordpress/i18n";
import { useEntityProp } from "@wordpress/core-data";
import { ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { usePostCommentsCount, useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import useDeprecatedTextAlign from "../utils/deprecated-text-align-attributes.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/comments-title/edit.jsx
function Edit(props) {
useDeprecatedTextAlign(props);
const { attributes, setAttributes, context } = props;
const { showPostTitle, showCommentsCount, level = 2, levelOptions } = attributes;
const { postId, postType } = context;
const TagName = "h" + level;
const [rawTitle] = useEntityProp("postType", postType, "title", postId);
const isSiteEditor = typeof postId === "undefined";
const blockProps = useBlockProps();
const { threadCommentsDepth, threadComments, commentsPerPage, pageComments } = useSelect((select) => {
const { getSettings } = select(store);
return getSettings().__experimentalDiscussionSettings ?? {};
}, []);
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const { totalItems, hasResolved } = usePostCommentsCount(postId);
let commentsCount;
if (isSiteEditor) {
const nestedCommentsNumber = threadComments ? Math.min(threadCommentsDepth, 3) - 1 : 0;
const commentsNumber = parseInt(nestedCommentsNumber) + parseInt(pageComments ? commentsPerPage : 3);
commentsCount = Math.min(commentsNumber, 3);
} else commentsCount = totalItems ?? (hasResolved ? 0 : void 0);
const blockControls = /* @__PURE__ */ jsx(BlockControls, {
group: "block",
children: /* @__PURE__ */ jsx(HeadingLevelDropdown, {
value: level,
options: levelOptions,
onChange: (newLevel) => setAttributes({ level: newLevel })
})
});
const inspectorControls = /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
showPostTitle: true,
showCommentsCount: true
});
},
dropdownMenuProps,
children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Show post title"),
isShownByDefault: true,
hasValue: () => !showPostTitle,
onDeselect: () => setAttributes({ showPostTitle: true }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Show post title"),
checked: showPostTitle,
onChange: (value) => setAttributes({ showPostTitle: value })
})
}), /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Show comments count"),
isShownByDefault: true,
hasValue: () => !showCommentsCount,
onDeselect: () => setAttributes({ showCommentsCount: true }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Show comments count"),
checked: showCommentsCount,
onChange: (value) => setAttributes({ showCommentsCount: value })
})
})]
}) });
const postTitle = isSiteEditor ? __("Post Title") : rawTitle;
let placeholder;
if (showCommentsCount && commentsCount !== void 0) {
if (showPostTitle) {
if (commentsCount === 1) placeholder = sprintf(__("One response to \"%s\""), postTitle);
else placeholder = sprintf(_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(_n("%s response", "%s responses", commentsCount), commentsCount);
} else if (showPostTitle) {
if (commentsCount === 1) placeholder = sprintf(__("Response to \"%s\""), postTitle);
else placeholder = sprintf(__("Responses to \"%s\""), postTitle);
} else if (commentsCount === 1) placeholder = __("Response");
else placeholder = __("Responses");
return /* @__PURE__ */ jsxs(Fragment, { children: [
blockControls,
inspectorControls,
/* @__PURE__ */ jsx(TagName, {
...blockProps,
children: placeholder
})
] });
}
export { Edit as default };
//# sourceMappingURL=edit.mjs.map