@wordpress/block-library
Version:
Block library for the WordPress editor.
67 lines (66 loc) • 2.94 kB
JavaScript
import { InspectorControls, useBlockProps } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { __, sprintf } from "@wordpress/i18n";
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/post-author-name/edit.jsx
function PostAuthorNameEdit(props) {
useDeprecatedTextAlign(props);
const { attributes: { isLink, linkTarget }, setAttributes, context: { postType, postId } } = props;
const { authorName, supportsAuthor } = useSelect((select) => {
const { getEditedEntityRecord, getUser, getPostType } = select(store);
const _authorId = getEditedEntityRecord("postType", postType, postId)?.author;
return {
authorName: _authorId ? getUser(_authorId) : null,
supportsAuthor: getPostType(postType)?.supports?.author ?? false
};
}, [postType, postId]);
const blockProps = useBlockProps();
const displayName = authorName?.name || __("Author Name");
const displayAuthor = isLink ? /* @__PURE__ */ jsx("a", {
href: "#author-pseudo-link",
onClick: (event) => event.preventDefault(),
className: "wp-block-post-author-name__link",
children: displayName
}) : displayName;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
isLink: false,
linkTarget: "_self"
});
},
dropdownMenuProps,
children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Link to author archive"),
isShownByDefault: true,
hasValue: () => isLink,
onDeselect: () => setAttributes({ isLink: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Link to author archive"),
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"
})
})]
}) }), /* @__PURE__ */ jsx("div", {
...blockProps,
children: !supportsAuthor && postType !== void 0 ? sprintf(__("This post type (%s) does not support the author."), postType) : displayAuthor
})] });
}
export { PostAuthorNameEdit as default };
//# sourceMappingURL=edit.mjs.map