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

View File

@ -224,12 +224,21 @@ class _ToolbarBody extends HookWidget {
onPressed: () { onPressed: () {
final line = controller.firstSelectedLine; final line = controller.firstSelectedLine;
if (line.startsWith('* ')) { // if theres a list in place, remove it
controller.removeAtBeginningOfEverySelectedLine('* '); final listRemoved = () {
} else if (line.startsWith('- ')) { for (final c in unorderedListTypes) {
controller.removeAtBeginningOfEverySelectedLine('- '); if (line.startsWith('$c ')) {
} else { controller.removeAtBeginningOfEverySelectedLine('$c ');
controller.insertAtBeginningOfEverySelectedLine('- '); 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), icon: const Icon(Icons.format_list_bulleted),