Skip to content

Commit

Permalink
Use the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Apr 13, 2024
1 parent 3d93f10 commit c4b7073
Showing 1 changed file with 36 additions and 56 deletions.
92 changes: 36 additions & 56 deletions packages/editor/src/bindings/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,53 @@
* WordPress dependencies
*/
import { _x } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useCallback } from '@wordpress/element';

const CONTENT = 'content';

export default {
name: 'core/pattern-overrides',
label: _x( 'Pattern Overrides', 'block bindings source' ),
useSource( { clientId, setAttributes }, _, attributeName ) {
const selected = useSelect(
( select ) => {
const { getBlockAttributes, getBlockParents, getBlockName } =
select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );
const parents = getBlockParents( clientId, true );
const patternClientId = parents.find(
( id ) => getBlockName( id ) === 'core/block'
);
return {
patternClientId,
blockName: currentBlockAttributes?.metadata?.name,
placeholder: currentBlockAttributes?.[ attributeName ],
value: getBlockAttributes( patternClientId )?.[ CONTENT ]?.[
currentBlockAttributes?.metadata?.name
]?.[ attributeName ],
};
},
[ clientId, attributeName ]
getValue( { registry, clientId, attributeName } ) {
const { getBlockAttributes, getBlockParents, getBlockName } =
registry.select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );
const parents = getBlockParents( clientId, true );
const patternClientId = parents.find(
( id ) => getBlockName( id ) === 'core/block'
);
const { patternClientId, blockName, placeholder, value } = selected;
const { getBlockAttributes } = useSelect( blockEditorStore );
const { updateBlockAttributes } = useDispatch( blockEditorStore );

const updateValue = useCallback(
( newValue, nextAttributes ) => {
if ( patternClientId ) {
const currentBindingValue =
getBlockAttributes( patternClientId )?.[ CONTENT ];
updateBlockAttributes( patternClientId, {
[ CONTENT ]: {
...currentBindingValue,
[ blockName ]: {
...currentBindingValue?.[ blockName ],
[ attributeName ]: newValue,
},
},
} );
} else {
setAttributes( nextAttributes );
}
},
[
setAttributes,
patternClientId,
blockName,
attributeName,
getBlockAttributes,
updateBlockAttributes,
]
return getBlockAttributes( patternClientId )?.[ CONTENT ]?.[
currentBlockAttributes?.metadata?.name
]?.[ attributeName ];
},
setValue( { registry, clientId, attributeName, value } ) {
const { getBlockAttributes, getBlockParents, getBlockName } =
registry.select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );
const parents = getBlockParents( clientId, true );
const patternClientId = parents.find(
( id ) => getBlockName( id ) === 'core/block'
);
const blockName = currentBlockAttributes?.metadata?.name;

if ( ! patternClientId ) {
return;
}

return {
placeholder,
value,
updateValue,
};
const currentBindingValue =
getBlockAttributes( patternClientId )?.[ CONTENT ];
registry
.dispatch( blockEditorStore )
.updateBlockAttributes( patternClientId, {
[ CONTENT ]: {
...currentBindingValue,
[ blockName ]: {
...currentBindingValue?.[ blockName ],
[ attributeName ]: value,
},
},
} );
},
lockAttributesEditing: false,
};

0 comments on commit c4b7073

Please sign in to comment.