UNPKG

@wordpress/block-library

Version:
208 lines (204 loc) 7.3 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = PostTitleEdit; var _clsx = _interopRequireDefault(require("clsx")); var _blockEditor = require("@wordpress/block-editor"); var _components = require("@wordpress/components"); var _i18n = require("@wordpress/i18n"); var _blocks = require("@wordpress/blocks"); var _coreData = require("@wordpress/core-data"); var _data = require("@wordpress/data"); var _hooks = require("../utils/hooks"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ function PostTitleEdit({ attributes: { level, levelOptions, textAlign, isLink, rel, linkTarget }, setAttributes, context: { postType, postId, queryId }, insertBlocksAfter }) { const TagName = level === 0 ? 'p' : `h${level}`; const isDescendentOfQueryLoop = Number.isFinite(queryId); const userCanEdit = (0, _data.useSelect)(select => { /** * useCanEditEntity may trigger an OPTIONS request to the REST API * via the canUser resolver. However, when the Post Title is a * descendant of a Query Loop block, the title cannot be edited. In * order to avoid these unnecessary requests, we call the hook * without the proper data, resulting in returning early without * making them. */ if (isDescendentOfQueryLoop) { return false; } return select(_coreData.store).canUser('update', { kind: 'postType', name: postType, id: postId }); }, [isDescendentOfQueryLoop, postType, postId]); const [rawTitle = '', setTitle, fullTitle] = (0, _coreData.useEntityProp)('postType', postType, 'title', postId); const [link] = (0, _coreData.useEntityProp)('postType', postType, 'link', postId); const onSplitAtEnd = () => { insertBlocksAfter((0, _blocks.createBlock)((0, _blocks.getDefaultBlockName)())); }; const blockProps = (0, _blockEditor.useBlockProps)({ className: (0, _clsx.default)({ [`has-text-align-${textAlign}`]: textAlign }) }); const blockEditingMode = (0, _blockEditor.useBlockEditingMode)(); const dropdownMenuProps = (0, _hooks.useToolsPanelDropdownMenuProps)(); let titleElement = /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, { ...blockProps, children: (0, _i18n.__)('Title') }); if (postType && postId) { titleElement = userCanEdit ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.PlainText, { tagName: TagName, placeholder: (0, _i18n.__)('No title'), value: rawTitle, onChange: setTitle, __experimentalVersion: 2, __unstableOnSplitAtEnd: onSplitAtEnd, ...blockProps }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, { ...blockProps, dangerouslySetInnerHTML: { __html: fullTitle?.rendered } }); } if (isLink && postType && postId) { titleElement = userCanEdit ? /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, { ...blockProps, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.PlainText, { tagName: "a", href: link, target: linkTarget, rel: rel, placeholder: !rawTitle.length ? (0, _i18n.__)('No title') : null, value: rawTitle, onChange: setTitle, __experimentalVersion: 2, __unstableOnSplitAtEnd: onSplitAtEnd }) }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, { ...blockProps, children: /*#__PURE__*/(0, _jsxRuntime.jsx)("a", { href: link, target: linkTarget, rel: rel, onClick: event => event.preventDefault(), dangerouslySetInnerHTML: { __html: fullTitle?.rendered } }) }); } return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [blockEditingMode === 'default' && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_blockEditor.BlockControls, { group: "block", children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.HeadingLevelDropdown, { value: level, options: levelOptions, onChange: newLevel => setAttributes({ level: newLevel }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.AlignmentControl, { value: textAlign, onChange: nextAlign => { setAttributes({ textAlign: nextAlign }); } })] }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.InspectorControls, { children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalToolsPanel, { label: (0, _i18n.__)('Settings'), resetAll: () => { setAttributes({ rel: '', linkTarget: '_self', isLink: false }); }, dropdownMenuProps: dropdownMenuProps, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Make title a link'), isShownByDefault: true, hasValue: () => isLink, onDeselect: () => setAttributes({ isLink: false }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToggleControl, { __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Make title a link'), onChange: () => setAttributes({ isLink: !isLink }), checked: isLink }) }), isLink && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Open in new tab'), isShownByDefault: true, hasValue: () => linkTarget === '_blank', 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)(_components.__experimentalToolsPanelItem, { label: (0, _i18n.__)('Link rel'), isShownByDefault: true, hasValue: () => !!rel, onDeselect: () => setAttributes({ rel: '' }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: (0, _i18n.__)('Link rel'), value: rel, onChange: newRel => setAttributes({ rel: newRel }) }) })] })] }) })] }), titleElement] }); } //# sourceMappingURL=edit.js.map