Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pattern Overrides] Use a single checkbox to turn on pattern overrides for all allowed attributes #57009

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading