-
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.
This commit adds a new filter for block supports controls for global …
…styles so we can hook into the global styles context.
- Loading branch information
Showing
13 changed files
with
141 additions
and
15 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/** | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks'; | ||
|
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,102 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { set } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { useContext, useState } from '@wordpress/element'; | ||
import { | ||
ColorEdit, | ||
TypographyPanel, | ||
BorderPanel, | ||
DimensionsPanel, | ||
useDisplayBlockControls, | ||
InspectorControls, | ||
} from '@wordpress/block-editor'; | ||
import { Button, PanelBody } from '@wordpress/components'; | ||
import { getCSSVarFromStyleValue } from '@wordpress/style-engine'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { GlobalStylesContext } from '../components/global-styles/context'; | ||
|
||
export const withEditBlockControls = createHigherOrderComponent( | ||
( BlockEdit ) => ( props ) => { | ||
const { name } = props; | ||
const [ shouldPushToGlobalStyles, setShouldPushToGlobalStyles ] = | ||
useState( false ); | ||
const { setUserConfig } = useContext( GlobalStylesContext ); | ||
|
||
const shouldDisplayControls = useDisplayBlockControls(); | ||
const blockSupportControlProps = { | ||
...props, | ||
setBlockGlobalStyles: ( path, newValue ) => { | ||
if ( ! shouldPushToGlobalStyles ) { | ||
return; | ||
} | ||
const fullPath = `styles.blocks.${ name }.${ path }`; | ||
setUserConfig( ( currentConfig ) => { | ||
// Deep clone `currentConfig` to avoid mutating it later. | ||
const newUserConfig = JSON.parse( | ||
JSON.stringify( currentConfig ) | ||
); | ||
set( newUserConfig, fullPath, getCSSVarFromStyleValue( newValue ) ); | ||
return newUserConfig; | ||
} ); | ||
}, | ||
}; | ||
|
||
return ( | ||
<> | ||
{ shouldDisplayControls && ( | ||
<> | ||
<ColorEdit { ...blockSupportControlProps } /> | ||
<TypographyPanel { ...blockSupportControlProps } /> | ||
<BorderPanel { ...blockSupportControlProps } /> | ||
<DimensionsPanel { ...blockSupportControlProps } /> | ||
<InspectorControls __experimentalGroup="advanced"> | ||
<PanelBody | ||
title={ __( 'Apply styles to all blocks' ) } | ||
> | ||
<Button | ||
variant={ | ||
! shouldPushToGlobalStyles | ||
? 'primary' | ||
: 'secondary' | ||
} | ||
isPressed={ shouldPushToGlobalStyles } | ||
onClick={ () => { | ||
setShouldPushToGlobalStyles( | ||
! shouldPushToGlobalStyles | ||
); | ||
} } | ||
> | ||
{ shouldPushToGlobalStyles ? __( 'Do not apply changes' ) : __( 'Apply changes' ) } | ||
</Button> | ||
<p> | ||
{ __( | ||
"When activated, all changes to this block's styles will be applied to all blocks of this type. Note that only typography, dimensions, color and border styles will be copied." | ||
) } | ||
</p> | ||
</PanelBody> | ||
</InspectorControls> | ||
</> | ||
) } | ||
<BlockEdit { ...props } /> | ||
</> | ||
); | ||
}, | ||
'withEditBlockControls' | ||
); | ||
|
||
addFilter( | ||
'editor.BlockEdit', | ||
'core/edit-site/block-supports-styles', | ||
withEditBlockControls | ||
); |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*/ | ||
import './components'; | ||
import './template-part-edit'; | ||
import './block-supports-styles'; |
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