From 5ced129d1db1152419169285fa02b0e708cab395 Mon Sep 17 00:00:00 2001 From: CodeDoctorDE Date: Tue, 7 Nov 2023 13:10:32 +0100 Subject: [PATCH] Add more formatting tools --- app/lib/l10n/app_en.arb | 7 ++- app/lib/pages/notes/card.dart | 97 +++++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 22 deletions(-) diff --git a/app/lib/l10n/app_en.arb b/app/lib/l10n/app_en.arb index 12eedc2cdcb..7f6b50988e7 100644 --- a/app/lib/l10n/app_en.arb +++ b/app/lib/l10n/app_en.arb @@ -279,5 +279,10 @@ "example": "1" } } - } + }, + "bold": "Bold", + "italic": "Italic", + "strikethrough": "Strikethrough", + "code": "Code", + "link": "Link" } \ No newline at end of file diff --git a/app/lib/pages/notes/card.dart b/app/lib/pages/notes/card.dart index e84e9018045..39a6a827953 100644 --- a/app/lib/pages/notes/card.dart +++ b/app/lib/pages/notes/card.dart @@ -53,6 +53,8 @@ class _NoteCardState extends State { bool _loading = false; + final _formattingScrollController = ScrollController(); + @override void initState() { super.initState(); @@ -73,6 +75,15 @@ class _NoteCardState extends State { }); } + @override + void dispose() { + _nameController.dispose(); + _descriptionController.dispose(); + _nameFocus.dispose(); + _formattingScrollController.dispose(); + super.dispose(); + } + @override void didUpdateWidget(covariant NoteCard oldWidget) { super.didUpdateWidget(oldWidget); @@ -96,15 +107,18 @@ class _NoteCardState extends State { var description = _descriptionController.text; final selection = _descriptionController.selection; if (!selection.isValid) return; - final start = selection.baseOffset; - final lineStart = description.lastIndexOf("\n", start - 1); + final start = max(1, selection.start); + final end = max(1, selection.end); + final lineStart = description.lastIndexOf("\n", start - 1) + 1; description = switch (position) { - PastePositing.line => description.substring(0, lineStart + 1) + + PastePositing.line => description.substring(0, lineStart) + text + - description.substring(lineStart + 1), + description.substring(lineStart), PastePositing.selection => description.substring(0, start) + text + - description.substring(selection.extentOffset), + description.substring(start, end) + + text + + description.substring(end), }; _descriptionController.text = description; _descriptionController.selection = TextSelection.collapsed( @@ -332,22 +346,63 @@ class _NoteCardState extends State { if (widget.primary) SizedBox( height: 50, - child: ListView(scrollDirection: Axis.horizontal, children: [ - ...[ - PhosphorIconsLight.textHOne, - PhosphorIconsLight.textHTwo, - PhosphorIconsLight.textHThree, - PhosphorIconsLight.textHFour, - PhosphorIconsLight.textHFive, - PhosphorIconsLight.textHSix - ].mapIndexed((index, element) => IconButton( - icon: PhosphorIcon(element), - onPressed: () => _addDescription( - PastePositing.line, - "${"#" * (index + 1)} ", - ), - )) - ]), + child: Scrollbar( + controller: _formattingScrollController, + child: ListView( + controller: _formattingScrollController, + scrollDirection: Axis.horizontal, + children: [ + ...[ + PhosphorIconsLight.textHOne, + PhosphorIconsLight.textHTwo, + PhosphorIconsLight.textHThree, + PhosphorIconsLight.textHFour, + PhosphorIconsLight.textHFive, + PhosphorIconsLight.textHSix + ].mapIndexed((index, element) => IconButton( + icon: PhosphorIcon(element), + onPressed: () => _addDescription( + PastePositing.line, + "${"#" * (index + 1)} ", + ), + )), + const SizedBox(width: 8), + ...[ + ( + '**', + PhosphorIconsLight.textB, + AppLocalizations.of(context).bold + ), + ( + '*', + PhosphorIconsLight.textItalic, + AppLocalizations.of(context).italic + ), + ( + '~~', + PhosphorIconsLight.textStrikethrough, + AppLocalizations.of(context).strikethrough + ), + ( + '`', + PhosphorIconsLight.code, + AppLocalizations.of(context).code + ), + ( + '[', + PhosphorIconsLight.link, + AppLocalizations.of(context).link + ), + ].map((e) => IconButton( + icon: PhosphorIcon(e.$2), + tooltip: e.$3, + onPressed: () => _addDescription( + PastePositing.selection, + e.$1, + ), + )), + ]), + ), ), MarkdownField( decoration: InputDecoration(