UNPKG

@wordpress/block-library

Version:
322 lines (317 loc) 11 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 _compose = require("@wordpress/compose"); var _element = require("@wordpress/element"); var _data = require("@wordpress/data"); var _i18n = require("@wordpress/i18n"); var _htmlEntities = require("@wordpress/html-entities"); var _coreData = require("@wordpress/core-data"); var _hooks = require("../utils/hooks"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ const AUTHORS_QUERY = { who: 'authors', per_page: 100, _fields: 'id,name', context: 'view' }; function AuthorCombobox({ value, onChange }) { const [filterValue, setFilterValue] = (0, _element.useState)(''); const { authors, isLoading } = (0, _data.useSelect)(select => { const { getUsers, isResolving } = select(_coreData.store); const query = { ...AUTHORS_QUERY }; if (filterValue) { query.search = filterValue; query.search_columns = ['name']; } return { authors: getUsers(query), isLoading: isResolving('getUsers', [query]) }; }, [filterValue]); const authorOptions = (0, _element.useMemo)(() => { const fetchedAuthors = (authors !== null && authors !== void 0 ? authors : []).map(author => { return { value: author.id, label: (0, _htmlEntities.decodeEntities)(author.name) }; }); // Ensure the current author is included in the list. const foundAuthor = fetchedAuthors.findIndex(fetchedAuthor => value?.id === fetchedAuthor.value); let currentAuthor = []; if (foundAuthor < 0 && value) { currentAuthor = [{ value: value.id, label: (0, _htmlEntities.decodeEntities)(value.name) }]; } else if (foundAuthor < 0 && !value) { currentAuthor = [{ value: 0, label: (0, _i18n.__)('(No author)') }]; } return [...currentAuthor, ...fetchedAuthors]; }, [authors, value]); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ComboboxControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Author'), options: authorOptions, value: value?.id, onFilterValueChange: (0, _compose.debounce)(setFilterValue, 300), onChange: onChange, allowReset: false, isLoading: isLoading }); } function PostAuthorEdit({ isSelected, context: { postType, postId, queryId }, attributes, setAttributes }) { const isDescendentOfQueryLoop = Number.isFinite(queryId); const dropdownMenuProps = (0, _hooks.useToolsPanelDropdownMenuProps)(); const { authorDetails, canAssignAuthor, supportsAuthor } = (0, _data.useSelect)(select => { var _getPostType$supports; const { getEditedEntityRecord, getUser, getPostType } = select(_coreData.store); const currentPost = getEditedEntityRecord('postType', postType, postId); const authorId = currentPost?.author; return { authorDetails: authorId ? getUser(authorId, { context: 'view' }) : null, supportsAuthor: (_getPostType$supports = getPostType(postType)?.supports?.author) !== null && _getPostType$supports !== void 0 ? _getPostType$supports : false, canAssignAuthor: currentPost?._links?.['wp:action-assign-author'] ? true : 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 handleSelect = nextAuthorId => { editEntityRecord('postType', postType, postId, { author: nextAuthorId }); }; const showAuthorControl = !!postId && !isDescendentOfQueryLoop && canAssignAuthor; 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: /*#__PURE__*/(0, _jsxRuntime.jsx)(AuthorCombobox, { value: authorDetails, 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