{"version":3,"file":"edit.mjs","names":["blockEditorPrivateApis","blockEditorStore","VStack"],"sources":["../../src/html/edit.jsx"],"sourcesContent":["import clsx from 'clsx';\nimport { __ } from '@wordpress/i18n';\nimport { useEffect, useState } from '@wordpress/element';\nimport {\n\tBlockControls,\n\tBlockIcon,\n\tInspectorControls,\n\tuseBlockProps,\n\tstore as blockEditorStore,\n\tprivateApis as blockEditorPrivateApis,\n} from '@wordpress/block-editor';\nimport { parse, serialize, getBlockContent } from '@wordpress/blocks';\nimport { useSelect, useDispatch, useRegistry } from '@wordpress/data';\nimport { useResizeObserver } from '@wordpress/compose';\nimport deprecated from '@wordpress/deprecated';\nimport {\n\tToolbarButton,\n\tToolbarGroup,\n\tPlaceholder,\n\tButton,\n\t__experimentalVStack as VStack,\n} from '@wordpress/components';\nimport { Icon, code, html } from '@wordpress/icons';\nimport { unlock } from '../lock-unlock';\nimport HTMLEditModal from './modal';\n\nconst { InnerContent } = unlock( blockEditorPrivateApis );\n\nexport default function HTMLEdit( { clientId, attributes } ) {\n\tconst [ isModalOpen, setIsModalOpen ] = useState( false );\n\tconst registry = useRegistry();\n\tconst { updateBlock, replaceInnerBlocks } = useDispatch( blockEditorStore );\n\tconst content = useSelect(\n\t\t( select ) => {\n\t\t\tconst block = select( blockEditorStore ).getBlock( clientId );\n\t\t\treturn block ? getBlockContent( block ) : '';\n\t\t},\n\t\t[ clientId ]\n\t);\n\t// Markup with no visible output in the canvas (scripts, which the canvas\n\t// strips, styles, or an empty container a third-party script fills in on\n\t// the front end) leaves the block without a size. Track that so the block\n\t// can show a marker instead of reserving space the front end won't have.\n\tconst [ hasNoRenderedSize, setHasNoRenderedSize ] = useState( false );\n\tconst resizeObserverRef = useResizeObserver( ( [ entry ] ) => {\n\t\tconst [ box ] = entry.borderBoxSize;\n\t\tsetHasNoRenderedSize( ! box.inlineSize || ! box.blockSize );\n\t} );\n\tconst blockProps = useBlockProps( {\n\t\tref: resizeObserverRef,\n\t\tclassName: clsx( 'block-library-html__edit', {\n\t\t\t'has-no-output': hasNoRenderedSize,\n\t\t} ),\n\t} );\n\n\t// Re-parse the edited content: static HTML becomes the block's\n\t// `innerContent` fragments and `<!-- wp:* -->` delimited segments become\n\t// inner blocks mounted at their positions within the static markup.\n\tconst onUpdate = ( nextContent ) => {\n\t\tif ( nextContent === content ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst [ parsedBlock ] = parse(\n\t\t\t`<!-- wp:html -->\\n${ nextContent }\\n<!-- /wp:html -->`\n\t\t);\n\t\tconst nextInnerBlocks = parsedBlock?.innerBlocks ?? [];\n\t\tconst prevInnerBlocks = registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.getBlocks( clientId );\n\t\t// Keep the existing inner blocks, and thereby their client IDs and\n\t\t// selection, when their markup is unchanged — e.g. when only the\n\t\t// surrounding static HTML was edited.\n\t\tconst innerBlocksUnchanged =\n\t\t\tprevInnerBlocks.length === nextInnerBlocks.length &&\n\t\t\tprevInnerBlocks.every(\n\t\t\t\t( block, index ) =>\n\t\t\t\t\tserialize( block ) === serialize( nextInnerBlocks[ index ] )\n\t\t\t);\n\n\t\tregistry.batch( () => {\n\t\t\tupdateBlock( clientId, {\n\t\t\t\tinnerContent:\n\t\t\t\t\tparsedBlock?.innerContent ??\n\t\t\t\t\t( nextContent ? [ nextContent ] : [] ),\n\t\t\t} );\n\t\t\tif ( ! innerBlocksUnchanged ) {\n\t\t\t\treplaceInnerBlocks( clientId, nextInnerBlocks, false );\n\t\t\t}\n\t\t} );\n\t};\n\n\t// Migrate the deprecated `content` attribute. The block's markup now lives\n\t// in its inner content, but a block created via `createBlock( 'core/html',\n\t// { content } )` still arrives with a `content` attribute. As soon as it\n\t// loads, move that markup into the block's inner content and clear the\n\t// attribute.\n\tuseEffect( () => {\n\t\tif ( ! attributes.content ) {\n\t\t\treturn;\n\t\t}\n\n\t\tdeprecated( 'The content attribute on the Custom HTML block', {\n\t\t\tsince: '7.1',\n\t\t\talternative: 'inner content',\n\t\t} );\n\n\t\tupdateBlock( clientId, {\n\t\t\tattributes: { content: undefined },\n\t\t\tinnerContent: [ attributes.content ],\n\t\t} );\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [ attributes.content ] );\n\n\t// Show placeholder when content is empty\n\tif ( ! content?.trim() ) {\n\t\treturn (\n\t\t\t<div { ...blockProps }>\n\t\t\t\t<Placeholder\n\t\t\t\t\ticon={ <BlockIcon icon={ code } /> }\n\t\t\t\t\tlabel={ __( 'Custom HTML' ) }\n\t\t\t\t\tinstructions={ __(\n\t\t\t\t\t\t'Add custom HTML code and preview how it looks.'\n\t\t\t\t\t) }\n\t\t\t\t>\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\tonClick={ () => setIsModalOpen( true ) }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Edit HTML' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t</Placeholder>\n\t\t\t\t{ isModalOpen && (\n\t\t\t\t\t<HTMLEditModal\n\t\t\t\t\t\tonRequestClose={ () => setIsModalOpen( false ) }\n\t\t\t\t\t\tcontent={ content }\n\t\t\t\t\t\tonUpdate={ onUpdate }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t</div>\n\t\t);\n\t}\n\n\treturn (\n\t\t<div { ...blockProps }>\n\t\t\t<BlockControls>\n\t\t\t\t<ToolbarGroup>\n\t\t\t\t\t<ToolbarButton onClick={ () => setIsModalOpen( true ) }>\n\t\t\t\t\t\t{ __( 'Edit code' ) }\n\t\t\t\t\t</ToolbarButton>\n\t\t\t\t</ToolbarGroup>\n\t\t\t</BlockControls>\n\t\t\t<InspectorControls>\n\t\t\t\t<VStack className=\"block-library-html__edit-code\" expanded>\n\t\t\t\t\t<Button\n\t\t\t\t\t\tclassName=\"block-library-html__edit-code-button\"\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"secondary\"\n\t\t\t\t\t\tonClick={ () => setIsModalOpen( true ) }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Edit code' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t</VStack>\n\t\t\t</InspectorControls>\n\t\t\t<InnerContent clientId={ clientId } />\n\t\t\t{ hasNoRenderedSize && (\n\t\t\t\t// Floats over the block's position without affecting the\n\t\t\t\t// layout. A click focuses, and thereby selects, the block,\n\t\t\t\t// which stays reachable by keyboard and in the List View.\n\t\t\t\t<div\n\t\t\t\t\tclassName=\"block-library-html__no-output-marker\"\n\t\t\t\t\taria-hidden=\"true\"\n\t\t\t\t>\n\t\t\t\t\t<Icon icon={ html } size={ 20 } />\n\t\t\t\t</div>\n\t\t\t) }\n\t\t\t{ isModalOpen && (\n\t\t\t\t<HTMLEditModal\n\t\t\t\t\tonRequestClose={ () => setIsModalOpen( false ) }\n\t\t\t\t\tcontent={ content }\n\t\t\t\t\tonUpdate={ onUpdate }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</div>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;AA0BA,MAAM,EAAE,iBAAiB,OAAQA,WAAuB;AAExD,SAAwB,SAAU,EAAE,UAAU,cAAe;CAC5D,MAAM,CAAE,aAAa,kBAAmB,SAAU,KAAM;CACxD,MAAM,WAAW,YAAY;CAC7B,MAAM,EAAE,aAAa,uBAAuB,YAAaC,KAAiB;CAC1E,MAAM,UAAU,WACb,WAAY;EACb,MAAM,QAAQ,OAAQA,KAAiB,CAAC,CAAC,SAAU,QAAS;EAC5D,OAAO,QAAQ,gBAAiB,KAAM,IAAI;CAC3C,GACA,CAAE,QAAS,CACZ;CAKA,MAAM,CAAE,mBAAmB,wBAAyB,SAAU,KAAM;CACpE,MAAM,oBAAoB,mBAAqB,CAAE,WAAa;EAC7D,MAAM,CAAE,OAAQ,MAAM;EACtB,qBAAsB,CAAE,IAAI,cAAc,CAAE,IAAI,SAAU;CAC3D,CAAE;CACF,MAAM,aAAa,cAAe;EACjC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