@wordpress/block-library
Version:
Block library for the WordPress editor.
217 lines (216 loc) • 5.89 kB
JavaScript
import { jsx, jsxs } from "react/jsx-runtime";
import { useState, memo } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import { __ } from "@wordpress/i18n";
import {
BlockContextProvider,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
__experimentalUseBlockPreview as useBlockPreview
} from "@wordpress/block-editor";
import { Spinner } from "@wordpress/components";
import { store as coreStore } from "@wordpress/core-data";
import { useCommentQueryArgs, useCommentTree } from "./hooks";
const TEMPLATE = [
["core/avatar"],
["core/comment-author-name"],
["core/comment-date"],
["core/comment-content"],
["core/comment-reply-link"],
["core/comment-edit-link"]
];
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;
};
function CommentTemplateInnerBlocks({
comment,
activeCommentId,
setActiveCommentId,
firstCommentId,
blocks
}) {
const { children, ...innerBlocksProps } = useInnerBlocksProps(
{},
{ template: TEMPLATE }
);
return /* @__PURE__ */ jsxs("li", { ...innerBlocksProps, children: [
comment.commentId === (activeCommentId || firstCommentId) ? children : null,
/* @__PURE__ */ jsx(
MemoizedCommentTemplatePreview,
{
blocks,
commentId: comment.commentId,
setActiveCommentId,
isHidden: comment.commentId === (activeCommentId || firstCommentId)
}
),
comment?.children?.length > 0 ? /* @__PURE__ */ jsx(
CommentsList,
{
comments: comment.children,
activeCommentId,
setActiveCommentId,
blocks,
firstCommentId
}
) : null
] });
}
const CommentTemplatePreview = ({
blocks,
commentId,
setActiveCommentId,
isHidden
}) => {
const blockPreviewProps = useBlockPreview({
blocks
});
const handleOnClick = () => {
setActiveCommentId(commentId);
};
const style = {
display: isHidden ? "none" : void 0
};
return /* @__PURE__ */ jsx(
"div",
{
...blockPreviewProps,
tabIndex: 0,
role: "button",
style,
onClick: handleOnClick,
onKeyPress: handleOnClick
}
);
};
const MemoizedCommentTemplatePreview = memo(CommentTemplatePreview);
const CommentsList = ({
comments,
blockProps,
activeCommentId,
setActiveCommentId,
blocks,
firstCommentId
}) => /* @__PURE__ */ jsx("ol", { ...blockProps, children: comments && comments.map(({ commentId, ...comment }, index) => /* @__PURE__ */ jsx(
BlockContextProvider,
{
value: {
// If the commentId is negative it means that this comment is a
// "placeholder" and that the block is most likely being used in the
// site editor. In this case, we have to set the commentId to `null`
// because otherwise the (non-existent) comment with a negative ID
// would be requested from the REST API.
commentId: commentId < 0 ? null : commentId
},
children: /* @__PURE__ */ jsx(
CommentTemplateInnerBlocks,
{
comment: { commentId, ...comment },
activeCommentId,
setActiveCommentId,
blocks,
firstCommentId
}
)
},
comment.commentId || index
)) });
function CommentTemplateEdit({
clientId,
context: { postId }
}) {
const blockProps = useBlockProps();
const [activeCommentId, setActiveCommentId] = useState();
const {
commentOrder,
threadCommentsDepth,
threadComments,
commentsPerPage,
pageComments
} = useSelect((select) => {
const { getSettings } = select(blockEditorStore);
return getSettings().__experimentalDiscussionSettings;
});
const commentQuery = useCommentQueryArgs({
postId
});
const { topLevelComments, blocks } = useSelect(
(select) => {
const { getEntityRecords } = select(coreStore);
const { getBlocks } = select(blockEditorStore);
return {
// Request only top-level comments. Replies are embedded.
topLevelComments: commentQuery ? getEntityRecords("root", "comment", commentQuery) : null,
blocks: getBlocks(clientId)
};
},
[clientId, commentQuery]
);
let commentTree = useCommentTree(
// Reverse the order of top comments if needed.
commentOrder === "desc" && topLevelComments ? [...topLevelComments].reverse() : topLevelComments
);
if (!topLevelComments) {
return /* @__PURE__ */ jsx("p", { ...blockProps, children: /* @__PURE__ */ jsx(Spinner, {}) });
}
if (!postId) {
commentTree = getCommentsPlaceholder({
perPage: commentsPerPage,
pageComments,
threadComments,
threadCommentsDepth
});
}
if (!commentTree.length) {
return /* @__PURE__ */ jsx("p", { ...blockProps, children: __("No results found.") });
}
return /* @__PURE__ */ jsx(
CommentsList,
{
comments: commentTree,
blockProps,
blocks,
activeCommentId,
setActiveCommentId,
firstCommentId: commentTree[0]?.commentId
}
);
}
export {
CommentTemplateEdit as default
};
//# sourceMappingURL=edit.js.map