@wordpress/block-library
Version:
Block library for the WordPress editor.
145 lines (141 loc) • 5.1 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = PostTermsEdit;
var _clsx = _interopRequireDefault(require("clsx"));
var _blockEditor = require("@wordpress/block-editor");
var _blocks = require("@wordpress/blocks");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _htmlEntities = require("@wordpress/html-entities");
var _i18n = require("@wordpress/i18n");
var _coreData = require("@wordpress/core-data");
var _usePostTerms = _interopRequireDefault(require("./use-post-terms"));
var _jsxRuntime = require("react/jsx-runtime");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
// Allowed formats for the prefix and suffix fields.
const ALLOWED_FORMATS = ['core/bold', 'core/image', 'core/italic', 'core/link', 'core/strikethrough', 'core/text-color'];
function PostTermsEdit({
attributes,
clientId,
context,
isSelected,
setAttributes,
insertBlocksAfter
}) {
const {
term,
textAlign,
separator,
prefix,
suffix
} = attributes;
const {
postId,
postType
} = context;
const blockEditingMode = (0, _blockEditor.useBlockEditingMode)();
const showControls = blockEditingMode === 'default';
const selectedTerm = (0, _data.useSelect)(select => {
if (!term) {
return {};
}
const {
getTaxonomy
} = select(_coreData.store);
const taxonomy = getTaxonomy(term);
return taxonomy?.visibility?.publicly_queryable ? taxonomy : {};
}, [term]);
const {
postTerms,
hasPostTerms,
isLoading
} = (0, _usePostTerms.default)({
postId,
term: selectedTerm
});
const hasPost = postId && postType;
const blockInformation = (0, _blockEditor.useBlockDisplayInformation)(clientId);
const blockProps = (0, _blockEditor.useBlockProps)({
className: (0, _clsx.default)({
[`has-text-align-${textAlign}`]: textAlign,
[`taxonomy-${term}`]: term
})
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [showControls && /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockControls, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.AlignmentToolbar, {
value: textAlign,
onChange: nextAlign => {
setAttributes({
textAlign: nextAlign
});
}
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.InspectorControls, {
group: "advanced",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
autoComplete: "off",
label: (0, _i18n.__)('Separator'),
value: separator || '',
onChange: nextValue => {
setAttributes({
separator: nextValue
});
},
help: (0, _i18n.__)('Enter character(s) used to separate terms.')
})
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
...blockProps,
children: [isLoading && hasPost && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Spinner, {}), !isLoading && (isSelected || prefix) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.RichText, {
identifier: "prefix",
allowedFormats: ALLOWED_FORMATS,
className: "wp-block-post-terms__prefix",
"aria-label": (0, _i18n.__)('Prefix'),
placeholder: (0, _i18n.__)('Prefix') + ' ',
value: prefix,
onChange: value => setAttributes({
prefix: value
}),
tagName: "span"
}), (!hasPost || !term) && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: blockInformation.title
}), hasPost && !isLoading && hasPostTerms && postTerms.map(postTerm => /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
href: postTerm.link,
onClick: event => event.preventDefault(),
rel: "tag",
children: (0, _htmlEntities.decodeEntities)(postTerm.name)
}, postTerm.id)).reduce((prev, curr) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [prev, /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
className: "wp-block-post-terms__separator",
children: separator || ' '
}), curr]
})), hasPost && !isLoading && !hasPostTerms && (selectedTerm?.labels?.no_terms || (0, _i18n.__)('Term items not found.')), !isLoading && (isSelected || suffix) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.RichText, {
identifier: "suffix",
allowedFormats: ALLOWED_FORMATS,
className: "wp-block-post-terms__suffix",
"aria-label": (0, _i18n.__)('Suffix'),
placeholder: ' ' + (0, _i18n.__)('Suffix'),
value: suffix,
onChange: value => setAttributes({
suffix: value
}),
tagName: "span",
__unstableOnSplitAtEnd: () => insertBlocksAfter((0, _blocks.createBlock)((0, _blocks.getDefaultBlockName)()))
})]
})]
});
}
//# sourceMappingURL=edit.js.map
;