Skip to content

Commit a9296eb

Browse files
committed
恢复带选择的文字(fixed qinci#1)
1 parent a219100 commit a9296eb

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

lib/src/main/java/ren/qinc/edit/PerformEdit.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.support.annotation.NonNull;
2020
import android.text.Editable;
2121
import android.text.TextWatcher;
22+
import android.util.Log;
2223
import android.widget.EditText;
2324

2425
import java.util.Stack;
@@ -30,16 +31,20 @@
3031
public class PerformEdit {
3132
//操作序号(一次编辑可能对应多个操作,如替换文字,就是删除+插入)
3233
int index;
34+
//撤销栈
3335
Stack<Action> history = new Stack<>();
36+
//恢复栈
3437
Stack<Action> historyBack = new Stack<>();
3538

3639
private Editable editable;
40+
private EditText editText;
3741
//自动操作标志,防止重复回调,导致无限撤销
3842
private boolean flag = false;
3943

4044
public PerformEdit(@NonNull EditText editText) {
4145
CheckNull(editText, "EditText不能为空");
42-
editable = editText.getText();
46+
this.editable = editText.getText();
47+
this.editText = editText;
4348
editText.addTextChangedListener(new Watcher());
4449
}
4550

@@ -74,10 +79,12 @@ public final void undo() {
7479
historyBack.push(action);
7580
if (action.isAdd) {
7681
//撤销添加
77-
editable.delete(action.cursor, action.cursor + action.actionTarget.length());
82+
editable.delete(action.startCursor, action.startCursor + action.actionTarget.length());
83+
editText.setSelection(action.startCursor, action.startCursor);
7884
} else {
7985
//插销删除
80-
editable.insert(action.cursor, action.actionTarget);
86+
editable.insert(action.startCursor, action.actionTarget);
87+
editText.setSelection(action.startCursor, action.endCursor);
8188
}
8289
//释放操作
8390
flag = false;
@@ -98,10 +105,12 @@ public final void redo() {
98105
history.push(action);
99106
if (action.isAdd) {
100107
//恢复添加
101-
editable.insert(action.cursor, action.actionTarget);
108+
editable.insert(action.startCursor, action.actionTarget);
109+
editText.setSelection(action.startCursor, action.endCursor);
102110
} else {
103111
//恢复删除
104-
editable.delete(action.cursor, action.cursor + action.actionTarget.length());
112+
editable.delete(action.startCursor, action.startCursor + action.actionTarget.length());
113+
editText.setSelection(action.startCursor, action.startCursor);
105114
}
106115
flag = false;
107116
//判断是否是下一个动作是否和本动作是同一个操作
@@ -127,7 +136,7 @@ private class Watcher implements TextWatcher {
127136
*
128137
* @param s the s
129138
* @param start the start 起始光标
130-
* @param count the count 选择数量
139+
* @param count the endCursor 选择数量
131140
* @param after the after 替换增加的文字数
132141
*/
133142
@Override
@@ -136,9 +145,18 @@ public final void beforeTextChanged(CharSequence s, int start, int count, int af
136145
int end = start + count;
137146
if (end > start && end <= s.length()) {
138147
CharSequence charSequence = s.subSequence(start, end);
139-
//发生文字变化
148+
//删除了文字
140149
if (charSequence.length() > 0) {
141150
Action action = new Action(charSequence, start, false);
151+
if (count > 1) {
152+
//如果一次超过一个字符,说名用户选择了,然后替换或者删除操作
153+
action.setSelectCount(count);
154+
}else if(count==1&&count==after){
155+
//一个字符替换
156+
action.setSelectCount(count);
157+
}
158+
//还有一种情况:选择一个字符,然后删除(暂时没有考虑这种情况)
159+
142160
history.push(action);
143161
historyBack.clear();
144162
action.setIndex(++index);
@@ -152,17 +170,18 @@ public final void beforeTextChanged(CharSequence s, int start, int count, int af
152170
* @param s the s
153171
* @param start the start 起始光标
154172
* @param before the before 选择数量
155-
* @param count the count 添加的数量
173+
* @param count the endCursor 添加的数量
156174
*/
157175
@Override
158176
public final void onTextChanged(CharSequence s, int start, int before, int count) {
159177
if (flag) return;
160178
int end = start + count;
161179
if (end > start) {
162180
CharSequence charSequence = s.subSequence(start, end);
163-
//发生文字变化
181+
//添加文字
164182
if (charSequence.length() > 0) {
165183
Action action = new Action(charSequence, start, true);
184+
166185
history.push(action);
167186
historyBack.clear();
168187
if (before > 0) {
@@ -191,18 +210,25 @@ private class Action {
191210
/** 改变字符. */
192211
CharSequence actionTarget;
193212
/** 光标位置. */
194-
int cursor;
213+
int startCursor;
214+
int endCursor;
195215
/** 标志增加操作. */
196216
boolean isAdd;
197217
/** 操作序号. */
198218
int index;
199219

200-
public Action(CharSequence actionTag, int cursor, boolean add) {
220+
221+
public Action(CharSequence actionTag, int startCursor, boolean add) {
201222
this.actionTarget = actionTag;
202-
this.cursor = cursor;
223+
this.startCursor = startCursor;
224+
this.endCursor = startCursor;
203225
this.isAdd = add;
204226
}
205227

228+
public void setSelectCount(int count) {
229+
this.endCursor = endCursor + count;
230+
}
231+
206232
public void setIndex(int index) {
207233
this.index = index;
208234
}

0 commit comments

Comments
 (0)