Skip to content

Commit 979c1e6

Browse files
committed
Added support for most of the text and document related features of Ace.
1 parent 15ae76c commit 979c1e6

File tree

3 files changed

+190
-17
lines changed

3 files changed

+190
-17
lines changed

aceeditor/src/main/java/com/susmit/aceeditor/AceEditor.java

Lines changed: 173 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@
1616
import android.webkit.WebView;
1717
import android.webkit.WebViewClient;
1818
import android.widget.PopupWindow;
19+
import android.widget.Toast;
20+
21+
import org.json.JSONArray;
22+
import org.json.JSONException;
23+
import org.json.JSONObject;
24+
25+
import java.util.Iterator;
26+
import java.util.LinkedList;
27+
import java.util.List;
1928

2029
public class AceEditor extends WebView
2130
{
2231
Context context;
2332
private PopupWindow pw;
2433
private View popupView;
34+
private LayoutInflater inflater;
2535

2636
private ResultReceivedListener received;
2737
private OnLoadedEditorListener onLoadedEditorListener;
38+
private OnSelectionActionPerformedListener onSelectionActionPerformedListener;
2839

29-
private LayoutInflater inflater;
3040
private float x;
3141
private float y;
3242
private boolean actAfterSelect;
3343
private int requestedValue;
44+
private String findString;
3445

3546
private boolean loadedUI;
3647

@@ -62,7 +73,7 @@ private void initialize()
6273

6374
setResultReceivedListener(new ResultReceivedListener() {
6475
@Override
65-
public void onReceived(String text, int FLAG_VALUE) {
76+
public void onReceived(int FLAG_VALUE, String... results) {
6677

6778
}
6879
});
@@ -74,11 +85,66 @@ public void onCreate() {
7485
}
7586
});
7687

88+
setOnSelectionActionPerformedListener(new OnSelectionActionPerformedListener() {
89+
@Override
90+
public void onSelectionFinished(boolean usingSelectAllOption) {
91+
92+
}
93+
94+
@Override
95+
public void onCut() {
96+
97+
}
98+
99+
@Override
100+
public void onCopy() {
101+
102+
}
103+
104+
@Override
105+
public void onPaste() {
106+
107+
}
108+
109+
@Override
110+
public void onUndo() {
111+
112+
}
113+
114+
@Override
115+
public void onRedo() {
116+
117+
}
118+
});
119+
77120
setWebChromeClient(new WebChromeClient() {
78121
@Override
79122
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
80123
result.confirm();
81-
received.onReceived(message, requestedValue);
124+
List<String> results = new LinkedList<>();
125+
try {
126+
JSONArray objArr = new JSONArray(message);
127+
for(int i = 0; i<objArr.length(); i++) {
128+
results.add(String.valueOf(objArr.get(i)));
129+
}
130+
}
131+
catch (JSONException e)
132+
{
133+
try {
134+
JSONObject obj = new JSONObject(message);
135+
Iterator<String> keyInerator = obj.keys();
136+
while (keyInerator.hasNext()) {
137+
String key = keyInerator.next();
138+
results.add(String.valueOf(obj.get(key)));
139+
}
140+
}
141+
catch (JSONException e1) {
142+
results.add(message);
143+
}
144+
}
145+
String []res = new String[results.size()];
146+
res = results.toArray(res);
147+
received.onReceived(requestedValue, res);
82148
return true;
83149
}
84150
});
@@ -98,6 +164,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
98164
return false;
99165
}
100166
});
167+
101168
setOnTouchListener(new View.OnTouchListener()
102169
{
103170
float downTime;
@@ -119,9 +186,11 @@ public boolean onTouch(View v, MotionEvent event) {
119186
y = event.getY();
120187
if(tot <= 500)
121188
v.performClick();
122-
else
123-
if(actAfterSelect)
124-
pw.showAtLocation(v, Gravity.NO_GRAVITY,(int)x - getResources().getDisplayMetrics().widthPixels/3,getResources().getDisplayMetrics().heightPixels/12 + (int)y);
189+
else {
190+
if (actAfterSelect)
191+
pw.showAtLocation(v, Gravity.NO_GRAVITY, (int) x - getResources().getDisplayMetrics().widthPixels / 3, getResources().getDisplayMetrics().heightPixels / 12 + (int) y);
192+
onSelectionActionPerformedListener.onSelectionFinished(false);
193+
}
125194
break;
126195
case MotionEvent.ACTION_MOVE:
127196
xtimes = (int) (x - event.getX()) / 25;
@@ -197,6 +266,7 @@ public void onClick(View v) {
197266
AceEditor.this.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_X, 0, KeyEvent.META_CTRL_ON));
198267
AceEditor.this.requestFocus();
199268
pw.dismiss();
269+
onSelectionActionPerformedListener.onCut();
200270
}
201271
});
202272
popupView.findViewById(R.id.copy).setOnClickListener(new OnClickListener() {
@@ -205,6 +275,7 @@ public void onClick(View v) {
205275
AceEditor.this.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_C, 0, KeyEvent.META_CTRL_ON));
206276
AceEditor.this.requestFocus();
207277
pw.dismiss();
278+
onSelectionActionPerformedListener.onCopy();
208279
}
209280
});
210281
popupView.findViewById(R.id.paste).setOnClickListener(new OnClickListener() {
@@ -213,25 +284,29 @@ public void onClick(View v) {
213284
AceEditor.this.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_V, 0, KeyEvent.META_CTRL_ON));
214285
AceEditor.this.requestFocus();
215286
pw.dismiss();
287+
onSelectionActionPerformedListener.onPaste();
216288
}
217289
});
218290
popupView.findViewById(R.id.selectall).setOnClickListener(new OnClickListener() {
219291
@Override
220292
public void onClick(View v) {
221293
AceEditor.this.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A, 0, KeyEvent.META_CTRL_ON));
222294
popupView.findViewById(R.id.prevOptSet).performClick();
295+
onSelectionActionPerformedListener.onSelectionFinished(true);
223296
}
224297
});
225298
popupView.findViewById(R.id.undo).setOnClickListener(new OnClickListener() {
226299
@Override
227300
public void onClick(View v) {
228301
AceEditor.this.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_Z, 0, KeyEvent.META_CTRL_ON));
302+
onSelectionActionPerformedListener.onUndo();
229303
}
230304
});
231305
popupView.findViewById(R.id.redo).setOnClickListener(new OnClickListener() {
232306
@Override
233307
public void onClick(View v) {
234308
AceEditor.this.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_Z, 0, KeyEvent.META_CTRL_ON|KeyEvent.META_SHIFT_ON));
309+
onSelectionActionPerformedListener.onRedo();
235310
}
236311
});
237312
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
@@ -254,10 +329,9 @@ public void setOnLoadedEditorListener(OnLoadedEditorListener listener)
254329
this.onLoadedEditorListener = listener;
255330
}
256331

257-
public void requestText()
332+
public void setOnSelectionActionPerformedListener(OnSelectionActionPerformedListener listener)
258333
{
259-
requestedValue = Request.VALUE_TEXT;
260-
loadUrl("javascript:alert(editor.getValue());");
334+
this.onSelectionActionPerformedListener = listener;
261335
}
262336

263337
public void showOptionsAfterSelection(boolean show)
@@ -280,16 +354,97 @@ public void insertTextAtCursor(String text)
280354
loadUrl("javascript:editor.insert(\"" + text +"\");");
281355
}
282356

283-
public void requestLines()
357+
358+
public void requestText()
284359
{
285-
requestedValue = Request.VALUE_LINES;
360+
requestedValue = Request.TEXT_REQUEST;
361+
loadUrl("javascript:alert(editor.getValue());");
362+
}
363+
364+
public void requestRowCount()
365+
{
366+
requestedValue = Request.ROW_COUNT_REQUEST;
286367
loadUrl("javascript:alert(editor.session.getLength());");
287368
}
288369

289370
public void requsetSelectedText()
290371
{
291-
requestedValue = Request.VALUE_SELECTED_TEXT;
292-
loadUrl("javascript:alert(editor.getCopyText());");
372+
requestedValue = Request.TEXT_REQUEST;
373+
loadUrl("javascript:alert(editor.getSelectedText());");
374+
}
375+
376+
public void requestCursorCoords()
377+
{
378+
requestedValue = Request.CURSOR_COORDS_REQUEST;
379+
loadUrl("javascript:alert(JSON.stringify(editor.getCursorPosition()))");
380+
}
381+
382+
public void requestLine(int lineNumber)
383+
{
384+
requestedValue = Request.TEXT_REQUEST;
385+
loadUrl("javascript:alert(editor.session.getLine("+ String.valueOf(lineNumber) + "));");
386+
}
387+
388+
public void requestLinesBetween(int startLine, int endLine)
389+
{
390+
requestedValue = Request.MULTIPLE_LINES_REQUEST;
391+
loadUrl("javascript:alert(JSON.stringify(editor.session.getLines("+ String.valueOf(startLine) + ", "+ String.valueOf(endLine) + ")));");
392+
}
393+
394+
public void startFind(String toFind, boolean backwards, boolean wrap, boolean caseSensitive, boolean wholeWord)
395+
{
396+
findString = toFind;
397+
loadUrl("javascript:editor.find('" + toFind + "', backwards: "+ String.valueOf(backwards) +
398+
", wrap: "+ String.valueOf(wrap) +
399+
",caseSensitive: "+ String.valueOf(caseSensitive) +
400+
",wholeWord: "+ String.valueOf(wholeWord) +",regExp: false});");
401+
}
402+
403+
public void findNext()
404+
{
405+
if(findString == null) {
406+
return;
407+
}
408+
loadUrl("javascript:editor.findNext();");
409+
}
410+
411+
public void findNext(String errorToastMessage, int showFor)
412+
{
413+
if(findString == null) {
414+
Toast.makeText(context,errorToastMessage,showFor).show();
415+
return;
416+
}
417+
loadUrl("javascript:editor.findNext();");
418+
}
419+
420+
public void findPrevious()
421+
{
422+
if(findString == null) {
423+
return;
424+
}
425+
loadUrl("javascript:editor.findPrevious();");
426+
}
427+
428+
public void findPrevious(String toastMessage, int showFor)
429+
{
430+
if(findString == null) {
431+
Toast.makeText(context,toastMessage,showFor).show();
432+
return;
433+
}
434+
loadUrl("javascript:editor.findPrevious();");
435+
}
436+
437+
public void replace(String replaceText, boolean replaceAll)
438+
{
439+
if(replaceAll)
440+
loadUrl("javascript:editor.replaceAll('" + replaceText + "');");
441+
else
442+
loadUrl("javascript:editor.replace('" + replaceText + "');");
443+
}
444+
445+
public void endFind()
446+
{
447+
findString = null;
293448
}
294449

295450
public void setTheme(Theme theme)
@@ -303,9 +458,11 @@ public void setMode(Mode mode)
303458
}
304459

305460
public static class Request{
306-
public static int VALUE_TEXT = 0;
307-
public static int VALUE_LINES = 1;
308-
public static int VALUE_SELECTED_TEXT = 2;
461+
public static int GENERIC_REQUEST = 0;
462+
public static int TEXT_REQUEST = 1;
463+
public static int ROW_COUNT_REQUEST = 2;
464+
public static int CURSOR_COORDS_REQUEST = 3;
465+
public static int MULTIPLE_LINES_REQUEST = 4;
309466
}
310467

311468
public static enum Theme
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.susmit.aceeditor;
2+
3+
/**
4+
* Created by susmit on 26/1/18.
5+
*/
6+
7+
public interface OnSelectionActionPerformedListener {
8+
9+
void onSelectionFinished(boolean usingSelectAllOption);
10+
void onCut();
11+
void onCopy();
12+
void onPaste();
13+
void onUndo();
14+
void onRedo();
15+
16+
}

aceeditor/src/main/java/com/susmit/aceeditor/ResultReceivedListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
public interface ResultReceivedListener
88
{
9-
void onReceived(String text, int FLAG_VALUE);
9+
void onReceived(int FLAG_VALUE, String... results);
1010
}

0 commit comments

Comments
 (0)