Skip to content

Commit

Permalink
编辑笔记页面: 笔记加载完,才能保存
Browse files Browse the repository at this point in the history
  • Loading branch information
xingstarx committed May 20, 2018
1 parent 2a5c828 commit 1e4c2cc
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/src/main/assets/RichTextEditor/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
</script>

</html>
7 changes: 7 additions & 0 deletions app/src/main/assets/markdownEditor/editor-mobile.min.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
</script>
</body></html>
2 changes: 2 additions & 0 deletions app/src/main/java/org/houxg/leamonax/editor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/org/houxg/leamonax/editor/HostApp.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/houxg/leamonax/model/CompleteEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.houxg.leamonax.model;

public class CompleteEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
19 changes: 17 additions & 2 deletions app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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));
Expand Down Expand Up @@ -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<Wrapper>() {
@Override
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@
<string name="allow">允许</string>
<string name="lea_api_error_need_upgrade_account">需要升级蚂蚁笔记账户</string>
<string name="webview_select_picture">选择图片</string>
<string name="note_not_load_completed">笔记未加载完,不能保存</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@
<string name="allow">allow</string>
<string name="lea_api_error_need_upgrade_account">need upgrade Leanote account</string>
<string name="webview_select_picture">Select Picture</string>
<string name="note_not_load_completed">Notes are not loaded and cannot be saved</string>
</resources>

0 comments on commit 1e4c2cc

Please sign in to comment.