@wordpress/block-library
Version:
Block library for the WordPress editor.
92 lines (91 loc) • 3.22 kB
JavaScript
// packages/block-library/src/missing/edit.js
import { __, sprintf } from "@wordpress/i18n";
import { RawHTML } from "@wordpress/element";
import { Button } from "@wordpress/components";
import { createBlock } from "@wordpress/blocks";
import { useDispatch, useSelect } from "@wordpress/data";
import {
Warning,
useBlockProps,
store as blockEditorStore
} from "@wordpress/block-editor";
import { safeHTML } from "@wordpress/dom";
import { jsx, jsxs } from "react/jsx-runtime";
function MissingEdit({ attributes, clientId }) {
const { originalName, originalUndelimitedContent } = attributes;
const hasContent = !!originalUndelimitedContent;
const { hasFreeformBlock, hasHTMLBlock } = useSelect(
(select) => {
const { canInsertBlockType, getBlockRootClientId } = select(blockEditorStore);
return {
hasFreeformBlock: canInsertBlockType(
"core/freeform",
getBlockRootClientId(clientId)
),
hasHTMLBlock: canInsertBlockType(
"core/html",
getBlockRootClientId(clientId)
)
};
},
[clientId]
);
const { replaceBlock } = useDispatch(blockEditorStore);
function convertToHTML() {
replaceBlock(
clientId,
createBlock("core/html", {
content: originalUndelimitedContent
})
);
}
const actions = [];
let messageHTML;
const convertToHtmlButton = /* @__PURE__ */ jsx(
Button,
{
__next40pxDefaultSize: true,
onClick: convertToHTML,
variant: "primary",
children: __("Keep as HTML")
},
"convert"
);
if (hasContent && !hasFreeformBlock && (!originalName || originalName === "core/freeform")) {
if (hasHTMLBlock) {
messageHTML = __(
"It appears you are trying to use the deprecated Classic block. You can leave this block intact, convert its content to a Custom HTML block, or remove it entirely. Alternatively, if you have unsaved changes, you can save them and refresh to use the Classic block."
);
actions.push(convertToHtmlButton);
} else {
messageHTML = __(
"It appears you are trying to use the deprecated Classic block. You can leave this block intact, or remove it entirely. Alternatively, if you have unsaved changes, you can save them and refresh to use the Classic block."
);
}
} else if (hasContent && hasHTMLBlock) {
messageHTML = sprintf(
/* translators: %s: block name */
__(
'Your site doesn\u2019t include support for the "%s" block. You can leave it as-is, convert it to custom HTML, or remove it.'
),
originalName
);
actions.push(convertToHtmlButton);
} else {
messageHTML = sprintf(
/* translators: %s: block name */
__(
'Your site doesn\u2019t include support for the "%s" block. You can leave it as-is or remove it.'
),
originalName
);
}
return /* @__PURE__ */ jsxs("div", { ...useBlockProps({ className: "has-warning" }), children: [
/* @__PURE__ */ jsx(Warning, { actions, children: messageHTML }),
/* @__PURE__ */ jsx(RawHTML, { children: safeHTML(originalUndelimitedContent) })
] });
}
export {
MissingEdit as default
};
//# sourceMappingURL=edit.js.map