Skip to content

Commit

Permalink
Adapt code after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 22, 2024
1 parent 0cc1812 commit 9fcf841
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 64 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}

return attributes;
}, [ bindings, name, clientId, blockContext, registry, sources ] );
}, [ blockBindings, name, clientId, blockContext, registry, sources ] );

const { setAttributes } = props;

Expand Down
18 changes: 2 additions & 16 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ export const registerBlockBindingsSource = ( source ) => {

// Check the `usesContext` property is correct.
if ( usesContext && ! Array.isArray( usesContext ) ) {
console.error( 'Block bindings source usesContext must be an array.' );
warning( 'Block bindings source usesContext must be an array.' );
return;
}

Expand Down Expand Up @@ -899,21 +899,7 @@ export const registerBlockBindingsSource = ( source ) => {
return;
}

// Merge context from server and client.
let mergedUsesContext = [
...( existingSource?.usesContext || [] ),
...( usesContext || [] ),
];
// Remove duplicates.
mergedUsesContext =
mergedUsesContext.length > 0
? [ ...new Set( mergedUsesContext ) ]
: undefined;

unlock( dispatch( blocksStore ) ).addBlockBindingsSource( {
...source,
usesContext: mergedUsesContext,
} );
unlock( dispatch( blocksStore ) ).addBlockBindingsSource( source );
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export function addBlockBindingsSource( source ) {
type: 'ADD_BLOCK_BINDINGS_SOURCE',
name: source.name,
label: source.label,
getValues: source.getValues,
usesContext: source.usesContext,
getValues: source.getValues,
setValues: source.setValues,
getPlaceholder: source.getPlaceholder,
canUserEditValue: source.canUserEditValue,
Expand Down
30 changes: 11 additions & 19 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,31 +374,23 @@ export function collections( state = {}, action ) {
export function blockBindingsSources( state = {}, action ) {
switch ( action.type ) {
case 'ADD_BLOCK_BINDINGS_SOURCE':
// Filter the name property, the type property, and the undefined values.
const newProperties = Object.fromEntries(
Object.entries( action ).filter(
( [ key, value ] ) =>
value !== undefined && key !== 'name' && key !== 'type'
)
);
// Merge usesContext with existing values, potentially defined in the server registration.
let mergedUsesContext = [
...( state[ action.name ]?.usesContext || [] ),
...( action.usesContext || [] ),
];
// Remove duplicates.
mergedUsesContext =
mergedUsesContext.length > 0
? [ ...new Set( mergedUsesContext ) ]
: undefined;

return {
...state,
[ action.name ]: {
// Keep the existing properties if it has been bootstrapped.
...state[ action.name ],
// Update with the new properties.
...newProperties,
canUserEditValue:
action.canUserEditValue || ( () => false ),
},
};
case 'ADD_BOOTSTRAPPED_BLOCK_BINDINGS_SOURCE':
return {
...state,
[ action.name ]: {
// Don't override the label if it's already set.
label: state[ action.name ]?.label || action.label,
usesContext: mergedUsesContext,
getValues: action.getValues,
setValues: action.setValues,
getPlaceholder: action.getPlaceholder,
Expand Down
43 changes: 28 additions & 15 deletions packages/editor/src/bindings/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@ const CONTENT = 'content';

export default {
name: 'core/pattern-overrides',
getValue( { registry, clientId, context, attributeName } ) {
getValues( { registry, clientId, context, bindings } ) {
const patternOverridesContent = context[ 'pattern/overrides' ];
const { getBlockAttributes } = registry.select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );

if ( ! patternOverridesContent ) {
return currentBlockAttributes[ attributeName ];
}

const overridableValue =
patternOverridesContent?.[
currentBlockAttributes?.metadata?.name
]?.[ attributeName ];
const overridesValues = {};
for ( const attributeName of Object.keys( bindings ) ) {
const overridableValue =
patternOverridesContent?.[
currentBlockAttributes?.metadata?.name
]?.[ attributeName ];

// If there is no pattern client ID, or it is not overwritten, return the default value.
if ( overridableValue === undefined ) {
return currentBlockAttributes[ attributeName ];
// If it has not been overriden, return the original value.
// Check undefined because empty string is a valid value.
if ( overridableValue === undefined ) {
overridesValues[ attributeName ] =
currentBlockAttributes[ attributeName ];
continue;
} else {
overridesValues[ attributeName ] =
overridableValue === '' ? undefined : overridableValue;
}
}

return overridableValue === '' ? undefined : overridableValue;
return overridesValues;
},
setValues( { registry, clientId, attributes } ) {
setValues( { registry, clientId, bindings } ) {
const { getBlockAttributes, getBlockParentsByBlockName, getBlocks } =
registry.select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );
Expand All @@ -43,6 +47,15 @@ export default {
true
);

// Extract the updated attributes from the source bindings.
const attributes = Object.entries( bindings ).reduce(
( attrs, [ key, { newValue } ] ) => {
attrs[ key ] = newValue;
return attrs;
},
{}
);

// If there is no pattern client ID, sync blocks with the same name and same attributes.
if ( ! patternClientId ) {
const syncBlocksWithSameName = ( blocks ) => {
Expand Down
36 changes: 24 additions & 12 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,37 @@ export default {
getPlaceholder( { args } ) {
return args.key;
},
getValue( { registry, context, args } ) {
return registry
getValues( { registry, context, bindings } ) {
const meta = registry
.select( coreDataStore )
.getEditedEntityRecord(
'postType',
context?.postType,
context?.postId
).meta?.[ args.key ];
)?.meta;
const newValues = {};
for ( const [ attributeName, source ] of Object.entries( bindings ) ) {
newValues[ attributeName ] = meta?.[ source.args.key ];
}
return newValues;
},
setValue( { registry, context, args, value } ) {
setValues( { registry, context, bindings } ) {
const newMeta = {};
Object.values( bindings ).forEach( ( { args, newValue } ) => {
newMeta[ args.key ] = newValue;
} );
registry
.dispatch( coreDataStore )
.editEntityRecord( 'postType', context?.postType, context?.postId, {
meta: {
[ args.key ]: value,
},
meta: newMeta,
} );
},
canUserEditValue( { select, context, args } ) {
// Lock editing in query loop.
if ( context?.query || context?.queryId ) {
return false;
}

const postType =
context?.postType || select( editorStore ).getCurrentPostType();

Expand All @@ -52,11 +64,11 @@ export default {
}

// Check that the user has the capability to edit post meta.
const canUserEdit = select( coreDataStore ).canUserEditEntityRecord(
'postType',
context?.postType,
context?.postId
);
const canUserEdit = select( coreDataStore ).canUser( 'update', {
kind: 'postType',
name: context?.postType,
id: context?.postId,
} );
if ( ! canUserEdit ) {
return false;
}
Expand Down

0 comments on commit 9fcf841

Please sign in to comment.