diff --git a/packages/block-editor/src/components/block-settings/container.native.js b/packages/block-editor/src/components/block-settings/container.native.js index e9f16b185a5564..7f9d8a9073d398 100644 --- a/packages/block-editor/src/components/block-settings/container.native.js +++ b/packages/block-editor/src/components/block-settings/container.native.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { InspectorControls } from '@wordpress/block-editor'; +import { InspectorControls, useSetting } from '@wordpress/block-editor'; import { BottomSheet, ColorSettings, @@ -29,6 +29,11 @@ function BottomSheetSettings( { settings, ...props } ) { + const colorSettings = { + colors: useSetting( 'color.palette' ) || settings.colors, + gradients: useSetting( 'color.gradients' ) || settings.gradients, + }; + return ( - + { const PADDING = 12; @@ -84,3 +159,37 @@ describe( 'getBlockColors', () => { ); } ); } ); + +describe( 'parseColorVariables', () => { + it( 'returns the parsed colors values correctly', () => { + const blockColors = parseColorVariables( + JSON.stringify( DEFAULT_GLOBAL_STYLES ), + GLOBAL_STYLES_PALETTE + ); + expect( blockColors ).toEqual( + expect.objectContaining( PARSED_GLOBAL_STYLES ) + ); + } ); +} ); + +describe( 'getGlobalStyles', () => { + it( 'returns the global styles data correctly', () => { + const globalStyles = getGlobalStyles( + JSON.stringify( DEFAULT_GLOBAL_STYLES ), + JSON.stringify( DEFAULT_FEATURES ) + ); + const gradients = parseColorVariables( + JSON.stringify( DEFAULT_FEATURES ), + GLOBAL_STYLES_PALETTE + )?.color?.gradients; + + expect( globalStyles ).toEqual( + expect.objectContaining( { + __experimentalFeatures: { + color: { palette: GLOBAL_STYLES_PALETTE, gradients }, + }, + __experimentalGlobalStylesBaseStyles: PARSED_GLOBAL_STYLES, + } ) + ); + } ); +} ); diff --git a/packages/components/src/mobile/global-styles-context/utils.native.js b/packages/components/src/mobile/global-styles-context/utils.native.js index c8e2bce0c91f8a..35dc09f98b581d 100644 --- a/packages/components/src/mobile/global-styles-context/utils.native.js +++ b/packages/components/src/mobile/global-styles-context/utils.native.js @@ -66,3 +66,38 @@ export function getBlockColors( blockStyleAttributes, defaultColors ) { return blockStyles; } + +export function parseColorVariables( styles, colorPalette ) { + const stylesBase = styles; + const colorPrefixRegex = /var\(--wp--preset--color--(.*?)\)/g; + + return stylesBase + ? JSON.parse( + stylesBase?.replace( colorPrefixRegex, ( _$1, $2 ) => { + const mappedColor = find( colorPalette, { + slug: $2, + } ); + return mappedColor?.color; + } ) + ) + : styles; +} + +export function getGlobalStyles( rawStyles, rawFeatures ) { + const features = JSON.parse( rawFeatures ); + const palette = features?.color?.palette; + const gradients = parseColorVariables( rawFeatures, palette )?.color + ?.gradients; + const globalStyles = parseColorVariables( rawStyles, palette ); + + return { + ...( palette || gradients + ? { + __experimentalFeatures: { + color: { palette, gradients }, + }, + } + : {} ), + __experimentalGlobalStylesBaseStyles: globalStyles, + }; +} diff --git a/packages/editor/src/components/provider/index.native.js b/packages/editor/src/components/provider/index.native.js index 421d930e17e16a..1f76a311ffaad6 100644 --- a/packages/editor/src/components/provider/index.native.js +++ b/packages/editor/src/components/provider/index.native.js @@ -1,6 +1,3 @@ -/** - * External dependencies - */ /** * WordPress dependencies */ @@ -15,10 +12,6 @@ import RNReactNativeGutenbergBridge, { subscribeUpdateCapabilities, subscribeShowNotice, } from '@wordpress/react-native-bridge'; - -/** - * WordPress dependencies - */ import { Component } from '@wordpress/element'; import { count as wordCount } from '@wordpress/wordcount'; import { @@ -35,6 +28,7 @@ import { validateThemeGradients, store as blockEditorStore, } from '@wordpress/block-editor'; +import { getGlobalStyles } from '@wordpress/components'; const postTypeEntities = [ { name: 'post', baseURL: '/wp/v2/posts' }, @@ -128,15 +122,22 @@ class NativeEditorProvider extends Component { this.subscriptionParentUpdateEditorSettings = subscribeUpdateEditorSettings( ( editorSettings ) => { - // Reset the colors and gradients in case one theme was set with custom items and then updated to a theme without custom elements. - editorSettings.colors = validateThemeColors( - editorSettings.colors - ); - editorSettings.gradients = validateThemeGradients( - editorSettings.gradients - ); - - this.props.updateSettings( editorSettings ); + const { + colors: updatedColors, + gradients: updatedGradients, + rawFeatures, + rawStyles, + } = editorSettings; + const updatedSettings = { + // Reset the colors and gradients in case one theme was set with custom items and then updated to a theme without custom elements. + colors: validateThemeColors( updatedColors ), + gradients: validateThemeGradients( updatedGradients ), + ...( rawStyles + ? getGlobalStyles( rawStyles, rawFeatures ) + : {} ), + }; + + this.props.updateSettings( updatedSettings ); } );