@wordpress/block-library
Version:
Block library for the WordPress editor.
79 lines (78 loc) • 3.39 kB
JavaScript
import { __, _x } from "@wordpress/i18n";
import { useSelect } from "@wordpress/data";
import { InspectorControls, useBlockProps } from "@wordpress/block-editor";
import { store } from "@wordpress/core-data";
import { ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { 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/comment-author-name/edit.jsx
/**
* Renders the `core/comment-author-name` 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.isLink Whether the author name should be linked.
* @param {string} props.attributes.linkTarget Target of the link.
* @param {Object} props.context Inherited context.
* @param {string} props.context.commentId The comment ID.
*
* @return {React.JSX.Element} React element.
*/
function Edit(props) {
const { attributes: { isLink, linkTarget }, context: { commentId }, setAttributes } = props;
useDeprecatedTextAlign(props);
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const blockProps = useBlockProps();
let displayName = useSelect((select) => {
const { getEntityRecord } = select(store);
const comment = getEntityRecord("root", "comment", commentId);
const authorName = comment?.author_name;
if (comment && !authorName) return getEntityRecord("root", "user", comment.author)?.name ?? __("Anonymous");
return authorName ?? "";
}, [commentId]);
const inspectorControls = /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
isLink: true,
linkTarget: "_self"
});
},
dropdownMenuProps,
children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Link to authors URL"),
isShownByDefault: true,
hasValue: () => !isLink,
onDeselect: () => setAttributes({ isLink: true }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Link to authors URL"),
onChange: () => setAttributes({ isLink: !isLink }),
checked: isLink
})
}), isLink && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Open in new tab"),
isShownByDefault: true,
hasValue: () => linkTarget !== "_self",
onDeselect: () => setAttributes({ linkTarget: "_self" }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Open in new tab"),
onChange: (value) => setAttributes({ linkTarget: value ? "_blank" : "_self" }),
checked: linkTarget === "_blank"
})
})]
}) });
if (!commentId || !displayName) displayName = _x("Comment Author", "block title");
const displayAuthor = isLink ? /* @__PURE__ */ jsx("a", {
href: "#comment-author-pseudo-link",
onClick: (event) => event.preventDefault(),
children: displayName
}) : displayName;
return /* @__PURE__ */ jsxs(Fragment, { children: [inspectorControls, /* @__PURE__ */ jsx("div", {
...blockProps,
children: displayAuthor
})] });
}
export { Edit as default };
//# sourceMappingURL=edit.mjs.map