Skip to content

Commit

Permalink
Fix editing text between
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 18, 2023
1 parent c4c10cd commit 9d5d873
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
6 changes: 4 additions & 2 deletions api/lib/src/models/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ sealed class TextParagraph with _$TextParagraph {
return copyWith(textSpans: spans);
}

TextParagraph replace(TextSpan span, [int start = 0, int length = 0]) {
TextParagraph replace(TextSpan span, [int start = 0, int? length]) {
length ??= 0;
var subSpans = <TextSpan>[];
final end = start + length;

Expand All @@ -241,8 +242,9 @@ sealed class TextParagraph with _$TextParagraph {
}

TextParagraph replaceText(String text,
[int start = 0, int length = 0, bool replaceWholeSpan = false]) {
[int start = 0, int? length, bool replaceWholeSpan = false]) {
final indexed = getIndexedSpan(start);
length ??= replaceWholeSpan ? indexed?.model.length : 0;
return replace(
indexed != null
? indexed.model.copyWith(text: text)
Expand Down
17 changes: 11 additions & 6 deletions app/lib/handlers/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,17 @@ class LabelHandler extends Handler<LabelTool>
},
orElse: () => (0, text.length),
);
final end = min(indexed + length, context.selection.end);

return TextEditingValue(
text: text,
selection: context.selection,
composing: TextRange(start: indexed, end: length),
selection: TextSelection(
baseOffset: context.selection.baseOffset.clamp(0, text.length),
extentOffset: context.selection.extentOffset.clamp(0, text.length),
affinity: context.selection.affinity,
isDirectional: context.selection.isDirectional,
),
composing: TextRange(start: indexed, end: end),
);
}

Expand Down Expand Up @@ -410,9 +416,7 @@ class LabelHandler extends Handler<LabelTool>
final lastValue = currentTextEditingValue;
final start =
replace ? lastValue.composing.start : lastValue.selection.start;
final length = replace
? lastValue.composing.end - start
: lastValue.selection.end - start;
final length = replace ? null : lastValue.selection.end - start;
newIndex += lastValue.selection.start;
_context = _context?.map(text: (e) {
final old = e.element;
Expand Down Expand Up @@ -461,7 +465,6 @@ class LabelHandler extends Handler<LabelTool>
selection: TextSelection.collapsed(offset: newIndex),
);
});
_connection?.setEditingState(currentTextEditingValue);
_bloc?.refresh();
if (_bloc != null) _refreshToolbar(_bloc!);
}
Expand Down Expand Up @@ -624,6 +627,7 @@ class LabelHandler extends Handler<LabelTool>
extentOffset: newSelection,
),
);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand Down Expand Up @@ -723,6 +727,7 @@ class LabelHandler extends Handler<LabelTool>
),
orElse: () => _context);
_bloc?.refresh();
_connection?.setEditingState(currentTextEditingValue);
if (_bloc != null) _refreshToolbar(_bloc!);
}

Expand Down

0 comments on commit 9d5d873

Please sign in to comment.