make end indice exclusive

This commit is contained in:
Filip Krawczyk 2022-08-25 21:56:49 +02:00
parent 8aefdfbf27
commit 87726f283b
2 changed files with 17 additions and 14 deletions

View File

@ -14,10 +14,10 @@ extension Utilities on String {
int getEndOfTheLine(int from) {
for (var i = from; i < length; i++) {
if (this[i] == '\n') return i;
if (this[i] == '\n') return i + 1;
}
return length - 1;
return length;
}
/// returns the line that ends at endingIndex

View File

@ -57,9 +57,9 @@ class EditorToolbar extends HookWidget {
return MobxProvider(
create: (context) => EditorToolbarStore(instanceHost),
child: ObserverBuilder<EditorToolbarStore>(builder: (context, store) {
child: Builder(builder: (context) {
return AsyncStoreListener(
asyncStore: store.imageUploadState,
asyncStore: context.read<EditorToolbarStore>().imageUploadState,
child: AnimatedSwitcher(
duration: kThemeAnimationDuration,
transitionBuilder: (child, animation) {
@ -214,15 +214,15 @@ class _ToolbarBody extends HookWidget {
PopupMenuItem(
value: h,
child: Text(h.name.toUpperCase()),
onTap: () {
final header = '${'#' * h.value} ';
if (!controller.firstSelectedLine.startsWith(header)) {
controller.insertAtBeginningOfFirstSelectedLine(header);
}
},
),
],
onSelected: (val) {
final header = '${'#' * val.value} ';
if (!controller.firstSelectedLine.startsWith(header)) {
controller.insertAtBeginningOfFirstSelectedLine(header);
}
},
tooltip: L10n.of(context).editor_header,
child: const Icon(Icons.h_mobiledata),
),
@ -424,9 +424,12 @@ extension on TextEditingController {
}
String get firstSelectedLine {
if (text.isEmpty) return '';
return text.substring(text.getBeginningOfTheLine(selection.start - 1),
text.getEndOfTheLine(selection.end));
if (text.isEmpty) {
return '';
}
final val = text.substring(text.getBeginningOfTheLine(selection.start - 1),
text.getEndOfTheLine(selection.end) - 1);
return val;
}
void insertAtBeginningOfFirstSelectedLine(String s) {