Flutter Engine
The Flutter Engine
TextEditingDelta.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.plugin.editing;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.VisibleForTesting;
9import io.flutter.Log;
10import org.json.JSONException;
11import org.json.JSONObject;
12
13/// A representation of the change that occured to an editing state, along with the resulting
14/// composing and selection regions.
15public final class TextEditingDelta {
16 private @NonNull CharSequence oldText;
17 private @NonNull CharSequence deltaText;
18 private int deltaStart;
19 private int deltaEnd;
20 private int newSelectionStart;
21 private int newSelectionEnd;
22 private int newComposingStart;
23 private int newComposingEnd;
24
25 private static final String TAG = "TextEditingDelta";
26
28 @NonNull CharSequence oldEditable,
29 int replacementDestinationStart,
30 int replacementDestinationEnd,
31 @NonNull CharSequence replacementSource,
32 int selectionStart,
33 int selectionEnd,
34 int composingStart,
35 int composingEnd) {
36 newSelectionStart = selectionStart;
37 newSelectionEnd = selectionEnd;
38 newComposingStart = composingStart;
39 newComposingEnd = composingEnd;
40
41 setDeltas(
42 oldEditable,
43 replacementSource.toString(),
44 replacementDestinationStart,
45 replacementDestinationEnd);
46 }
47
48 // Non text update delta constructor.
50 @NonNull CharSequence oldText,
51 int selectionStart,
52 int selectionEnd,
53 int composingStart,
54 int composingEnd) {
55 newSelectionStart = selectionStart;
56 newSelectionEnd = selectionEnd;
57 newComposingStart = composingStart;
58 newComposingEnd = composingEnd;
59
60 setDeltas(oldText, "", -1, -1);
61 }
62
63 @VisibleForTesting
64 @NonNull
65 public CharSequence getOldText() {
66 return oldText;
67 }
68
69 @VisibleForTesting
70 @NonNull
71 public CharSequence getDeltaText() {
72 return deltaText;
73 }
74
75 @VisibleForTesting
76 public int getDeltaStart() {
77 return deltaStart;
78 }
79
80 @VisibleForTesting
81 public int getDeltaEnd() {
82 return deltaEnd;
83 }
84
85 @VisibleForTesting
86 public int getNewSelectionStart() {
87 return newSelectionStart;
88 }
89
90 @VisibleForTesting
91 public int getNewSelectionEnd() {
92 return newSelectionEnd;
93 }
94
95 @VisibleForTesting
96 public int getNewComposingStart() {
97 return newComposingStart;
98 }
99
100 @VisibleForTesting
101 public int getNewComposingEnd() {
102 return newComposingEnd;
103 }
104
105 private void setDeltas(
106 @NonNull CharSequence oldText, @NonNull CharSequence newText, int newStart, int newExtent) {
107 this.oldText = oldText;
108 deltaText = newText;
109 deltaStart = newStart;
110 deltaEnd = newExtent;
111 }
112
113 @NonNull
114 public JSONObject toJSON() {
115 JSONObject delta = new JSONObject();
116
117 try {
118 delta.put("oldText", oldText.toString());
119 delta.put("deltaText", deltaText.toString());
120 delta.put("deltaStart", deltaStart);
121 delta.put("deltaEnd", deltaEnd);
122 delta.put("selectionBase", newSelectionStart);
123 delta.put("selectionExtent", newSelectionEnd);
124 delta.put("composingBase", newComposingStart);
125 delta.put("composingExtent", newComposingEnd);
126 } catch (JSONException e) {
127 Log.e(TAG, "unable to create JSONObject: " + e);
128 }
129
130 return delta;
131 }
132}
static void e(@NonNull String tag, @NonNull String message)
Definition: Log.java:84
TextEditingDelta( @NonNull CharSequence oldText, int selectionStart, int selectionEnd, int composingStart, int composingEnd)
TextEditingDelta( @NonNull CharSequence oldEditable, int replacementDestinationStart, int replacementDestinationEnd, @NonNull CharSequence replacementSource, int selectionStart, int selectionEnd, int composingStart, int composingEnd)
#define TAG()