-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathextended_text_utils.dart
721 lines (657 loc) · 22.1 KB
/
extended_text_utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import 'dart:math';
import 'dart:ui' as ui;
import 'package:extended_text_library/src/background_text_span.dart';
import 'package:extended_text_library/src/extended_widget_span.dart';
import 'package:extended_text_library/src/special_inline_span_base.dart';
import 'package:extended_text_library/src/special_text_span.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
extension ExtendedTextLibraryExtension on String {
String joinChar([String char = ExtendedTextLibraryUtils.zeroWidthSpace]) =>
Characters(this).join(char);
}
class ExtendedTextLibraryUtils {
ExtendedTextLibraryUtils._();
static const String zeroWidthSpace = '\u{200B}';
// it seems TextPainter works for WidgetSpan on 1.17.0
// code under 1.17.0
static Offset getCaretOffset(
TextPosition textPosition,
TextPainter textPainter,
bool hasPlaceholderSpan, {
ValueChanged<double>? caretHeightCallBack,
Offset? effectiveOffset,
Rect caretPrototype = Rect.zero,
ui.BoxHeightStyle boxHeightStyle = ui.BoxHeightStyle.tight,
ui.BoxWidthStyle boxWidthStyle = ui.BoxWidthStyle.tight,
}) {
effectiveOffset ??= Offset.zero;
///zmt
/// fix widget span
if (hasPlaceholderSpan) {
///if first index, check by first span
int offset = textPosition.offset;
List<TextBox> boxs = textPainter.getBoxesForSelection(
TextSelection(
baseOffset: offset,
extentOffset: offset + 1,
affinity: textPosition.affinity),
boxWidthStyle: boxWidthStyle,
boxHeightStyle: boxHeightStyle,
);
if (boxs.isNotEmpty) {
final Rect rect = boxs.toList().last.toRect();
caretHeightCallBack?.call(rect.height);
return rect.topLeft + effectiveOffset;
} else {
if (offset <= 0) {
offset = 1;
}
boxs = textPainter.getBoxesForSelection(
TextSelection(
baseOffset: offset - 1,
extentOffset: offset,
affinity: textPosition.affinity,
),
boxWidthStyle: boxWidthStyle,
boxHeightStyle: boxHeightStyle,
);
if (boxs.isNotEmpty) {
final Rect rect = boxs.toList().last.toRect();
caretHeightCallBack?.call(rect.height);
if (textPosition.offset <= 0) {
return rect.topLeft + effectiveOffset;
} else {
return rect.topRight + effectiveOffset;
}
}
}
}
final Offset caretOffset =
textPainter.getOffsetForCaret(textPosition, caretPrototype) +
effectiveOffset;
return caretOffset;
}
static bool hitTestChild(
BoxHitTestResult result,
RenderBox child,
Offset effectiveOffset, {
required Offset position,
}) {
final TextParentData textParentData = child.parentData as TextParentData;
final Offset? offset = textParentData.offset;
if (offset == null) {
return false;
}
final Matrix4 transform = Matrix4.translationValues(
offset.dx + effectiveOffset.dx, offset.dy + effectiveOffset.dy, 0.0);
final bool isHit = result.addWithPaintTransform(
transform: transform,
position: position,
hitTest: (BoxHitTestResult result, Offset transformed) {
assert(() {
final Offset manualPosition = position - offset - effectiveOffset;
return (transformed.dx - manualPosition.dx).abs() <
precisionErrorTolerance &&
(transformed.dy - manualPosition.dy).abs() <
precisionErrorTolerance;
}());
return child.hitTest(result, position: transformed);
},
);
return isHit;
}
static TextPosition convertTextInputPostionToTextPainterPostion(
InlineSpan text, TextPosition textPosition) {
int caretOffset = textPosition.offset;
int textOffset = 0;
text.visitChildren((InlineSpan ts) {
if (ts is SpecialInlineSpanBase) {
final int length = (ts as SpecialInlineSpanBase).actualText.length;
caretOffset -= length - getInlineOffset(ts);
textOffset += length;
} else {
textOffset += getInlineOffset(ts);
}
if (textOffset >= textPosition.offset) {
return false;
}
return true;
});
if (caretOffset != textPosition.offset) {
return TextPosition(
offset: max(0, caretOffset), affinity: textPosition.affinity);
}
return textPosition;
}
static TextSelection convertTextInputSelectionToTextPainterSelection(
InlineSpan text, TextSelection selection) {
if (selection.isValid) {
if (selection.isCollapsed) {
final TextPosition extent =
convertTextInputPostionToTextPainterPostion(text, selection.extent);
if (selection.extent != extent) {
selection = selection.copyWith(
baseOffset: extent.offset,
extentOffset: extent.offset,
affinity: selection.affinity,
isDirectional: selection.isDirectional);
return selection;
}
} else {
final TextPosition extent =
convertTextInputPostionToTextPainterPostion(text, selection.extent);
final TextPosition base =
convertTextInputPostionToTextPainterPostion(text, selection.base);
if (selection.extent != extent || selection.base != base) {
selection = selection.copyWith(
baseOffset: base.offset,
extentOffset: extent.offset,
affinity: selection.affinity,
isDirectional: selection.isDirectional);
return selection;
}
}
}
return selection;
}
static TextPosition? convertTextPainterPostionToTextInputPostion(
InlineSpan text, TextPosition? textPosition,
{bool? end}) {
if (textPosition != null) {
int caretOffset = textPosition.offset;
if (caretOffset <= 0) {
return textPosition;
}
int textOffset = 0;
text.visitChildren((InlineSpan ts) {
if (ts is SpecialInlineSpanBase) {
final SpecialInlineSpanBase specialTs = ts as SpecialInlineSpanBase;
final int length = specialTs.actualText.length;
caretOffset += length - getInlineOffset(ts);
///make sure caret is not in text when deleteAll is true
if (specialTs.deleteAll &&
caretOffset >= specialTs.start &&
caretOffset <= specialTs.end) {
if (end != null) {
caretOffset = end ? specialTs.end : specialTs.start;
} else {
if (caretOffset >
(specialTs.end - specialTs.start) / 2.0 + specialTs.start) {
//move caretOffset to end
caretOffset = specialTs.end;
} else {
caretOffset = specialTs.start;
}
}
return false;
}
}
textOffset += getInlineOffset(ts);
if (textOffset >= textPosition.offset) {
return false;
}
return true;
});
if (caretOffset != textPosition.offset) {
return TextPosition(
offset: caretOffset, affinity: textPosition.affinity);
}
}
return textPosition;
}
static TextSelection convertTextPainterSelectionToTextInputSelection(
InlineSpan text, TextSelection selection,
{bool selectWord = false}) {
if (selection.isValid) {
if (selection.isCollapsed) {
final TextPosition? extent =
convertTextPainterPostionToTextInputPostion(text, selection.extent);
if (selection.extent != extent) {
selection = selection.copyWith(
baseOffset: extent!.offset,
extentOffset: extent.offset,
affinity: selection.affinity,
isDirectional: selection.isDirectional);
return selection;
}
} else {
final TextPosition? extent =
convertTextPainterPostionToTextInputPostion(text, selection.extent,
end: selectWord ? true : null);
final TextPosition? base = convertTextPainterPostionToTextInputPostion(
text, selection.base,
end: selectWord ? false : null);
if (selection.extent != extent || selection.base != base) {
selection = selection.copyWith(
baseOffset: base!.offset,
extentOffset: extent!.offset,
affinity: selection.affinity,
isDirectional: selection.isDirectional);
return selection;
}
}
}
return selection;
}
static TextPosition makeSureCaretNotInSpecialText(
InlineSpan text, TextPosition textPosition) {
int caretOffset = textPosition.offset;
if (caretOffset <= 0) {
return textPosition;
}
int textOffset = 0;
text.visitChildren((InlineSpan ts) {
if (ts is SpecialInlineSpanBase) {
final SpecialInlineSpanBase specialTs = ts as SpecialInlineSpanBase;
///make sure caret is not in text when deleteAll is true
if (specialTs.deleteAll &&
caretOffset >= specialTs.start &&
caretOffset <= specialTs.end) {
if (caretOffset >
(specialTs.end - specialTs.start) / 2.0 + specialTs.start) {
//move caretOffset to end
caretOffset = specialTs.end;
} else {
caretOffset = specialTs.start;
}
return false;
}
}
textOffset += getInlineOffset(ts);
if (textOffset >= textPosition.offset) {
return false;
}
return true;
});
if (caretOffset != textPosition.offset) {
return TextPosition(offset: caretOffset, affinity: textPosition.affinity);
}
return textPosition;
}
//double getImageSpanCorrectPosition(
// ExtendedWidgetSpan widgetSpan, TextDirection direction) {
// // var correctPosition = image.width / 2.0;
// //if (direction == TextDirection.rtl) correctPosition = -correctPosition;
// return 15.0;
// //return correctPosition;
//}
/// correct caret Offset
/// make sure caret is not in text when caretIn is false
static TextEditingValue correctCaretOffset(
TextEditingValue value,
InlineSpan textSpan,
TextInputConnection? textInputConnection, {
TextEditingValue? oldValue,
}) {
final TextSelection selection = value.selection;
if (selection.isValid && selection.isCollapsed) {
int caretOffset = selection.extentOffset;
// move to previous or next
// https://github.com/fluttercandies/extended_text_field/issues/210
bool? movePrevious;
if (oldValue != null) {
final TextSelection oldSelection = oldValue.selection;
if (oldSelection.isValid && oldSelection.isCollapsed) {
final int moveOffset = selection.baseOffset - oldSelection.baseOffset;
if (moveOffset < 0) {
movePrevious = true;
} else if (moveOffset > 0) {
movePrevious = false;
}
}
}
// correct caret Offset
// make sure caret is not in text when deleteAll is true
//
textSpan.visitChildren((InlineSpan span) {
if (span is SpecialInlineSpanBase &&
(span as SpecialInlineSpanBase).deleteAll) {
final SpecialInlineSpanBase specialTs = span as SpecialInlineSpanBase;
if (caretOffset >= specialTs.start && caretOffset <= specialTs.end) {
if (movePrevious != null) {
if (movePrevious) {
caretOffset = specialTs.start;
} else {
caretOffset = specialTs.end;
}
} else {
if (caretOffset >
(specialTs.end - specialTs.start) / 2.0 + specialTs.start) {
//move caretOffset to end
caretOffset = specialTs.end;
} else {
caretOffset = specialTs.start;
}
}
return false;
}
}
return true;
});
///tell textInput caretOffset is changed.
if (caretOffset != selection.baseOffset) {
value = value.copyWith(
selection: selection.copyWith(
baseOffset: caretOffset, extentOffset: caretOffset));
textInputConnection?.setEditingState(value);
}
}
return value;
}
static TextEditingValue handleSpecialTextSpanDelete(
TextEditingValue value,
TextEditingValue? oldValue,
InlineSpan oldTextSpan,
TextInputConnection? textInputConnection) {
final String? oldText = oldValue?.text;
String newText = value.text;
///take care of image span
if (oldText != null && oldText.length > newText.length) {
final int difStart = value.selection.extentOffset;
//int difEnd = oldText.length - 1;
// for (; difStart < newText.length; difStart++) {
// if (oldText[difStart] != newText[difStart]) {
// break;
// }
// }
int caretOffset = value.selection.extentOffset;
if (difStart > 0) {
oldTextSpan.visitChildren((InlineSpan span) {
if (span is SpecialInlineSpanBase &&
(span as SpecialInlineSpanBase).deleteAll) {
final SpecialInlineSpanBase specialTs =
span as SpecialInlineSpanBase;
if (difStart > specialTs.start && difStart < specialTs.end) {
//difStart = ts.start;
newText = newText.replaceRange(specialTs.start, difStart, '');
caretOffset -= difStart - specialTs.start;
return false;
}
}
return true;
});
if (newText != value.text) {
value = TextEditingValue(
text: newText,
selection: value.selection.copyWith(
baseOffset: caretOffset,
extentOffset: caretOffset,
affinity: value.selection.affinity,
isDirectional: value.selection.isDirectional));
textInputConnection?.setEditingState(value);
}
}
}
return value;
}
//bool hasSpecialText(List<TextSpan> value) {
// if (value == null) return false;
//
// for (var textSpan in value) {
// if (textSpan is SpecialTextSpan) return true;
// if (hasSpecialText(textSpan.children)) {
// return true;
// }
// }
// return false;
//}
static bool hasSpecialText(InlineSpan textSpan) {
return hasT<SpecialInlineSpanBase>(textSpan);
}
static bool hasT<T>(InlineSpan? textSpan) {
if (textSpan == null) {
return false;
}
if (textSpan is T) {
return true;
}
if (textSpan is TextSpan && textSpan.children != null) {
for (final InlineSpan ts in textSpan.children!) {
final bool has = hasT<T>(ts);
if (has) {
return true;
}
}
}
return false;
}
// void textSpanNestToArray(InlineSpan? textSpan, List<InlineSpan> list) {
// if (textSpan == null) {
// return;
// }
// list.add(textSpan);
// if (textSpan is TextSpan && textSpan.children != null) {
// for (final InlineSpan ts in textSpan.children!) {
// textSpanNestToArray(ts, list);
// }
// }
// }
// List<InlineSpan> textSpanNestToArray(InlineSpan inlineSpan,
// {bool Function(InlineSpan element)? test}) {
// final List<InlineSpan> list = <InlineSpan>[];
// inlineSpan.visitChildren((InlineSpan span) {
// if (test?.call(span) ?? true) {
// list.add(span);
// }
// return true;
// });
// return list;
// }
static String textSpanToActualText(InlineSpan textSpan) {
final StringBuffer buffer = StringBuffer();
textSpan.visitChildren((InlineSpan span) {
if (span is SpecialInlineSpanBase) {
buffer.write((span as SpecialInlineSpanBase).actualText);
} else {
// ignore: invalid_use_of_protected_member
span.computeToPlainText(buffer);
}
return true;
});
return buffer.toString();
}
/// Walks this text span and its descendants in pre-order and calls [visitor]
/// for each span that has text.
// bool _visitTextSpan(InlineSpan textSpan, bool visitor(InlineSpan span)) {
// String? text = getInlineText(textSpan);
// if (textSpan is SpecialInlineSpanBase) {
// text = (textSpan as SpecialInlineSpanBase).actualText;
// }
// if (text != null) {
// if (!visitor(textSpan)) {
// return false;
// }
// }
// if (textSpan is TextSpan && textSpan.children != null) {
// for (final InlineSpan child in textSpan.children!) {
// if (!_visitTextSpan(child, visitor)) {
// return false;
// }
// //if (!child.visitTextSpan(visitor)) return false;
// }
// }
// return true;
// }
static int getInlineOffset(InlineSpan inlineSpan) {
if (inlineSpan is TextSpan && inlineSpan.text != null) {
return inlineSpan.text!.length;
}
if (inlineSpan is PlaceholderSpan) {
return 1;
}
return 0;
}
// String? getInlineText(InlineSpan inlineSpan) {
// if (inlineSpan is TextSpan && inlineSpan.text != null) {
// return inlineSpan.text;
// }
// if (inlineSpan is PlaceholderSpan) {
// return '\uFFFC';
// }
// return '';
// }
/// join char into text
static InlineSpan joinChar(
InlineSpan value,
Accumulator offset,
String char,
) {
late InlineSpan output;
String? actualText;
bool deleteAll = false;
if (value is SpecialInlineSpanBase) {
final SpecialInlineSpanBase base = value as SpecialInlineSpanBase;
actualText = base.actualText;
deleteAll = base.deleteAll;
} else {
deleteAll = false;
}
if (value is TextSpan) {
List<InlineSpan>? children;
final int start = offset.value;
String? text = value.text;
actualText ??= text;
if (actualText != null) {
actualText = actualText.joinChar();
offset.increment(actualText.length);
}
if (text != null) {
text = text.joinChar();
}
if (value.children != null) {
children = <InlineSpan>[];
for (final InlineSpan child in value.children!) {
children.add(joinChar(child, offset, char));
}
}
if (value is BackgroundTextSpan) {
output = BackgroundTextSpan(
background: value.background,
clipBorderRadius: value.clipBorderRadius,
paintBackground: value.paintBackground,
text: text ?? '',
actualText: actualText,
start: start,
style: value.style,
recognizer: value.recognizer,
deleteAll: deleteAll,
semanticsLabel: value.semanticsLabel,
);
} else {
output = SpecialTextSpan(
text: text ?? '',
actualText: actualText,
children: children,
start: start,
style: value.style,
recognizer: value.recognizer,
deleteAll: deleteAll,
semanticsLabel: value.semanticsLabel,
);
}
} else if (value is WidgetSpan) {
output = ExtendedWidgetSpan(
child: value.child,
start: offset.value,
alignment: value.alignment,
style: value.style,
baseline: value.baseline,
actualText: actualText,
);
offset.increment(actualText?.length ?? 1);
} else {
output = value;
}
return output;
}
// move by keyboard left -1 and right +1
// make sure keyboard left/right support for SpecialInlineSpan
static TextSelection convertKeyboardMoveSelection(
InlineSpan text,
TextSelection selection,
) {
if (selection.isValid) {
if (selection.isCollapsed) {
final TextPosition? extent = convertKeyboardMoveTextPostion(
text,
selection.extent,
);
if (selection.extent != extent) {
selection = selection.copyWith(
baseOffset: extent!.offset,
extentOffset: extent.offset,
affinity: selection.affinity,
isDirectional: selection.isDirectional);
return selection;
}
} else {
final TextPosition? extent = convertKeyboardMoveTextPostion(
text,
selection.extent,
);
final TextPosition? base = convertKeyboardMoveTextPostion(
text,
selection.base,
);
if (selection.extent != extent || selection.base != base) {
selection = selection.copyWith(
baseOffset: base!.offset,
extentOffset: extent!.offset,
affinity: selection.affinity,
isDirectional: selection.isDirectional);
return selection;
}
}
}
return selection;
}
/// move by keyboard left -1 and right +1
/// make sure keyboard left/right support for SpecialInlineSpan
static TextPosition? convertKeyboardMoveTextPostion(
InlineSpan text,
TextPosition? textPosition,
) {
if (textPosition != null) {
int caretOffset = textPosition.offset;
if (caretOffset <= 0) {
return textPosition;
}
int textOffset = 0;
text.visitChildren((InlineSpan ts) {
if (ts is SpecialInlineSpanBase) {
final SpecialInlineSpanBase specialTs = ts as SpecialInlineSpanBase;
final int length = specialTs.actualText.length;
textOffset += length;
///make sure caret is not in text when deleteAll is true
if (specialTs.deleteAll &&
caretOffset >= specialTs.start &&
caretOffset <= specialTs.end) {
if (caretOffset == specialTs.start ||
caretOffset == specialTs.end) {
return false;
} else if (caretOffset == specialTs.start + 1) {
caretOffset = specialTs.end;
} else if (caretOffset == specialTs.end - 1) {
caretOffset = specialTs.start;
}
return false;
}
} else {
textOffset += getInlineOffset(ts);
}
if (textOffset >= textPosition.offset) {
return false;
}
return true;
});
if (caretOffset != textPosition.offset) {
return TextPosition(
offset: caretOffset, affinity: textPosition.affinity);
}
}
return textPosition;
}
}