-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make font size an implicit attribute of the block #21153
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0487f45
Make font size an implicit attribute of the block
oandregal 1fbd486
Export getFontSizeObjectByValue utility
oandregal 364cf70
Remove withFontSizes HOC
oandregal 23f62e6
Destructure props
oandregal 66da488
Add CSS variable to block style
oandregal 619b0dd
Refactor
youknowriad 51e0122
Add custom font-size attribute deprecation
youknowriad e60bce1
Prevent overriding other typography atts
oandregal 8e7e9b4
Do not render line-height control if block doesnt declare support
oandregal d05f2b2
Update paragraph example to use fontSize from style
oandregal 519c96c
Dedupe classes
oandregal 652e85b
Remove unnecessary classnames
oandregal b3bc3dd
Update snapshot
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export { getFontSize, getFontSizeClass } from './utils'; | ||
export { | ||
getFontSize, | ||
getFontSizeClass, | ||
getFontSizeObjectByValue, | ||
} from './utils'; | ||
export { default as FontSizePicker } from './font-size-picker'; | ||
export { default as withFontSizes } from './with-font-sizes'; |
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,151 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { hasBlockSupport } from '@wordpress/blocks'; | ||
import { useSelect } from '@wordpress/data'; | ||
import TokenList from '@wordpress/token-list'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
getFontSize, | ||
getFontSizeClass, | ||
getFontSizeObjectByValue, | ||
FontSizePicker, | ||
} from '../components/font-sizes'; | ||
import { cleanEmptyObject } from './utils'; | ||
|
||
export const FONT_SIZE_SUPPORT_KEY = '__experimentalFontSize'; | ||
|
||
/** | ||
* Filters registered block settings, extending attributes to include | ||
* `fontSize` and `fontWeight` attributes. | ||
* | ||
* @param {Object} settings Original block settings | ||
* @return {Object} Filtered block settings | ||
*/ | ||
function addAttributes( settings ) { | ||
if ( ! hasBlockSupport( settings, FONT_SIZE_SUPPORT_KEY ) ) { | ||
return settings; | ||
} | ||
|
||
// Allow blocks to specify a default value if needed. | ||
if ( ! settings.attributes.fontSize ) { | ||
Object.assign( settings.attributes, { | ||
fontSize: { | ||
type: 'string', | ||
}, | ||
} ); | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
/** | ||
* Override props assigned to save component to inject font size. | ||
* | ||
* @param {Object} props Additional props applied to save element | ||
* @param {Object} blockType Block type | ||
* @param {Object} attributes Block attributes | ||
* @return {Object} Filtered props applied to save element | ||
*/ | ||
function addSaveProps( props, blockType, attributes ) { | ||
if ( ! hasBlockSupport( blockType, FONT_SIZE_SUPPORT_KEY ) ) { | ||
return props; | ||
} | ||
|
||
// Use TokenList to dedupe classes. | ||
const classes = new TokenList( props.className ); | ||
classes.add( getFontSizeClass( attributes.fontSize ) ); | ||
const newClassName = classes.value; | ||
props.className = newClassName ? newClassName : undefined; | ||
|
||
return props; | ||
} | ||
|
||
/** | ||
* Filters registered block settings to expand the block edit wrapper | ||
* by applying the desired styles and classnames. | ||
* | ||
* @param {Object} settings Original block settings | ||
* @return {Object} Filtered block settings | ||
*/ | ||
function addEditProps( settings ) { | ||
if ( ! hasBlockSupport( settings, FONT_SIZE_SUPPORT_KEY ) ) { | ||
return settings; | ||
} | ||
|
||
const existingGetEditWrapperProps = settings.getEditWrapperProps; | ||
settings.getEditWrapperProps = ( attributes ) => { | ||
let props = {}; | ||
if ( existingGetEditWrapperProps ) { | ||
props = existingGetEditWrapperProps( attributes ); | ||
} | ||
return addSaveProps( props, settings, attributes ); | ||
}; | ||
|
||
return settings; | ||
} | ||
|
||
/** | ||
* Inspector control panel containing the font size related configuration | ||
* | ||
* @param {Object} props | ||
* | ||
* @return {WPElement} Font size edit element. | ||
*/ | ||
export function FontSizeEdit( props ) { | ||
const { | ||
name: blockName, | ||
attributes: { fontSize, style }, | ||
setAttributes, | ||
} = props; | ||
|
||
const { fontSizes } = useSelect( ( select ) => | ||
select( 'core/block-editor' ).getSettings() | ||
); | ||
|
||
if ( ! hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY ) ) { | ||
return null; | ||
} | ||
|
||
const fontSizeObject = getFontSize( | ||
fontSizes, | ||
fontSize, | ||
style?.typography?.fontSize | ||
); | ||
const onChange = ( value ) => { | ||
const fontSizeSlug = getFontSizeObjectByValue( fontSizes, value ).slug; | ||
|
||
setAttributes( { | ||
style: cleanEmptyObject( { | ||
...style, | ||
typography: { | ||
...style?.typography, | ||
fontSize: fontSizeSlug ? undefined : value, | ||
}, | ||
} ), | ||
fontSize: fontSizeSlug, | ||
} ); | ||
}; | ||
|
||
return ( | ||
<FontSizePicker value={ fontSizeObject.size } onChange={ onChange } /> | ||
); | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'core/font/addAttribute', | ||
addAttributes | ||
); | ||
|
||
addFilter( | ||
'blocks.getSaveContent.extraProps', | ||
'core/font/addSaveProps', | ||
addSaveProps | ||
); | ||
|
||
addFilter( 'blocks.registerBlockType', 'core/font/addEditProps', addEditProps ); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Noting here that this has the side-effect of removing duplicated classes for old blocks that are updated. I can't think of any reason why this would be a problem but thought to highlight anyway before merging. cc @youknowriad
I've tested this with old content (preset values, custom values, duplicated classes) and works nicely, so I'm going to land in a few hours.