feat: editor toolbar menu item status sync

This commit is contained in:
hyb1996 2018-02-23 12:51:34 +08:00
parent 6f9b302e9b
commit 69861e8b4f
3 changed files with 30 additions and 21 deletions

View File

@ -8,6 +8,7 @@ import android.content.IntentFilter;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
@ -38,6 +39,7 @@ import com.stardust.scriptdroid.ui.edit.theme.Theme;
import com.stardust.scriptdroid.ui.edit.theme.Themes;
import com.stardust.scriptdroid.ui.log.LogActivity_;
import com.stardust.scriptdroid.ui.widget.EWebView;
import com.stardust.scriptdroid.ui.widget.SimpleTextWatcher;
import com.stardust.scriptdroid.ui.widget.ToolbarMenuItem;
import com.stardust.util.BackPressedHandler;
import com.stardust.widget.ViewSwitcher;
@ -99,8 +101,6 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
@ViewById(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
private static final String KEY_EDITOR_THEME = "我...深爱着...你呀...17.9.28";
private String mName;
private File mFile;
private boolean mReadOnly = false;
@ -111,7 +111,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
private BroadcastReceiver mOnRunFinishedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_ON_EXECUTION_FINISHED)) {
if (ACTION_ON_EXECUTION_FINISHED.equals(intent.getAction())) {
mScriptExecution = null;
setMenuItemStatus(R.id.run, true);
String msg = intent.getStringExtra(Scripts.EXTRA_EXCEPTION_MESSAGE);
@ -252,12 +252,12 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
private void setUpEditor() {
mEditor.setCursorChangeCallback((line, cursor) -> {
mEditor.getCodeEditText().addTextChangedListener(new SimpleTextWatcher(s -> {
setMenuItemStatus(R.id.save, mEditor.isTextChanged());
autoComplete(line, cursor);
});
setMenuItemStatus(R.id.undo, mEditor.canUndo());
setMenuItemStatus(R.id.redo, mEditor.canRedo());
}));
mEditor.setCursorChangeCallback(this::autoComplete);
}
private void autoComplete(String line, int cursor) {
@ -319,6 +319,9 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
.doOnNext(s -> {
mEditor.markTextAsSaved();
setMenuItemStatus(R.id.save, false);
setMenuItemStatus(R.id.undo, false);
setMenuItemStatus(R.id.redo, false);
});
}

View File

@ -10,9 +10,6 @@ import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.ui.edit.theme.Theme;
import com.stardust.util.ClipboardUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -37,6 +34,7 @@ import io.reactivex.Observable;
*/
public class CodeEditor extends HVScrollView {
public interface CursorChangeCallback {
void onCursorChange(String line, int ch);
@ -48,7 +46,7 @@ public class CodeEditor extends HVScrollView {
private String mKeywords;
private int mFoundIndex = -1;
private CodeEditText mCodeEditText;
private PreformEdit mPreformEdit;
private TextViewRedoUndo mTextViewRedoUndo;
private JavaScriptHighlighter mJavaScriptHighlighter;
private Theme mTheme;
private JsBeautifier mJsBeautifier;
@ -69,7 +67,7 @@ public class CodeEditor extends HVScrollView {
setFillViewport(true);
inflate(getContext(), R.layout.code_editor, this);
mCodeEditText = (CodeEditText) findViewById(R.id.code_edit_text);
mPreformEdit = new PreformEdit(mCodeEditText);
mTextViewRedoUndo = new TextViewRedoUndo(mCodeEditText);
mJavaScriptHighlighter = new JavaScriptHighlighter(mTheme, mCodeEditText);
setTheme(Theme.getDefault(getContext()));
mJsBeautifier = new JsBeautifier(this, "js/beautify.js");
@ -147,7 +145,15 @@ public class CodeEditor extends HVScrollView {
public boolean isTextChanged() {
return mPreformEdit.isTextChanged();
return mTextViewRedoUndo.isTextChanged();
}
public boolean canUndo() {
return mTextViewRedoUndo.canUndo();
}
public boolean canRedo(){
return mTextViewRedoUndo.canRedo();
}
@Override
@ -162,7 +168,7 @@ public class CodeEditor extends HVScrollView {
public void setInitialText(String text) {
mCodeEditText.setText(text);
mPreformEdit.setDefaultText(text);
mTextViewRedoUndo.setDefaultText(text);
}
public void jumpTo(int line, int col) {
@ -205,11 +211,11 @@ public class CodeEditor extends HVScrollView {
public void undo() {
mPreformEdit.undo();
mTextViewRedoUndo.undo();
}
public void redo() {
mPreformEdit.redo();
mTextViewRedoUndo.redo();
}
@ -304,7 +310,7 @@ public class CodeEditor extends HVScrollView {
public void markTextAsSaved() {
mPreformEdit.clearHistory();
mTextViewRedoUndo.clearHistory();
}
}

View File

@ -27,7 +27,7 @@ import java.util.Stack;
* 撤销和恢复撤销
* Created by 沈钦赐 on 16/6/23.
*/
public class PreformEdit {
public class TextViewRedoUndo {
//操作序号(一次编辑可能对应多个操作如替换文字就是删除+插入)
int index;
//撤销栈
@ -40,7 +40,7 @@ public class PreformEdit {
//自动操作标志防止重复回调,导致无限撤销
private boolean flag = false;
public PreformEdit(@NonNull EditText editText) {
public TextViewRedoUndo(@NonNull EditText editText) {
CheckNull(editText, "EditText不能为空");
this.editable = editText.getText();
this.editText = editText;
@ -219,7 +219,7 @@ public class PreformEdit {
editable = s;
onEditableChanged(s);
}
PreformEdit.this.onTextChanged(s);
TextViewRedoUndo.this.onTextChanged(s);
}
}