Skip to content

Commit

Permalink
Block editor: remove __unstableElementContext and filter `EditorSty…
Browse files Browse the repository at this point in the history
…les` instead (#52888)
  • Loading branch information
ellatrix authored Sep 14, 2023
1 parent f92f9e2 commit a8c5605
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 206 deletions.
133 changes: 0 additions & 133 deletions packages/block-editor/src/components/duotone/components.js

This file was deleted.

7 changes: 0 additions & 7 deletions packages/block-editor/src/components/duotone/index.js

This file was deleted.

65 changes: 65 additions & 0 deletions packages/block-editor/src/components/duotone/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,68 @@ export function getValuesFromColors( colors = [] ) {

return values;
}

/**
* Stylesheet for disabling a global styles duotone filter.
*
* @param {string} selector Selector to disable the filter for.
*
* @return {string} Filter none style.
*/
export function getDuotoneUnsetStylesheet( selector ) {
return `${ selector }{filter:none}`;
}

/**
* SVG and stylesheet needed for rendering the duotone filter.
*
* @param {string} selector Selector to apply the filter to.
* @param {string} id Unique id for this duotone filter.
*
* @return {string} Duotone filter style.
*/
export function getDuotoneStylesheet( selector, id ) {
return `${ selector }{filter:url(#${ id })}`;
}

/**
* The SVG part of the duotone filter.
*
* @param {string} id Unique id for this duotone filter.
* @param {string[]} colors Color strings from dark to light.
*
* @return {string} Duotone SVG.
*/
export function getDuotoneFilter( id, colors ) {
const values = getValuesFromColors( colors );
return `
<svg
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 0 0"
width="0"
height="0"
focusable="false"
role="none"
aria-hidden="true"
style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
>
<defs>
<filter id="${ id }">
<!--
Use sRGB instead of linearRGB so transparency looks correct.
Use perceptual brightness to convert to grayscale.
-->
<feColorMatrix color-interpolation-filters="sRGB" type="matrix" values=" .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 "></feColorMatrix>
<!-- Use sRGB instead of linearRGB to be consistent with how CSS gradients work. -->
<feComponentTransfer color-interpolation-filters="sRGB">
<feFuncR type="table" tableValues="${ values.r.join( ' ' ) }"></feFuncR>
<feFuncG type="table" tableValues="${ values.g.join( ' ' ) }"></feFuncG>
<feFuncB type="table" tableValues="${ values.b.join( ' ' ) }"></feFuncB>
<feFuncA type="table" tableValues="${ values.a.join( ' ' ) }"></feFuncA>
</feComponentTransfer>
<!-- Re-mask the image with the original transparency since the feColorMatrix above loses that information. -->
<feComposite in2="SourceGraphic" operator="in"></feComposite>
</filter>
</defs>
</svg>`;
}
42 changes: 27 additions & 15 deletions packages/block-editor/src/components/editor-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import a11yPlugin from 'colord/plugins/a11y';
*/
import { SVG } from '@wordpress/components';
import { useCallback, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import transformStyles from '../../utils/transform-styles';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

extend( [ namesPlugin, a11yPlugin ] );

Expand Down Expand Up @@ -65,33 +68,42 @@ function useDarkThemeBodyClassName( styles, scope ) {
}

export default function EditorStyles( { styles, scope } ) {
const stylesArray = useMemo(
() => Object.values( styles ?? [] ),
[ styles ]
const overrides = useSelect(
( select ) => unlock( select( blockEditorStore ) ).getStyleOverrides(),
[]
);
const transformedStyles = useMemo(
() =>
const [ transformedStyles, transformedSvgs ] = useMemo( () => {
const _styles = Object.values( styles ?? [] );

for ( const [ id, override ] of overrides ) {
const index = _styles.findIndex( ( { id: _id } ) => id === _id );
const overrideWithId = { ...override, id };
if ( index === -1 ) {
_styles.push( overrideWithId );
} else {
_styles[ index ] = overrideWithId;
}
}

return [
transformStyles(
stylesArray.filter( ( style ) => style?.css ),
_styles.filter( ( style ) => style?.css ),
scope
),
[ stylesArray, scope ]
);

const transformedSvgs = useMemo(
() =>
stylesArray
_styles
.filter( ( style ) => style.__unstableType === 'svgs' )
.map( ( style ) => style.assets )
.join( '' ),
[ stylesArray ]
);
];
}, [ styles, overrides, scope ] );

return (
<>
{ /* Use an empty style element to have a document reference,
but this could be any element. */ }
<style ref={ useDarkThemeBodyClassName( stylesArray, scope ) } />
<style
ref={ useDarkThemeBodyClassName( transformedStyles, scope ) }
/>
{ transformedStyles.map( ( css, index ) => (
<style key={ index }>{ css }</style>
) ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
store as blocksStore,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { renderToString, useContext, useMemo } from '@wordpress/element';
import { useContext, useMemo } from '@wordpress/element';
import { getCSSRules } from '@wordpress/style-engine';

/**
Expand All @@ -23,7 +23,7 @@ import {
} from './typography-utils';
import { GlobalStylesContext } from './context';
import { useGlobalSetting } from './hooks';
import { PresetDuotoneFilter } from '../duotone/components';
import { getDuotoneFilter } from '../duotone/utils';
import { getGapCSSValue } from '../../hooks/gap';
import { store as blockEditorStore } from '../../store';
import { LAYOUT_DEFINITIONS } from '../../layouts/definitions';
Expand Down Expand Up @@ -164,11 +164,9 @@ function getPresetsSvgFilters( blockPresets = {} ) {
.filter( ( origin ) => presetByOrigin[ origin ] )
.flatMap( ( origin ) =>
presetByOrigin[ origin ].map( ( preset ) =>
renderToString(
<PresetDuotoneFilter
preset={ preset }
key={ preset.slug }
/>
getDuotoneFilter(
`wp-duotone-${ preset.slug }`,
preset.colors
)
)
)
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
export * from './colors';
export * from './gradients';
export * from './font-sizes';
export * from './duotone';
export { AlignmentControl, AlignmentToolbar } from './alignment-control';
export { default as Autocomplete } from './autocomplete';
export {
Expand Down
Loading

1 comment on commit a8c5605

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in a8c5605.
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/6186239919
📝 Reported issues:

Please sign in to comment.