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

add requestFocus property #287

Open
wants to merge 85 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
535f754
Made changes in the code to fit our new HBRichTextEditor lib
geldarhb Jun 15, 2016
a78221a
Changed getEditorHTML to getElementHTML - so it is generalised and no…
geldarhb Jun 19, 2016
f8e7a63
Committing another leftover for converting getEditorHTML to getElemen…
geldarhb Jun 19, 2016
0006d1d
Sealing getElementHTML function
geldarhb Jun 19, 2016
3c88470
added rpc empty example
alinz Sep 24, 2016
0a5c808
rewrote with latest webview
alinz Sep 25, 2016
06e8b24
wip
alinz Sep 26, 2016
d94aa03
updatetd code base
alinz Sep 26, 2016
86c8fe9
cleaned up
alinz Sep 26, 2016
a6230e1
cleaned up and added some basic functionality
alinz Sep 26, 2016
4ad07f4
added sendToBridge and onBridgeMessage
alinz Sep 27, 2016
cddc619
got the message passing fully functional
alinz Sep 27, 2016
88df0d1
added basic rpc and multiple callbacks for onMessage functions
alinz Sep 27, 2016
352863c
updated rpc example
alinz Sep 27, 2016
05188ac
got 2 ways to work
alinz Sep 27, 2016
27e3eaa
added rpc calls
alinz Sep 27, 2016
23f1661
cleaned up code base
alinz Sep 28, 2016
e8f648e
got basic to work
alinz Sep 28, 2016
cfd6cea
cleaned up code
alinz Sep 28, 2016
eef8c70
fixed an issue related to native invoker
alinz Sep 28, 2016
602b4a0
fixed path is not set
alinz Sep 28, 2016
b7de2c6
fixed README
alinz Sep 29, 2016
5cba955
make the code ready for android
alinz Sep 29, 2016
268e82a
wip for android
alinz Sep 30, 2016
9aa1b75
android code is ready
alinz Sep 30, 2016
283b59a
made the code robust by using listeners
alinz Sep 30, 2016
0feaf01
moved base WebViewBridge and WebViewBridgeRPC to assets folder
alinz Sep 30, 2016
64e1ef9
upgraded example project to latest gradle
alinz Sep 30, 2016
2d7bae8
trying
eladgel Oct 9, 2016
b81b784
Merge commit '64e1ef93cc1d77d3fff4347e91dbd374b916496b'
eladgel Oct 9, 2016
4404bdb
make it so
eladgel Oct 9, 2016
7f76e93
make it so
eladgel Oct 9, 2016
7b9e330
Merge branch 'master' of https://github.com/HoneyBook/react-native-we…
eladgel Oct 9, 2016
c443c94
removed unused file
eladgel Oct 9, 2016
9c7e99c
working like a mother pupper
eladgel Oct 9, 2016
28c6c3d
Update RCTWebViewBridge.m
geldarhb Oct 18, 2016
30821ea
Changed main to the correct source folder
yedidyak Nov 1, 2016
f1b3437
Merge pull request #1 from yedidyak/patch-1
geldarhb Nov 1, 2016
7e3171b
Added getSelectedHTML method
geldarhb Nov 29, 2016
0f366f8
Adding getSelectedHTML method
geldarhb Nov 29, 2016
b910f60
Changing the default to NO when coming back from BG
geldarhb Feb 16, 2017
405d0dd
add requestFocus property
Jan 15, 2019
f3440dd
Update RCTWebViewBridgeManager.m
Feb 26, 2019
77c8b40
Update RCTWebViewBridge.h
Feb 26, 2019
10b7370
Update package.json
Feb 26, 2019
914b7e0
Merge branch 'master' of github.com:HoneyBook/react-native-webview-br…
geldarhb Nov 26, 2019
2e5b24c
Update build.gradle
Jan 5, 2020
05a1d53
Update WebViewBridgePackage.java
Jan 5, 2020
e5ec3fb
Update build.gradle
Jan 5, 2020
880310b
Update project.pbxproj
Apr 1, 2020
36573ef
Update RCTWebViewBridge.m
Apr 1, 2020
9d842a2
Update RCTWebViewBridgeManager.m
Apr 1, 2020
0ec60a1
Create WKWebView+SynchronousEvaluateJavaScript.h
Apr 1, 2020
0c1ae36
Create WKWebView+SynchronousEvaluateJavaScript.m
Apr 1, 2020
da5b938
Merge pull request #1 from HoneyBook/new-guy
Apr 2, 2020
4434b45
Update index.ios.js
Apr 13, 2020
88344ad
Update index.android.js
Apr 13, 2020
9819a67
Merge commit '8f303f50c23d67f68a4219a4d33a0bac9d9237b5' into testing
Apr 20, 2020
d5788ec
reverting
Apr 20, 2020
24df2f2
Update index.ios.js
Apr 20, 2020
36bd6fc
Update package.json
Apr 20, 2020
7096d87
Update react-native-webview-bridge.podspec
Apr 20, 2020
411cb25
Update RCTWebViewBridge.m
Apr 20, 2020
ca4f144
Update WebViewBridgeManager.java
May 9, 2020
fb9ec11
Update build.gradle
May 9, 2020
579fe12
Update package.json
May 10, 2020
bb74cd7
ready
May 10, 2020
a907bd3
done
May 10, 2020
ac17ff3
Update package.json
May 10, 2020
3a484f0
update
May 10, 2020
f2a11f2
update version
May 12, 2020
ec8d371
fixed android webview brdige
May 19, 2020
ddb3410
new version
May 19, 2020
b401953
updated
May 20, 2020
8138cce
updating react native webview
Jul 22, 2020
d191967
Update index.ios.js
Jul 22, 2020
46c51ca
Update index.android.js
Jul 22, 2020
205d7c7
Update index.android.js
yossigreenhb Nov 10, 2022
ebe0684
Update index.ios.js
yossigreenhb Nov 10, 2022
b006c8e
Update package.json
yossigreenhb Nov 10, 2022
92d0bfb
Bump react-native-webview from 10.3.2 to 11.0.0
dependabot[bot] Nov 10, 2022
03a4249
Bump ua-parser-js from 0.7.17 to 0.7.33
dependabot[bot] Jan 25, 2023
1b39dc5
Merge pull request #4 from HoneyBook/dependabot/npm_and_yarn/ua-parse…
Jan 3, 2024
a3de56c
Merge pull request #2 from HoneyBook/dependabot/npm_and_yarn/react-na…
Jan 3, 2024
1b2af02
Update build.gradle
yossigreenhb May 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
.DS_Store

.tags
.tags1
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/react-native-webview-bridge.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

244 changes: 244 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public JavascriptBridge(WebView webView) {
}

@JavascriptInterface
public void send(String message) {
public void nativeAndroidSend(String message) {
WritableMap event = Arguments.createMap();
event.putString("message", message);
ReactContext reactContext = (ReactContext) this.webView.getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
this.webView.getId(),
"topChange",
event);
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("WebViewBridgeMessageEvent", event);

}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.github.alinz.reactnativewebviewbridge;

import android.content.Context;
import android.webkit.WebView;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.views.webview.ReactWebViewManager;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

Expand All @@ -15,7 +19,17 @@
public class WebViewBridgeManager extends ReactWebViewManager {
private static final String REACT_CLASS = "RCTWebViewBridge";

public static final int COMMAND_SEND_TO_BRIDGE = 101;
public static final int COMMAND_INJECT_WEBVIEW_BRIDGE = 101;
public static final int COMMAND_INJECT_RPC = 102;
public static final int COMMAND_SEND_TO_BRIDGE = 103;

private ReactApplicationContext reactApplicationContext;

public WebViewBridgeManager(ReactApplicationContext reactApplicationContext) {
super();
//we need to know the context because we need to load files from asset
this.reactApplicationContext = reactApplicationContext;
}

@Override
public String getName() {
Expand All @@ -29,6 +43,8 @@ Map<String, Integer> getCommandsMap() {
Map<String, Integer> commandsMap = super.getCommandsMap();

commandsMap.put("sendToBridge", COMMAND_SEND_TO_BRIDGE);
commandsMap.put("injectWebViewBridge", COMMAND_INJECT_WEBVIEW_BRIDGE);
commandsMap.put("injectRPC", COMMAND_INJECT_RPC);

return commandsMap;
}
Expand All @@ -48,13 +64,56 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
case COMMAND_SEND_TO_BRIDGE:
sendToBridge(root, args.getString(0));
break;
case COMMAND_INJECT_WEBVIEW_BRIDGE:
injectWebViewBridgeScript(root);
break;
case COMMAND_INJECT_RPC:
injectWebViewBridgeRPCScript(root);
break;
default:
//do nothing!!!!
}
}

private static String inputStreamToString(InputStream input) throws IOException {
StringBuilder builder = new StringBuilder();
int ch;
while((ch = input.read()) != -1){
builder.append((char)ch);
}
input.close();
return builder.toString();
}

private static String loadAsset(String filename, final Context context) {
String output = null;

try {
InputStream inputStream = context.getAssets().open(filename);
output = inputStreamToString(inputStream);
} catch (IOException e) {
e.printStackTrace();
}

return output;
}

private void injectWebViewBridgeScript(WebView root) {
String injectContent = loadAsset("WebViewBridge.js", this.reactApplicationContext);
if (injectContent != null) {
evaluateJavascript(root, injectContent);
}
}

private void injectWebViewBridgeRPCScript(WebView root) {
String injectContent = loadAsset("WebViewBridgeRPC.js", this.reactApplicationContext);
if (injectContent != null) {
evaluateJavascript(root, injectContent);
}
}

private void sendToBridge(WebView root, String message) {
String script = "WebViewBridge.onMessage('" + message + "');";
String script = "(function(){ if (WebViewBridge && WebViewBridge.__push__) { WebViewBridge.__push__(\"" + message + "\"); } }());";
WebViewBridgeManager.evaluateJavascript(root, script);
}

Expand All @@ -65,6 +124,18 @@ static private void evaluateJavascript(WebView root, String javascript) {
root.loadUrl("javascript:" + javascript);
}
}

@ReactProp(name = "requestFocus")
public void requestFocus(WebView root, boolean isRequestFocus) {
if(isRequestFocus) {
root.requestFocus();
}
}

@ReactProp(name = "injectedJavaScript")
public void setInjectedJavaScript(WebView root, @Nullable String injectedJavaScript) {
evaluateJavascript(root, injectedJavaScript);
}

@ReactProp(name = "allowFileAccessFromFileURLs")
public void setAllowFileAccessFromFileURLs(WebView root, boolean allows) {
Expand All @@ -75,4 +146,4 @@ public void setAllowFileAccessFromFileURLs(WebView root, boolean allows) {
public void setAllowUniversalAccessFromFileURLs(WebView root, boolean allows) {
root.getSettings().setAllowUniversalAccessFromFileURLs(allows);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactAppli
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
return Arrays.<ViewManager>asList(
new WebViewBridgeManager()
new WebViewBridgeManager(reactApplicationContext)
);
}

Expand Down
8 changes: 7 additions & 1 deletion ios/RCTWebViewBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

#if __has_include(<React/RCTView.h>)
#import <React/RCTView.h>

#else
#import "RCTView.h"
#endif

@class RCTWebViewBridge;

/**
Expand Down Expand Up @@ -44,5 +48,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
- (void)goBack;
- (void)reload;
- (void)sendToBridge:(NSString *)message;
- (NSString*) getElementHTML:(NSString*)elementId;
- (NSString*) getSelectedHTML;

@end
12 changes: 12 additions & 0 deletions ios/RCTWebViewBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ - (void)reload
[_webView reload];
}

- (NSString*) getElementHTML:(NSString*)elementId
{
NSString* JSString = [NSString stringWithFormat:@"document.getElementById(\"%@\").innerHTML",elementId];
return [_webView stringByEvaluatingJavaScriptFromString:JSString];
}

- (NSString*) getSelectedHTML {
NSString* JSString = [NSString stringWithFormat:@"document.getSelection()"];
NSLog(@"selectedHTML is: %@",JSString);
return [_webView stringByEvaluatingJavaScriptFromString:JSString];
}

- (void)sendToBridge:(NSString *)message
{
//we are warpping the send message in a function to make sure that if
Expand Down
52 changes: 47 additions & 5 deletions ios/RCTWebViewBridgeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@
*/

#import "RCTWebViewBridgeManager.h"
#import "RCTWebViewBridge.h"

#if __has_include(<React/RCTBridge.h>)
#import <React/RCTBridge.h>
#else
#import "RCTBridge.h"
#endif

#if __has_include(<React/RCTUIManager.h>)
#import <React/RCTUIManager.h>
#import "RCTWebViewBridge.h"
#else
#import "RCTUIManager.h"
#endif

#if __has_include(<React/UIView+React.h>)
#import <React/UIView+React.h>
#else
#import "UIView+React.h"
#endif

@interface RCTWebViewBridgeManager () <RCTWebViewBridgeDelegate>

Expand Down Expand Up @@ -49,8 +63,8 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
RCT_REMAP_VIEW_PROPERTY(allowsInlineMediaPlayback, _webView.allowsInlineMediaPlayback, BOOL)
RCT_REMAP_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, _webView.mediaPlaybackRequiresUserAction, BOOL)
RCT_EXPORT_VIEW_PROPERTY(onBridgeMessage, RCTDirectEventBlock)
RCT_REMAP_VIEW_PROPERTY(keyboardDisplayRequiresUserAction, _webView.keyboardDisplayRequiresUserAction, BOOL)

- (NSDictionary<NSString *, id> *)constantsToExport
{
Expand Down Expand Up @@ -87,7 +101,7 @@ + (BOOL)requiresMainQueueSetup
RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWebViewBridge *> *viewRegistry) {
RCTWebViewBridge *view = viewRegistry[reactTag];
RCTWebViewBridge* view = viewRegistry[reactTag];
if (![view isKindOfClass:[RCTWebViewBridge class]]) {
RCTLogError(@"Invalid view returned from registry, expecting RCTWebViewBridge, got: %@", view);
} else {
Expand All @@ -108,6 +122,34 @@ + (BOOL)requiresMainQueueSetup
}];
}

RCT_EXPORT_METHOD(getElementHTML:(nonnull NSNumber *)reactTag :(NSString*)elementId :(RCTResponseSenderBlock)callback)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWebViewBridge *> *viewRegistry) {
RCTWebViewBridge *view = viewRegistry[reactTag];
if (![view isKindOfClass:[RCTWebViewBridge class]]) {
RCTLogError(@"Invalid view returned from registry, expecting RCTWebViewBridge, got: %@", view);
} else {
NSString* html = [view getElementHTML:elementId];
NSLog(@"Editor HTML is %@", html);
callback(@[[NSNull null], html]);
}
}];
}

RCT_EXPORT_METHOD(getSelectedHTML:(nonnull NSNumber *)reactTag :(NSString*)elementId :(RCTResponseSenderBlock)callback)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWebViewBridge *> *viewRegistry) {
RCTWebViewBridge *view = viewRegistry[reactTag];
if (![view isKindOfClass:[RCTWebViewBridge class]]) {
RCTLogError(@"Invalid view returned from registry, expecting RCTWebViewBridge, got: %@", view);
} else {
NSString* html = [view getSelectedHTML];
NSLog(@"Selected HTML is %@", html);
callback(@[[NSNull null], html]);
}
}];
}

RCT_EXPORT_METHOD(sendToBridge:(nonnull NSNumber *)reactTag
value:(NSString*)message)
{
Expand Down Expand Up @@ -139,8 +181,8 @@ - (BOOL)webView:(__unused RCTWebViewBridge *)webView
_shouldStartLoadLock = nil;
return returnValue;
} else {
RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
return YES;
RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to NO");
return NO;
}
}

Expand Down
Loading