@wordpress/block-library
Version:
Block library for the WordPress editor.
135 lines (129 loc) • 4.21 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = QuoteEdit;
var _clsx = _interopRequireDefault(require("clsx"));
var _i18n = require("@wordpress/i18n");
var _blockEditor = require("@wordpress/block-editor");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _icons = require("@wordpress/icons");
var _deprecated2 = require("./deprecated");
var _caption = require("../utils/caption");
var _jsxRuntime = require("react/jsx-runtime");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const isWebPlatform = _element.Platform.OS === 'web';
const TEMPLATE = [['core/paragraph', {}]];
/**
* At the moment, deprecations don't handle create blocks from attributes
* (like when using CPT templates). For this reason, this hook is necessary
* to avoid breaking templates using the old quote block format.
*
* @param {Object} attributes Block attributes.
* @param {string} clientId Block client ID.
*/
const useMigrateOnLoad = (attributes, clientId) => {
const registry = (0, _data.useRegistry)();
const {
updateBlockAttributes,
replaceInnerBlocks
} = (0, _data.useDispatch)(_blockEditor.store);
(0, _element.useEffect)(() => {
// As soon as the block is loaded, migrate it to the new version.
if (!attributes.value) {
// No need to migrate if it doesn't have the value attribute.
return;
}
const [newAttributes, newInnerBlocks] = (0, _deprecated2.migrateToQuoteV2)(attributes);
(0, _deprecated.default)('Value attribute on the quote block', {
since: '6.0',
version: '6.5',
alternative: 'inner blocks'
});
registry.batch(() => {
updateBlockAttributes(clientId, newAttributes);
replaceInnerBlocks(clientId, newInnerBlocks);
});
}, [attributes.value]);
};
function QuoteEdit({
attributes,
setAttributes,
insertBlocksAfter,
clientId,
className,
style,
isSelected
}) {
const {
textAlign
} = attributes;
useMigrateOnLoad(attributes, clientId);
const blockProps = (0, _blockEditor.useBlockProps)({
className: (0, _clsx.default)(className, {
[`has-text-align-${textAlign}`]: textAlign
}),
...(!isWebPlatform && {
style
})
});
const innerBlocksProps = (0, _blockEditor.useInnerBlocksProps)(blockProps, {
template: TEMPLATE,
templateInsertUpdatesSelection: true,
__experimentalCaptureToolbars: true,
renderAppender: false
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__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)(_components.BlockQuotation, {
...innerBlocksProps,
children: [innerBlocksProps.children, /*#__PURE__*/(0, _jsxRuntime.jsx)(_caption.Caption, {
attributeKey: "citation",
tagName: isWebPlatform ? 'cite' : 'p',
style: isWebPlatform && {
display: 'block'
},
isSelected: isSelected,
attributes: attributes,
setAttributes: setAttributes,
__unstableMobileNoFocusOnMount: true,
icon: _icons.verse,
label: (0, _i18n.__)('Quote citation'),
placeholder:
// translators: placeholder text used for the
// citation
(0, _i18n.__)('Add citation'),
addLabel: (0, _i18n.__)('Add citation'),
removeLabel: (0, _i18n.__)('Remove citation'),
excludeElementClassName: true,
className: "wp-block-quote__citation",
insertBlocksAfter: insertBlocksAfter,
...(!isWebPlatform ? {
textAlign
} : {})
})]
})]
});
}
//# sourceMappingURL=edit.js.map
;