Skip to content

Commit

Permalink
Refactor text logic to enable multi input characters
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 17, 2023
1 parent f0ff2e3 commit c4c10cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
10 changes: 7 additions & 3 deletions api/lib/src/models/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,14 @@ sealed class TextParagraph with _$TextParagraph {
return copyWith(textSpans: subSpans);
}

TextParagraph replaceText(String text, [int start = 0, int length = 0]) {
TextParagraph replaceText(String text,
[int start = 0, int length = 0, bool replaceWholeSpan = false]) {
final indexed = getIndexedSpan(start);
return replace(
getSpan(start)?.copyWith(text: text) ?? TextSpan.text(text: text),
start,
indexed != null
? indexed.model.copyWith(text: text)
: TextSpan.text(text: text),
(replaceWholeSpan ? indexed?.index : null) ?? start,
length);
}

Expand Down
64 changes: 43 additions & 21 deletions app/lib/handlers/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class LabelHandler extends Handler<LabelTool>
enableSuggestions: false,
enableInteractiveSelection: true,
))
..setEditingState(currentTextEditingValue ?? const TextEditingValue())
..setEditingState(currentTextEditingValue)
..setStyle(
fontFamily: style.fontFamily,
fontSize: style.fontSize! * pixelRatio,
Expand Down Expand Up @@ -354,20 +354,35 @@ class LabelHandler extends Handler<LabelTool>
AutofillScope? get currentAutofillScope => null;

@override
TextEditingValue? get currentTextEditingValue => _context?.element == null
? null
: TextEditingValue(
text: _context!.text!,
selection: _context!.selection,
composing: TextRange(start: 0, end: _context!.text!.length),
);
TextEditingValue get currentTextEditingValue {
final context = _context;
if (context == null) return const TextEditingValue();
final element = context.element;
final text = context.text;
if (element == null || text == null) return const TextEditingValue();
var (indexed, length) = element.maybeMap(
text: (e) {
final indexed =
e.area.paragraph.getIndexedSpan(context.selection.start);
if (indexed == null) return (0, text.length);
return (indexed.index, indexed.model.length);
},
orElse: () => (0, text.length),
);

return TextEditingValue(
text: text,
selection: context.selection,
composing: TextRange(start: indexed, end: length),
);
}

@override
void performAction(TextInputAction action) {
switch (action) {
case TextInputAction.newline:
case TextInputAction.done:
_updateText('\n');
_updateText('\n', false);
break;
default:
}
Expand All @@ -385,22 +400,27 @@ class LabelHandler extends Handler<LabelTool>
_updateText(value.text);
}

void _updateText(String value) {
void _updateText(String value, [bool replace = true]) {
TextElement element;
final state = _bloc?.state;
if (state is! DocumentLoadSuccess || _context == null) return;
final data = state.data;

var newIndex = value.length;
final selection = _context!.selection;
final start = selection.start;
final length = selection.end - start;
newIndex += selection.start;
final lastValue = currentTextEditingValue;
final start =
replace ? lastValue.composing.start : lastValue.selection.start;
final length = replace
? lastValue.composing.end - start
: lastValue.selection.end - start;
newIndex += lastValue.selection.start;
_context = _context?.map(text: (e) {
final old = e.element;
if (old != null) {
final currentProperty = e.getSpanProperty(data);
final newSpan =
e.getDefinedForcedSpanProperty(data) != e.getSpanProperty(data);
e.getDefinedForcedSpanProperty(data) != currentProperty &&
currentProperty != null;
final paragraph = newSpan
? old.area.paragraph.replace(
text.TextSpan.text(
Expand All @@ -410,7 +430,7 @@ class LabelHandler extends Handler<LabelTool>
),
start,
length)
: old.area.paragraph.replaceText(value, start, length);
: old.area.paragraph.replaceText(value, start, length, replace);
final area = old.area.copyWith(
paragraph: paragraph,
);
Expand All @@ -432,18 +452,16 @@ class LabelHandler extends Handler<LabelTool>
);
}, markdown: (e) {
var text = e.text ?? '';
text =
text.replaceRange(start, selection.end.clamp(0, text.length), value);
text = text.replaceRange(
start, lastValue.selection.end.clamp(0, text.length), value);
return e.copyWith(
element: e.element?.copyWith(
text: text,
),
selection: TextSelection.collapsed(offset: newIndex),
);
});
_connection?.setEditingState(const TextEditingValue(
text: '',
));
_connection?.setEditingState(currentTextEditingValue);
_bloc?.refresh();
if (_bloc != null) _refreshToolbar(_bloc!);
}
Expand Down Expand Up @@ -532,6 +550,7 @@ class LabelHandler extends Handler<LabelTool>
});
bloc.refresh();
_refreshToolbar(bloc);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand All @@ -549,6 +568,7 @@ class LabelHandler extends Handler<LabelTool>
),
);
bloc.refresh();
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand Down Expand Up @@ -588,6 +608,7 @@ class LabelHandler extends Handler<LabelTool>
);
bloc.refresh();
_refreshToolbar(bloc);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand Down Expand Up @@ -651,6 +672,7 @@ class LabelHandler extends Handler<LabelTool>

bloc.refresh();
_refreshToolbar(bloc);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand Down

0 comments on commit c4c10cd

Please sign in to comment.