From 935f69a1915d4744888d5b041366802fad4ee348 Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 10 Aug 2023 14:18:21 +0300 Subject: [PATCH] Use context instead to filter --- .../src/components/block-preview/auto.js | 7 +- .../src/components/editor-styles/index.js | 64 +++-- packages/block-editor/src/hooks/duotone.js | 47 +--- .../components/sidebar-block-editor/index.js | 21 +- .../src/components/visual-editor/index.js | 29 +- .../components/block-editor/editor-canvas.js | 31 ++- .../src/components/global-styles/preview.js | 260 +++++++++--------- .../src/components/revisions/index.js | 33 +-- .../src/components/style-book/index.js | 59 ++-- .../index.js | 17 +- 10 files changed, 295 insertions(+), 273 deletions(-) diff --git a/packages/block-editor/src/components/block-preview/auto.js b/packages/block-editor/src/components/block-preview/auto.js index 8972370cac6897..5fe3b16043890a 100644 --- a/packages/block-editor/src/components/block-preview/auto.js +++ b/packages/block-editor/src/components/block-preview/auto.js @@ -108,9 +108,10 @@ function ScaledBlockPreview( { : minHeight, } } > - - { contentResizeListener } - + + { contentResizeListener } + + ); diff --git a/packages/block-editor/src/components/editor-styles/index.js b/packages/block-editor/src/components/editor-styles/index.js index cfbe8bfebc3e24..980bea98b149f1 100644 --- a/packages/block-editor/src/components/editor-styles/index.js +++ b/packages/block-editor/src/components/editor-styles/index.js @@ -9,18 +9,23 @@ import a11yPlugin from 'colord/plugins/a11y'; * WordPress dependencies */ import { SVG } from '@wordpress/components'; -import { useCallback, useMemo } from '@wordpress/element'; -import { useSelect } from '@wordpress/data'; +import { + useCallback, + useMemo, + createContext, + useState, +} from '@wordpress/element'; /** * Internal dependencies */ import transformStyles from '../../utils/transform-styles'; -import { store as blockEditorStore } from '../../store'; const EDITOR_STYLES_SELECTOR = '.editor-styles-wrapper'; extend( [ namesPlugin, a11yPlugin ] ); +export const updateStyleContext = createContext(); + function useDarkThemeBodyClassName( styles ) { return useCallback( ( node ) => { @@ -69,18 +74,42 @@ function useDarkThemeBodyClassName( styles ) { ); } -export default function EditorStyles( { styles } ) { - const settings = useSelect( - ( select ) => select( blockEditorStore ).getSettings().styles, - [] - ); - const stylesArray = useMemo( - () => [ - ...Object.values( settings ?? [] ), - ...Object.values( styles ?? [] ), - ], - [ styles, settings ] - ); +export default function EditorStyles( { styles, children } ) { + const [ overrides, setOverrides ] = useState( [] ); + const updateStyle = useCallback( ( style ) => { + setOverrides( ( _overrides ) => { + const index = _overrides.findIndex( + ( override ) => override.id === style.id + ); + if ( index === -1 ) { + return [ ..._overrides, style ]; + } + return [ + ..._overrides.slice( 0, index ), + style, + ..._overrides.slice( index + 1 ), + ]; + } ); + return () => { + setOverrides( ( _overrides ) => + _overrides.filter( ( override ) => override.id !== style.id ) + ); + }; + }, [] ); + const stylesArray = useMemo( () => { + const _styles = Object.values( styles ?? [] ); + + for ( const override of overrides ) { + const index = _styles.findIndex( ( { id } ) => id === override.id ); + if ( index === -1 ) { + _styles.push( override ); + } else { + _styles[ index ] = override; + } + } + + return _styles; + }, [ styles, overrides ] ); const transformedStyles = useMemo( () => transformStyles( @@ -100,7 +129,7 @@ export default function EditorStyles( { styles } ) { ); return ( - <> + { /* Use an empty style element to have a document reference, but this could be any element. */ } - { children } + + + { children } + ); } diff --git a/packages/edit-site/src/components/global-styles/preview.js b/packages/edit-site/src/components/global-styles/preview.js index 1bbc2b2f4d59e8..9fe42d2b2b4e32 100644 --- a/packages/edit-site/src/components/global-styles/preview.js +++ b/packages/edit-site/src/components/global-styles/preview.js @@ -124,158 +124,164 @@ const StylesPreview = ( { label, isFocused, withHoverView } ) => { onMouseLeave={ () => setIsHovered( false ) } tabIndex={ -1 } > - - + - - - Aa - - - { highlightedColors.map( - ( { slug, color }, index ) => ( - + Aa + + + { highlightedColors.map( + ( { slug, color }, index ) => ( + + ) + ) } + + + + + + { paletteColors + .slice( 0, 4 ) + .map( ( { color }, index ) => ( +
- ) - ) } - - - - - + + - { paletteColors - .slice( 0, 4 ) - .map( ( { color }, index ) => ( + + { label && (
- ) ) } - - - - - { label && ( -
- { label } -
- ) } -
+ > + { label } +
+ ) } +
+
- + ) } diff --git a/packages/edit-site/src/components/revisions/index.js b/packages/edit-site/src/components/revisions/index.js index 4757c9b27213ea..7fb70c1e3f2a2b 100644 --- a/packages/edit-site/src/components/revisions/index.js +++ b/packages/edit-site/src/components/revisions/index.js @@ -80,22 +80,23 @@ function Revisions( { onClose, userConfig, blocks } ) { name="revisions" tabIndex={ 0 } > - - - - - - - + + + + + + + + ); diff --git a/packages/edit-site/src/components/style-book/index.js b/packages/edit-site/src/components/style-book/index.js index b6931b8e656653..6053517171d24b 100644 --- a/packages/edit-site/src/components/style-book/index.js +++ b/packages/edit-site/src/components/style-book/index.js @@ -304,35 +304,38 @@ const StyleBookBody = ( { tabIndex={ 0 } { ...( onClick ? buttonModeProps : {} ) } > - - - 600, - } ) } - examples={ examples } - category={ category } - label={ - title - ? sprintf( - // translators: %s: Category of blocks, e.g. Text. - __( 'Examples of blocks in the %s category' ), - title - ) - : __( 'Examples of blocks' ) - } - isSelected={ isSelected } - onSelect={ onSelect } - /> + STYLE_BOOK_IFRAME_STYLES + + buttonModeStyles + } + + 600, + } ) } + examples={ examples } + category={ category } + label={ + title + ? sprintf( + // translators: %s: Category of blocks, e.g. Text. + __( + 'Examples of blocks in the %s category' + ), + title + ) + : __( 'Examples of blocks' ) + } + isSelected={ isSelected } + onSelect={ onSelect } + /> + ); }; diff --git a/packages/edit-widgets/src/components/widget-areas-block-editor-content/index.js b/packages/edit-widgets/src/components/widget-areas-block-editor-content/index.js index 386d4730e4dab4..fdf49226fb7925 100644 --- a/packages/edit-widgets/src/components/widget-areas-block-editor-content/index.js +++ b/packages/edit-widgets/src/components/widget-areas-block-editor-content/index.js @@ -40,14 +40,15 @@ export default function WidgetAreasBlockEditorContent( { - - - - - - - - + + + + + + + + +
);