Skip to content

Commit 88f29c5

Browse files
committed
JS to Java callback proof of concept
- Added a callback handler injected into the WebView - Set up a 'DOM has loaded' callback to load the example HTML into the editor
1 parent d2aa4f3 commit 88f29c5

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.view.View;
1010
import android.view.ViewGroup;
1111
import android.webkit.ConsoleMessage;
12+
import android.webkit.JavascriptInterface;
1213
import android.webkit.JsResult;
1314
import android.webkit.WebChromeClient;
1415
import android.webkit.WebSettings;
@@ -99,31 +100,13 @@ public void onConsoleMessage(String message, int lineNumber, String sourceId) {
99100
});
100101
String htmlEditor = getHtmlEditor();
101102

103+
mWebView.addJavascriptInterface(new JsCallbackHandler(), "nativeCallbackHandler");
104+
102105
mWebView.loadDataWithBaseURL("file:///android_asset/", htmlEditor, "text/html", "utf-8", "");
103106

104107
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
105108
WebView.setWebContentsDebuggingEnabled(true);
106109
}
107-
// TODO: Replace postDelay with a callback from JS
108-
mWebView.postDelayed(new Runnable() {
109-
@Override
110-
public void run() {
111-
String htmlFile = "";
112-
try {
113-
htmlFile = getStringFromAsset("example-content.html");
114-
htmlFile = htmlFile.replace("\\", "\\\\");
115-
htmlFile = htmlFile.replace("\"", "\\\"");
116-
htmlFile = htmlFile.replace("'", "\\'");
117-
htmlFile = htmlFile.replace("\r", "\\r");
118-
htmlFile = htmlFile.replace("\n", "\\n");
119-
} catch (IOException e) {
120-
AppLog.e(T.EDITOR, e.getMessage());
121-
}
122-
123-
// Load example file into editor content field
124-
mWebView.loadUrl("JavaScript:ZSSEditor.getField('zss_field_content').setHTML('" + htmlFile + "');");
125-
}
126-
}, 5000);
127110
}
128111

129112
private String getStringFromAsset(String filename) throws IOException {
@@ -189,4 +172,31 @@ public void appendGallery(MediaGallery mediaGallery) {
189172
public Spanned getSpannedContent() {
190173
return null;
191174
}
192-
}
175+
176+
class JsCallbackHandler {
177+
@JavascriptInterface
178+
public void executeCallback(final String callbackId) {
179+
if (callbackId.equals("callback-dom-loaded")) {
180+
// Run on UI thread
181+
mWebView.post(new Runnable() {
182+
public void run() {
183+
String htmlFile = "";
184+
try {
185+
htmlFile = getStringFromAsset("example-content.html");
186+
htmlFile = htmlFile.replace("\\", "\\\\");
187+
htmlFile = htmlFile.replace("\"", "\\\"");
188+
htmlFile = htmlFile.replace("'", "\\'");
189+
htmlFile = htmlFile.replace("\r", "\\r");
190+
htmlFile = htmlFile.replace("\n", "\\n");
191+
} catch (IOException e) {
192+
AppLog.e(T.EDITOR, e.getMessage());
193+
}
194+
195+
// Load example file into editor content field
196+
mWebView.loadUrl("JavaScript:ZSSEditor.getField('zss_field_content').setHTML('" + htmlFile + "');");
197+
}
198+
});
199+
}
200+
}
201+
}
202+
}

libs/editor-common/assets/ZSSRichTextEditor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99

1010
// If we are using iOS or desktop
11-
var isUsingiOS = true;
11+
var isUsingiOS = false;
12+
var isUsingAndroid = true;
1213

1314
// THe default callback parameter separator
1415
var defaultCallbackSeparator = '~';
@@ -197,6 +198,8 @@ ZSSEditor.callback = function(callbackScheme, callbackPath) {
197198

198199
if (isUsingiOS) {
199200
ZSSEditor.callbackThroughIFrame(url);
201+
} else if (isUsingAndroid) {
202+
nativeCallbackHandler.executeCallback(callbackScheme);
200203
} else {
201204
console.log(url);
202205
}

0 commit comments

Comments
 (0)