-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Carlos Bravo <carlos.bravo@automattic.com> Co-authored-by: Mario Santos <santosguillamot@gmail.com>
- Loading branch information
1 parent
54fc4ec
commit 03751d9
Showing
20 changed files
with
425 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* Behaviors. | ||
* | ||
* Updates the block editor settings with the theme's behaviors. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
add_filter( | ||
'block_editor_settings_all', | ||
function( $settings ) { | ||
$theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_data(); | ||
if ( array_key_exists( 'behaviors', $theme_data ) ) { | ||
$settings['behaviors'] = $theme_data['behaviors']; | ||
} | ||
return $settings; | ||
}, | ||
PHP_INT_MAX | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { SelectControl } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { select } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
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. | ||
* | ||
* Currently, only the `core/image` block is supported. | ||
* | ||
* @param {WPComponent} BlockEdit Original component. | ||
* | ||
* @return {WPComponent} Wrapped component. | ||
*/ | ||
export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => { | ||
return ( props ) => { | ||
// Only add behaviors to the core/image block. | ||
if ( props.name !== 'core/image' ) { | ||
return <BlockEdit { ...props } />; | ||
} | ||
|
||
const settings = | ||
select( blockEditorStore ).getSettings()?.__experimentalFeatures | ||
?.blocks?.[ props.name ]?.behaviors; | ||
|
||
if ( | ||
! settings || | ||
// If every behavior is disabled, do not show the behaviors inspector control. | ||
Object.entries( settings ).every( ( [ , value ] ) => ! value ) | ||
) { | ||
return <BlockEdit { ...props } />; | ||
} | ||
|
||
const { behaviors: blockBehaviors } = props.attributes; | ||
|
||
// Get the theme behaviors for the block from the theme.json. | ||
const themeBehaviors = | ||
select( blockEditorStore ).getBehaviors()?.blocks?.[ props.name ]; | ||
|
||
// Block behaviors take precedence over theme behaviors. | ||
const behaviors = merge( themeBehaviors, blockBehaviors || {} ); | ||
|
||
return ( | ||
<> | ||
<BlockEdit { ...props } /> | ||
<InspectorControls group="advanced"> | ||
<SelectControl | ||
__nextHasNoMarginBottom | ||
label={ __( 'Behaviors' ) } | ||
// At the moment we are only supporting one behavior (Lightbox) | ||
value={ behaviors?.lightbox ? 'lightbox' : '' } | ||
options={ Object.entries( settings ) | ||
.filter( ( [ , behaviorValue ] ) => behaviorValue ) // Filter out behaviors that are disabled. | ||
.map( ( [ behaviorName ] ) => ( { | ||
value: behaviorName, | ||
label: | ||
// Capitalize the first letter of the behavior name. | ||
behaviorName[ 0 ].toUpperCase() + | ||
behaviorName.slice( 1 ).toLowerCase(), | ||
} ) ) | ||
.concat( { | ||
value: '', | ||
label: __( 'No behaviors' ), | ||
} ) } | ||
onChange={ ( nextValue ) => { | ||
// If the user selects something, it means that they want to | ||
// change the default value (true) so we save it in the attributes. | ||
props.setAttributes( { | ||
behaviors: { | ||
lightbox: nextValue === 'lightbox', | ||
}, | ||
} ); | ||
} } | ||
hideCancelButton={ true } | ||
help={ __( 'Add behaviors' ) } | ||
size="__unstable-large" | ||
/> | ||
</InspectorControls> | ||
</> | ||
); | ||
}; | ||
}, 'withBehaviors' ); | ||
|
||
addFilter( | ||
'editor.BlockEdit', | ||
'core/behaviors/with-inspector-control', | ||
withBehaviors | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
03751d9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 03751d9.
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/5073396028
📝 Reported issues:
No Behaviors
should be the default as defined in the core theme.json #50923 in/test/e2e/specs/editor/various/behaviors.spec.js