UNPKG

@wordpress/block-library

Version:
178 lines (177 loc) • 7.75 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_data = require("@wordpress/data"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_components = require("@wordpress/components"); let _wordpress_core_data = require("@wordpress/core-data"); let __hooks_js = require("./hooks.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/comment-template/edit.jsx /** * Function that returns a comment structure that will be rendered with default placeholders. * * Each comment has a `commentId` property that is always a negative number in * case of the placeholders. This is to ensure that the comment does not * conflict with the actual (real) comments. * * @param {Object} settings Discussion Settings. * @param {number} [settings.perPage] - Comments per page setting or block attribute. * @param {boolean} [settings.pageComments] - Enable break comments into pages setting. * @param {boolean} [settings.threadComments] - Enable threaded (nested) comments setting. * @param {number} [settings.threadCommentsDepth] - Level deep of threaded comments. * * @typedef {{id: null, children: EmptyComment[]}} EmptyComment * @return {EmptyComment[]} Inner blocks of the Comment Template */ const getCommentsPlaceholder = ({ perPage, pageComments, threadComments, threadCommentsDepth }) => { const commentsDepth = !threadComments ? 1 : Math.min(threadCommentsDepth, 3); const buildChildrenComment = (commentsLevel) => { if (commentsLevel < commentsDepth) { const nextLevel = commentsLevel + 1; return [{ commentId: -(commentsLevel + 3), children: buildChildrenComment(nextLevel) }]; } return []; }; const placeholderComments = [{ commentId: -1, children: buildChildrenComment(1) }]; if ((!pageComments || perPage >= 2) && commentsDepth < 3) placeholderComments.push({ commentId: -2, children: [] }); if ((!pageComments || perPage >= 3) && commentsDepth < 2) placeholderComments.push({ commentId: -3, children: [] }); return placeholderComments; }; /** * Component which renders the inner blocks of the Comment Template. * * @param {Object} props Component props. * @param {Array} [props.comment] - A comment object. * @param {Array} [props.activeCommentId] - The ID of the comment that is currently active. * @param {Array} [props.setActiveCommentId] - The setter for activeCommentId. * @param {Array} [props.firstCommentId] - ID of the first comment in the array. * @param {Array} [props.blocks] - Array of blocks returned from * getBlocks() in parent . * @return {Element} Inner blocks of the Comment Template */ function CommentTemplateInnerBlocks({ comment, activeCommentId, setActiveCommentId, firstCommentId, blocks }) { const { children, ...innerBlocksProps } = (0, _wordpress_block_editor.useInnerBlocksProps)({}); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", { ...innerBlocksProps, children: [ comment.commentId === (activeCommentId || firstCommentId) ? children : null, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedCommentTemplatePreview, { blocks, commentId: comment.commentId, setActiveCommentId, isHidden: comment.commentId === (activeCommentId || firstCommentId) }), comment?.children?.length > 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CommentsList, { comments: comment.children, activeCommentId, setActiveCommentId, blocks, firstCommentId }) : null ] }); } const CommentTemplatePreview = ({ blocks, commentId, setActiveCommentId, isHidden }) => { const blockPreviewProps = (0, _wordpress_block_editor.__experimentalUseBlockPreview)({ blocks }); const handleOnClick = () => { setActiveCommentId(commentId); }; const style = { display: isHidden ? "none" : void 0 }; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...blockPreviewProps, tabIndex: 0, role: "button", style, onClick: handleOnClick, onKeyPress: handleOnClick }); }; const MemoizedCommentTemplatePreview = (0, _wordpress_element.memo)(CommentTemplatePreview); /** * Component that renders a list of (nested) comments. It is called recursively. * * @param {Object} props Component props. * @param {Array} [props.comments] - Array of comment objects. * @param {Array} [props.blockProps] - Props from parent's `useBlockProps()`. * @param {Array} [props.activeCommentId] - The ID of the comment that is currently active. * @param {Array} [props.setActiveCommentId] - The setter for activeCommentId. * @param {Array} [props.blocks] - Array of blocks returned from getBlocks() in parent. * @param {Object} [props.firstCommentId] - The ID of the first comment in the array of * comment objects. * @return {Element} List of comments. */ const CommentsList = ({ comments, blockProps, activeCommentId, setActiveCommentId, blocks, firstCommentId }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ol", { ...blockProps, children: comments && comments.map(({ commentId, ...comment }, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockContextProvider, { value: { commentId: commentId < 0 ? null : commentId }, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CommentTemplateInnerBlocks, { comment: { commentId, ...comment }, activeCommentId, setActiveCommentId, blocks, firstCommentId }) }, comment.commentId || index)) }); function CommentTemplateEdit({ clientId, context: { postId } }) { const blockProps = (0, _wordpress_block_editor.useBlockProps)(); const [activeCommentId, setActiveCommentId] = (0, _wordpress_element.useState)(); const { commentOrder, threadCommentsDepth, threadComments, commentsPerPage, pageComments } = (0, _wordpress_data.useSelect)((select) => { const { getSettings } = select(_wordpress_block_editor.store); return getSettings().__experimentalDiscussionSettings ?? {}; }, []); const commentQuery = (0, __hooks_js.useCommentQueryArgs)({ postId }); const { topLevelComments, blocks } = (0, _wordpress_data.useSelect)((select) => { const { getEntityRecords } = select(_wordpress_core_data.store); const { getBlocks } = select(_wordpress_block_editor.store); return { topLevelComments: commentQuery ? getEntityRecords("root", "comment", commentQuery) : null, blocks: getBlocks(clientId) }; }, [clientId, commentQuery]); let commentTree = (0, __hooks_js.useCommentTree)(commentOrder === "desc" && topLevelComments ? [...topLevelComments].reverse() : topLevelComments); if (!topLevelComments) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { ...blockProps, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Spinner, {}) }); if (!postId) commentTree = getCommentsPlaceholder({ perPage: commentsPerPage, pageComments, threadComments, threadCommentsDepth }); if (!commentTree.length) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { ...blockProps, children: (0, _wordpress_i18n.__)("No results found.") }); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CommentsList, { comments: commentTree, blockProps, blocks, activeCommentId, setActiveCommentId, firstCommentId: commentTree[0]?.commentId }); } exports.default = CommentTemplateEdit; //# sourceMappingURL=edit.cjs.map