-
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.
Extract the getSupportedStyles selector to the blocks store as a priv…
…ate selector (#47606)
- Loading branch information
1 parent
581eabf
commit ddcaa70
Showing
20 changed files
with
432 additions
and
206 deletions.
There are no files selected for viewing
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
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,10 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments'; | ||
|
||
export const { lock, unlock } = | ||
__dangerousOptInToUnstableAPIsOnlyForCoreModules( | ||
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', | ||
'@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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import createSelector from 'rememo'; | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlockType } from './selectors'; | ||
import { __EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY } from '../api/constants'; | ||
|
||
const ROOT_BLOCK_SUPPORTS = [ | ||
'background', | ||
'backgroundColor', | ||
'color', | ||
'linkColor', | ||
'buttonColor', | ||
'fontFamily', | ||
'fontSize', | ||
'fontStyle', | ||
'fontWeight', | ||
'lineHeight', | ||
'padding', | ||
'contentSize', | ||
'wideSize', | ||
'blockGap', | ||
'textDecoration', | ||
'textTransform', | ||
'letterSpacing', | ||
]; | ||
|
||
/** | ||
* Filters the list of supported styles for a given element. | ||
* | ||
* @param {string[]} blockSupports list of supported styles. | ||
* @param {string|undefined} name block name. | ||
* @param {string|undefined} element element name. | ||
* | ||
* @return {string[]} filtered list of supported styles. | ||
*/ | ||
function filterElementBlockSupports( blockSupports, name, element ) { | ||
return blockSupports.filter( ( support ) => { | ||
if ( support === 'fontSize' && element === 'heading' ) { | ||
return false; | ||
} | ||
|
||
// This is only available for links | ||
if ( support === 'textDecoration' && ! name && element !== 'link' ) { | ||
return false; | ||
} | ||
|
||
// This is only available for heading | ||
if ( | ||
support === 'textTransform' && | ||
! name && | ||
! [ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes( | ||
element | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
// This is only available for headings | ||
if ( | ||
support === 'letterSpacing' && | ||
! name && | ||
! [ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes( | ||
element | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} ); | ||
} | ||
|
||
/** | ||
* Returns the list of supported styles for a given block name and element. | ||
*/ | ||
export const getSupportedStyles = createSelector( | ||
( state, name, element ) => { | ||
if ( ! name ) { | ||
return filterElementBlockSupports( | ||
ROOT_BLOCK_SUPPORTS, | ||
name, | ||
element | ||
); | ||
} | ||
|
||
const blockType = getBlockType( state, name ); | ||
|
||
if ( ! blockType ) { | ||
return []; | ||
} | ||
|
||
const supportKeys = []; | ||
|
||
// Check for blockGap support. | ||
// Block spacing support doesn't map directly to a single style property, so needs to be handled separately. | ||
// Also, only allow `blockGap` support if serialization has not been skipped, to be sure global spacing can be applied. | ||
if ( | ||
blockType?.supports?.spacing?.blockGap && | ||
blockType?.supports?.spacing?.__experimentalSkipSerialization !== | ||
true && | ||
! blockType?.supports?.spacing?.__experimentalSkipSerialization?.some?.( | ||
( spacingType ) => spacingType === 'blockGap' | ||
) | ||
) { | ||
supportKeys.push( 'blockGap' ); | ||
} | ||
|
||
// check for shadow support | ||
if ( blockType?.supports?.shadow ) { | ||
supportKeys.push( 'shadow' ); | ||
} | ||
|
||
Object.keys( STYLE_PROPERTY ).forEach( ( styleName ) => { | ||
if ( ! STYLE_PROPERTY[ styleName ].support ) { | ||
return; | ||
} | ||
|
||
// Opting out means that, for certain support keys like background color, | ||
// blocks have to explicitly set the support value false. If the key is | ||
// unset, we still enable it. | ||
if ( STYLE_PROPERTY[ styleName ].requiresOptOut ) { | ||
if ( | ||
STYLE_PROPERTY[ styleName ].support[ 0 ] in | ||
blockType.supports && | ||
get( | ||
blockType.supports, | ||
STYLE_PROPERTY[ styleName ].support | ||
) !== false | ||
) { | ||
supportKeys.push( styleName ); | ||
return; | ||
} | ||
} | ||
|
||
if ( | ||
get( | ||
blockType.supports, | ||
STYLE_PROPERTY[ styleName ].support, | ||
false | ||
) | ||
) { | ||
supportKeys.push( styleName ); | ||
} | ||
} ); | ||
|
||
return filterElementBlockSupports( supportKeys, name, element ); | ||
}, | ||
( state, name ) => [ state.blockTypes[ name ] ] | ||
); |
Oops, something went wrong.
ddcaa70
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 ddcaa70.
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/4086606358
📝 Reported issues:
/test/e2e/specs/site-editor/template-part.spec.js