UNPKG

@wordpress/block-library

Version:
112 lines (111 loc) 3.53 kB
// packages/block-library/src/freeform/edit.js import { BlockControls, BlockIcon, useBlockProps, store as blockEditorStore } from "@wordpress/block-editor"; import { useDispatch, useSelect } from "@wordpress/data"; import { Button, Placeholder, ToolbarGroup, ToolbarButton } from "@wordpress/components"; import { useState, useRef, RawHTML } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import { classic } from "@wordpress/icons"; import ConvertToBlocksButton from "./convert-to-blocks-button.mjs"; import MigrationNotice from "./migration-notice.mjs"; import ModalEdit from "./modal.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; function FreeformEdit({ attributes, setAttributes, clientId, onReplace }) { const { content } = attributes; const [isOpen, setOpen] = useState(false); const editButtonRef = useRef(null); const canRemove = useSelect( (select) => select(blockEditorStore).canRemoveBlock(clientId), [clientId] ); const { removeBlock } = useDispatch(blockEditorStore); const isDeprecationMode = window.__experimentalClassicBlockDeprecation; return /* @__PURE__ */ jsxs(Fragment, { children: [ canRemove && !isDeprecationMode && /* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx( ConvertToBlocksButton, { content, onReplace } ) }) }), /* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx( ToolbarButton, { ref: editButtonRef, onClick: () => setOpen(true), children: __("Edit") } ) }) }), /* @__PURE__ */ jsxs("div", { ...useBlockProps(), children: [ isDeprecationMode && canRemove && content && /* @__PURE__ */ jsx( MigrationNotice, { content, onReplace } ), content ? /* @__PURE__ */ jsx(RawHTML, { children: content }) : /* @__PURE__ */ jsxs( Placeholder, { icon: /* @__PURE__ */ jsx(BlockIcon, { icon: classic }), label: __("Classic"), instructions: isDeprecationMode ? __( "The Classic block is being phased out. It\u2019s recommended to use other blocks for the best editing experience." ) : __("Use the classic editor to add content."), children: [ isDeprecationMode && canRemove && /* @__PURE__ */ jsx( Button, { __next40pxDefaultSize: true, variant: "primary", onClick: () => removeBlock(clientId), children: __("Remove block") } ), /* @__PURE__ */ jsx( Button, { __next40pxDefaultSize: true, variant: isDeprecationMode && canRemove ? "secondary" : "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