@wordpress/block-library
Version:
Block library for the WordPress editor.
127 lines (126 loc) • 4.32 kB
JavaScript
import { InspectorControls, useBlockProps } from "@wordpress/block-editor";
import { RangeControl, Spinner, ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { __, sprintf } from "@wordpress/i18n";
import { useServerSideRender } from "@wordpress/server-side-render";
import { useDisabled } from "@wordpress/compose";
import { SelectControl } from "@wordpress/ui";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import HtmlRenderer from "../utils/html-renderer.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/latest-comments/edit.jsx
/**
* Minimum number of comments a user can show using this block.
*
* @type {number}
*/
const MIN_COMMENTS = 1;
/**
* Maximum number of comments a user can show using this block.
*
* @type {number}
*/
const MAX_COMMENTS = 100;
const DISPLAY_CONTENT_ITEMS = [
{
label: __("No content"),
value: "none"
},
{
label: __("Excerpt"),
value: "excerpt"
},
{
label: __("Full content"),
value: "full"
}
];
function LatestComments({ attributes, setAttributes, name }) {
const { commentsToShow, displayAvatar, displayDate, displayContent } = attributes;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const { content, status, error } = useServerSideRender({
attributes,
skipBlockSupportAttributes: true,
block: name,
urlQueryArgs: { _locale: "site" }
});
const disabledRef = useDisabled();
const blockProps = useBlockProps({ ref: disabledRef });
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
commentsToShow: 5,
displayAvatar: true,
displayDate: true,
displayContent: "excerpt"
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !displayAvatar,
label: __("Display avatar"),
onDeselect: () => setAttributes({ displayAvatar: true }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Display avatar"),
checked: displayAvatar,
onChange: () => setAttributes({ displayAvatar: !displayAvatar })
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !displayDate,
label: __("Display date"),
onDeselect: () => setAttributes({ displayDate: true }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Display date"),
checked: displayDate,
onChange: () => setAttributes({ displayDate: !displayDate })
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => displayContent !== "excerpt",
label: __("Display content"),
onDeselect: () => setAttributes({ displayContent: "excerpt" }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(SelectControl, {
label: __("Display content"),
items: DISPLAY_CONTENT_ITEMS,
value: DISPLAY_CONTENT_ITEMS.find((item) => item.value === displayContent) ?? DISPLAY_CONTENT_ITEMS[0],
onValueChange: (item) => item && setAttributes({ displayContent: item.value })
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => commentsToShow !== 5,
label: __("Number of comments"),
onDeselect: () => setAttributes({ commentsToShow: 5 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(RangeControl, {
label: __("Number of comments"),
value: commentsToShow,
onChange: (value) => setAttributes({ commentsToShow: value }),
min: MIN_COMMENTS,
max: MAX_COMMENTS,
required: true
})
})
]
}) }),
status === "loading" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(Spinner, {})
}),
status === "error" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx("p", { children: sprintf(__("Error: %s"), error) })
}),
status === "success" && /* @__PURE__ */ jsx(HtmlRenderer, {
wrapperProps: blockProps,
html: content
})
] });
}
export { LatestComments as default };
//# sourceMappingURL=edit.mjs.map