@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
8 lines (7 loc) • 5.19 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/bindings/pattern-overrides.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\nconst CONTENT = 'content';\n\n/**\n * @type {WPBlockBindingsSource}\n */\nexport default {\n\tname: 'core/pattern-overrides',\n\tgetValues( { select, clientId, context, bindings } ) {\n\t\tconst patternOverridesContent = context[ 'pattern/overrides' ];\n\t\tconst { getBlockAttributes } = select( blockEditorStore );\n\t\tconst currentBlockAttributes = getBlockAttributes( clientId );\n\n\t\tconst overridesValues = {};\n\t\tfor ( const attributeName of Object.keys( bindings ) ) {\n\t\t\tconst overridableValue =\n\t\t\t\tpatternOverridesContent?.[\n\t\t\t\t\tcurrentBlockAttributes?.metadata?.name\n\t\t\t\t]?.[ attributeName ];\n\n\t\t\t// If it has not been overridden, return the original value.\n\t\t\t// Check undefined because empty string is a valid value.\n\t\t\tif ( overridableValue === undefined ) {\n\t\t\t\toverridesValues[ attributeName ] =\n\t\t\t\t\tcurrentBlockAttributes[ attributeName ];\n\t\t\t\tcontinue;\n\t\t\t} else {\n\t\t\t\toverridesValues[ attributeName ] =\n\t\t\t\t\toverridableValue === '' ? undefined : overridableValue;\n\t\t\t}\n\t\t}\n\t\treturn overridesValues;\n\t},\n\tsetValues( { select, dispatch, clientId, bindings } ) {\n\t\tconst { getBlockAttributes, getBlockParentsByBlockName, getBlocks } =\n\t\t\tselect( blockEditorStore );\n\t\tconst currentBlockAttributes = getBlockAttributes( clientId );\n\t\tconst blockName = currentBlockAttributes?.metadata?.name;\n\t\tif ( ! blockName ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst [ patternClientId ] = getBlockParentsByBlockName(\n\t\t\tclientId,\n\t\t\t'core/block',\n\t\t\ttrue\n\t\t);\n\n\t\t// Extract the updated attributes from the source bindings.\n\t\tconst attributes = Object.entries( bindings ).reduce(\n\t\t\t( attrs, [ key, { newValue } ] ) => {\n\t\t\t\tattrs[ key ] = newValue;\n\t\t\t\treturn attrs;\n\t\t\t},\n\t\t\t{}\n\t\t);\n\n\t\t// If there is no pattern client ID, sync blocks with the same name and same attributes.\n\t\tif ( ! patternClientId ) {\n\t\t\tconst syncBlocksWithSameName = ( blocks ) => {\n\t\t\t\tfor ( const block of blocks ) {\n\t\t\t\t\tif ( block.attributes?.metadata?.name === blockName ) {\n\t\t\t\t\t\tdispatch( blockEditorStore ).updateBlockAttributes(\n\t\t\t\t\t\t\tblock.clientId,\n\t\t\t\t\t\t\tattributes\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tsyncBlocksWithSameName( block.innerBlocks );\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tsyncBlocksWithSameName( getBlocks() );\n\t\t\treturn;\n\t\t}\n\t\tconst currentBindingValue =\n\t\t\tgetBlockAttributes( patternClientId )?.[ CONTENT ];\n\n\t\tdispatch( blockEditorStore ).updateBlockAttributes( patternClientId, {\n\t\t\t[ CONTENT ]: {\n\t\t\t\t...currentBindingValue,\n\t\t\t\t[ blockName ]: {\n\t\t\t\t\t...currentBindingValue?.[ blockName ],\n\t\t\t\t\t...Object.entries( attributes ).reduce(\n\t\t\t\t\t\t( acc, [ key, value ] ) => {\n\t\t\t\t\t\t\t// TODO: We need a way to represent `undefined` in the serialized overrides.\n\t\t\t\t\t\t\t// Also see: https://github.com/WordPress/gutenberg/pull/57249#discussion_r1452987871\n\t\t\t\t\t\t\t// We use an empty string to represent undefined for now until\n\t\t\t\t\t\t\t// we support a richer format for overrides and the block bindings API.\n\t\t\t\t\t\t\tacc[ key ] = value === undefined ? '' : value;\n\t\t\t\t\t\t\treturn acc;\n\t\t\t\t\t\t},\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},\n\tcanUserEditValue: () => true,\n};\n"],
"mappings": ";AAGA,SAAS,SAAS,wBA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