remove list item if empty + enter pressed
This commit is contained in:
parent
b2ef0883e3
commit
8aefdfbf27
|
@ -40,6 +40,19 @@ extension on TextEditingValue {
|
|||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// cuts [characterCount] number of chars from before the cursor
|
||||
TextEditingValue trimBeforeCursor(int characterCount) {
|
||||
final beg = text.substring(0, selection.baseOffset);
|
||||
final end = text.substring(selection.baseOffset);
|
||||
|
||||
return copyWith(
|
||||
text: beg.substring(0, beg.length - characterCount - 1) + end,
|
||||
selection: selection.copyWith(
|
||||
baseOffset: selection.baseOffset - characterCount,
|
||||
extentOffset: selection.extentOffset - characterCount,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides convenience formatting in markdown text fields
|
||||
|
@ -59,27 +72,43 @@ class MarkdownFormatter extends TextInputFormatter {
|
|||
|
||||
TextEditingValue unorderedListContinuation(
|
||||
String listChar, TextEditingValue tev) {
|
||||
final regex = RegExp(r'(\s*)' '${RegExp.escape(listChar)} ');
|
||||
final regex = RegExp(r'(\s*)' '${RegExp.escape(listChar)} (.*)');
|
||||
final match = regex.matchAsPrefix(lineBefore);
|
||||
|
||||
if (match == null) {
|
||||
return tev;
|
||||
}
|
||||
|
||||
final listItemBody = match.group(2);
|
||||
final indent = match.group(1);
|
||||
|
||||
if (listItemBody == null || listItemBody.isEmpty) {
|
||||
return tev.trimBeforeCursor(listChar.length + (indent?.length ?? 1));
|
||||
}
|
||||
|
||||
return tev.append('$indent$listChar ');
|
||||
}
|
||||
|
||||
TextEditingValue orderedListContinuation(
|
||||
String afterNumberChar, TextEditingValue tev) {
|
||||
final regex =
|
||||
RegExp(r'(\s*)(\d+)' '${RegExp.escape(afterNumberChar)} ');
|
||||
RegExp(r'(\s*)(\d+)' '${RegExp.escape(afterNumberChar)} (.*)');
|
||||
final match = regex.matchAsPrefix(lineBefore);
|
||||
|
||||
if (match == null) {
|
||||
return tev;
|
||||
}
|
||||
final indent = match.group(1);
|
||||
final number = int.tryParse(match.group(2)!) ?? 0 + 1;
|
||||
|
||||
final listItemBody = match.group(3)!;
|
||||
final indent = match.group(1)!;
|
||||
final numberStr = match.group(2)!;
|
||||
|
||||
if (listItemBody.isEmpty) {
|
||||
return tev.trimBeforeCursor(
|
||||
indent.length + numberStr.length + afterNumberChar.length + 1);
|
||||
}
|
||||
|
||||
final number = (int.tryParse(match.group(2)!) ?? 0) + 1;
|
||||
return tev.append('$indent$number$afterNumberChar ');
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue