Skip to content

Commit

Permalink
Use behaviors.blocks.core/image.lightbox
Browse files Browse the repository at this point in the history
  • Loading branch information
michalczaplinski committed May 23, 2023
1 parent 8d6b0d0 commit 76b2a75
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
6 changes: 5 additions & 1 deletion lib/theme.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"version": 2,
"behaviors": {
"lightbox": false
"blocks": {
"core/image": {
"lightbox": false
}
}
},
"settings": {
"appearanceTools": false,
Expand Down
21 changes: 18 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"change-case": "^4.1.2",
"classnames": "^2.3.1",
"colord": "^2.7.0",
"deepmerge": "^4.3.0",
"diff": "^4.0.2",
"dom-scroll-into-view": "^1.2.1",
"fast-deep-equal": "^3.1.3",
Expand Down
15 changes: 10 additions & 5 deletions packages/block-editor/src/hooks/behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { select } from '@wordpress/data';
import { InspectorControls } from '../components';
import { store as blockEditorStore } from '../store';

/**
* External dependencies
*/
import merge from 'deepmerge';

/**
* Override the default edit UI to include a new block inspector control for
* assigning behaviors to blocks if behaviors are enabled in the theme.json.
Expand Down Expand Up @@ -44,12 +49,12 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => {

const { behaviors: blockBehaviors } = props.attributes;

// Get the theme behaviors from the theme.json.
const themeBehaviors = select( blockEditorStore ).getBehaviors();
// Get the theme behaviors for the block from the theme.json.
const themeBehaviors =
select( blockEditorStore ).getBehaviors()?.blocks?.[ props.name ];

// By default, use the block behaviors.
// If the theme has behaviors, but the block does not, use the theme behaviors.
const behaviors = blockBehaviors || themeBehaviors || {};
// Block behaviors take precedence over theme behaviors.
const behaviors = merge( themeBehaviors, blockBehaviors || {} );

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/specs/editor/various/behaviors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ test.describe( 'Testing behaviors functionality', () => {
requestUtils,
page,
} ) => {
// In this theme, the default value for settings.behaviors.lightbox is `true`.
// In this theme, the default value for settings.behaviors.blocks.core/image.lightbox is `true`.
await requestUtils.activateTheme( 'behaviors-enabled' );
const media = await createMedia( { admin, requestUtils } );

Expand Down
6 changes: 5 additions & 1 deletion test/gutenberg-test-themes/behaviors-enabled/theme.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"version": 2,
"behaviors": {
"lightbox": true
"blocks": {
"core/image": {
"lightbox": true
}
}
}
}

0 comments on commit 76b2a75

Please sign in to comment.