Skip to content

Commit

Permalink
Moved JsCallbackHandler to its own file
Browse files Browse the repository at this point in the history
- Added a JsCallbackListener interface for callbacks from the handler to the editor fragment
- Updated ZSSEditor.callback to send the full callback path
  • Loading branch information
aforcier committed Apr 9, 2015
1 parent 5da8d79 commit f1f933b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
Expand All @@ -29,7 +28,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;

public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener {
public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener, JsCallbackListener {
private static final String ARG_PARAM_TITLE = "param_title";
private static final String ARG_PARAM_CONTENT = "param_content";

Expand Down Expand Up @@ -89,6 +88,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
AppLog.d(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId());
return true;
Expand All @@ -99,16 +99,11 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu
AppLog.d(T.EDITOR, message);
return true;
}

@Override
public void onConsoleMessage(String message, int lineNumber, String sourceId) {
AppLog.d(T.EDITOR, message + " -- from line " + lineNumber + " of " + sourceId);
}
});

String htmlEditor = getHtmlFromFile("android-editor.html");

mWebView.addJavascriptInterface(new JsCallbackHandler(), JS_CALLBACK_HANDLER);
mWebView.addJavascriptInterface(new JsCallbackHandler(this), JS_CALLBACK_HANDLER);

mWebView.loadDataWithBaseURL("file:///android_asset/", htmlEditor, "text/html", "utf-8", "");

Expand Down Expand Up @@ -187,24 +182,22 @@ public Spanned getSpannedContent() {
return null;
}

class JsCallbackHandler {
@JavascriptInterface
public void executeCallback(final String callbackId) {
if (callbackId.equals("callback-dom-loaded")) {
// Run on UI thread
mWebView.post(new Runnable() {
public void run() {
String title = "I'm editing a post!";
String contentHtml = getHtmlFromFile("example-content.html");

// Load example content into editor
mWebView.loadUrl("javascript:ZSSEditor.getField('zss_field_title').setHTML('" +
Utils.escapeHtml(title) + "');");
mWebView.loadUrl("javascript:ZSSEditor.getField('zss_field_content').setHTML('" +
Utils.escapeHtml(contentHtml) + "');");
}
});
private void execJavaScriptFromString(String javaScript) {
mWebView.loadUrl("javascript:" + javaScript);
}

public void onDomLoaded() {
mWebView.post(new Runnable() {
public void run() {
String title = "I'm editing a post!";
String contentHtml = getHtmlFromFile("example-content.html");

// Load example content into editor
execJavaScriptFromString("ZSSEditor.getField('zss_field_title').setHTML('" +
Utils.escapeHtml(title) + "');");
execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setHTML('" +
Utils.escapeHtml(contentHtml) + "');");
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.wordpress.android.editor;

import android.webkit.JavascriptInterface;

import org.wordpress.android.util.AppLog;

public class JsCallbackHandler {
private static final String CALLBACK_LOG = "callback-log";

private static final String CALLBACK_DOM_LOADED = "callback-dom-loaded";

private static final String CALLBACK_SELECTION_STYLE = "callback-selection-style";
private static final String CALLBACK_SELECTION_CHANGED = "callback-selection-changed";

private static final String CALLBACK_FOCUS_IN = "callback-focus-in";
private static final String CALLBACK_FOCUS_OUT = "callback-focus-out";

private static final String CALLBACK_IMAGE_REPLACED = "callback-image-replaced";
private static final String CALLBACK_IMAGE_TAP = "callback-image-tap";
private static final String CALLBACK_INPUT = "callback-input";
private static final String CALLBACK_LINK_TAP = "callback-link-tap";

private static final String CALLBACK_NEW_FIELD = "callback-new-field";

private final JsCallbackListener mJsCallbackListener;

public JsCallbackHandler(EditorFragmentAbstract editorFragmentAbstract) {
mJsCallbackListener = (JsCallbackListener) editorFragmentAbstract;
}

@JavascriptInterface
public void executeCallback(String callbackId, String params) {
switch (callbackId) {
case CALLBACK_DOM_LOADED:
mJsCallbackListener.onDomLoaded();
break;
case CALLBACK_LOG:
// Strip 'msg=' from beginning of string
AppLog.d(AppLog.T.EDITOR, callbackId + ": " + params.substring(4));
break;
default:
AppLog.d(AppLog.T.EDITOR, "unhandled callback: " + callbackId + ":" + params);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.wordpress.android.editor;

public interface JsCallbackListener {
void onDomLoaded();
}
4 changes: 3 additions & 1 deletion libs/editor-common/assets/ZSSRichTextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ ZSSEditor.callback = function(callbackScheme, callbackPath) {
if (isUsingiOS) {
ZSSEditor.callbackThroughIFrame(url);
} else if (isUsingAndroid) {
nativeCallbackHandler.executeCallback(callbackScheme);
nativeCallbackHandler.executeCallback(callbackScheme, callbackPath);
} else {
console.log(url);
}
Expand Down Expand Up @@ -1877,6 +1877,8 @@ ZSSField.prototype.callback = function(callbackScheme, callbackPath) {

if (isUsingiOS) {
ZSSEditor.callbackThroughIFrame(url);
} else if (isUsingAndroid) {
nativeCallbackHandler.executeCallback(callbackScheme, callbackPath);
} else {
console.log(url);
}
Expand Down

0 comments on commit f1f933b

Please sign in to comment.