From 4f91c6067d44f46fbac734b892662d16645471af Mon Sep 17 00:00:00 2001 From: CodeDoctorDE Date: Tue, 26 Dec 2023 18:47:42 +0100 Subject: [PATCH] Fix various issues with the label tool --- api/lib/src/models/text.dart | 13 +++-- app/lib/handlers/label.dart | 49 ++++++++++++------- app/lib/models/label.dart | 4 ++ .../metadata/android/en-US/changelogs/86.txt | 3 +- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/api/lib/src/models/text.dart b/api/lib/src/models/text.dart index b8eff7bf5075..205a317dcf46 100644 --- a/api/lib/src/models/text.dart +++ b/api/lib/src/models/text.dart @@ -135,13 +135,15 @@ sealed class TextParagraph with _$TextParagraph { String get text => textSpans.map((e) => e.text).join(); String substring(int start, [int? end]) => text.substring(start, end); - TextSpan? getSpan(int index) => getIndexedSpan(index)?.model; + TextSpan? getSpan(int index, [bool cut = true]) => + getIndexedSpan(index, cut)?.model; - IndexedModel? getIndexedSpan(int index) { + IndexedModel? getIndexedSpan(int index, [bool cut = true]) { var currentLength = 0; for (var span in textSpans) { if (currentLength + span.length > index) { - return IndexedModel(currentLength, span.subSpan(index - currentLength)); + return IndexedModel( + currentLength, cut ? span.subSpan(index - currentLength) : span); } currentLength += span.length; } @@ -164,8 +166,9 @@ sealed class TextParagraph with _$TextParagraph { break; } firstIndex ??= currentLength; - spans - .add(cut ? span.subSpan(start - currentLength, end - start) : span); + spans.add(cut + ? span.subSpan(start - currentLength, end - start - currentLength) + : span); } currentLength += span.length; } diff --git a/app/lib/handlers/label.dart b/app/lib/handlers/label.dart index ebb579095002..59af7aef3318 100644 --- a/app/lib/handlers/label.dart +++ b/app/lib/handlers/label.dart @@ -175,7 +175,6 @@ class LabelHandler extends Handler ), ); } - _connection?.setEditingState(currentTextEditingValue); if (!(_connection?.attached ?? false)) { _connection = TextInput.attach( this, @@ -197,9 +196,12 @@ class LabelHandler extends Handler textDirection: TextDirection.ltr, textAlign: TextAlign.left, ); + } else { + _updateEditingState(); } _bloc = context.getDocumentBloc(); _connection!.show(); + _refreshToolbar(_bloc!); context.refresh(); } @@ -257,7 +259,7 @@ class LabelHandler extends Handler } bloc.refresh(); _refreshToolbar(bloc); - _connection?.setEditingState(currentTextEditingValue); + _updateEditingState(); } void _refreshToolbar(DocumentBloc bloc) { @@ -359,19 +361,26 @@ class LabelHandler extends Handler TextEditingValue get currentTextEditingValue { final context = _context; if (context == null) return const TextEditingValue(); + final state = _bloc?.state; + if (state is! DocumentLoaded) 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); + e.area.paragraph.getIndexedSpan(context.selection.start, false); 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); + if (context.maybeMap( + text: (e) => e.shouldNewSpan(state.data), orElse: () => false)) { + indexed = min(context.selection.start, text.length); + length = 0; + } + final end = min(indexed + length, text.length); return TextEditingValue( text: text, @@ -385,6 +394,9 @@ class LabelHandler extends Handler ); } + void _updateEditingState() => + _connection?.setEditingState(currentTextEditingValue); + @override void performAction(TextInputAction action) { switch (action) { @@ -414,29 +426,30 @@ class LabelHandler extends Handler if (state is! DocumentLoadSuccess || _context == null) return; final data = state.data; - var newIndex = value.length; final lastValue = currentTextEditingValue; final start = replace ? lastValue.composing.start : lastValue.selection.start; final length = replace ? null : lastValue.selection.end - start; - newIndex += lastValue.selection.start; + final newIndex = + lastValue.selection.end - lastValue.text.length + value.length; + final currentText = value.substring( + start, value.length - lastValue.text.length + lastValue.composing.end); + print( + 'Text: $currentText, currentText: $currentText, start: ${lastValue.composing.start}, end: ${lastValue.composing.end}, value: $value, lastValue: ${lastValue.text}}'); _context = _context?.map(text: (e) { final old = e.element; if (old != null) { - final currentProperty = e.getSpanProperty(data); - final newSpan = - e.getDefinedForcedSpanProperty(data) != currentProperty && - currentProperty != null; + final newSpan = e.shouldNewSpan(data); final paragraph = newSpan ? old.area.paragraph.replace( text.TextSpan.text( - text: value, + text: currentText, property: e.forcedSpanProperty ?? const text.SpanProperty.undefined(), ), start, length) - : old.area.paragraph.replaceText(value, start, length, replace); + : old.area.paragraph.replaceText(currentText, start, length, true); final area = old.area.copyWith( paragraph: paragraph, ); @@ -555,7 +568,7 @@ class LabelHandler extends Handler }); bloc.refresh(); _refreshToolbar(bloc); - _connection?.setEditingState(currentTextEditingValue); + _updateEditingState(); return null; }, ), @@ -573,7 +586,7 @@ class LabelHandler extends Handler ), ); bloc.refresh(); - _connection?.setEditingState(currentTextEditingValue); + _updateEditingState(); return null; }, ), @@ -613,7 +626,7 @@ class LabelHandler extends Handler ); bloc.refresh(); _refreshToolbar(bloc); - _connection?.setEditingState(currentTextEditingValue); + _updateEditingState(); return null; }, ), @@ -629,7 +642,7 @@ class LabelHandler extends Handler extentOffset: newSelection, ), ); - _connection?.setEditingState(currentTextEditingValue); + _updateEditingState(); return null; }, ), @@ -678,7 +691,7 @@ class LabelHandler extends Handler bloc.refresh(); _refreshToolbar(bloc); - _connection?.setEditingState(currentTextEditingValue); + _updateEditingState(); return null; }, ), @@ -729,7 +742,7 @@ class LabelHandler extends Handler ), orElse: () => _context); _bloc?.refresh(); - _connection?.setEditingState(currentTextEditingValue); + _updateEditingState(); if (_bloc != null) _refreshToolbar(_bloc!); } diff --git a/app/lib/models/label.dart b/app/lib/models/label.dart index 0af069040b91..32116614da0a 100644 --- a/app/lib/models/label.dart +++ b/app/lib/models/label.dart @@ -160,6 +160,10 @@ extension TextContextHelper on TextContext { : const DefinedSpanProperty()); } + bool shouldNewSpan(NoteData document) => + getDefinedSpanProperty(document) != + getDefinedForcedSpanProperty(document); + bool paragraphModified() => getProperty().maybeMap(orElse: () => true, named: (p) => false); diff --git a/fastlane/metadata/android/en-US/changelogs/86.txt b/fastlane/metadata/android/en-US/changelogs/86.txt index 422b6e7e8382..8fb3f612e20f 100644 --- a/fastlane/metadata/android/en-US/changelogs/86.txt +++ b/fastlane/metadata/android/en-US/changelogs/86.txt @@ -1,2 +1,3 @@ * Add support for multi character input languages ([#539](https://github.com/LinwoodDev/Butterfly/issues/539)) -* Fix moving data directory ([#562](https://github.com/LinwoodDev/Butterfly/issues/562)) \ No newline at end of file +* Fix moving data directory ([#562](https://github.com/LinwoodDev/Butterfly/issues/562)) +* Fix various issues with the label tool \ No newline at end of file