make list completion more reusable

This commit is contained in:
Filip Krawczyk 2022-08-25 17:24:05 +02:00
parent cc8441dabc
commit 6729a040ea
2 changed files with 24 additions and 11 deletions

View File

@ -1,5 +1,8 @@
import 'package:flutter/services.dart';
const unorderedListTypes = ['*', '+', '-'];
const orderedListTypes = [')', '.'];
extension Utilities on String {
int getBeginningOfTheLine(int from) {
if (from <= 0) return 0;
@ -80,11 +83,12 @@ class MarkdownFormatter extends TextInputFormatter {
return tev.append('$indent$number$afterNumberChar ');
}
newVal = unorderedListContinuation('-', newVal);
newVal = unorderedListContinuation('*', newVal);
newVal = unorderedListContinuation('+', newVal);
newVal = orderedListContinuation('.', newVal);
newVal = orderedListContinuation(')', newVal);
for (final c in unorderedListTypes) {
newVal = unorderedListContinuation(c, newVal);
}
for (final c in orderedListTypes) {
newVal = orderedListContinuation(c, newVal);
}
}
return newVal;

View File

@ -224,12 +224,21 @@ class _ToolbarBody extends HookWidget {
onPressed: () {
final line = controller.firstSelectedLine;
if (line.startsWith('* ')) {
controller.removeAtBeginningOfEverySelectedLine('* ');
} else if (line.startsWith('- ')) {
controller.removeAtBeginningOfEverySelectedLine('- ');
} else {
controller.insertAtBeginningOfEverySelectedLine('- ');
// if theres a list in place, remove it
final listRemoved = () {
for (final c in unorderedListTypes) {
if (line.startsWith('$c ')) {
controller.removeAtBeginningOfEverySelectedLine('$c ');
return true;
}
}
return false;
}();
// if no list, then let's add one
if (!listRemoved) {
controller.insertAtBeginningOfEverySelectedLine(
'${unorderedListTypes.last} ');
}
},
icon: const Icon(Icons.format_list_bulleted),