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 and youknowriad committed Feb 20, 2024
1 parent 16f7948 commit 50244c6
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 @@ -426,23 +426,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

0 comments on commit 50244c6

Please sign in to comment.