Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit c873951

Browse files
author
Yuncong Zhang
committed
Fix emoji selection issue.
1 parent 834775b commit c873951

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

Runtime/rendering/editable.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,34 @@ int _handleControl(bool rightArrow, bool leftArrow, bool ctrl, int newOffset) {
327327

328328
int _handleHorizontalArrows(bool rightArrow, bool leftArrow, bool shift, int newOffset) {
329329
if (rightArrow && this._extentOffset < this.text.text.Length) {
330-
newOffset += 1;
331-
if (shift) {
332-
this._previousCursorLocation += 1;
330+
if (newOffset < this.text.text.Length - 1 && char.IsHighSurrogate(this.text.text[newOffset])) {
331+
// handle emoji, which takes 2 bytes
332+
newOffset += 2;
333+
if (shift) {
334+
this._previousCursorLocation += 2;
335+
}
336+
}
337+
else {
338+
newOffset += 1;
339+
if (shift) {
340+
this._previousCursorLocation += 1;
341+
}
333342
}
334343
}
335344

336345
if (leftArrow && this._extentOffset > 0) {
337-
newOffset -= 1;
338-
if (shift) {
339-
this._previousCursorLocation -= 1;
346+
if (newOffset > 1 && char.IsLowSurrogate(this.text.text[newOffset - 1])) {
347+
// handle emoji, which takes 2 bytes
348+
newOffset -= 2;
349+
if (shift) {
350+
this._previousCursorLocation -= 2;
351+
}
352+
}
353+
else {
354+
newOffset -= 1;
355+
if (shift) {
356+
this._previousCursorLocation -= 1;
357+
}
340358
}
341359
}
342360

@@ -637,12 +655,10 @@ public float textScaleFactor {
637655
}
638656

639657
public TextSelection selection {
640-
get { return this._selection; }
658+
get {
659+
return this._selection;
660+
}
641661
set {
642-
if (this._selection == value) {
643-
return;
644-
}
645-
646662
this._selection = value;
647663
this._selectionRects = null;
648664
this.markNeedsPaint();

Runtime/service/text_input.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,23 @@ public class TextEditingValue : IEquatable<TextEditingValue> {
203203

204204
public TextEditingValue(string text = "", TextSelection selection = null, TextRange composing = null) {
205205
this.text = text;
206-
this.selection = selection ?? TextSelection.collapsed(-1);
207206
this.composing = composing ?? TextRange.empty;
207+
208+
if (selection != null) {
209+
// handle emoji, which takes 2 bytes
210+
// if selection cuts in the middle of the emoji, move it to the end
211+
int start = selection.start, end = selection.end;
212+
if (start < text.Length && char.IsLowSurrogate(text[start])) {
213+
start++;
214+
}
215+
if (end < text.Length && char.IsLowSurrogate(text[end])) {
216+
end++;
217+
}
218+
this.selection = selection.copyWith(start, end);
219+
}
220+
else {
221+
this.selection = TextSelection.collapsed(-1);
222+
}
208223
}
209224

210225
public static TextEditingValue fromJson(JSONObject json) {

0 commit comments

Comments
 (0)