@wordpress/block-library
Version:
Block library for the WordPress editor.
75 lines (69 loc) • 2.27 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { RawHTML } from '@wordpress/element';
import { Button } from '@wordpress/components';
import { createBlock } from '@wordpress/blocks';
import { withDispatch, useSelect } from '@wordpress/data';
import { Warning, useBlockProps, store as blockEditorStore } from '@wordpress/block-editor';
import { safeHTML } from '@wordpress/dom';
function MissingBlockWarning(_ref) {
let {
attributes,
convertToHTML,
clientId
} = _ref;
const {
originalName,
originalUndelimitedContent
} = attributes;
const hasContent = !!originalUndelimitedContent;
const hasHTMLBlock = useSelect(select => {
const {
canInsertBlockType,
getBlockRootClientId
} = select(blockEditorStore);
return canInsertBlockType('core/html', getBlockRootClientId(clientId));
}, [clientId]);
const actions = [];
let messageHTML;
if (hasContent && hasHTMLBlock) {
messageHTML = sprintf(
/* translators: %s: block name */
__('Your site doesn’t include support for the "%s" block. You can leave this block intact, convert its content to a Custom HTML block, or remove it entirely.'), originalName);
actions.push(createElement(Button, {
key: "convert",
onClick: convertToHTML,
variant: "primary"
}, __('Keep as HTML')));
} else {
messageHTML = sprintf(
/* translators: %s: block name */
__('Your site doesn’t include support for the "%s" block. You can leave this block intact or remove it entirely.'), originalName);
}
return createElement("div", useBlockProps({
className: 'has-warning'
}), createElement(Warning, {
actions: actions
}, messageHTML), createElement(RawHTML, null, safeHTML(originalUndelimitedContent)));
}
const MissingEdit = withDispatch((dispatch, _ref2) => {
let {
clientId,
attributes
} = _ref2;
const {
replaceBlock
} = dispatch(blockEditorStore);
return {
convertToHTML() {
replaceBlock(clientId, createBlock('core/html', {
content: attributes.originalUndelimitedContent
}));
}
};
})(MissingBlockWarning);
export default MissingEdit;
//# sourceMappingURL=edit.js.map