Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor text logic to enable multi input characters #554

Merged
merged 4 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 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 @@ -240,10 +241,15 @@ 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, bool replaceWholeSpan = false]) {
final indexed = getIndexedSpan(start);
length ??= replaceWholeSpan ? indexed?.model.length : 0;
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
70 changes: 49 additions & 21 deletions app/lib/handlers/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class LabelHandler extends Handler<LabelTool>
),
);
}
_connection?.setEditingState(currentTextEditingValue);
if (!(_connection?.attached ?? false)) {
_connection = TextInput.attach(
this,
Expand All @@ -188,7 +189,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 +355,41 @@ 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),
);
final end = min(indexed + length, context.selection.end);

return TextEditingValue(
text: text,
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),
);
}

@override
void performAction(TextInputAction action) {
switch (action) {
case TextInputAction.newline:
case TextInputAction.done:
_updateText('\n');
_updateText('\n', false);
break;
default:
}
Expand All @@ -385,22 +407,25 @@ 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 ? null : 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 +435,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 +457,15 @@ 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: '',
));
_bloc?.refresh();
if (_bloc != null) _refreshToolbar(_bloc!);
}
Expand Down Expand Up @@ -532,6 +554,7 @@ class LabelHandler extends Handler<LabelTool>
});
bloc.refresh();
_refreshToolbar(bloc);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand All @@ -549,6 +572,7 @@ class LabelHandler extends Handler<LabelTool>
),
);
bloc.refresh();
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand Down Expand Up @@ -588,6 +612,7 @@ class LabelHandler extends Handler<LabelTool>
);
bloc.refresh();
_refreshToolbar(bloc);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand All @@ -603,6 +628,7 @@ class LabelHandler extends Handler<LabelTool>
extentOffset: newSelection,
),
);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand Down Expand Up @@ -651,6 +677,7 @@ class LabelHandler extends Handler<LabelTool>

bloc.refresh();
_refreshToolbar(bloc);
_connection?.setEditingState(currentTextEditingValue);
return null;
},
),
Expand Down Expand Up @@ -701,6 +728,7 @@ class LabelHandler extends Handler<LabelTool>
),
orElse: () => _context);
_bloc?.refresh();
_connection?.setEditingState(currentTextEditingValue);
if (_bloc != null) _refreshToolbar(_bloc!);
}

Expand Down