-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Remove inline style colors atributes from paragraph. #3722
Changes from all commits
a9d480f
4a62e8a
74b47db
267aad1
eae5a48
240fd08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,9 +36,6 @@ export function addAttribute( settings ) { | |
settings.attributes = assign( settings.attributes, { | ||
anchor: { | ||
type: 'string', | ||
source: 'attribute', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this attributes definition modified? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change allows the server to have access to anchor attribute which is required to generate the styles in the server. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This will also mean existing blocks with anchors will lose the attribute value after this change (since it was not encoded in the comment delimiters). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this was mentioned in #3722 (comment) |
||
attribute: 'id', | ||
selector: '*', | ||
}, | ||
} ); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlockRandomAnchor, hasBlockSupport } from '../api'; | ||
|
||
const CUSTOM_STYLE_ATTRIBUTES = [ 'backgroundColor', 'textColor' ]; | ||
|
||
const hasCustomStyleAttributes = ( attributes ) => ( | ||
CUSTOM_STYLE_ATTRIBUTES.some( attr => attributes[ attr ] ) | ||
); | ||
|
||
function withCustomStylesAnchor( BlockEdit ) { | ||
return class CustomStylesAnchor extends Component { | ||
componentWillReceiveProps( nextProps ) { | ||
if ( hasBlockSupport( nextProps.name, 'anchor' ) && | ||
hasBlockSupport( nextProps.name, 'customStyles' ) && | ||
! nextProps.attributes.anchor && | ||
hasCustomStyleAttributes( nextProps.attributes ) | ||
) { | ||
nextProps.setAttributes( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @gziolo, what we are doing here is verifying if no anchor was set yet ( by the user or automatically). If that's the case we check if attributes needing custom styles were used and if yes we set an automatically generated anchor attribute. I think it's not enough to pass a prop we really want to save the attribute for it to be serialized. The general question here is what is our extensibility solution to allow to set an attribute depending on other. E.g: If I want to have a plugin that sets red text color attribute if the background is blue what would be the best way to do it? Right now it looks like using our hooks we can achieve this result using this solution, but maybe there is a simpler way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okey, |
||
anchor: getBlockRandomAnchor( nextProps.name ), | ||
} ); | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<BlockEdit { ...this.props } /> | ||
); | ||
} | ||
}; | ||
} | ||
|
||
export function addCustomStylesClass( extraProps, blockType, attributes ) { | ||
if ( hasBlockSupport( blockType.name, 'customStyles' ) && hasCustomStyleAttributes( attributes ) ) { | ||
extraProps.className = classnames( extraProps.className, 'has-custom-styles' ); | ||
} | ||
|
||
return extraProps; | ||
} | ||
|
||
export default function customAnchorStyles() { | ||
addFilter( 'blocks.BlockEdit', 'core/custom-styles/inspector-control', withCustomStylesAnchor ); | ||
addFilter( 'blocks.getSaveContent.extraProps', 'core/custom-styles/save-props', addCustomStylesClass ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,39 @@ function gutenberg_render_block( $block ) { | |
return ''; | ||
} | ||
|
||
/** | ||
* Given an array of parsed blocks, returns the css styles they require. | ||
* | ||
* @since 1.8.1 | ||
* | ||
* @param array $blocks Parsed block array. | ||
* | ||
* @return string CSS style block or empty string | ||
*/ | ||
function do_blocks_styles( $blocks ) { | ||
$map_attrs_styles = array( | ||
'textColor' => 'color', | ||
'backgroundColor' => 'background-color', | ||
); | ||
|
||
$styles = ''; | ||
foreach ( $blocks as $block ) { | ||
$attributes = is_array( $block['attrs'] ) ? $block['attrs'] : array(); | ||
if ( isset( $attributes['anchor'] ) ) { | ||
$rules = ''; | ||
foreach ( $map_attrs_styles as $attr_key => $css_property ) { | ||
if ( isset( $attributes[ $attr_key ] ) ) { | ||
$rules .= "\n\t" . esc_attr( sprintf( '%1$s: %2$s;', $css_property, $attributes[ $attr_key ] ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to do the whitespace prettifying here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this whitespace prettifying when we check the source code the styles are messy and all on the same line but they work. I'm not certain what we normally do in this cases. |
||
} | ||
} | ||
if ( '' !== $rules ) { | ||
$styles .= sprintf( "\n.has-custom-styles#%1\$s { %2\$s\n}", esc_attr( $attributes['anchor'] ), $rules ); | ||
} | ||
} | ||
} | ||
return '' !== $styles ? sprintf( "<style type\"text/css\">%1\$s\n</style>", $styles ) : ''; | ||
} | ||
|
||
/** | ||
* Parses dynamic blocks out of `post_content` and re-renders them. | ||
* | ||
|
@@ -182,7 +215,7 @@ function gutenberg_render_block( $block ) { | |
function do_blocks( $content ) { | ||
$blocks = gutenberg_parse_blocks( $content ); | ||
|
||
$content_after_blocks = ''; | ||
$content_after_blocks = do_blocks_styles( $blocks ); | ||
foreach ( $blocks as $block ) { | ||
$content_after_blocks .= gutenberg_render_block( $block ); | ||
} | ||
|
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.
Not sure it's worth concerning about, but there's a decent amount of redundancy between the class name and style applied to a block with custom styles, i.e.:
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.
I agree the naming was redundant, I changed the id's so now they are in format 'paragraph-....'.