-
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
Add duotone to image block #26751
Closed
Closed
Add duotone to image block #26751
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,59 @@ | ||
<?php | ||
/** | ||
* Duotone filter markup. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* SVG and stylesheet needed for rendering the duotone filter. | ||
* | ||
* @param string $selector CSS selectors to apply the duotone to. | ||
* @param string $id Unique slug for this duotone filter. | ||
* @param array $values R, G, and B values to filter with. | ||
* @return string Duotone stylesheet and SVG. | ||
*/ | ||
function gutenberg_render_duotone_filter( $selector, $id, $values ) { | ||
ob_start(); | ||
|
||
?> | ||
|
||
<style> | ||
<?php echo $selector; ?> { | ||
filter: url( <?php echo esc_url( '#' . $id ); ?> ); | ||
} | ||
</style> | ||
|
||
<svg | ||
xmlns:xlink="http://www.w3.org/1999/xlink" | ||
viewBox="0 0 0 0" | ||
width="0" | ||
height="0" | ||
focusable="false" | ||
role="none" | ||
style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;" | ||
> | ||
<defs> | ||
<filter id="<?php echo esc_attr( $id ); ?>"> | ||
<feColorMatrix | ||
type="matrix" | ||
<?php // phpcs:disable Generic.WhiteSpace.DisallowSpaceIndent ?> | ||
values=".299 .587 .114 0 0 | ||
.299 .587 .114 0 0 | ||
.299 .587 .114 0 0 | ||
0 0 0 1 0" | ||
<?php // phpcs:enable Generic.WhiteSpace.DisallowSpaceIndent ?> | ||
/> | ||
<feComponentTransfer color-interpolation-filters="sRGB" > | ||
<feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $values['r'] ) ); ?>" /> | ||
<feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $values['g'] ) ); ?>" /> | ||
<feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $values['b'] ) ); ?>" /> | ||
</feComponentTransfer> | ||
</filter> | ||
</defs> | ||
</svg> | ||
|
||
<?php | ||
|
||
return ob_get_clean(); | ||
} |
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
81 changes: 81 additions & 0 deletions
81
packages/block-editor/src/components/duotone-filter/index.js
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,81 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SVG } from '@wordpress/components'; | ||
|
||
/** | ||
* Values for the SVG `feComponentTransfer`. | ||
* | ||
* @typedef Values {Object} | ||
* @property {number[]} r Red values. | ||
* @property {number[]} g Green values. | ||
* @property {number[]} b Blue values. | ||
*/ | ||
|
||
/** | ||
* SVG and stylesheet needed for rendering the duotone filter. | ||
* | ||
* @param {Object} props Duotone props. | ||
* @param {string} props.selector Selector to apply the filter to. | ||
* @param {string} props.id Unique id for this duotone filter. | ||
* @param {Values} props.values R, G, and B values to filter with. | ||
* @return {WPElement} Duotone element. | ||
*/ | ||
export default function DuotoneFilter( { selector, id, values } ) { | ||
const stylesheet = ` | ||
${ selector } { | ||
filter: url( #${ id } ); | ||
} | ||
`; | ||
|
||
return ( | ||
<> | ||
<SVG | ||
xmlnsXlink="http://www.w3.org/1999/xlink" | ||
viewBox="0 0 0 0" | ||
width="0" | ||
height="0" | ||
focusable="false" | ||
role="none" | ||
style={ { | ||
visibility: 'hidden', | ||
position: 'absolute', | ||
left: '-9999px', | ||
overflow: 'hidden', | ||
} } | ||
> | ||
<defs> | ||
<filter id={ id }> | ||
<feColorMatrix | ||
type="matrix" | ||
// Use perceptual brightness to convert to grayscale. | ||
// prettier-ignore | ||
values=".299 .587 .114 0 0 | ||
.299 .587 .114 0 0 | ||
.299 .587 .114 0 0 | ||
0 0 0 1 0" | ||
/> | ||
<feComponentTransfer | ||
// Use sRGB instead of linearRGB to be consistent with how CSS gradients work. | ||
colorInterpolationFilters="sRGB" | ||
> | ||
<feFuncR | ||
type="table" | ||
tableValues={ values.r.join( ' ' ) } | ||
/> | ||
<feFuncG | ||
type="table" | ||
tableValues={ values.g.join( ' ' ) } | ||
/> | ||
<feFuncB | ||
type="table" | ||
tableValues={ values.b.join( ' ' ) } | ||
/> | ||
</feComponentTransfer> | ||
</filter> | ||
</defs> | ||
</SVG> | ||
<style dangerouslySetInnerHTML={ { __html: stylesheet } } /> | ||
</> | ||
); | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/block-editor/src/components/duotone-toolbar/custom-duotone-bar.js
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,45 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __experimentalCustomGradientBar as CustomGradientBar } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
getColorStopsFromValues, | ||
getCustomDuotoneIdFromColorStops, | ||
getGradientFromValues, | ||
getValuesFromColorStops, | ||
} from './utils'; | ||
|
||
const PLACEHOLDER_VALUES = { | ||
r: [ 0.2, 0.8 ], | ||
g: [ 0.2, 0.8 ], | ||
b: [ 0.2, 0.8 ], | ||
}; | ||
|
||
export default function CustomDuotoneBar( { value, onChange } ) { | ||
const hasGradient = !! value?.values; | ||
const values = hasGradient ? value.values : PLACEHOLDER_VALUES; | ||
const background = getGradientFromValues( values ); | ||
const controlPoints = getColorStopsFromValues( values ); | ||
return ( | ||
<div className="components-custom-duotone-picker"> | ||
<CustomGradientBar | ||
disableInserter | ||
disableAlpha | ||
background={ background } | ||
hasGradient={ hasGradient } | ||
value={ controlPoints } | ||
onChange={ ( newColorStops ) => { | ||
const newDuotone = { | ||
id: getCustomDuotoneIdFromColorStops( newColorStops ), | ||
values: getValuesFromColorStops( newColorStops ), | ||
}; | ||
onChange( newDuotone ); | ||
} } | ||
/> | ||
</div> | ||
); | ||
} |
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.
Why is this thing not absorbed in the block support?
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.
It's needed later in the image block render and in any block that wants to implement duotone without block supports
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 was thinking that ideally, all blocks should rely on the block support and don't implement this adhoc. Is this possible?
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.
Yes, if we go with what you said in #26751 (comment), we can make that happen. 👍
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.
If this is getting moved exclusively to block supports, you can start reviewing the blocks supports parts unique to https://github.com/WordPress/gutenberg/pull/26752/files#diff-fd626ca219f3d805667d480eabb2d890a5a8d2e015b37f03cf8980edf1ae3d58 and WordPress/wordpress-develop#984