add: progress bar and large file support for editor
This commit is contained in:
1 parent
bcbbfd1ec6
commit
8f3d88c94c
4 files changed
+69
-21
No files matched your search
@@ -4,30 +4,33 @@ import android.content.Context;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.support.annotation.RequiresApi;
|
import android.support.annotation.RequiresApi;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Log;
|
import android.util.TypedValue;
|
||||||
import android.view.MotionEvent;
|
import android.view.Gravity;
|
||||||
import android.view.View;
|
|
||||||
import android.webkit.JavascriptInterface;
|
import android.webkit.JavascriptInterface;
|
||||||
import android.webkit.WebChromeClient;
|
import android.webkit.WebChromeClient;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
import android.widget.ScrollView;
|
|
||||||
|
|
||||||
import com.stardust.pio.PFile;
|
import com.stardust.pio.PFile;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringEscapeUtils;
|
|
||||||
import org.jdeferred.Deferred;
|
import org.jdeferred.Deferred;
|
||||||
import org.jdeferred.DoneCallback;
|
import org.jdeferred.DoneCallback;
|
||||||
import org.jdeferred.impl.DeferredObject;
|
import org.jdeferred.impl.DeferredObject;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
import io.reactivex.Observable;
|
import io.reactivex.Observable;
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||||
|
import io.reactivex.annotations.NonNull;
|
||||||
|
import io.reactivex.functions.Consumer;
|
||||||
|
import io.reactivex.schedulers.Schedulers;
|
||||||
import io.reactivex.subjects.PublishSubject;
|
import io.reactivex.subjects.PublishSubject;
|
||||||
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/9/27.
|
* Created by Stardust on 2017/9/27.
|
||||||
@@ -35,9 +38,7 @@ import io.reactivex.subjects.PublishSubject;
|
|||||||
|
|
||||||
public class CodeMirrorEditor extends FrameLayout {
|
public class CodeMirrorEditor extends FrameLayout {
|
||||||
|
|
||||||
|
private static final String LOG_TAG = "CodeMirrorEditor";
|
||||||
private static String[] sAvailableThemes;
|
|
||||||
private String mTheme = "neo";
|
|
||||||
|
|
||||||
public interface Callback {
|
public interface Callback {
|
||||||
void onChange();
|
void onChange();
|
||||||
@@ -45,6 +46,9 @@ public class CodeMirrorEditor extends FrameLayout {
|
|||||||
void updateCodeCompletion(int fromLine, int fromCh, int toLine, int toCh, String[] list);
|
void updateCodeCompletion(int fromLine, int fromCh, int toLine, int toCh, String[] list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String[] sAvailableThemes;
|
||||||
|
private String mTheme = "neo";
|
||||||
|
private MaterialProgressBar mProgressBar;
|
||||||
private WebView mWebView;
|
private WebView mWebView;
|
||||||
private JavaScriptBridge mJavaScriptBridge = new JavaScriptBridge();
|
private JavaScriptBridge mJavaScriptBridge = new JavaScriptBridge();
|
||||||
private Callback mCallback;
|
private Callback mCallback;
|
||||||
@@ -116,9 +120,18 @@ public class CodeMirrorEditor extends FrameLayout {
|
|||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
setupWebView();
|
setupWebView();
|
||||||
|
setupProgress();
|
||||||
mWebView.loadUrl("file:///android_asset/editor/index.html");
|
mWebView.loadUrl("file:///android_asset/editor/index.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupProgress() {
|
||||||
|
mProgressBar = new MaterialProgressBar(getContext());
|
||||||
|
int dp50 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
|
||||||
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(dp50, dp50);
|
||||||
|
params.gravity = Gravity.CENTER;
|
||||||
|
addView(mProgressBar, params);
|
||||||
|
}
|
||||||
|
|
||||||
private void setupWebView() {
|
private void setupWebView() {
|
||||||
mWebView = new WebView(getContext());
|
mWebView = new WebView(getContext());
|
||||||
setupWebSettings();
|
setupWebSettings();
|
||||||
@@ -141,20 +154,43 @@ public class CodeMirrorEditor extends FrameLayout {
|
|||||||
mWebView.setWebChromeClient(new WebChromeClient());
|
mWebView.setWebChromeClient(new WebChromeClient());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setProgress(boolean onProgress) {
|
||||||
|
mProgressBar.setVisibility(onProgress ? VISIBLE : GONE);
|
||||||
|
}
|
||||||
|
|
||||||
public void setText(final String text) {
|
public void setText(final String text) {
|
||||||
|
mJavaScriptBridge.mTextFromAndroid = text;
|
||||||
mPageFinished.promise().done(new DoneCallback<Void>() {
|
mPageFinished.promise().done(new DoneCallback<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void onDone(Void result) {
|
public void onDone(Void result) {
|
||||||
String escapedString = StringEscapeUtils.escapeEcmaScript(text);
|
evalJavaScript("editor.setValue(__bridge__.getText());");
|
||||||
evalJavaScript("editor.setValue('" + escapedString + "');");
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void loadFile(final File file) {
|
||||||
|
setProgress(true);
|
||||||
|
Observable.fromCallable(new Callable<String>() {
|
||||||
|
@Override
|
||||||
|
public String call() throws Exception {
|
||||||
|
return PFile.read(file);
|
||||||
|
}
|
||||||
|
}).subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(new Consumer<String>() {
|
||||||
|
@Override
|
||||||
|
public void accept(@NonNull String s) throws Exception {
|
||||||
|
setText(s);
|
||||||
|
setProgress(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Observable<String> getText() {
|
public Observable<String> getText() {
|
||||||
mJavaScriptBridge.mText = PublishSubject.create();
|
mJavaScriptBridge.mTextFromJs = PublishSubject.create();
|
||||||
evalJavaScript("__bridge__.setText(editor.getValue());");
|
evalJavaScript("__bridge__.setText(editor.getValue());");
|
||||||
return mJavaScriptBridge.mText;
|
return mJavaScriptBridge.mTextFromJs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -167,8 +203,12 @@ public class CodeMirrorEditor extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void evalJavaScript(String script) {
|
private void evalJavaScript(String script) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
|
mWebView.evaluateJavascript(script, null);
|
||||||
|
} else {
|
||||||
mWebView.loadUrl("javascript:" + script);
|
mWebView.loadUrl("javascript:" + script);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void replace(String text, int fromLine, int fromCh, int toLine, int toCh) {
|
public void replace(String text, int fromLine, int fromCh, int toLine, int toCh) {
|
||||||
String js = "editor.replaceRange('%s', {line: %d, ch: %d}, {line: %d, ch: %d})";
|
String js = "editor.replaceRange('%s', {line: %d, ch: %d}, {line: %d, ch: %d})";
|
||||||
@@ -177,17 +217,25 @@ public class CodeMirrorEditor extends FrameLayout {
|
|||||||
|
|
||||||
public class JavaScriptBridge {
|
public class JavaScriptBridge {
|
||||||
|
|
||||||
private PublishSubject<String> mText;
|
private PublishSubject<String> mTextFromJs;
|
||||||
|
private String mTextFromAndroid;
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public void setText(String text) {
|
public void setText(String text) {
|
||||||
if (mText != null) {
|
if (mTextFromJs != null) {
|
||||||
mText.onNext(text);
|
mTextFromJs.onNext(text);
|
||||||
mText.onComplete();
|
mTextFromJs.onComplete();
|
||||||
mText = null;
|
mTextFromJs = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JavascriptInterface
|
||||||
|
public String getText() {
|
||||||
|
String t = mTextFromAndroid;
|
||||||
|
mTextFromAndroid = null;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public void onChange() {
|
public void onChange() {
|
||||||
if (mCallback != null) {
|
if (mCallback != null) {
|
||||||
@@ -224,6 +272,7 @@ public class CodeMirrorEditor extends FrameLayout {
|
|||||||
super.onPageFinished(view, url);
|
super.onPageFinished(view, url);
|
||||||
mPageFinished.resolve(null);
|
mPageFinished.resolve(null);
|
||||||
evalJavaScript(INIT_SCRIPT);
|
evalJavaScript(INIT_SCRIPT);
|
||||||
|
setProgress(false);
|
||||||
// evalJavaScript("editor.setSize(" + mWebView.getMeasuredWidth() + ", " + mWebView.getMeasuredHeight() + ");");
|
// evalJavaScript("editor.setSize(" + mWebView.getMeasuredWidth() + ", " + mWebView.getMeasuredHeight() + ");");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ public class EditorView extends FrameLayout {
|
|||||||
if (mName == null) {
|
if (mName == null) {
|
||||||
mName = mFile.getName();
|
mName = mFile.getName();
|
||||||
}
|
}
|
||||||
mEditor.setText(PFile.read(mFile));
|
mEditor.loadFile(mFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
-1
@@ -36,7 +36,6 @@ import java.util.Map;
|
|||||||
* Created by Stardust on 2017/2/17.
|
* Created by Stardust on 2017/2/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// TODO: 2017/7/21 refactor
|
|
||||||
public class InputMethodEnhanceBar extends RecyclerView {
|
public class InputMethodEnhanceBar extends RecyclerView {
|
||||||
|
|
||||||
public interface OnHintClickListener {
|
public interface OnHintClickListener {
|
||||||
|
|||||||
@@ -71,6 +71,6 @@
|
|||||||
android:id="@+id/input_method_enhance_bar"
|
android:id="@+id/input_method_enhance_bar"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="35dp"
|
android:layout_height="35dp"
|
||||||
android:background="#e7ebec"/>
|
android:background="#77f2f3f7"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
Reference in new issue
Block a user