Compare commits

...

3 Commits

Author SHA1 Message Date
Filip Krawczyk 8c0c478847 add safearea for toolbar 2022-07-04 17:20:41 +02:00
Filip Krawczyk cab4aeebb7 put regex to private variable 2022-07-04 17:18:38 +02:00
Filip Krawczyk 7db538084a improve list continuation, add support for numbered list
* make list continuation more universal
* add support for indentation
* add support for numbered list continuation
2022-07-04 17:17:35 +02:00
3 changed files with 40 additions and 8 deletions

View File

@ -37,18 +37,44 @@ class MarkdownFormatter extends TextInputFormatter {
TextEditingValue oldValue, TextEditingValue newValue) {
if (oldValue.text.length > newValue.text.length) return newValue;
var newVal = newValue;
final char = newValue.text[newValue.selection.baseOffset - 1];
if (char == '\n') {
final lineBefore =
newValue.text.lineBefore(newValue.selection.baseOffset - 2);
if (lineBefore.startsWith('- ')) {
return newValue.append('- ');
TextEditingValue listContinuation(String listChar, TextEditingValue tev) {
final regex = RegExp('(\\s*)${RegExp.escape(listChar)} ');
final match = regex.matchAsPrefix(lineBefore);
if (match == null) {
return tev;
}
final indent = match.group(1);
return tev.append('$indent$listChar ');
}
if (lineBefore.startsWith('* ')) {
return newValue.append('* ');
TextEditingValue numberedListContinuation(
String afterNumberChar, TextEditingValue tev) {
final regex = RegExp('(\\s*)(\\d+)${RegExp.escape(afterNumberChar)} ');
final match = regex.matchAsPrefix(lineBefore);
if (match == null) {
return tev;
}
final indent = match.group(1);
final number = int.parse(match.group(2)!) + 1;
return tev.append('$indent$number$afterNumberChar ');
}
newVal = listContinuation('-', newVal);
newVal = listContinuation('*', newVal);
newVal = numberedListContinuation('.', newVal);
newVal = numberedListContinuation(')', newVal);
}
return newValue;
return newVal;
}
}

View File

@ -134,6 +134,7 @@ class CreatePostPage extends HookWidget {
)
],
),
Toolbar.safeArea,
].spaced(6),
),
),

View File

@ -62,12 +62,13 @@ extension on TextEditingController {
class Toolbar extends HookWidget {
final TextEditingController controller;
static const _height = 50.0;
const Toolbar(this.controller);
@override
Widget build(BuildContext context) {
return Container(
height: 50,
height: _height,
width: double.infinity,
color: Theme.of(context).cardColor,
child: Material(
@ -144,6 +145,8 @@ class Toolbar extends HookWidget {
),
);
}
static Widget safeArea = const SizedBox(height: _height);
}
class AddLinkDialog extends HookWidget {
@ -151,9 +154,11 @@ class AddLinkDialog extends HookWidget {
final String url;
final String selection;
static final _websiteRegex = RegExp(r'https?:\/\/', caseSensitive: false);
AddLinkDialog(this.selection)
: title = selection.startsWith('https?://') ? '' : selection,
url = selection.startsWith('https?://') ? selection : '';
: title = selection.startsWith(_websiteRegex) ? '' : selection,
url = selection.startsWith(_websiteRegex) ? selection : '';
@override
Widget build(BuildContext context) {