Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Hide help button from UBE #37221

Merged
merged 12 commits into from
Jan 14, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,18 @@ private void injectCssScript() {
String injectWPBarsCssScript = getFileContentFromAssets("gutenberg-web-single-block/wp-bar-override.css");
injectWPBarsCssScript = removeWhiteSpace(removeNewLines(injectWPBarsCssScript));
evaluateJavaScript(String.format(INJECT_CSS_SCRIPT_TEMPLATE, injectWPBarsCssScript));

String injectExternalCssScript = getOnGutenbergReadyExternalStyles();
twstokes marked this conversation as resolved.
Show resolved Hide resolved
injectExternalCssScript = removeWhiteSpace(removeNewLines(injectExternalCssScript));
evaluateJavaScript(String.format(INJECT_CSS_SCRIPT_TEMPLATE, injectExternalCssScript));
}
});
}

protected String getOnGutenbergReadyExternalStyles() {
return new String();
}

private void injectOnGutenbergReadyExternalSources() {
List<String> list = getOnGutenbergReadyExternalSources();
for (String file : list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ public struct FallbackJavascriptInjection {
}
}

public extension SourceFile {
func jsScript(with argument: String? = nil) throws -> WKUserScript {
let content = try getContent()
let formatted = String(format: content, argument ?? [])
return formatted.toJsScript()
}
}

internal extension String {
func toJsScript() -> WKUserScript {
WKUserScript(source: self, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ open class GutenbergWebSingleBlockViewController: UIViewController {
return []
}

/// Requests a set of CSS styles to be added to the web view when the editor has started loading.
/// - Returns: Array of all the styles to be added
open func onGutenbergLoadStyles() -> [WKUserScript] {
return []
}

/// Requests a set of JS Scripts to be added to the web view when Gutenberg has been initialized.
/// - Returns: Array of all the scripts to be added
open func onGutenbergReadyScripts() -> [WKUserScript] {
Expand Down Expand Up @@ -152,6 +158,7 @@ extension GutenbergWebSingleBlockViewController: WKNavigationDelegate {
evaluateJavascript(jsInjection.injectWPBarsCssScript)
evaluateJavascript(jsInjection.injectLocalStorageScript)
onPageLoadScripts().forEach(evaluateJavascript)
onGutenbergLoadStyles().forEach(evaluateJavascript)
}

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
Expand All @@ -160,6 +167,7 @@ extension GutenbergWebSingleBlockViewController: WKNavigationDelegate {
evaluateJavascript(jsInjection.preventAutosavesScript)
evaluateJavascript(jsInjection.injectEditorCssScript)
evaluateJavascript(jsInjection.gutenbergObserverScript)
onGutenbergLoadStyles().forEach(evaluateJavascript)
}
}

Expand Down
17 changes: 16 additions & 1 deletion packages/react-native-bridge/ios/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,29 @@ public struct SourceFile {
self.bundle = bundle
}

func getContent() throws -> String {
public func getContent() throws -> String {
guard let path = bundle.path(forResource: name, ofType: type.rawValue) else {
throw SourceFileError.sourceFileNotFound("\(name).\(type)")
}
return try String(contentsOfFile: path, encoding: .utf8)
}
}

extension SourceFile {
public func jsScript(with argument: String? = nil) throws -> WKUserScript {
let content = try getContent()
let formatted = String(format: content, argument ?? [])

switch self.type {
case .css:
let injectCssScriptTemplate = "window.injectCss(`%@`)"
return String(format: injectCssScriptTemplate, formatted).toJsScript()
case .js, .json:
return formatted.toJsScript()
}
}
}

extension SourceFile {
static let editorStyle = SourceFile(name: "editor-style-overrides", type: .css)
static let wpBarsStyle = SourceFile(name: "wp-bar-override", type: .css)
Expand Down