@wordpress/block-library
Version:
Block library for the WordPress editor.
51 lines (50 loc) • 2.25 kB
JavaScript
import { BlockControls, BlockIcon, privateApis, store, useBlockProps } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { Button, Placeholder, ToolbarButton, ToolbarGroup } from "@wordpress/components";
import { useRef, useState } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import { classic } from "@wordpress/icons";
import { unlock } from "../lock-unlock.mjs";
import ConvertToBlocksButton from "./convert-to-blocks-button.mjs";
import ModalEdit from "./modal.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/freeform/edit.jsx
const { SafeRawHTML } = unlock(privateApis);
function FreeformEdit({ attributes, setAttributes, clientId }) {
const { content } = attributes;
const [isOpen, setOpen] = useState(false);
const editButtonRef = useRef(null);
const canRemove = useSelect((select) => select(store).canRemoveBlock(clientId), [clientId]);
return /* @__PURE__ */ jsxs(Fragment, { children: [
canRemove && /* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(ConvertToBlocksButton, { clientId }) }) }),
/* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(ToolbarButton, {
ref: editButtonRef,
onClick: () => setOpen(true),
children: __("Edit")
}) }) }),
/* @__PURE__ */ jsxs("div", {
...useBlockProps(),
children: [content ? /* @__PURE__ */ jsx(SafeRawHTML, { html: content }) : /* @__PURE__ */ jsx(Placeholder, {
icon: /* @__PURE__ */ jsx(BlockIcon, { icon: classic }),
label: __("Classic"),
instructions: __("Use the classic editor to add content."),
children: /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "primary",
onClick: () => setOpen(true),
children: __("Edit contents")
})
}), isOpen && /* @__PURE__ */ jsx(ModalEdit, {
clientId,
content,
onClose: () => {
setOpen(false);
if (editButtonRef.current) editButtonRef.current.focus();
},
onChange: (newContent) => setAttributes({ content: newContent })
})]
})
] });
}
export { FreeformEdit as default };
//# sourceMappingURL=edit.mjs.map