Skip to content

Commit

Permalink
[Pattern Overrides] Use a single checkbox to turn on pattern override…
Browse files Browse the repository at this point in the history
…s for all allowed attributes (#57009)

* Use a single checkbox to turn on pattern overrides

* Add check before setting/unsetting connection source

* Code review
  • Loading branch information
kevin940726 authored Dec 21, 2023
1 parent bf87c21 commit 1275d2f
Showing 1 changed file with 46 additions and 40 deletions.
86 changes: 46 additions & 40 deletions packages/patterns/src/components/partial-syncing-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,38 @@ import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';

function PartialSyncingControls( { name, attributes, setAttributes } ) {
const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];
const attributeSources = Object.keys( syncedAttributes ).map(
( attributeName ) =>
attributes.connections?.attributes?.[ attributeName ]?.source
);
const isConnectedToOtherSources = attributeSources.every(
( source ) => source && source !== 'pattern_attributes'
);

// Render nothing if all supported attributes are connected to other sources.
if ( isConnectedToOtherSources ) {
return null;
}

function updateConnections( isChecked ) {
let updatedConnections = {
...attributes.connections,
attributes: { ...attributes.connections?.attributes },
};

function updateConnections( attributeName, isChecked ) {
if ( ! isChecked ) {
let updatedConnections = {
...attributes.connections,
attributes: {
...attributes.connections?.attributes,
[ attributeName ]: undefined,
},
};
if ( Object.keys( updatedConnections.attributes ).length === 1 ) {
updatedConnections.attributes = undefined;
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if (
updatedConnections.attributes[ attributeName ]?.source ===
'pattern_attributes'
) {
delete updatedConnections.attributes[ attributeName ];
}
}
if (
Object.keys( updatedConnections ).length === 1 &&
updateConnections.attributes === undefined
) {
if ( ! Object.keys( updatedConnections.attributes ).length ) {
delete updatedConnections.attributes;
}
if ( ! Object.keys( updatedConnections ).length ) {
updatedConnections = undefined;
}
setAttributes( {
Expand All @@ -42,15 +57,13 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
return;
}

const updatedConnections = {
...attributes.connections,
attributes: {
...attributes.connections?.attributes,
[ attributeName ]: {
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if ( ! updatedConnections.attributes[ attributeName ] ) {
updatedConnections.attributes[ attributeName ] = {
source: 'pattern_attributes',
},
},
};
};
}
}

if ( typeof attributes.metadata?.id === 'string' ) {
setAttributes( { connections: updatedConnections } );
Expand All @@ -71,25 +84,18 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
<InspectorControls group="advanced">
<BaseControl __nextHasNoMarginBottom>
<BaseControl.VisualLabel>
{ __( 'Synced attributes' ) }
{ __( 'Pattern overrides' ) }
</BaseControl.VisualLabel>
{ Object.entries( syncedAttributes ).map(
( [ attributeName, label ] ) => (
<CheckboxControl
key={ attributeName }
__nextHasNoMarginBottom
label={ label }
checked={
attributes.connections?.attributes?.[
attributeName
]?.source === 'pattern_attributes'
}
onChange={ ( isChecked ) => {
updateConnections( attributeName, isChecked );
} }
/>
)
) }
<CheckboxControl
__nextHasNoMarginBottom
label={ __( 'Allow instance overrides' ) }
checked={ attributeSources.some(
( source ) => source === 'pattern_attributes'
) }
onChange={ ( isChecked ) => {
updateConnections( isChecked );
} }
/>
</BaseControl>
</InspectorControls>
);
Expand Down

1 comment on commit 1275d2f

@github-actions
Copy link

@github-actions github-actions bot commented on 1275d2f Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 1275d2f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7286687538
📝 Reported issues:

Please sign in to comment.