Skip to content

Commit

Permalink
Rename updateTheme to be more generic for update settings include GSS (
Browse files Browse the repository at this point in the history
  • Loading branch information
chipsnyder authored May 6, 2021
1 parent 7102a51 commit 906a9f6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 36 deletions.
24 changes: 13 additions & 11 deletions packages/editor/src/components/provider/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import RNReactNativeGutenbergBridge, {
subscribeSetTitle,
subscribeMediaAppend,
subscribeReplaceBlock,
subscribeUpdateTheme,
subscribeUpdateEditorSettings,
subscribeUpdateCapabilities,
subscribeShowNotice,
} from '@wordpress/react-native-bridge';
Expand Down Expand Up @@ -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 );
}
);

Expand Down Expand Up @@ -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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/react-native-bridge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
42 changes: 25 additions & 17 deletions packages/react-native-bridge/ios/Gutenberg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ extension RNReactNativeGutenbergBridge {
case mediaUpload
case setFocusOnTitle
case mediaAppend
case updateTheme
case updateEditorSettings
case replaceBlock
case updateCapabilities
case showNotice
Expand Down
2 changes: 1 addition & 1 deletion test/native/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 906a9f6

Please sign in to comment.