Skip to content

Commit

Permalink
Block Bindings: Fix disable bindings editing when source is undefined (
Browse files Browse the repository at this point in the history
…#58961)

* Disable editing if source is undefined

* Add tests when source is undefined
  • Loading branch information
SantosGuillamot authored Feb 13, 2024
1 parent 0c6f3cb commit 1a2a1d3
Show file tree
Hide file tree
Showing 5 changed files with 480 additions and 27 deletions.
17 changes: 12 additions & 5 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,19 @@ export function RichTextWrapper(
for ( const [ attribute, args ] of Object.entries(
blockBindings
) ) {
// If any of the attributes with source "rich-text" is part of the bindings,
// has a source with `lockAttributesEditing`, disable it.
if (
blockTypeAttributes?.[ attribute ]?.source ===
'rich-text' &&
getBlockBindingsSource( args.source )?.lockAttributesEditing
blockTypeAttributes?.[ attribute ]?.source !== 'rich-text'
) {
break;
}

// If the source is not defined, or if its value of `lockAttributesEditing` is `true`, disable it.
const blockBindingsSource = getBlockBindingsSource(
args.source
);
if (
! blockBindingsSource ||
blockBindingsSource.lockAttributesEditing
) {
shouldDisableEditing = true;
break;
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ function ButtonEdit( props ) {
return {};
}

const { getBlockBindingsSource } = unlock(
const blockBindingsSource = unlock(
select( blockEditorStore )
);
).getBlockBindingsSource( metadata?.bindings?.url?.source );

return {
lockUrlControls:
!! metadata?.bindings?.url &&
getBlockBindingsSource( metadata?.bindings?.url?.source )
?.lockAttributesEditing,
( ! blockBindingsSource ||
blockBindingsSource?.lockAttributesEditing ),
};
},
[ isSelected ]
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,15 @@ export function ImageEdit( {
return {};
}

const { getBlockBindingsSource } = unlock(
const blockBindingsSource = unlock(
select( blockEditorStore )
);
).getBlockBindingsSource( metadata?.bindings?.url?.source );

return {
lockUrlControls:
!! metadata?.bindings?.url &&
getBlockBindingsSource( metadata?.bindings?.url?.source )
?.lockAttributesEditing,
( ! blockBindingsSource ||
blockBindingsSource?.lockAttributesEditing ),
};
},
[ isSingleSelected ]
Expand Down
21 changes: 15 additions & 6 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,23 +423,32 @@ export default function Image( {
} = metadata?.bindings || {};
const hasParentPattern =
getBlockParentsByBlockName( clientId, 'core/block' ).length > 0;
const urlBindingSource = getBlockBindingsSource(
urlBinding?.source
);
const altBindingSource = getBlockBindingsSource(
altBinding?.source
);
const titleBindingSource = getBlockBindingsSource(
titleBinding?.source
);
return {
lockUrlControls:
!! urlBinding &&
getBlockBindingsSource( urlBinding?.source )
?.lockAttributesEditing,
( ! urlBindingSource ||
urlBindingSource?.lockAttributesEditing ),
lockHrefControls:
// Disable editing the link of the URL if the image is inside a pattern instance.
// This is a temporary solution until we support overriding the link on the frontend.
hasParentPattern,
lockAltControls:
!! altBinding &&
getBlockBindingsSource( altBinding?.source )
?.lockAttributesEditing,
( ! altBindingSource ||
altBindingSource?.lockAttributesEditing ),
lockTitleControls:
!! titleBinding &&
getBlockBindingsSource( titleBinding?.source )
?.lockAttributesEditing,
( ! titleBindingSource ||
titleBindingSource?.lockAttributesEditing ),
};
},
[ clientId, isSingleSelected, metadata?.bindings ]
Expand Down
Loading

1 comment on commit 1a2a1d3

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 1a2a1d3.
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/7889915199
📝 Reported issues:

Please sign in to comment.