{"version":3,"file":"pattern-overrides-controls.mjs","names":[],"sources":["../../src/components/pattern-overrides-controls.tsx"],"sourcesContent":["import { useState, useId } from '@wordpress/element';\nimport {\n\tInspectorControls,\n\tuseBlockBindingsUtils,\n} from '@wordpress/block-editor';\nimport { BaseControl, Button } from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { PATTERN_OVERRIDES_BINDING_SOURCE } from '../constants';\nimport {\n\tAllowOverridesModal,\n\tDisallowOverridesModal,\n} from './allow-overrides-modal';\nimport type { PatternOverridesControlsProps } from './types';\n\nfunction PatternOverridesControls( {\n\tattributes,\n\tsetAttributes,\n\tname: blockName,\n}: PatternOverridesControlsProps ) {\n\tconst controlId = useId();\n\tconst [ showAllowOverridesModal, setShowAllowOverridesModal ] =\n\t\tuseState( false );\n\tconst [ showDisallowOverridesModal, setShowDisallowOverridesModal ] =\n\t\tuseState( false );\n\n\tconst hasName = !! attributes.metadata?.name;\n\tconst defaultBindings = attributes.metadata?.bindings?.__default;\n\tconst hasOverrides =\n\t\thasName && defaultBindings?.source === PATTERN_OVERRIDES_BINDING_SOURCE;\n\tconst isConnectedToOtherSources =\n\t\tdefaultBindings?.source &&\n\t\tdefaultBindings.source !== PATTERN_OVERRIDES_BINDING_SOURCE;\n\tconst { updateBlockBindings } = useBlockBindingsUtils();\n\n\tfunction updateBindings( isChecked: boolean, customName?: string ) {\n\t\tif ( customName ) {\n\t\t\tsetAttributes( {\n\t\t\t\tmetadata: {\n\t\t\t\t\t...attributes.metadata,\n\t\t\t\t\tname: customName,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\n\t\tupdateBlockBindings( {\n\t\t\t__default: isChecked\n\t\t\t\t? { source: PATTERN_OVERRIDES_BINDING_SOURCE }\n\t\t\t\t: undefined,\n\t\t} );\n\t}\n\n\t// Avoid overwriting other (e.g. meta) bindings.\n\tif ( isConnectedToOtherSources ) {\n\t\treturn null;\n\t}\n\n\tconst hasUnsupportedImageAttributes =\n\t\tblockName === 'core/image' && !! attributes.href?.length;\n\n\tconst helpText =\n\t\t! hasOverrides && hasUnsupportedImageAttributes\n\t\t\t? __(\n\t\t\t\t\t`Overrides currently don't support image links. Remove the link first before enabling overrides.`\n\t\t\t\t)\n\t\t\t: __(\n\t\t\t\t\t'Allow changes to this block throughout instances of this pattern.'\n\t\t\t\t);\n\n\treturn (\n\t\t<>\n\t\t\t<InspectorControls group=\"advanced\">\n\t\t\t\t<BaseControl\n\t\t\t\t\tid={ controlId }\n\t\t\t\t\tlabel={ __( 'Overrides' ) }\n\t\t\t\t\thelp={ helpText }\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\tclassName=\"pattern-overrides-control__allow-overrides-button\"\n\t\t\t\t\t\tvariant=\"secondary\"\n\t\t\t\t\t\taria-haspopup=\"dialog\"\n\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\tif ( hasOverrides ) {\n\t\t\t\t\t\t\t\tsetShowDisallowOverridesModal( true );\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tsetShowAllowOverridesModal( true );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} }\n\t\t\t\t\t\tdisabled={\n\t\t\t\t\t\t\t! hasOverrides && hasUnsupportedImageAttributes\n\t\t\t\t\t\t}\n\t\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\t>\n\t\t\t\t\t\t{ hasOverrides\n\t\t\t\t\t\t\t? __( 'Disable overrides' )\n\t\t\t\t\t\t\t: __( 'Enable overrides' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t</BaseControl>\n\t\t\t</InspectorControls>\n\n\t\t\t{ showAllowOverridesModal && (\n\t\t\t\t<AllowOverridesModal\n\t\t\t\t\tinitialName={ attributes.metadata?.name }\n\t\t\t\t\tonClose={ () => setShowAllowOverridesModal( false ) }\n\t\t\t\t\tonSave={ ( newName ) => {\n\t\t\t\t\t\tupdateBindings( true, newName );\n\t\t\t\t\t} }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ showDisallowOverridesModal && (\n\t\t\t\t<DisallowOverridesModal\n\t\t\t\t\tonClose={ () => setShowDisallowOverridesModal( false ) }\n\t\t\t\t\tonSave={ () => updateBindings( false ) }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nexport default PatternOverridesControls;\n"],"mappings":";;;;;;;;AAcA,SAAS,yBAA0B,EAClC,YACA,eACA,MAAM,aAC4B;CAClC,MAAM,YAAY,MAAM;CACxB,MAAM,CAAE,yBAAyB,8BAChC,SAAU,KAAM;CACjB,MAAM,CAAE,4BAA4B,iCACnC,SAAU,KAAM;CAEjB,MAAM,UAAU,CAAC,CAAE,WAAW,UAAU;CACxC,MAAM,kBAAkB,WAAW,UAAU,UAAU;CACvD,MAAM,eACL,WAAW,iBAAiB,WAAW;CACxC,MAAM,4BACL,iBAAiB,UACjB,gBAAgB,WAAW;CAC5B,MAAM,EAAE,wBAAwB,sBAAsB;CAEtD,SAAS,eAAgB,WAAoB,YAAsB;EAClE,IAAK,YACJ,cAAe,EACd,UAAU;GACT,GAAG,WAAW;GACd,MAAM;EACP,EACD,CAAE;EAGH,oBAAqB,EACpB,WAAW,YACR,EAAE,QAAQ,iCAAiC,IAC3C,KAAA,EACJ,CAAE;CACH;CAGA,IAAK,2BACJ,OAAO;CAGR,MAAM,gCACL,cAAc,gBAAgB,CAAC,CAAE,WAAW,MAAM;CAEnD,MAAM,WACL,CAAE,gBAAgB,gCACf,GACA,iGACD,IACC,GACA,mEACD;CAEH,OACC,qBAAA,UAAA,EAAA,UAAA;EACC,oBAAC,mBAAD;GAAmB,OAAM;GACxB,UAAA,oBAAC,aAAD;IACC,IAAK;IACL,OAAQ,GAAI,WAAY;IACxB,MAAO;IAEP,UAAA,oBAAC,QAAD;KACC,uBAAA;KACA,WAAU;KACV,SAAQ;KACR,iBAAc;KACd,eAAgB;MACf,IAAK,cACJ,8BAA+B,IAAK;WAEpC,2BAA4B,IAAK;KAEnC;KACA,UACC,CAAE,gBAAgB;KAEnB,wBAAA;KAEE,UAAA,eACC,GAAI,mBAAoB,IACxB,GAAI,kBAAmB;IACnB,CAAA;GACI,CAAA;EACK,CAAA;EAEjB,2BACD,oBAAC,qBAAD;GACC,aAAc,WAAW,UAAU;GACnC,eAAgB,2BAA4B,KAAM;GAClD,SAAW,YAAa;IACvB,eAAgB,MAAM,OAAQ;GAC/B;EACA,CAAA;EAEA,8BACD,oBAAC,wBAAD;GACC,eAAgB,8BAA+B,KAAM;GACrD,cAAe,eAAgB,KAAM;EACrC,CAAA;CAED,EAAA,CAAA;AAEJ"}