Skip to content

Commit 1e4c2cc

Browse files
committed
编辑笔记页面: 笔记加载完,才能保存
1 parent 2a5c828 commit 1e4c2cc

11 files changed

Lines changed: 74 additions & 2 deletions

File tree

app/src/main/assets/RichTextEditor/editor.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@
242242
function isChildOfBody(elm) {
243243
return tinyMCE.editors[0].$.contains(tinyMCE.editors[0].getBody(), elm);
244244
}
245+
246+
function setTitleAndContent(title, content) {
247+
setTitle(title);
248+
tinyMCE.editors[0].setContent(content);
249+
hostApp.loadCompleted();
250+
return "";
251+
}
245252
</script>
246253

247254
</html>

app/src/main/assets/markdownEditor/editor-mobile.min.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@
1010
$('#title').attr('placeholderText', '标题')
1111
$('#wmd-input-sub').attr('placeholderText', '内容')
1212
}
13+
14+
function setTitleAndContent(title, content) {
15+
document.getElementById('title').innerHTML = title;
16+
ZSSEditor.getField('mdEditor').setHTML(content);
17+
hostApp.loadCompleted();
18+
return "";
19+
}
1320
</script>
1421
</body></html>

app/src/main/java/org/houxg/leamonax/editor/Editor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public String getSelection() {
7373
return "";
7474
}
7575

76+
public abstract void setTitleAndContent(String title, String content);
77+
7678
public interface EditorListener {
7779
void onPageLoaded();
7880
void onClickedLink(String title, String url);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.houxg.leamonax.editor;
2+
3+
import android.webkit.JavascriptInterface;
4+
5+
import org.greenrobot.eventbus.EventBus;
6+
import org.houxg.leamonax.model.CompleteEvent;
7+
8+
public class HostApp {
9+
10+
@JavascriptInterface
11+
public void loadCompleted() {
12+
EventBus.getDefault().post(new CompleteEvent());
13+
}
14+
15+
}

app/src/main/java/org/houxg/leamonax/editor/MarkdownEditor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void init(WebView view) {
2525
mWebView = view;
2626
mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
2727
mWebView.getSettings().setJavaScriptEnabled(true);
28+
mWebView.addJavascriptInterface(new HostApp(), "hostApp");
2829
mWebView.setWebViewClient(new Editor.EditorClient());
2930
mWebView.setWebChromeClient(new WebChromeClient());
3031
mWebView.loadUrl("file:///android_asset/markdownEditor/editor-mobile.min.html?lang=" + Locale.getDefault().getLanguage());
@@ -71,6 +72,12 @@ public String getContent() {
7172
return content;
7273
}
7374

75+
@Override
76+
public void setTitleAndContent(String title, String content) {
77+
String script = String.format(Locale.US, "setTitleAndContent('%s', '%s');", HtmlUtils.escapeHtml(title), HtmlUtils.escapeHtml(content));
78+
new JsRunner().get(mWebView, script);
79+
}
80+
7481
@Override
7582
public void insertImage(String title, String url) {
7683
execJs(String.format(Locale.US, "ZSSEditor.insertImage('%s', '%s');", url, title));

app/src/main/java/org/houxg/leamonax/editor/RichTextEditor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void init(WebView view) {
2929
mWebView = view;
3030
mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
3131
mWebView.getSettings().setJavaScriptEnabled(true);
32+
mWebView.addJavascriptInterface(new HostApp(), "hostApp");
3233
mWebView.setWebViewClient(new EditorClient());
3334
mWebView.setWebChromeClient(new EditorChromeClient());
3435
mWebView.addJavascriptInterface(new TinnyMceCallback(this), JS_CALLBACK_HANDLER);
@@ -81,6 +82,14 @@ public String getContent() {
8182
return content;
8283
}
8384

85+
@Override
86+
public void setTitleAndContent(String title, String content) {
87+
content = HtmlUtils.escapeHtml(content);
88+
XLog.i(TAG + "escaped=" + content);
89+
String script = String.format(Locale.US, "setTitleAndContent('%s', '%s');", title, content);
90+
new JsRunner().get(mWebView, script);
91+
}
92+
8493
@Override
8594
public void insertImage(String title, String url) {
8695
execJs(String.format(Locale.US, "insertImage('%s');", url));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.houxg.leamonax.model;
2+
3+
public class CompleteEvent {
4+
}

app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public void setContent(String content) {
166166
mEditor.setContent(content);
167167
}
168168

169+
public void setTitleAndContent(String title, String content) {
170+
mEditor.setTitleAndContent(title, content);
171+
}
172+
169173
public String getTitle() {
170174
return mEditor.getTitle();
171175
}

app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
import com.elvishew.xlog.XLog;
1616

17+
import org.greenrobot.eventbus.EventBus;
18+
import org.greenrobot.eventbus.Subscribe;
19+
import org.greenrobot.eventbus.ThreadMode;
1720
import org.houxg.leamonax.Leamonax;
1821
import org.houxg.leamonax.R;
1922
import org.houxg.leamonax.ReadableException;
2023
import org.houxg.leamonax.database.NoteDataStore;
24+
import org.houxg.leamonax.model.CompleteEvent;
2125
import org.houxg.leamonax.model.Note;
2226
import org.houxg.leamonax.model.Tag;
2327
import org.houxg.leamonax.service.NoteFileService;
@@ -58,6 +62,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
5862
private Wrapper mOriginal;
5963
private Wrapper mModified;
6064
private boolean mIsNewNote;
65+
private boolean mIsMenuSaveEnabled = false;
6166

6267
private LeaViewPager mPager;
6368

@@ -81,6 +86,7 @@ protected void onCreate(Bundle savedInstanceState) {
8186
finish();
8287
return;
8388
}
89+
EventBus.getDefault().register(this);
8490
mIsNewNote = getIntent().getBooleanExtra(EXT_IS_NEW_NOTE, false);
8591
mOriginal = new Wrapper(NoteDataStore.getByLocalId(noteLocalId));
8692
mModified = new Wrapper(NoteDataStore.getByLocalId(noteLocalId));
@@ -114,12 +120,17 @@ protected void onDestroy() {
114120
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
115121
this.sendBroadcast(intent);
116122
super.onDestroy();
123+
EventBus.getDefault().unregister(this);
117124
}
118125

119126
@Override
120127
public boolean onOptionsItemSelected(final MenuItem item) {
121128
switch (item.getItemId()) {
122129
case R.id.action_save:
130+
if (!mIsMenuSaveEnabled) {
131+
ToastUtils.show(this, R.string.note_not_load_completed);
132+
return true;
133+
}
123134
filterUnchanged()
124135
.doOnNext(new Action1<Wrapper>() {
125136
@Override
@@ -323,8 +334,12 @@ public Uri createAttach(String filePath) {
323334

324335
@Override
325336
public void onInitialized() {
326-
mEditorFragment.setTitle(mModified.note.getTitle());
327-
mEditorFragment.setContent(mModified.note.getContent());
337+
mEditorFragment.setTitleAndContent(mModified.note.getTitle(), mModified.note.getContent());
338+
}
339+
340+
@Subscribe(threadMode = ThreadMode.MAIN)
341+
public void onEvent(CompleteEvent event) {
342+
mIsMenuSaveEnabled = true;
328343
}
329344

330345
@Override

app/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,5 @@
121121
<string name="allow">允许</string>
122122
<string name="lea_api_error_need_upgrade_account">需要升级蚂蚁笔记账户</string>
123123
<string name="webview_select_picture">选择图片</string>
124+
<string name="note_not_load_completed">笔记未加载完,不能保存</string>
124125
</resources>

0 commit comments

Comments
 (0)