From 1e4c2cc02c8c1661c876f8ec46dde59de5fa7663 Mon Sep 17 00:00:00 2001 From: xingxing Date: Sun, 20 May 2018 15:53:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E7=AC=94=E8=AE=B0=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2:=20=E7=AC=94=E8=AE=B0=E5=8A=A0=E8=BD=BD=E5=AE=8C?= =?UTF-8?q?=EF=BC=8C=E6=89=8D=E8=83=BD=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/assets/RichTextEditor/editor.html | 7 +++++++ .../markdownEditor/editor-mobile.min.html | 7 +++++++ .../org/houxg/leamonax/editor/Editor.java | 2 ++ .../org/houxg/leamonax/editor/HostApp.java | 15 +++++++++++++++ .../houxg/leamonax/editor/MarkdownEditor.java | 7 +++++++ .../houxg/leamonax/editor/RichTextEditor.java | 9 +++++++++ .../houxg/leamonax/model/CompleteEvent.java | 4 ++++ .../leamonax/ui/edit/EditorFragment.java | 4 ++++ .../leamonax/ui/edit/NoteEditActivity.java | 19 +++++++++++++++++-- app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 11 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/houxg/leamonax/editor/HostApp.java create mode 100644 app/src/main/java/org/houxg/leamonax/model/CompleteEvent.java diff --git a/app/src/main/assets/RichTextEditor/editor.html b/app/src/main/assets/RichTextEditor/editor.html index 7de1866..be80392 100644 --- a/app/src/main/assets/RichTextEditor/editor.html +++ b/app/src/main/assets/RichTextEditor/editor.html @@ -242,6 +242,13 @@ function isChildOfBody(elm) { return tinyMCE.editors[0].$.contains(tinyMCE.editors[0].getBody(), elm); } + + function setTitleAndContent(title, content) { + setTitle(title); + tinyMCE.editors[0].setContent(content); + hostApp.loadCompleted(); + return ""; + } diff --git a/app/src/main/assets/markdownEditor/editor-mobile.min.html b/app/src/main/assets/markdownEditor/editor-mobile.min.html index 3622612..d1a3fae 100644 --- a/app/src/main/assets/markdownEditor/editor-mobile.min.html +++ b/app/src/main/assets/markdownEditor/editor-mobile.min.html @@ -10,5 +10,12 @@ $('#title').attr('placeholderText', '标题') $('#wmd-input-sub').attr('placeholderText', '内容') } + + function setTitleAndContent(title, content) { + document.getElementById('title').innerHTML = title; + ZSSEditor.getField('mdEditor').setHTML(content); + hostApp.loadCompleted(); + return ""; + } \ No newline at end of file diff --git a/app/src/main/java/org/houxg/leamonax/editor/Editor.java b/app/src/main/java/org/houxg/leamonax/editor/Editor.java index 81df414..4496b50 100644 --- a/app/src/main/java/org/houxg/leamonax/editor/Editor.java +++ b/app/src/main/java/org/houxg/leamonax/editor/Editor.java @@ -73,6 +73,8 @@ public String getSelection() { return ""; } + public abstract void setTitleAndContent(String title, String content); + public interface EditorListener { void onPageLoaded(); void onClickedLink(String title, String url); diff --git a/app/src/main/java/org/houxg/leamonax/editor/HostApp.java b/app/src/main/java/org/houxg/leamonax/editor/HostApp.java new file mode 100644 index 0000000..1d84a25 --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/editor/HostApp.java @@ -0,0 +1,15 @@ +package org.houxg.leamonax.editor; + +import android.webkit.JavascriptInterface; + +import org.greenrobot.eventbus.EventBus; +import org.houxg.leamonax.model.CompleteEvent; + +public class HostApp { + + @JavascriptInterface + public void loadCompleted() { + EventBus.getDefault().post(new CompleteEvent()); + } + +} diff --git a/app/src/main/java/org/houxg/leamonax/editor/MarkdownEditor.java b/app/src/main/java/org/houxg/leamonax/editor/MarkdownEditor.java index 8f784bc..1adfc68 100644 --- a/app/src/main/java/org/houxg/leamonax/editor/MarkdownEditor.java +++ b/app/src/main/java/org/houxg/leamonax/editor/MarkdownEditor.java @@ -25,6 +25,7 @@ public void init(WebView view) { mWebView = view; mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY); mWebView.getSettings().setJavaScriptEnabled(true); + mWebView.addJavascriptInterface(new HostApp(), "hostApp"); mWebView.setWebViewClient(new Editor.EditorClient()); mWebView.setWebChromeClient(new WebChromeClient()); mWebView.loadUrl("file:///android_asset/markdownEditor/editor-mobile.min.html?lang=" + Locale.getDefault().getLanguage()); @@ -71,6 +72,12 @@ public String getContent() { return content; } + @Override + public void setTitleAndContent(String title, String content) { + String script = String.format(Locale.US, "setTitleAndContent('%s', '%s');", HtmlUtils.escapeHtml(title), HtmlUtils.escapeHtml(content)); + new JsRunner().get(mWebView, script); + } + @Override public void insertImage(String title, String url) { execJs(String.format(Locale.US, "ZSSEditor.insertImage('%s', '%s');", url, title)); diff --git a/app/src/main/java/org/houxg/leamonax/editor/RichTextEditor.java b/app/src/main/java/org/houxg/leamonax/editor/RichTextEditor.java index ece15e8..bf7800d 100644 --- a/app/src/main/java/org/houxg/leamonax/editor/RichTextEditor.java +++ b/app/src/main/java/org/houxg/leamonax/editor/RichTextEditor.java @@ -29,6 +29,7 @@ public void init(WebView view) { mWebView = view; mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY); mWebView.getSettings().setJavaScriptEnabled(true); + mWebView.addJavascriptInterface(new HostApp(), "hostApp"); mWebView.setWebViewClient(new EditorClient()); mWebView.setWebChromeClient(new EditorChromeClient()); mWebView.addJavascriptInterface(new TinnyMceCallback(this), JS_CALLBACK_HANDLER); @@ -81,6 +82,14 @@ public String getContent() { return content; } + @Override + public void setTitleAndContent(String title, String content) { + content = HtmlUtils.escapeHtml(content); + XLog.i(TAG + "escaped=" + content); + String script = String.format(Locale.US, "setTitleAndContent('%s', '%s');", title, content); + new JsRunner().get(mWebView, script); + } + @Override public void insertImage(String title, String url) { execJs(String.format(Locale.US, "insertImage('%s');", url)); diff --git a/app/src/main/java/org/houxg/leamonax/model/CompleteEvent.java b/app/src/main/java/org/houxg/leamonax/model/CompleteEvent.java new file mode 100644 index 0000000..26ebc14 --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/model/CompleteEvent.java @@ -0,0 +1,4 @@ +package org.houxg.leamonax.model; + +public class CompleteEvent { +} diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java b/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java index acbeee6..9c5f272 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java @@ -166,6 +166,10 @@ public void setContent(String content) { mEditor.setContent(content); } + public void setTitleAndContent(String title, String content) { + mEditor.setTitleAndContent(title, content); + } + public String getTitle() { return mEditor.getTitle(); } diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java b/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java index 46e6a00..8b18363 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java @@ -14,10 +14,14 @@ import com.elvishew.xlog.XLog; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; import org.houxg.leamonax.Leamonax; import org.houxg.leamonax.R; import org.houxg.leamonax.ReadableException; import org.houxg.leamonax.database.NoteDataStore; +import org.houxg.leamonax.model.CompleteEvent; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Tag; import org.houxg.leamonax.service.NoteFileService; @@ -58,6 +62,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi private Wrapper mOriginal; private Wrapper mModified; private boolean mIsNewNote; + private boolean mIsMenuSaveEnabled = false; private LeaViewPager mPager; @@ -81,6 +86,7 @@ protected void onCreate(Bundle savedInstanceState) { finish(); return; } + EventBus.getDefault().register(this); mIsNewNote = getIntent().getBooleanExtra(EXT_IS_NEW_NOTE, false); mOriginal = new Wrapper(NoteDataStore.getByLocalId(noteLocalId)); mModified = new Wrapper(NoteDataStore.getByLocalId(noteLocalId)); @@ -114,12 +120,17 @@ protected void onDestroy() { intent.setAction("android.appwidget.action.APPWIDGET_UPDATE"); this.sendBroadcast(intent); super.onDestroy(); + EventBus.getDefault().unregister(this); } @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { case R.id.action_save: + if (!mIsMenuSaveEnabled) { + ToastUtils.show(this, R.string.note_not_load_completed); + return true; + } filterUnchanged() .doOnNext(new Action1() { @Override @@ -323,8 +334,12 @@ public Uri createAttach(String filePath) { @Override public void onInitialized() { - mEditorFragment.setTitle(mModified.note.getTitle()); - mEditorFragment.setContent(mModified.note.getContent()); + mEditorFragment.setTitleAndContent(mModified.note.getTitle(), mModified.note.getContent()); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(CompleteEvent event) { + mIsMenuSaveEnabled = true; } @Override diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 2c7bc67..b0df1cc 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -121,4 +121,5 @@ 允许 需要升级蚂蚁笔记账户 选择图片 + 笔记未加载完,不能保存 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 47a9f73..3d15663 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,4 +123,5 @@ allow need upgrade Leanote account Select Picture + Notes are not loaded and cannot be saved