From 7102a514094a2f92dcf38f6b2ad6cd2c91c27972 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Tue, 6 Apr 2021 22:05:43 +0300 Subject: [PATCH 01/16] Adds new Prop for Global Styles Settings --- .../java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt index 5e1e712cb414ab..7b4841e0402770 100644 --- a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt +++ b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt @@ -33,6 +33,8 @@ data class GutenbergProps @JvmOverloads constructor( editorTheme?.also { theme -> theme.getSerializable(PROP_COLORS)?.let { putSerializable(PROP_COLORS, it) } theme.getSerializable(PROP_GRADIENTS)?.let { putSerializable(PROP_GRADIENTS, it) } + theme.getSerializable(PROP_GLOBAL_STYLES) + ?.let { putSerializable(PROP_GLOBAL_STYLES, it) } } } @@ -63,6 +65,7 @@ data class GutenbergProps @JvmOverloads constructor( private const val PROP_TRANSLATIONS = "translations" private const val PROP_COLORS = "colors" private const val PROP_GRADIENTS = "gradients" + private const val PROP_GLOBAL_STYLES = "rawGlobalStylesBaseStyles" const val PROP_CAPABILITIES = "capabilities" const val PROP_CAPABILITIES_CONTACT_INFO_BLOCK = "contactInfoBlock" From 906a9f66e12a3637b31772e79d563eadf399daf2 Mon Sep 17 00:00:00 2001 From: Chip Date: Thu, 6 May 2021 15:10:36 -0400 Subject: [PATCH 02/16] Rename updateTheme to be more generic for update settings include GSS (#31566) --- .../src/components/provider/index.native.js | 24 ++++++----- .../RNReactNativeGutenbergBridgeModule.java | 4 +- packages/react-native-bridge/index.js | 7 +++- .../react-native-bridge/ios/Gutenberg.swift | 42 +++++++++++-------- .../ios/GutenbergBridgeDataSource.swift | 5 ++- .../ios/RNReactNativeGutenbergBridge.swift | 2 +- test/native/setup.js | 2 +- 7 files changed, 50 insertions(+), 36 deletions(-) diff --git a/packages/editor/src/components/provider/index.native.js b/packages/editor/src/components/provider/index.native.js index 93f589bc7d9f47..421d930e17e16a 100644 --- a/packages/editor/src/components/provider/index.native.js +++ b/packages/editor/src/components/provider/index.native.js @@ -11,7 +11,7 @@ import RNReactNativeGutenbergBridge, { subscribeSetTitle, subscribeMediaAppend, subscribeReplaceBlock, - subscribeUpdateTheme, + subscribeUpdateEditorSettings, subscribeUpdateCapabilities, subscribeShowNotice, } from '@wordpress/react-native-bridge'; @@ -126,15 +126,17 @@ class NativeEditorProvider extends Component { } ); - this.subscriptionParentUpdateTheme = subscribeUpdateTheme( - ( theme ) => { + 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. - - theme.colors = validateThemeColors( theme.colors ); - - theme.gradients = validateThemeGradients( theme.gradients ); - - this.props.updateSettings( theme ); + editorSettings.colors = validateThemeColors( + editorSettings.colors + ); + editorSettings.gradients = validateThemeGradients( + editorSettings.gradients + ); + + this.props.updateSettings( editorSettings ); } ); @@ -176,8 +178,8 @@ class NativeEditorProvider extends Component { this.subscriptionParentMediaAppend.remove(); } - if ( this.subscriptionParentUpdateTheme ) { - this.subscriptionParentUpdateTheme.remove(); + if ( this.subscriptionParentUpdateEditorSettings ) { + this.subscriptionParentUpdateEditorSettings.remove(); } if ( this.subscriptionParentUpdateCapabilities ) { diff --git a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java index 7cdd5d9c33cee1..61bd2e5dee17d6 100644 --- a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java +++ b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java @@ -44,7 +44,7 @@ public class RNReactNativeGutenbergBridgeModule extends ReactContextBaseJavaModu private static final String EVENT_NAME_NOTIFY_MODAL_CLOSED = "notifyModalClosed"; private static final String EVENT_NAME_PREFERRED_COLOR_SCHEME = "preferredColorScheme"; private static final String EVENT_NAME_MEDIA_REPLACE_BLOCK = "replaceBlock"; - private static final String EVENT_NAME_UPDATE_THEME = "updateTheme"; + private static final String EVENT_NAME_UPDATE_EDITOR_SETTINGS = "updateEditorSettings"; private static final String EVENT_NAME_SHOW_NOTICE = "showNotice"; private static final String MAP_KEY_UPDATE_HTML = "html"; @@ -153,7 +153,7 @@ public void updateTheme(@Nullable Bundle editorTheme) { writableMap.putArray(MAP_KEY_THEME_UPDATE_GRADIENTS, Arguments.fromList((ArrayList)gradients)); } - emitToJS(EVENT_NAME_UPDATE_THEME, writableMap); + emitToJS(EVENT_NAME_UPDATE_EDITOR_SETTINGS, writableMap); } @ReactMethod diff --git a/packages/react-native-bridge/index.js b/packages/react-native-bridge/index.js index 73099bd1eff3db..6a5e5c3ca80b84 100644 --- a/packages/react-native-bridge/index.js +++ b/packages/react-native-bridge/index.js @@ -125,8 +125,11 @@ export function subscribeAndroidModalClosed( callback ) { : undefined; } -export function subscribeUpdateTheme( callback ) { - return gutenbergBridgeEvents.addListener( 'updateTheme', callback ); +export function subscribeEditorSettingsTheme( callback ) { + return gutenbergBridgeEvents.addListener( + 'updateEditorSettings', + callback + ); } export function subscribePreferredColorScheme( callback ) { diff --git a/packages/react-native-bridge/ios/Gutenberg.swift b/packages/react-native-bridge/ios/Gutenberg.swift index d6cc7605385125..755cdade135add 100644 --- a/packages/react-native-bridge/ios/Gutenberg.swift +++ b/packages/react-native-bridge/ios/Gutenberg.swift @@ -79,13 +79,18 @@ public class Gutenberg: NSObject { initialProps["capabilities"] = capabilities.toJSPayload() } - let editorTheme = dataSource.gutenbergEditorTheme() - if let colors = editorTheme?.colors { - initialProps["colors"] = colors - } - - if let gradients = editorTheme?.gradients { - initialProps["gradients"] = gradients + let editorSettings = dataSource.gutenbergEditorSettings() + if let rawGlobalStylesBaseStyles = editorSettings?.rawGlobalStylesBaseStyles { + initialProps["rawGlobalStylesBaseStyles"] = rawGlobalStylesBaseStyles + } else { + // We only need to include Colors and Gradients here if the Global Styles data wasn't provided + if let colors = editorSettings?.colors { + initialProps["colors"] = colors + } + + if let gradients = editorSettings?.gradients { + initialProps["gradients"] = gradients + } } return initialProps @@ -184,19 +189,22 @@ public class Gutenberg: NSObject { bridgeModule.sendEventIfNeeded(.setFocusOnTitle, body: nil) } - public func updateTheme(_ editorTheme: GutenbergEditorTheme?) { - - var themeUpdates = [String : Any]() - - if let colors = editorTheme?.colors { - themeUpdates["colors"] = colors - } + public func updateEditorSettings(_ editorSettings: GutenbergEditorSettings?) { + var settingsUpdates = [String : Any]() + if let rawGlobalStylesBaseStyles = editorSettings?.rawGlobalStylesBaseStyles { + settingsUpdates["rawGlobalStylesBaseStyles"] = rawGlobalStylesBaseStyles + } else { + // We only need to include Colors and Gradients here if the Global Styles data wasn't provided + if let colors = editorSettings?.colors { + settingsUpdates["colors"] = colors + } - if let gradients = editorTheme?.gradients { - themeUpdates["gradients"] = gradients + if let gradients = editorSettings?.gradients { + settingsUpdates["gradients"] = gradients + } } - sendEvent(.updateTheme, body:themeUpdates) + sendEvent(.updateEditorSettings, body: settingsUpdates) } public func showNotice(_ message: String) { diff --git a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift index d6023f4d1508d7..a6f1cb2998123c 100644 --- a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift +++ b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift @@ -46,7 +46,7 @@ public protocol GutenbergBridgeDataSource: class { func gutenbergCapabilities() -> [Capabilities: Bool] /// Asks the data source for a list of theme colors. - func gutenbergEditorTheme() -> GutenbergEditorTheme? + func gutenbergEditorSettings() -> GutenbergEditorSettings? /// Asks the data source for a view to show while the Editor is loading. var loadingView: UIView? { get } @@ -70,7 +70,8 @@ public extension GutenbergBridgeDataSource { } } -public protocol GutenbergEditorTheme { +public protocol GutenbergEditorSettings { + var rawGlobalStylesBaseStyles: String? { get } var colors: [[String: String]]? { get } var gradients: [[String: String]]? { get } } diff --git a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift index 82f19d50a5a154..2b57384df96109 100644 --- a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift +++ b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift @@ -369,7 +369,7 @@ extension RNReactNativeGutenbergBridge { case mediaUpload case setFocusOnTitle case mediaAppend - case updateTheme + case updateEditorSettings case replaceBlock case updateCapabilities case showNotice diff --git a/test/native/setup.js b/test/native/setup.js index 9c89c8845d999a..47e584ce6406d3 100644 --- a/test/native/setup.js +++ b/test/native/setup.js @@ -34,7 +34,7 @@ jest.mock( '@wordpress/react-native-bridge', () => { subscribeFeaturedImageIdNativeUpdated: jest.fn(), subscribeMediaAppend: jest.fn(), subscribeAndroidModalClosed: jest.fn(), - subscribeUpdateTheme: jest.fn(), + subscribeUpdateEditorSettings: jest.fn(), subscribePreferredColorScheme: () => 'light', subscribeUpdateCapabilities: jest.fn(), subscribeShowNotice: jest.fn(), From 226780b30d5b2b3e0774a4a862cd1d03aab22565 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Fri, 7 May 2021 15:28:27 +0200 Subject: [PATCH 03/16] Fix wrong naming of subscribeUpdateEditorSettings --- packages/react-native-bridge/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-bridge/index.js b/packages/react-native-bridge/index.js index 6a5e5c3ca80b84..b2443dc36cc829 100644 --- a/packages/react-native-bridge/index.js +++ b/packages/react-native-bridge/index.js @@ -125,7 +125,7 @@ export function subscribeAndroidModalClosed( callback ) { : undefined; } -export function subscribeEditorSettingsTheme( callback ) { +export function subscribeUpdateEditorSettings( callback ) { return gutenbergBridgeEvents.addListener( 'updateEditorSettings', callback From 8ad3a37f6b8434c2d38d6dc6fd0972a5ae2c5285 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Fri, 7 May 2021 16:54:00 +0200 Subject: [PATCH 04/16] Update Gutenberg demo --- .../ios/GutenbergDemo/GutenbergViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift b/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift index 162fb297c28f07..0adbc4569660c7 100644 --- a/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift +++ b/packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift @@ -294,7 +294,7 @@ extension GutenbergViewController: GutenbergBridgeDataSource { return ExampleAttachmentDelegate() } - func gutenbergEditorTheme() -> GutenbergEditorTheme? { + func gutenbergEditorSettings() -> GutenbergEditorSettings? { return nil } From 01321341e3d15e57f0aa578aec132038e4462717 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 13 May 2021 23:18:28 +0300 Subject: [PATCH 05/16] Updates new props to rawStyles and rawFeatures --- .../org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt index 7b4841e0402770..2f1d4b2993989f 100644 --- a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt +++ b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt @@ -33,8 +33,10 @@ data class GutenbergProps @JvmOverloads constructor( editorTheme?.also { theme -> theme.getSerializable(PROP_COLORS)?.let { putSerializable(PROP_COLORS, it) } theme.getSerializable(PROP_GRADIENTS)?.let { putSerializable(PROP_GRADIENTS, it) } - theme.getSerializable(PROP_GLOBAL_STYLES) - ?.let { putSerializable(PROP_GLOBAL_STYLES, it) } + theme.getSerializable(PROP_STYLES) + ?.let { putSerializable(PROP_STYLES, it) } + theme.getSerializable(PROP_FEATURES) + ?.let { putSerializable(PROP_FEATURES, it) } } } @@ -65,7 +67,8 @@ data class GutenbergProps @JvmOverloads constructor( private const val PROP_TRANSLATIONS = "translations" private const val PROP_COLORS = "colors" private const val PROP_GRADIENTS = "gradients" - private const val PROP_GLOBAL_STYLES = "rawGlobalStylesBaseStyles" + private const val PROP_STYLES = "rawStyles" + private const val PROP_FEATURES = "rawFeatures" const val PROP_CAPABILITIES = "capabilities" const val PROP_CAPABILITIES_CONTACT_INFO_BLOCK = "contactInfoBlock" From bf75be199696aa131e1d3dc4397c67b2f8c0e3a6 Mon Sep 17 00:00:00 2001 From: Chip Snyder Date: Fri, 14 May 2021 16:22:22 -0400 Subject: [PATCH 06/16] Update iOS Bridge for new values --- .../react-native-bridge/ios/Gutenberg.swift | 48 ++++++++++--------- .../ios/GutenbergBridgeDataSource.swift | 4 +- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/packages/react-native-bridge/ios/Gutenberg.swift b/packages/react-native-bridge/ios/Gutenberg.swift index 755cdade135add..b23b6b5dc4ca62 100644 --- a/packages/react-native-bridge/ios/Gutenberg.swift +++ b/packages/react-native-bridge/ios/Gutenberg.swift @@ -80,17 +80,9 @@ public class Gutenberg: NSObject { } let editorSettings = dataSource.gutenbergEditorSettings() - if let rawGlobalStylesBaseStyles = editorSettings?.rawGlobalStylesBaseStyles { - initialProps["rawGlobalStylesBaseStyles"] = rawGlobalStylesBaseStyles - } else { - // We only need to include Colors and Gradients here if the Global Styles data wasn't provided - if let colors = editorSettings?.colors { - initialProps["colors"] = colors - } - - if let gradients = editorSettings?.gradients { - initialProps["gradients"] = gradients - } + let settingsUpdates = properties(from: editorSettings) + initialProps.merge(properties(from: editorSettings)) { (intialProp, settingsUpdates) -> Any in + settingsUpdates } return initialProps @@ -190,21 +182,31 @@ public class Gutenberg: NSObject { } public func updateEditorSettings(_ editorSettings: GutenbergEditorSettings?) { + let settingsUpdates = properties(from: editorSettings) + sendEvent(.updateEditorSettings, body: settingsUpdates) + } + + private func properties(from editorSettings: GutenbergEditorSettings?) -> [String : Any] { var settingsUpdates = [String : Any]() - if let rawGlobalStylesBaseStyles = editorSettings?.rawGlobalStylesBaseStyles { - settingsUpdates["rawGlobalStylesBaseStyles"] = rawGlobalStylesBaseStyles - } else { - // We only need to include Colors and Gradients here if the Global Styles data wasn't provided - if let colors = editorSettings?.colors { - settingsUpdates["colors"] = colors - } - - if let gradients = editorSettings?.gradients { - settingsUpdates["gradients"] = gradients - } + settingsUpdates["isFSETheme"] = editorSettings?.isFSETheme ?? false + + if let rawStyles = editorSettings?.rawStyles { + settingsUpdates["rawStyles"] = rawStyles } - sendEvent(.updateEditorSettings, body: settingsUpdates) + if let rawFeatures = editorSettings?.rawFeatures { + settingsUpdates["rawFeatures"] = rawFeatures + } + + if let colors = editorSettings?.colors { + settingsUpdates["colors"] = colors + } + + if let gradients = editorSettings?.gradients { + settingsUpdates["gradients"] = gradients + } + + return settingsUpdates } public func showNotice(_ message: String) { diff --git a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift index a6f1cb2998123c..b082cbd60682df 100644 --- a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift +++ b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift @@ -71,7 +71,9 @@ public extension GutenbergBridgeDataSource { } public protocol GutenbergEditorSettings { - var rawGlobalStylesBaseStyles: String? { get } + var isFSETheme: Bool { get } + var rawStyles: String? { get } + var rawFeatures: String? { get } var colors: [[String: String]]? { get } var gradients: [[String: String]]? { get } } From aeaf65f43c4883c1955e8d75d7ec57269b5fd037 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Tue, 18 May 2021 17:08:19 +0200 Subject: [PATCH 07/16] Mobile - Global styles: Pass settings and set color palette and gradients (#30684) * Mobile - Read global styles and set color palette and gradients * Parse all color variables * Fix gradients * Update global styles mocked data * Move color settings * Removed added spaces * Add tests * Update experimental features path and prepare for rawGlobalStylesBaseStyles * Remove mock data and update code to use latest API changes --- .../block-settings/container.native.js | 9 +- packages/components/src/index.native.js | 1 + .../test/utils.native.js | 111 +++++++++++++++++- .../global-styles-context/utils.native.js | 35 ++++++ .../src/components/provider/index.native.js | 33 +++--- 5 files changed, 170 insertions(+), 19 deletions(-) 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 ); } ); From 32a2b3aff85ed83d2a3c953f94d4f37685b4f2ca Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 27 May 2021 14:35:19 +0300 Subject: [PATCH 08/16] Removes Android rawFeatures prop --- .../java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt index d89f0557cf9df3..b1ac2fbd5ce4ac 100644 --- a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt +++ b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt @@ -37,8 +37,6 @@ data class GutenbergProps @JvmOverloads constructor( theme.getSerializable(PROP_GRADIENTS)?.let { putSerializable(PROP_GRADIENTS, it) } theme.getSerializable(PROP_STYLES) ?.let { putSerializable(PROP_STYLES, it) } - theme.getSerializable(PROP_FEATURES) - ?.let { putSerializable(PROP_FEATURES, it) } } } @@ -72,7 +70,6 @@ data class GutenbergProps @JvmOverloads constructor( private const val PROP_COLORS = "colors" private const val PROP_GRADIENTS = "gradients" private const val PROP_STYLES = "rawStyles" - private const val PROP_FEATURES = "rawFeatures" const val PROP_CAPABILITIES = "capabilities" const val PROP_CAPABILITIES_CONTACT_INFO_BLOCK = "contactInfoBlock" From 9741f39592d8cc5613f7c2557cbf0647650c605c Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Thu, 27 May 2021 14:46:05 +0200 Subject: [PATCH 09/16] Mobile - Remove usage of rawFeatures --- .../test/utils.native.js | 42 +++++++++---------- .../global-styles-context/utils.native.js | 16 +++---- .../src/components/provider/index.native.js | 36 +++++++--------- 3 files changed, 42 insertions(+), 52 deletions(-) diff --git a/packages/components/src/mobile/global-styles-context/test/utils.native.js b/packages/components/src/mobile/global-styles-context/test/utils.native.js index f4df181fed9531..eb10086fffcb3c 100644 --- a/packages/components/src/mobile/global-styles-context/test/utils.native.js +++ b/packages/components/src/mobile/global-styles-context/test/utils.native.js @@ -32,6 +32,21 @@ const GLOBAL_STYLES_PALETTE = [ }, ]; +const GLOBAL_STYLES_GRADIENTS = [ + { + slug: 'purple-to-blue', + gradient: + 'linear-gradient(160deg, var(--wp--preset--color--purple), var(--wp--preset--color--blue))', + name: 'Purple to Blue', + }, + { + slug: 'green-to-purple', + gradient: + 'linear-gradient(160deg, var(--wp--preset--color--green), var(--wp--preset--color--purple))', + name: 'Green to Purple', + }, +]; + const DEFAULT_GLOBAL_STYLES = { styles: { color: { @@ -48,26 +63,6 @@ const DEFAULT_GLOBAL_STYLES = { }, }; -const DEFAULT_FEATURES = { - color: { - palette: GLOBAL_STYLES_PALETTE, - gradients: [ - { - slug: 'purple-to-blue', - gradient: - 'linear-gradient(160deg, var(--wp--preset--color--purple), var(--wp--preset--color--blue))', - name: 'Purple to Blue', - }, - { - slug: 'green-to-purple', - gradient: - 'linear-gradient(160deg, var(--wp--preset--color--green), var(--wp--preset--color--purple))', - name: 'Green to Purple', - }, - ], - }, -}; - const PARSED_GLOBAL_STYLES = { styles: { color: { @@ -176,12 +171,13 @@ describe( 'getGlobalStyles', () => { it( 'returns the global styles data correctly', () => { const globalStyles = getGlobalStyles( JSON.stringify( DEFAULT_GLOBAL_STYLES ), - JSON.stringify( DEFAULT_FEATURES ) + GLOBAL_STYLES_PALETTE, + GLOBAL_STYLES_GRADIENTS ); const gradients = parseColorVariables( - JSON.stringify( DEFAULT_FEATURES ), + JSON.stringify( GLOBAL_STYLES_GRADIENTS ), GLOBAL_STYLES_PALETTE - )?.color?.gradients; + ); expect( globalStyles ).toEqual( expect.objectContaining( { 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 35dc09f98b581d..a30162d0578f88 100644 --- a/packages/components/src/mobile/global-styles-context/utils.native.js +++ b/packages/components/src/mobile/global-styles-context/utils.native.js @@ -83,18 +83,18 @@ export function parseColorVariables( styles, colorPalette ) { : 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 ); +export function getGlobalStyles( rawStyles, colors, gradients ) { + const parsedGradients = parseColorVariables( + JSON.stringify( gradients ), + colors + ); + const globalStyles = parseColorVariables( rawStyles, colors ); return { - ...( palette || gradients + ...( colors || gradients ? { __experimentalFeatures: { - color: { palette, gradients }, + color: { palette: colors, gradients: parsedGradients }, }, } : {} ), diff --git a/packages/editor/src/components/provider/index.native.js b/packages/editor/src/components/provider/index.native.js index b560870f5c1a66..063a46ce9ddd50 100644 --- a/packages/editor/src/components/provider/index.native.js +++ b/packages/editor/src/components/provider/index.native.js @@ -81,13 +81,11 @@ class NativeEditorProvider extends Component { } componentDidMount() { - const { capabilities, colors, gradients } = this.props; + const { capabilities, updateSettings } = this.props; - this.props.updateSettings( { + updateSettings( { ...capabilities, - // Set theme colors for the editor - ...( colors ? { colors } : {} ), - ...( gradients ? { gradients } : {} ), + ...this.getThemeColors( this.props ), } ); this.subscriptionParentGetHtml = subscribeParentGetHtml( () => { @@ -136,22 +134,8 @@ class NativeEditorProvider extends Component { this.subscriptionParentUpdateEditorSettings = subscribeUpdateEditorSettings( ( 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 ); + const themeColors = this.getThemeColors( editorSettings ); + updateSettings( themeColors ); } ); @@ -206,6 +190,16 @@ class NativeEditorProvider extends Component { } } + getThemeColors( { colors, gradients, rawStyles } ) { + return { + colors: validateThemeColors( colors ) ?? [], + gradients: validateThemeGradients( gradients ) ?? [], + ...( rawStyles + ? getGlobalStyles( rawStyles, colors, gradients ) + : {} ), + }; + } + componentDidUpdate( prevProps ) { if ( ! prevProps.isReady && this.props.isReady ) { const blocks = this.props.blocks; From 9a404b49488d053997676b55b07eedf1e6157046 Mon Sep 17 00:00:00 2001 From: Chip Snyder Date: Thu, 27 May 2021 16:03:00 -0400 Subject: [PATCH 10/16] Remove rawFeatures --- packages/react-native-bridge/ios/Gutenberg.swift | 4 ---- .../react-native-bridge/ios/GutenbergBridgeDataSource.swift | 1 - 2 files changed, 5 deletions(-) diff --git a/packages/react-native-bridge/ios/Gutenberg.swift b/packages/react-native-bridge/ios/Gutenberg.swift index b23b6b5dc4ca62..a7942f3e1368fe 100644 --- a/packages/react-native-bridge/ios/Gutenberg.swift +++ b/packages/react-native-bridge/ios/Gutenberg.swift @@ -194,10 +194,6 @@ public class Gutenberg: NSObject { settingsUpdates["rawStyles"] = rawStyles } - if let rawFeatures = editorSettings?.rawFeatures { - settingsUpdates["rawFeatures"] = rawFeatures - } - if let colors = editorSettings?.colors { settingsUpdates["colors"] = colors } diff --git a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift index b082cbd60682df..59e7f7709e2dc3 100644 --- a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift +++ b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift @@ -73,7 +73,6 @@ public extension GutenbergBridgeDataSource { public protocol GutenbergEditorSettings { var isFSETheme: Bool { get } var rawStyles: String? { get } - var rawFeatures: String? { get } var colors: [[String: String]]? { get } var gradients: [[String: String]]? { get } } From 792ab77824f3bbdf2bdb1a0548f540fc3945ec5f Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 28 May 2021 11:39:44 +0300 Subject: [PATCH 11/16] Adds raw Styles in React Native Bridge --- .../RNReactNativeGutenbergBridgeModule.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java index 07172011548c20..7b7a6cee27051a 100644 --- a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java +++ b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/RNReactNativeGutenbergBridgeModule.java @@ -57,6 +57,7 @@ public class RNReactNativeGutenbergBridgeModule extends ReactContextBaseJavaModu public static final String MAP_KEY_MEDIA_FILE_UPLOAD_MEDIA_TYPE = "mediaType"; private static final String MAP_KEY_THEME_UPDATE_COLORS = "colors"; private static final String MAP_KEY_THEME_UPDATE_GRADIENTS = "gradients"; + private static final String MAP_KEY_THEME_UPDATE_RAW_STYLES = "rawStyles"; public static final String MAP_KEY_MEDIA_FINAL_SAVE_RESULT_SUCCESS_VALUE = "success"; private static final String MAP_KEY_IS_PREFERRED_COLOR_SCHEME_DARK = "isPreferredColorSchemeDark"; @@ -144,6 +145,7 @@ public void updateTheme(@Nullable Bundle editorTheme) { WritableMap writableMap = new WritableNativeMap(); Serializable colors = editorTheme.getSerializable(MAP_KEY_THEME_UPDATE_COLORS); Serializable gradients = editorTheme.getSerializable(MAP_KEY_THEME_UPDATE_GRADIENTS); + Serializable rawStyles = editorTheme.getSerializable(MAP_KEY_THEME_UPDATE_RAW_STYLES); if (colors != null) { writableMap.putArray(MAP_KEY_THEME_UPDATE_COLORS, Arguments.fromList((ArrayList)colors)); @@ -153,6 +155,10 @@ public void updateTheme(@Nullable Bundle editorTheme) { writableMap.putArray(MAP_KEY_THEME_UPDATE_GRADIENTS, Arguments.fromList((ArrayList)gradients)); } + if (rawStyles != null) { + writableMap.putString(MAP_KEY_THEME_UPDATE_RAW_STYLES, rawStyles.toString()); + } + emitToJS(EVENT_NAME_UPDATE_EDITOR_SETTINGS, writableMap); } From 954d3372176426ba21d454888846d452e8367f96 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Fri, 28 May 2021 10:47:03 +0200 Subject: [PATCH 12/16] Mobile - Pass rawStyles on initial props and fix colors and gradients --- .../global-styles-context/test/utils.native.js | 2 ++ .../src/mobile/global-styles-context/utils.native.js | 12 +++++------- .../editor/src/components/provider/index.native.js | 7 ++++--- packages/react-native-editor/src/index.js | 2 ++ 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/components/src/mobile/global-styles-context/test/utils.native.js b/packages/components/src/mobile/global-styles-context/test/utils.native.js index eb10086fffcb3c..bac1c53d028989 100644 --- a/packages/components/src/mobile/global-styles-context/test/utils.native.js +++ b/packages/components/src/mobile/global-styles-context/test/utils.native.js @@ -181,6 +181,8 @@ describe( 'getGlobalStyles', () => { expect( globalStyles ).toEqual( expect.objectContaining( { + colors: GLOBAL_STYLES_PALETTE, + gradients, __experimentalFeatures: { color: { palette: GLOBAL_STYLES_PALETTE, gradients }, }, 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 a30162d0578f88..636559a8f9195d 100644 --- a/packages/components/src/mobile/global-styles-context/utils.native.js +++ b/packages/components/src/mobile/global-styles-context/utils.native.js @@ -91,13 +91,11 @@ export function getGlobalStyles( rawStyles, colors, gradients ) { const globalStyles = parseColorVariables( rawStyles, colors ); return { - ...( colors || gradients - ? { - __experimentalFeatures: { - color: { palette: colors, gradients: parsedGradients }, - }, - } - : {} ), + colors, + gradients: parsedGradients, + __experimentalFeatures: { + color: { palette: colors, gradients: parsedGradients }, + }, __experimentalGlobalStylesBaseStyles: globalStyles, }; } diff --git a/packages/editor/src/components/provider/index.native.js b/packages/editor/src/components/provider/index.native.js index 063a46ce9ddd50..0fa4e826aaf1ae 100644 --- a/packages/editor/src/components/provider/index.native.js +++ b/packages/editor/src/components/provider/index.native.js @@ -192,11 +192,12 @@ class NativeEditorProvider extends Component { getThemeColors( { colors, gradients, rawStyles } ) { return { - colors: validateThemeColors( colors ) ?? [], - gradients: validateThemeGradients( gradients ) ?? [], ...( rawStyles ? getGlobalStyles( rawStyles, colors, gradients ) - : {} ), + : { + colors: validateThemeColors( colors ), + gradients: validateThemeGradients( gradients ), + } ), }; } diff --git a/packages/react-native-editor/src/index.js b/packages/react-native-editor/src/index.js index 6198c043a3ab83..574ecac69bada5 100644 --- a/packages/react-native-editor/src/index.js +++ b/packages/react-native-editor/src/index.js @@ -79,6 +79,7 @@ const setupInitHooks = () => { featuredImageId, colors, gradients, + rawStyles, } = props; if ( initialData === undefined && __DEV__ ) { @@ -104,6 +105,7 @@ const setupInitHooks = () => { capabilities, colors, gradients, + rawStyles, }; } ); From 2e12d2c4483eb76edb8296d8159e7def7dca5ef5 Mon Sep 17 00:00:00 2001 From: Chip Snyder Date: Wed, 16 Jun 2021 15:43:06 -0400 Subject: [PATCH 13/16] Add Raw Features back --- packages/react-native-bridge/ios/Gutenberg.swift | 6 +++++- .../react-native-bridge/ios/GutenbergBridgeDataSource.swift | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/react-native-bridge/ios/Gutenberg.swift b/packages/react-native-bridge/ios/Gutenberg.swift index a7942f3e1368fe..cd143ae0d831f4 100644 --- a/packages/react-native-bridge/ios/Gutenberg.swift +++ b/packages/react-native-bridge/ios/Gutenberg.swift @@ -81,7 +81,7 @@ public class Gutenberg: NSObject { let editorSettings = dataSource.gutenbergEditorSettings() let settingsUpdates = properties(from: editorSettings) - initialProps.merge(properties(from: editorSettings)) { (intialProp, settingsUpdates) -> Any in + initialProps.merge(settingsUpdates) { (intialProp, settingsUpdates) -> Any in settingsUpdates } @@ -194,6 +194,10 @@ public class Gutenberg: NSObject { settingsUpdates["rawStyles"] = rawStyles } + if let rawFeatures = editorSettings?.rawFeatures { + settingsUpdates["rawFeatures"] = rawFeatures + } + if let colors = editorSettings?.colors { settingsUpdates["colors"] = colors } diff --git a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift index 59e7f7709e2dc3..b082cbd60682df 100644 --- a/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift +++ b/packages/react-native-bridge/ios/GutenbergBridgeDataSource.swift @@ -73,6 +73,7 @@ public extension GutenbergBridgeDataSource { public protocol GutenbergEditorSettings { var isFSETheme: Bool { get } var rawStyles: String? { get } + var rawFeatures: String? { get } var colors: [[String: String]]? { get } var gradients: [[String: String]]? { get } } From 1bbda5263ee1ad66a4494d0ea6f2d91eca031549 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 17 Jun 2021 09:13:41 +0300 Subject: [PATCH 14/16] Revert "Removes Android rawFeatures prop" This reverts commit 32a2b3aff85ed83d2a3c953f94d4f37685b4f2ca. --- .../java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt index 8234447af7cfa1..6f7ff9fc796a0e 100644 --- a/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt +++ b/packages/react-native-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt @@ -37,6 +37,8 @@ data class GutenbergProps @JvmOverloads constructor( theme.getSerializable(PROP_GRADIENTS)?.let { putSerializable(PROP_GRADIENTS, it) } theme.getSerializable(PROP_STYLES) ?.let { putSerializable(PROP_STYLES, it) } + theme.getSerializable(PROP_FEATURES) + ?.let { putSerializable(PROP_FEATURES, it) } } } @@ -70,6 +72,7 @@ data class GutenbergProps @JvmOverloads constructor( private const val PROP_COLORS = "colors" private const val PROP_GRADIENTS = "gradients" private const val PROP_STYLES = "rawStyles" + private const val PROP_FEATURES = "rawFeatures" const val PROP_CAPABILITIES = "capabilities" const val PROP_CAPABILITIES_CONTACT_INFO_BLOCK = "contactInfoBlock" From 1f34a2761f9281d607762a4a8639aa01ea0767ff Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Thu, 17 Jun 2021 13:31:39 +0200 Subject: [PATCH 15/16] Add rawFeatures in the mobile editor --- .../test/utils.native.js | 82 ++++++++++++++++++- .../global-styles-context/utils.native.js | 11 ++- .../src/components/provider/index.native.js | 4 +- packages/react-native-editor/src/index.js | 2 + 4 files changed, 94 insertions(+), 5 deletions(-) diff --git a/packages/components/src/mobile/global-styles-context/test/utils.native.js b/packages/components/src/mobile/global-styles-context/test/utils.native.js index bac1c53d028989..e29dc8bbed7fa8 100644 --- a/packages/components/src/mobile/global-styles-context/test/utils.native.js +++ b/packages/components/src/mobile/global-styles-context/test/utils.native.js @@ -79,6 +79,77 @@ const PARSED_GLOBAL_STYLES = { }, }; +const RAW_FEATURES = { + color: { + palette: { + core: [ + { + name: 'Black', + slug: 'black', + color: '#000000', + }, + { + name: 'Cyan bluish gray', + slug: 'cyan-bluish-gray', + color: '#abb8c3', + }, + { + name: 'White', + slug: 'white', + color: '#ffffff', + }, + ], + theme: [ + { + slug: 'green', + color: '#D1E4DD', + name: 'Green', + }, + { + slug: 'blue', + color: '#D1DFE4', + name: 'Blue', + }, + { + slug: 'purple', + color: '#D1D1E4', + name: 'Purple', + }, + ], + }, + gradients: { + core: [ + { + name: 'Vivid cyan blue to vivid purple', + gradient: + 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)', + slug: 'vivid-cyan-blue-to-vivid-purple', + }, + { + name: 'Light green cyan to vivid green cyan', + gradient: + 'linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)', + slug: 'light-green-cyan-to-vivid-green-cyan', + }, + ], + theme: [ + { + slug: 'purple-to-blue', + gradient: + 'linear-gradient(160deg, var(--wp--preset--color--purple), var(--wp--preset--color--blue))', + name: 'Purple to Blue', + }, + { + slug: 'green-to-purple', + gradient: + 'linear-gradient(160deg, var(--wp--preset--color--green), var(--wp--preset--color--purple))', + name: 'Green to Purple', + }, + ], + }, + }, +}; + describe( 'getBlockPaddings', () => { const PADDING = 12; @@ -169,8 +240,10 @@ describe( 'parseColorVariables', () => { describe( 'getGlobalStyles', () => { it( 'returns the global styles data correctly', () => { + const rawFeatures = JSON.stringify( RAW_FEATURES ); const globalStyles = getGlobalStyles( JSON.stringify( DEFAULT_GLOBAL_STYLES ), + rawFeatures, GLOBAL_STYLES_PALETTE, GLOBAL_STYLES_GRADIENTS ); @@ -178,13 +251,20 @@ describe( 'getGlobalStyles', () => { JSON.stringify( GLOBAL_STYLES_GRADIENTS ), GLOBAL_STYLES_PALETTE ); + const parsedExperimentalFeatures = parseColorVariables( + rawFeatures, + GLOBAL_STYLES_PALETTE + ); expect( globalStyles ).toEqual( expect.objectContaining( { colors: GLOBAL_STYLES_PALETTE, gradients, __experimentalFeatures: { - color: { palette: GLOBAL_STYLES_PALETTE, gradients }, + color: { + palette: parsedExperimentalFeatures?.color?.palette, + gradients: parsedExperimentalFeatures?.color?.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 636559a8f9195d..0cf5bee75c4c75 100644 --- a/packages/components/src/mobile/global-styles-context/utils.native.js +++ b/packages/components/src/mobile/global-styles-context/utils.native.js @@ -83,18 +83,25 @@ export function parseColorVariables( styles, colorPalette ) { : styles; } -export function getGlobalStyles( rawStyles, colors, gradients ) { +export function getGlobalStyles( rawStyles, rawFeatures, colors, gradients ) { const parsedGradients = parseColorVariables( JSON.stringify( gradients ), colors ); const globalStyles = parseColorVariables( rawStyles, colors ); + const parsedExperimentalFeatures = parseColorVariables( + rawFeatures, + colors + ); return { colors, gradients: parsedGradients, __experimentalFeatures: { - color: { palette: colors, gradients: parsedGradients }, + color: { + palette: parsedExperimentalFeatures?.color?.palette, + gradients: parsedExperimentalFeatures?.color?.gradients, + }, }, __experimentalGlobalStylesBaseStyles: globalStyles, }; diff --git a/packages/editor/src/components/provider/index.native.js b/packages/editor/src/components/provider/index.native.js index 0fa4e826aaf1ae..8440c998856622 100644 --- a/packages/editor/src/components/provider/index.native.js +++ b/packages/editor/src/components/provider/index.native.js @@ -190,10 +190,10 @@ class NativeEditorProvider extends Component { } } - getThemeColors( { colors, gradients, rawStyles } ) { + getThemeColors( { colors, gradients, rawStyles, rawFeatures } ) { return { ...( rawStyles - ? getGlobalStyles( rawStyles, colors, gradients ) + ? getGlobalStyles( rawStyles, rawFeatures, colors, gradients ) : { colors: validateThemeColors( colors ), gradients: validateThemeGradients( gradients ), diff --git a/packages/react-native-editor/src/index.js b/packages/react-native-editor/src/index.js index 818aa2a2a7bc48..c8b8fd8b152ab2 100644 --- a/packages/react-native-editor/src/index.js +++ b/packages/react-native-editor/src/index.js @@ -82,6 +82,7 @@ const setupInitHooks = () => { colors, gradients, rawStyles, + rawFeatures, } = props; if ( initialData === undefined && __DEV__ ) { @@ -108,6 +109,7 @@ const setupInitHooks = () => { colors, gradients, rawStyles, + rawFeatures, }; } ); From 3d780d37918b991b508edfa927989a095b979ac1 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Fri, 18 Jun 2021 16:18:56 +0200 Subject: [PATCH 16/16] Mobile - Global styles: Set theme's background and text color (#30810) * Mobile - Read global styles and set color palette and gradients * Mobile - Enable colors for blocks that support it * Parse all color variables * Mobile - Set background, title, text and link colors * Fix gradients * Add placeholder color * Update global styles mocked data * Move color settings * Removed added spaces * Add tests * Update experimental features path and prepare for rawGlobalStylesBaseStyles * Add missing provider * Get the right color attribute * Remove mock data * Mobile - Fix base global colors --- .../src/components/block-list/block.native.js | 7 +++++ .../src/components/provider/index.native.js | 3 +- packages/block-editor/src/hooks/color.js | 2 +- .../src/paragraph/edit.native.js | 5 ++++ .../global-styles-context/index.native.js | 5 ++++ .../src/components/layout/index.native.js | 30 ++++++++++++++----- .../src/components/post-title/index.native.js | 30 ++++++++++++++----- .../rich-text/src/component/index.native.js | 15 +++++----- 8 files changed, 71 insertions(+), 26 deletions(-) diff --git a/packages/block-editor/src/components/block-list/block.native.js b/packages/block-editor/src/components/block-list/block.native.js index 018cf4760a07eb..c81a2d5469ecc1 100644 --- a/packages/block-editor/src/components/block-list/block.native.js +++ b/packages/block-editor/src/components/block-list/block.native.js @@ -48,11 +48,13 @@ function BlockForType( { parentWidth, wrapperProps, blockWidth, + baseGlobalStyles, } ) { const defaultColors = useSetting( 'color.palette' ) || emptyArray; const globalStyle = useGlobalStyles(); const mergedStyle = useMemo( () => { return getMergedGlobalStyles( + baseGlobalStyles, globalStyle, wrapperProps.style, attributes, @@ -300,6 +302,7 @@ export default compose( [ withSelect( ( select, { clientId, rootClientId } ) => { const { getBlockIndex, + getSettings, isBlockSelected, __unstableGetBlockWithoutInnerBlocks, getSelectedBlockClientId, @@ -347,6 +350,9 @@ export default compose( [ isDescendantOfParentSelected || isParentSelected || parentId === ''; + const baseGlobalStyles = getSettings() + ?.__experimentalGlobalStylesBaseStyles; + return { icon, name: name || 'core/missing', @@ -360,6 +366,7 @@ export default compose( [ isParentSelected, firstToSelectId, isTouchable, + baseGlobalStyles, wrapperProps: getWrapperProps( attributes, blockType.getEditWrapperProps diff --git a/packages/block-editor/src/components/provider/index.native.js b/packages/block-editor/src/components/provider/index.native.js index 157cd72d1f237b..8851a2b9b47a15 100644 --- a/packages/block-editor/src/components/provider/index.native.js +++ b/packages/block-editor/src/components/provider/index.native.js @@ -10,6 +10,7 @@ import { useEffect } from '@wordpress/element'; import withRegistryProvider from './with-registry-provider'; import useBlockSync from './use-block-sync'; import { store as blockEditorStore } from '../../store'; +import { BlockRefsProvider } from './block-refs-provider'; /** @typedef {import('@wordpress/data').WPDataRegistry} WPDataRegistry */ @@ -24,7 +25,7 @@ function BlockEditorProvider( props ) { // Syncs the entity provider with changes in the block-editor store. useBlockSync( props ); - return children; + return { children }; } export default withRegistryProvider( BlockEditorProvider ); diff --git a/packages/block-editor/src/hooks/color.js b/packages/block-editor/src/hooks/color.js index 554781d057b5fd..2da9875c07584f 100644 --- a/packages/block-editor/src/hooks/color.js +++ b/packages/block-editor/src/hooks/color.js @@ -234,7 +234,7 @@ export function ColorEdit( props ) { localAttributes.current = attributes; }, [ attributes ] ); - if ( ! hasColorSupport( blockName ) || Platform.OS !== 'web' ) { + if ( ! hasColorSupport( blockName ) ) { return null; } diff --git a/packages/block-library/src/paragraph/edit.native.js b/packages/block-library/src/paragraph/edit.native.js index a51a708a222767..1cd75758543006 100644 --- a/packages/block-library/src/paragraph/edit.native.js +++ b/packages/block-library/src/paragraph/edit.native.js @@ -30,6 +30,11 @@ function ParagraphBlock( { const { align, content, placeholder } = attributes; const styles = { + ...( mergedStyle?.baseColors && { + color: mergedStyle.baseColors?.color?.text, + placeholderColor: mergedStyle.baseColors?.color?.text, + linkColor: mergedStyle.baseColors?.elements?.link?.color?.text, + } ), ...mergedStyle, ...style, }; diff --git a/packages/components/src/mobile/global-styles-context/index.native.js b/packages/components/src/mobile/global-styles-context/index.native.js index 81a60ccc8eb56e..1d4d0d45be6761 100644 --- a/packages/components/src/mobile/global-styles-context/index.native.js +++ b/packages/components/src/mobile/global-styles-context/index.native.js @@ -22,16 +22,21 @@ const GlobalStylesContext = createContext( { style: {} } ); GlobalStylesContext.BLOCK_STYLE_ATTRIBUTES = BLOCK_STYLE_ATTRIBUTES; export const getMergedGlobalStyles = ( + baseGlobalStyles, globalStyle, wrapperPropsStyle, blockAttributes, defaultColors ) => { + const baseGlobalColors = { + baseColors: baseGlobalStyles || {}, + }; const blockStyleAttributes = pick( blockAttributes, BLOCK_STYLE_ATTRIBUTES ); const mergedStyle = { + ...baseGlobalColors, ...globalStyle, ...wrapperPropsStyle, }; diff --git a/packages/edit-post/src/components/layout/index.native.js b/packages/edit-post/src/components/layout/index.native.js index f0abe54cfc9e97..d98bae6739ef59 100644 --- a/packages/edit-post/src/components/layout/index.native.js +++ b/packages/edit-post/src/components/layout/index.native.js @@ -9,7 +9,11 @@ import SafeArea from 'react-native-safe-area'; */ import { Component } from '@wordpress/element'; import { withSelect } from '@wordpress/data'; -import { BottomSheetSettings, FloatingToolbar } from '@wordpress/block-editor'; +import { + BottomSheetSettings, + FloatingToolbar, + store as blockEditorStore, +} from '@wordpress/block-editor'; import { compose, withPreferredColorScheme } from '@wordpress/compose'; import { HTMLTextInput, @@ -101,7 +105,7 @@ class Layout extends Component { } render() { - const { getStylesFromColorScheme, mode } = this.props; + const { getStylesFromColorScheme, mode, globalStyles } = this.props; const isHtmlView = mode === 'text'; @@ -118,6 +122,16 @@ class Layout extends Component { bottom: this.state.safeAreaInsets.bottom, }; + const editorStyles = [ + getStylesFromColorScheme( + styles.background, + styles.backgroundDark + ), + globalStyles?.background && { + backgroundColor: globalStyles.background, + }, + ]; + return ( - + { isHtmlView ? this.renderHTML() : this.renderVisual() } { ! isHtmlView && Platform.OS === 'android' && ( @@ -176,9 +185,14 @@ export default compose( [ editorStore ); const { getEditorMode } = select( editPostStore ); + const { getSettings } = select( blockEditorStore ); + const globalStyles = getSettings()?.__experimentalGlobalStylesBaseStyles + ?.color; + return { isReady: isEditorReady(), mode: getEditorMode(), + globalStyles, }; } ), withPreferredColorScheme, diff --git a/packages/editor/src/components/post-title/index.native.js b/packages/editor/src/components/post-title/index.native.js index 0587f26227ec83..4e117f73fb315b 100644 --- a/packages/editor/src/components/post-title/index.native.js +++ b/packages/editor/src/components/post-title/index.native.js @@ -19,6 +19,8 @@ import { withFocusOutside } from '@wordpress/components'; import { withInstanceId, compose } from '@wordpress/compose'; import { __, sprintf } from '@wordpress/i18n'; import { pasteHandler } from '@wordpress/blocks'; +import { store as blockEditorStore } from '@wordpress/block-editor'; +import { store as editorStore } from '@wordpress/editor'; /** * Internal dependencies @@ -107,12 +109,20 @@ class PostTitle extends Component { borderStyle, isDimmed, postType, + globalStyles, } = this.props; const decodedPlaceholder = decodeEntities( placeholder ); const borderColor = this.props.isSelected ? focusedBorderColor : 'transparent'; + const titleStyles = { + ...style, + ...( globalStyles?.text && { + color: globalStyles.text, + placeholderColor: globalStyles.text, + } ), + }; return ( { const { isPostTitleSelected, getEditedPostAttribute } = select( - 'core/editor' - ); - - const { getSelectedBlockClientId, getBlockRootClientId } = select( - 'core/block-editor' + editorStore ); + const { + getSelectedBlockClientId, + getBlockRootClientId, + getSettings, + } = select( blockEditorStore ); const selectedId = getSelectedBlockClientId(); const selectionIsNested = !! getBlockRootClientId( selectedId ); + const globalStyles = getSettings()?.__experimentalGlobalStylesBaseStyles + ?.color; return { postType: getEditedPostAttribute( 'type' ), isAnyBlockSelected: !! selectedId, isSelected: isPostTitleSelected(), isDimmed: selectionIsNested, + globalStyles, }; } ), withDispatch( ( dispatch ) => { const { undo, redo, togglePostTitleSelection } = dispatch( - 'core/editor' + editorStore ); const { clearSelectedBlock, insertDefaultBlock } = dispatch( - 'core/block-editor' + blockEditorStore ); return { diff --git a/packages/rich-text/src/component/index.native.js b/packages/rich-text/src/component/index.native.js index 195de53e802fed..8f5afcc4857168 100644 --- a/packages/rich-text/src/component/index.native.js +++ b/packages/rich-text/src/component/index.native.js @@ -3,21 +3,18 @@ /** * External dependencies */ +import { View, Platform } from 'react-native'; +import { get, pickBy, debounce, isString } from 'lodash'; +import memize from 'memize'; + /** * WordPress dependencies */ import RCTAztecView from '@wordpress/react-native-aztec'; -import { View, Platform } from 'react-native'; import { showUserSuggestions, showXpostSuggestions, } from '@wordpress/react-native-bridge'; -import { get, pickBy, debounce, isString } from 'lodash'; -import memize from 'memize'; - -/** - * WordPress dependencies - */ import { BlockFormatControls } from '@wordpress/block-editor'; import { Component } from '@wordpress/element'; import { compose, withPreferredColorScheme } from '@wordpress/compose'; @@ -982,10 +979,12 @@ export class RichText extends Component { text: html, eventCount: this.lastEventCount, selection, - linkTextColor: defaultTextDecorationColor, + linkTextColor: + style?.linkColor || defaultTextDecorationColor, } } placeholder={ this.props.placeholder } placeholderTextColor={ + style?.placeholderColor || this.props.placeholderTextColor || defaultPlaceholderTextColor }