UNPKG

@wordpress/block-library

Version:
279 lines (275 loc) 9.76 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _clsx = _interopRequireDefault(require("clsx")); var _blockEditor = require("@wordpress/block-editor"); var _components = require("@wordpress/components"); var _data = require("@wordpress/data"); var _i18n = require("@wordpress/i18n"); var _coreData = require("@wordpress/core-data"); var _hooks = require("../utils/hooks"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ const minimumUsersForCombobox = 25; const AUTHORS_QUERY = { who: 'authors', per_page: 100 }; function PostAuthorEdit({ isSelected, context: { postType, postId, queryId }, attributes, setAttributes }) { const isDescendentOfQueryLoop = Number.isFinite(queryId); const dropdownMenuProps = (0, _hooks.useToolsPanelDropdownMenuProps)(); const { authorId, authorDetails, authors, supportsAuthor } = (0, _data.useSelect)(select => { var _getPostType$supports; const { getEditedEntityRecord, getUser, getUsers, getPostType } = select(_coreData.store); const _authorId = getEditedEntityRecord('postType', postType, postId)?.author; return { authorId: _authorId, authorDetails: _authorId ? getUser(_authorId) : null, authors: getUsers(AUTHORS_QUERY), supportsAuthor: (_getPostType$supports = getPostType(postType)?.supports?.author) !== null && _getPostType$supports !== void 0 ? _getPostType$supports : false }; }, [postType, postId]); const { editEntityRecord } = (0, _data.useDispatch)(_coreData.store); const { textAlign, showAvatar, showBio, byline, isLink, linkTarget, avatarSize } = attributes; const avatarSizes = []; const authorName = authorDetails?.name || (0, _i18n.__)('Post Author'); if (authorDetails?.avatar_urls) { Object.keys(authorDetails.avatar_urls).forEach(size => { avatarSizes.push({ value: size, label: `${size} x ${size}` }); }); } const blockProps = (0, _blockEditor.useBlockProps)({ className: (0, _clsx.default)({ [`has-text-align-${textAlign}`]: textAlign }) }); const authorOptions = authors?.length ? authors.map(({ id, name }) => { return { value: id, label: name }; }) : []; const handleSelect = nextAuthorId => { editEntityRecord('postType', postType, postId, { author: nextAuthorId }); }; const showCombobox = authorOptions.length >= minimumUsersForCombobox; const showAuthorControl = !!postId && !isDescendentOfQueryLoop && authorOptions.length > 0; if (!supportsAuthor && postType !== undefined) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { ...blockProps, children: (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "post". (0, _i18n.__)('This post type (%s) does not support the author.'), postType) }); } return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.InspectorControls, { children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalToolsPanel, { label: (0, _i18n.__)('Settings'), resetAll: () => { setAttributes({ avatarSize: 48, showAvatar: true, isLink: false, linkTarget: '_self' }); }, dropdownMenuProps: dropdownMenuProps, children: [showAuthorControl && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { style: { gridColumn: '1 / -1' }, children: showCombobox && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ComboboxControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Author'), options: authorOptions, value: authorId, onChange: handleSelect, allowReset: false }) || /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.SelectControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Author'), value: authorId, options: authorOptions, onChange: handleSelect }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Show avatar'), isShownByDefault: true, hasValue: () => !showAvatar, onDeselect: () => setAttributes({ showAvatar: true }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToggleControl, { __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Show avatar'), checked: showAvatar, onChange: () => setAttributes({ showAvatar: !showAvatar }) }) }), showAvatar && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Avatar size'), isShownByDefault: true, hasValue: () => avatarSize !== 48, onDeselect: () => setAttributes({ avatarSize: 48 }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.SelectControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Avatar size'), value: avatarSize, options: avatarSizes, onChange: size => { setAttributes({ avatarSize: Number(size) }); } }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Show bio'), isShownByDefault: true, hasValue: () => !!showBio, onDeselect: () => setAttributes({ showBio: undefined }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToggleControl, { __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Show bio'), checked: !!showBio, onChange: () => setAttributes({ showBio: !showBio }) }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Link author name to author page'), isShownByDefault: true, hasValue: () => !!isLink, onDeselect: () => setAttributes({ isLink: false }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToggleControl, { __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Link author name to author page'), checked: isLink, onChange: () => setAttributes({ isLink: !isLink }) }) }), isLink && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Link target'), isShownByDefault: true, hasValue: () => linkTarget !== '_self', onDeselect: () => setAttributes({ linkTarget: '_self' }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToggleControl, { __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Open in new tab'), onChange: value => setAttributes({ linkTarget: value ? '_blank' : '_self' }), checked: linkTarget === '_blank' }) })] }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockControls, { group: "block", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.AlignmentControl, { value: textAlign, onChange: nextAlign => { setAttributes({ textAlign: nextAlign }); } }) }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { ...blockProps, children: [showAvatar && authorDetails?.avatar_urls && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: "wp-block-post-author__avatar", children: /*#__PURE__*/(0, _jsxRuntime.jsx)("img", { width: avatarSize, src: authorDetails.avatar_urls[avatarSize], alt: authorDetails.name }) }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: "wp-block-post-author__content", children: [(!_blockEditor.RichText.isEmpty(byline) || isSelected) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.RichText, { identifier: "byline", className: "wp-block-post-author__byline", "aria-label": (0, _i18n.__)('Post author byline text'), placeholder: (0, _i18n.__)('Write byline…'), value: byline, onChange: value => setAttributes({ byline: value }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)("p", { className: "wp-block-post-author__name", children: isLink ? /*#__PURE__*/(0, _jsxRuntime.jsx)("a", { href: "#post-author-pseudo-link", onClick: event => event.preventDefault(), children: authorName }) : authorName }), showBio && /*#__PURE__*/(0, _jsxRuntime.jsx)("p", { className: "wp-block-post-author__bio", dangerouslySetInnerHTML: { __html: authorDetails?.description } })] })] })] }); } var _default = exports.default = PostAuthorEdit; //# sourceMappingURL=edit.js.map