99import android .view .View ;
1010import android .view .ViewGroup ;
1111import android .webkit .ConsoleMessage ;
12- import android .webkit .JavascriptInterface ;
1312import android .webkit .JsResult ;
1413import android .webkit .WebChromeClient ;
1514import android .webkit .WebSettings ;
2928import java .io .InputStream ;
3029import java .io .InputStreamReader ;
3130
32- public class EditorFragment extends EditorFragmentAbstract implements View .OnClickListener {
31+ public class EditorFragment extends EditorFragmentAbstract implements View .OnClickListener , JsCallbackListener {
3332 private static final String ARG_PARAM_TITLE = "param_title" ;
3433 private static final String ARG_PARAM_CONTENT = "param_content" ;
3534
@@ -89,6 +88,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
8988 }
9089 });
9190 mWebView .setWebChromeClient (new WebChromeClient () {
91+ @ Override
9292 public boolean onConsoleMessage (ConsoleMessage cm ) {
9393 AppLog .d (T .EDITOR , cm .message () + " -- From line " + cm .lineNumber () + " of " + cm .sourceId ());
9494 return true ;
@@ -99,16 +99,11 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu
9999 AppLog .d (T .EDITOR , message );
100100 return true ;
101101 }
102-
103- @ Override
104- public void onConsoleMessage (String message , int lineNumber , String sourceId ) {
105- AppLog .d (T .EDITOR , message + " -- from line " + lineNumber + " of " + sourceId );
106- }
107102 });
108103
109104 String htmlEditor = getHtmlFromFile ("android-editor.html" );
110105
111- mWebView .addJavascriptInterface (new JsCallbackHandler (), JS_CALLBACK_HANDLER );
106+ mWebView .addJavascriptInterface (new JsCallbackHandler (this ), JS_CALLBACK_HANDLER );
112107
113108 mWebView .loadDataWithBaseURL ("file:///android_asset/" , htmlEditor , "text/html" , "utf-8" , "" );
114109
@@ -187,24 +182,22 @@ public Spanned getSpannedContent() {
187182 return null ;
188183 }
189184
190- class JsCallbackHandler {
191- @ JavascriptInterface
192- public void executeCallback (final String callbackId ) {
193- if (callbackId .equals ("callback-dom-loaded" )) {
194- // Run on UI thread
195- mWebView .post (new Runnable () {
196- public void run () {
197- String title = "I'm editing a post!" ;
198- String contentHtml = getHtmlFromFile ("example-content.html" );
199-
200- // Load example content into editor
201- mWebView .loadUrl ("javascript:ZSSEditor.getField('zss_field_title').setHTML('" +
202- Utils .escapeHtml (title ) + "');" );
203- mWebView .loadUrl ("javascript:ZSSEditor.getField('zss_field_content').setHTML('" +
204- Utils .escapeHtml (contentHtml ) + "');" );
205- }
206- });
185+ private void execJavaScriptFromString (String javaScript ) {
186+ mWebView .loadUrl ("javascript:" + javaScript );
187+ }
188+
189+ public void onDomLoaded () {
190+ mWebView .post (new Runnable () {
191+ public void run () {
192+ String title = "I'm editing a post!" ;
193+ String contentHtml = getHtmlFromFile ("example-content.html" );
194+
195+ // Load example content into editor
196+ execJavaScriptFromString ("ZSSEditor.getField('zss_field_title').setHTML('" +
197+ Utils .escapeHtml (title ) + "');" );
198+ execJavaScriptFromString ("ZSSEditor.getField('zss_field_content').setHTML('" +
199+ Utils .escapeHtml (contentHtml ) + "');" );
207200 }
208- }
201+ });
209202 }
210203}
0 commit comments