From 87726f283bd4068189542a2fe5b7a06b87842031 Mon Sep 17 00:00:00 2001 From: Filip Krawczyk Date: Thu, 25 Aug 2022 21:56:49 +0200 Subject: [PATCH] make end indice exclusive --- lib/markdown_formatter.dart | 4 ++-- lib/widgets/editor/editor_toolbar.dart | 27 ++++++++++++++------------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/markdown_formatter.dart b/lib/markdown_formatter.dart index 2c01df7..fb65b1a 100644 --- a/lib/markdown_formatter.dart +++ b/lib/markdown_formatter.dart @@ -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 diff --git a/lib/widgets/editor/editor_toolbar.dart b/lib/widgets/editor/editor_toolbar.dart index aebcf78..33bdc04 100644 --- a/lib/widgets/editor/editor_toolbar.dart +++ b/lib/widgets/editor/editor_toolbar.dart @@ -57,9 +57,9 @@ class EditorToolbar extends HookWidget { return MobxProvider( create: (context) => EditorToolbarStore(instanceHost), - child: ObserverBuilder(builder: (context, store) { + child: Builder(builder: (context) { return AsyncStoreListener( - asyncStore: store.imageUploadState, + asyncStore: context.read().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) {