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) { int getEndOfTheLine(int from) {
for (var i = from; i < length; i++) { 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 /// returns the line that ends at endingIndex

View File

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