@wordpress/block-editor
Version:
8 lines (7 loc) • 13.1 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/block-edit/edit.js"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tgetBlockDefaultClassName,\n\tgetBlockType,\n\thasBlockSupport,\n\tstore as blocksStore,\n} from '@wordpress/blocks';\nimport { withFilters } from '@wordpress/components';\nimport { useRegistry, useSelect } from '@wordpress/data';\nimport { useCallback, useContext, useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport BlockContext from '../block-context';\nimport isURLLike from '../link-control/is-url-like';\nimport {\n\thasPatternOverridesDefaultBinding,\n\treplacePatternOverridesDefaultBinding,\n} from '../../utils/block-bindings';\nimport { unlock } from '../../lock-unlock';\nimport { PrivateBlockContext } from '../block-list/private-block-context';\n\n/**\n * Default value used for blocks which do not define their own context needs,\n * used to guarantee that a block's `context` prop will always be an object. It\n * is assigned as a constant since it is always expected to be an empty object,\n * and in order to avoid unnecessary React reconciliations of a changing object.\n *\n * @type {{}}\n */\nconst DEFAULT_BLOCK_CONTEXT = {};\n\nconst Edit = ( props ) => {\n\tconst { name } = props;\n\tconst blockType = getBlockType( name );\n\n\tif ( ! blockType ) {\n\t\treturn null;\n\t}\n\n\t// `edit` and `save` are functions or components describing the markup\n\t// with which a block is displayed. If `blockType` is valid, assign\n\t// them preferentially as the render value for the block.\n\tconst Component = blockType.edit || blockType.save;\n\n\treturn <Component { ...props } />;\n};\n\nconst EditWithFilters = withFilters( 'editor.BlockEdit' )( Edit );\n\nconst EditWithGeneratedProps = ( props ) => {\n\tconst { name, clientId, attributes, setAttributes } = props;\n\tconst registry = useRegistry();\n\tconst blockType = getBlockType( name );\n\tconst blockContext = useContext( BlockContext );\n\tconst registeredSources = useSelect(\n\t\t( select ) =>\n\t\t\tunlock( select( blocksStore ) ).getAllBlockBindingsSources(),\n\t\t[]\n\t);\n\tconst { bindableAttributes } = useContext( PrivateBlockContext );\n\n\tconst { blockBindings, context, hasPatternOverrides } = useMemo( () => {\n\t\t// Assign context values using the block type's declared context needs.\n\t\tconst computedContext = blockType?.usesContext\n\t\t\t? Object.fromEntries(\n\t\t\t\t\tObject.entries( blockContext ).filter( ( [ key ] ) =>\n\t\t\t\t\t\tblockType.usesContext.includes( key )\n\t\t\t\t\t)\n\t\t\t )\n\t\t\t: DEFAULT_BLOCK_CONTEXT;\n\t\t// Add context requested by Block Bindings sources.\n\t\tif ( attributes?.metadata?.bindings ) {\n\t\t\tObject.values( attributes?.metadata?.bindings || {} ).forEach(\n\t\t\t\t( binding ) => {\n\t\t\t\t\tregisteredSources[ binding?.source ]?.usesContext?.forEach(\n\t\t\t\t\t\t( key ) => {\n\t\t\t\t\t\t\tcomputedContext[ key ] = blockContext[ key ];\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t);\n\t\t}\n\t\treturn {\n\t\t\tblockBindings: replacePatternOverridesDefaultBinding(\n\t\t\t\tattributes?.metadata?.bindings,\n\t\t\t\tbindableAttributes\n\t\t\t),\n\t\t\tcontext: computedContext,\n\t\t\thasPatternOverrides: hasPatternOverridesDefaultBinding(\n\t\t\t\tattributes?.metadata?.bindings\n\t\t\t),\n\t\t};\n\t}, [\n\t\tblockType?.usesContext,\n\t\tblockContext,\n\t\tattributes?.metadata?.bindings,\n\t\tbindableAttributes,\n\t\tregisteredSources,\n\t] );\n\n\tconst computedAttributes = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! blockBindings ) {\n\t\t\t\treturn attributes;\n\t\t\t}\n\n\t\t\tconst attributesFromSources = {};\n\t\t\tconst blockBindingsBySource = new Map();\n\n\t\t\tfor ( const [ attributeName, binding ] of Object.entries(\n\t\t\t\tblockBindings\n\t\t\t) ) {\n\t\t\t\tconst { source: sourceName, args: sourceArgs } = binding;\n\t\t\t\tconst source = registeredSources[ sourceName ];\n\t\t\t\tif (\n\t\t\t\t\t! source ||\n\t\t\t\t\t! bindableAttributes?.includes( attributeName )\n\t\t\t\t) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tblockBindingsBySource.set( source, {\n\t\t\t\t\t...blockBindingsBySource.get( source ),\n\t\t\t\t\t[ attributeName ]: {\n\t\t\t\t\t\targs: sourceArgs,\n\t\t\t\t\t},\n\t\t\t\t} );\n\t\t\t}\n\n\t\t\tif ( blockBindingsBySource.size ) {\n\t\t\t\tfor ( const [ source, bindings ] of blockBindingsBySource ) {\n\t\t\t\t\t// Get values in batch if the source supports it.\n\t\t\t\t\tlet values = {};\n\t\t\t\t\tif ( ! source.getValues ) {\n\t\t\t\t\t\tObject.keys( bindings ).forEach( ( attr ) => {\n\t\t\t\t\t\t\t// Default to the the source label when `getValues` doesn't exist.\n\t\t\t\t\t\t\tvalues[ attr ] = source.label;\n\t\t\t\t\t\t} );\n\t\t\t\t\t} else {\n\t\t\t\t\t\tvalues = source.getValues( {\n\t\t\t\t\t\t\tselect,\n\t\t\t\t\t\t\tcontext,\n\t\t\t\t\t\t\tclientId,\n\t\t\t\t\t\t\tbindings,\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\t\t\t\t\tfor ( const [ attributeName, value ] of Object.entries(\n\t\t\t\t\t\tvalues\n\t\t\t\t\t) ) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tattributeName === 'url' &&\n\t\t\t\t\t\t\t( ! value || ! isURLLike( value ) )\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t// Return null if value is not a valid URL.\n\t\t\t\t\t\t\tattributesFromSources[ attributeName ] = null;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tattributesFromSources[ attributeName ] = value;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...attributes,\n\t\t\t\t...attributesFromSources,\n\t\t\t};\n\t\t},\n\t\t[\n\t\t\tattributes,\n\t\t\tbindableAttributes,\n\t\t\tblockBindings,\n\t\t\tclientId,\n\t\t\tcontext,\n\t\t\tregisteredSources,\n\t\t]\n\t);\n\n\tconst setBoundAttributes = useCallback(\n\t\t( nextAttributes ) => {\n\t\t\tif ( ! blockBindings ) {\n\t\t\t\tsetAttributes( nextAttributes );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tregistry.batch( () => {\n\t\t\t\tconst keptAttributes = { ...nextAttributes };\n\t\t\t\tconst blockBindingsBySource = new Map();\n\n\t\t\t\t// Loop only over the updated attributes to avoid modifying the bound ones that haven't changed.\n\t\t\t\tfor ( const [ attributeName, newValue ] of Object.entries(\n\t\t\t\t\tkeptAttributes\n\t\t\t\t) ) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t! blockBindings[ attributeName ] ||\n\t\t\t\t\t\t! bindableAttributes?.includes( attributeName )\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst binding = blockBindings[ attributeName ];\n\t\t\t\t\tconst source = registeredSources[ binding?.source ];\n\t\t\t\t\tif ( ! source?.setValues ) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tblockBindingsBySource.set( source, {\n\t\t\t\t\t\t...blockBindingsBySource.get( source ),\n\t\t\t\t\t\t[ attributeName ]: {\n\t\t\t\t\t\t\targs: binding.args,\n\t\t\t\t\t\t\tnewValue,\n\t\t\t\t\t\t},\n\t\t\t\t\t} );\n\t\t\t\t\tdelete keptAttributes[ attributeName ];\n\t\t\t\t}\n\n\t\t\t\tif ( blockBindingsBySource.size ) {\n\t\t\t\t\tfor ( const [\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\tbindings,\n\t\t\t\t\t] of blockBindingsBySource ) {\n\t\t\t\t\t\tsource.setValues( {\n\t\t\t\t\t\t\tselect: registry.select,\n\t\t\t\t\t\t\tdispatch: registry.dispatch,\n\t\t\t\t\t\t\tcontext,\n\t\t\t\t\t\t\tclientId,\n\t\t\t\t\t\t\tbindings,\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst hasParentPattern = !! context[ 'pattern/overrides' ];\n\n\t\t\t\tif (\n\t\t\t\t\t// Don't update non-connected attributes if the block is using pattern overrides\n\t\t\t\t\t// and the editing is happening while overriding the pattern (not editing the original).\n\t\t\t\t\t! ( hasPatternOverrides && hasParentPattern ) &&\n\t\t\t\t\tObject.keys( keptAttributes ).length\n\t\t\t\t) {\n\t\t\t\t\t// Don't update href until it is supported.\n\t\t\t\t\tif ( hasPatternOverrides ) {\n\t\t\t\t\t\tdelete keptAttributes.href;\n\t\t\t\t\t}\n\t\t\t\t\tsetAttributes( keptAttributes );\n\t\t\t\t}\n\t\t\t} );\n\t\t},\n\t\t[\n\t\t\tbindableAttributes,\n\t\t\tblockBindings,\n\t\t\tclientId,\n\t\t\tcontext,\n\t\t\thasPatternOverrides,\n\t\t\tsetAttributes,\n\t\t\tregisteredSources,\n\t\t\tregistry,\n\t\t]\n\t);\n\n\tif ( ! blockType ) {\n\t\treturn null;\n\t}\n\n\tif ( blockType.apiVersion > 1 ) {\n\t\treturn (\n\t\t\t<EditWithFilters\n\t\t\t\t{ ...props }\n\t\t\t\tattributes={ computedAttributes }\n\t\t\t\tcontext={ context }\n\t\t\t\tsetAttributes={ setBoundAttributes }\n\t\t\t/>\n\t\t);\n\t}\n\n\t// Generate a class name for the block's editable form.\n\tconst generatedClassName = hasBlockSupport( blockType, 'className', true )\n\t\t? getBlockDefaultClassName( name )\n\t\t: null;\n\tconst className = clsx(\n\t\tgeneratedClassName,\n\t\tattributes?.className,\n\t\tprops.className\n\t);\n\n\treturn (\n\t\t<EditWithFilters\n\t\t\t{ ...props }\n\t\t\tattributes={ computedAttributes }\n\t\t\tclassName={ className }\n\t\t\tcontext={ context }\n\t\t\tsetAttributes={ setBoundAttributes }\n\t\t/>\n\t);\n};\n\nexport default EditWithGeneratedProps;\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;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