@wordpress/block-library
Version:
Block library for the WordPress editor.
110 lines (109 loc) • 4.41 kB
JavaScript
import clsx from "clsx";
import { __ } from "@wordpress/i18n";
import { useEffect, useState } from "@wordpress/element";
import { BlockControls, BlockIcon, InspectorControls, privateApis, store, useBlockProps } from "@wordpress/block-editor";
import { getBlockContent, parse, serialize } from "@wordpress/blocks";
import { useDispatch, useRegistry, useSelect } from "@wordpress/data";
import { useResizeObserver } from "@wordpress/compose";
import deprecated from "@wordpress/deprecated";
import { Button, Placeholder, ToolbarButton, ToolbarGroup, __experimentalVStack } from "@wordpress/components";
import { Icon, code, html } from "@wordpress/icons";
import { unlock } from "../lock-unlock.mjs";
import HTMLEditModal from "./modal.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/html/edit.jsx
const { InnerContent } = unlock(privateApis);
function HTMLEdit({ clientId, attributes }) {
const [isModalOpen, setIsModalOpen] = useState(false);
const registry = useRegistry();
const { updateBlock, replaceInnerBlocks } = useDispatch(store);
const content = useSelect((select) => {
const block = select(store).getBlock(clientId);
return block ? getBlockContent(block) : "";
}, [clientId]);
const [hasNoRenderedSize, setHasNoRenderedSize] = useState(false);
const resizeObserverRef = useResizeObserver(([entry]) => {
const [box] = entry.borderBoxSize;
setHasNoRenderedSize(!box.inlineSize || !box.blockSize);
});
const blockProps = useBlockProps({
ref: resizeObserverRef,
className: clsx("block-library-html__edit", { "has-no-output": hasNoRenderedSize })
});
const onUpdate = (nextContent) => {
if (nextContent === content) return;
const [parsedBlock] = parse(`<!-- wp:html -->\n${nextContent}\n<!-- /wp:html -->`);
const nextInnerBlocks = parsedBlock?.innerBlocks ?? [];
const prevInnerBlocks = registry.select(store).getBlocks(clientId);
const innerBlocksUnchanged = prevInnerBlocks.length === nextInnerBlocks.length && prevInnerBlocks.every((block, index) => serialize(block) === serialize(nextInnerBlocks[index]));
registry.batch(() => {
updateBlock(clientId, { innerContent: parsedBlock?.innerContent ?? (nextContent ? [nextContent] : []) });
if (!innerBlocksUnchanged) replaceInnerBlocks(clientId, nextInnerBlocks, false);
});
};
useEffect(() => {
if (!attributes.content) return;
deprecated("The content attribute on the Custom HTML block", {
since: "7.1",
alternative: "inner content"
});
updateBlock(clientId, {
attributes: { content: void 0 },
innerContent: [attributes.content]
});
}, [attributes.content]);
if (!content?.trim()) return /* @__PURE__ */ jsxs("div", {
...blockProps,
children: [/* @__PURE__ */ jsx(Placeholder, {
icon: /* @__PURE__ */ jsx(BlockIcon, { icon: code }),
label: __("Custom HTML"),
instructions: __("Add custom HTML code and preview how it looks."),
children: /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "primary",
onClick: () => setIsModalOpen(true),
children: __("Edit HTML")
})
}), isModalOpen && /* @__PURE__ */ jsx(HTMLEditModal, {
onRequestClose: () => setIsModalOpen(false),
content,
onUpdate
})]
});
return /* @__PURE__ */ jsxs("div", {
...blockProps,
children: [
/* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(ToolbarButton, {
onClick: () => setIsModalOpen(true),
children: __("Edit code")
}) }) }),
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(__experimentalVStack, {
className: "block-library-html__edit-code",
expanded: true,
children: /* @__PURE__ */ jsx(Button, {
className: "block-library-html__edit-code-button",
__next40pxDefaultSize: true,
variant: "secondary",
onClick: () => setIsModalOpen(true),
children: __("Edit code")
})
}) }),
/* @__PURE__ */ jsx(InnerContent, { clientId }),
hasNoRenderedSize && /* @__PURE__ */ jsx("div", {
className: "block-library-html__no-output-marker",
"aria-hidden": "true",
children: /* @__PURE__ */ jsx(Icon, {
icon: html,
size: 20
})
}),
isModalOpen && /* @__PURE__ */ jsx(HTMLEditModal, {
onRequestClose: () => setIsModalOpen(false),
content,
onUpdate
})
]
});
}
export { HTMLEdit as default };
//# sourceMappingURL=edit.mjs.map