Compare commits

...

10 Commits

Author SHA1 Message Date
Filip Krawczyk 8aefdfbf27 remove list item if empty + enter pressed 2022-08-25 18:46:32 +02:00
Filip Krawczyk b2ef0883e3 remove store from being a property. also better animation 2022-08-25 18:22:14 +02:00
Filip Krawczyk e3b561835b remove magic number in favor of calculating it on the fly 2022-08-25 17:50:06 +02:00
Filip Krawczyk 9ec5410273 remove unneed FocusScope & Focus 2022-08-25 17:45:22 +02:00
Filip Krawczyk d11a46393b remove unneeded material 2022-08-25 17:42:31 +02:00
Filip Krawczyk c83e93c755 add transition for toolbar appearing and disappearing 2022-08-25 17:40:37 +02:00
Filip Krawczyk 2cc82e6a45 fix 2022-08-25 17:30:02 +02:00
Filip Krawczyk 90553794b2
Update lib/widgets/editor/editor_toolbar.dart
Co-authored-by: Marcin Wojnarowski <xmarcinmarcin@gmail.com>
2022-08-25 17:29:28 +02:00
Filip Krawczyk b39d6b06d7 create widget for stuff that sticks to bottom 2022-08-25 17:27:40 +02:00
Filip Krawczyk 6729a040ea make list completion more reusable 2022-08-25 17:24:05 +02:00
6 changed files with 145 additions and 100 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;
@ -37,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
@ -56,35 +72,52 @@ 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 ');
}
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

@ -142,17 +142,11 @@ class CreatePostPage extends HookWidget {
),
),
),
SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Spacer(),
EditorToolbar(
editorFocusNode: editorFocusNode,
controller: bodyController,
instanceHost: context.read<CreatePostStore>().instanceHost,
),
],
BottomSticky(
child: EditorToolbar(
editorFocusNode: editorFocusNode,
controller: bodyController,
instanceHost: context.read<CreatePostStore>().instanceHost,
),
),
],

View File

@ -384,19 +384,13 @@ class _ManageAccount extends HookWidget {
const BottomSafe(),
],
),
SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Spacer(),
EditorToolbar(
editorFocusNode: bioFocusNode,
controller: bioController,
instanceHost: user.instanceHost,
),
],
BottomSticky(
child: EditorToolbar(
editorFocusNode: bioFocusNode,
controller: bioController,
instanceHost: user.instanceHost,
),
),
)
],
);
}

View File

@ -52,22 +52,18 @@ class Editor extends HookWidget {
);
}
return FocusScope(
child: Focus(
focusNode: focusNode,
child: TextField(
controller: actualController,
autofocus: autofocus,
keyboardType: TextInputType.multiline,
textCapitalization: TextCapitalization.sentences,
onChanged: onChanged,
onSubmitted: onSubmitted,
maxLines: maxLines,
minLines: minLines,
decoration: InputDecoration(labelText: labelText),
inputFormatters: [MarkdownFormatter()],
),
),
return TextField(
focusNode: focusNode,
controller: actualController,
autofocus: autofocus,
keyboardType: TextInputType.multiline,
textCapitalization: TextCapitalization.sentences,
onChanged: onChanged,
onSubmitted: onSubmitted,
maxLines: maxLines,
minLines: minLines,
decoration: InputDecoration(labelText: labelText),
inputFormatters: [MarkdownFormatter()],
);
}
}

View File

@ -42,52 +42,73 @@ enum HeaderLevel {
class EditorToolbar extends HookWidget {
final TextEditingController controller;
final String instanceHost;
final EditorToolbarStore store;
final FocusNode editorFocusNode;
static const _height = 50.0;
EditorToolbar({
const EditorToolbar({
required this.controller,
required this.instanceHost,
required this.editorFocusNode,
}) : store = EditorToolbarStore(instanceHost);
});
@override
Widget build(BuildContext context) {
final visible = useState(editorFocusNode.hasFocus);
useEffect(() {
editorFocusNode.addListener(() {
visible.value = editorFocusNode.hasFocus;
});
return;
}, [editorFocusNode]);
final visible = useListenable(editorFocusNode).hasFocus;
return MobxProvider(
create: (context) => store,
child: AsyncStoreListener(
asyncStore: store.imageUploadState,
child: visible.value
? Container(
height: _height,
width: double.infinity,
color: Theme.of(context).cardColor,
child: Material(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: _ToolbarBody(
controller: controller,
instanceHost: instanceHost,
create: (context) => EditorToolbarStore(instanceHost),
child: ObserverBuilder<EditorToolbarStore>(builder: (context, store) {
return AsyncStoreListener(
asyncStore: store.imageUploadState,
child: AnimatedSwitcher(
duration: kThemeAnimationDuration,
transitionBuilder: (child, animation) {
final offsetAnimation =
Tween<Offset>(begin: const Offset(0, 1.5), end: Offset.zero)
.animate(animation);
return SlideTransition(
position: offsetAnimation,
child: child,
);
},
child: visible
? Container(
height: _height,
width: double.infinity,
color: Theme.of(context).canvasColor,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: _ToolbarBody(
controller: controller,
instanceHost: instanceHost,
),
),
),
),
)
: const SizedBox.shrink(),
),
)
: const SizedBox.shrink(),
),
);
}),
);
}
static Widget safeArea = const SizedBox(height: _height);
static const safeArea = SizedBox(height: _height);
}
class BottomSticky extends StatelessWidget {
final Widget child;
const BottomSticky({required this.child});
@override
Widget build(BuildContext context) => SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Spacer(),
child,
],
),
);
}
class _ToolbarBody extends HookWidget {
@ -224,12 +245,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),
@ -263,11 +293,15 @@ class _ToolbarBody extends HookWidget {
IconButton(
onPressed: () {
controller.reformat((selection) {
final insides = selection.isNotEmpty ? selection : '___';
const textBeg = '\n::: spoiler spoiler\n';
final textMid = selection.isNotEmpty ? selection : '___';
const textEnd = '\n:::\n';
return _Reformat(
text: '\n::: spoiler spoiler\n$insides\n:::\n',
selectionBeginningShift: 21,
selectionEndingShift: 21 + insides.length - selection.length,
text: textBeg + textMid + textEnd,
selectionBeginningShift: textBeg.length,
selectionEndingShift:
textBeg.length + textMid.length - selection.length,
);
});
},

View File

@ -137,17 +137,11 @@ class WriteComment extends HookWidget {
EditorToolbar.safeArea,
],
),
SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Spacer(),
EditorToolbar(
editorFocusNode: editorFocusNode,
controller: controller,
instanceHost: post.instanceHost,
),
],
BottomSticky(
child: EditorToolbar(
editorFocusNode: editorFocusNode,
controller: controller,
instanceHost: post.instanceHost,
),
),
],