@wordpress/block-library
Version:
Block library for the WordPress editor.
75 lines (74 loc) • 3.24 kB
JavaScript
import { useEntityProp } from "@wordpress/core-data";
import { dateI18n, getSettings, humanTimeDiff } from "@wordpress/date";
import { InspectorControls, __experimentalDateFormatPicker, useBlockProps } from "@wordpress/block-editor";
import { ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { __, _x } from "@wordpress/i18n";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/comment-date/edit.jsx
/**
* Renders the `core/comment-date` block on the editor.
*
* @param {Object} props React props.
* @param {Object} props.setAttributes Callback for updating block attributes.
* @param {Object} props.attributes Block attributes.
* @param {string} props.attributes.format Format of the date.
* @param {string} props.attributes.isLink Whether the author name should be linked.
* @param {Object} props.context Inherited context.
* @param {string} props.context.commentId The comment ID.
*
* @return {React.JSX.Element} React element.
*/
function Edit({ attributes: { format, isLink }, context: { commentId }, setAttributes }) {
const blockProps = useBlockProps();
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
let [date] = useEntityProp("root", "comment", "date", commentId);
const [siteFormat = getSettings().formats.date] = useEntityProp("root", "site", "date_format");
const inspectorControls = /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
format: void 0,
isLink: true
});
},
dropdownMenuProps,
children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Date format"),
hasValue: () => format !== void 0,
onDeselect: () => setAttributes({ format: void 0 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(__experimentalDateFormatPicker, {
format,
defaultFormat: siteFormat,
onChange: (nextFormat) => setAttributes({ format: nextFormat })
})
}), /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Link to comment"),
hasValue: () => !isLink,
onDeselect: () => setAttributes({ isLink: true }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Link to comment"),
onChange: () => setAttributes({ isLink: !isLink }),
checked: isLink
})
})]
}) });
if (!commentId || !date) date = _x("Comment Date", "block title");
let commentDate = date instanceof Date ? /* @__PURE__ */ jsx("time", {
dateTime: dateI18n("c", date),
children: format === "human-diff" ? humanTimeDiff(date) : dateI18n(format || siteFormat, date)
}) : /* @__PURE__ */ jsx("time", { children: date });
if (isLink) commentDate = /* @__PURE__ */ jsx("a", {
href: "#comment-date-pseudo-link",
onClick: (event) => event.preventDefault(),
children: commentDate
});
return /* @__PURE__ */ jsxs(Fragment, { children: [inspectorControls, /* @__PURE__ */ jsx("div", {
...blockProps,
children: commentDate
})] });
}
export { Edit as default };
//# sourceMappingURL=edit.mjs.map