fluffychat/lib/utils/markdown_context_builder.dart
ggurdin 49e586a7ad
Fluffychat merge (#1685)
chore: Merge upstream changes

---------

Signed-off-by: Krille <c.kussowski@famedly.com>
Co-authored-by: krille-chan <christian-kussowski@posteo.de>
Co-authored-by: Krille <c.kussowski@famedly.com>
Co-authored-by: Linerly <linerly@proton.me>
Co-authored-by: Priit Jõerüüt <hwlate@joeruut.com>
Co-authored-by: 大王叫我来巡山 <hamburger2048@users.noreply.hosted.weblate.org>
Co-authored-by: fadelkon <fadelkon@posteo.net>
Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com>
Co-authored-by: Edgars Andersons <Edgars+Weblate@gaitenis.id.lv>
Co-authored-by: josé m <correoxm@disroot.org>
Co-authored-by: Bezruchenko Simon <worcposj44@gmail.com>
Co-authored-by: Christian <christian-pauly@posteo.de>
Co-authored-by: - <hitekex@yandex.ru>
Co-authored-by: Angelo Schirinzi <Odi-3@users.noreply.hosted.weblate.org>
Co-authored-by: xabirequejo <xabi.rn@gmail.com>
Co-authored-by: Piotr Orzechowski <piotr@orzechowski.tech>
Co-authored-by: Rex_sa <rex.sa@pm.me>
Co-authored-by: Tewuzij <tenajeza@outlook.com>
Co-authored-by: goknarbahceli <goknarbahceli@proton.me>
Co-authored-by: தமிழ்நேரம் <anishprabu.t@gmail.com>
Co-authored-by: Erin <erin@erindesu.cz>
Co-authored-by: EpicKiwi <me@epickiwi.fr>
Co-authored-by: Christian Tietze <me@christiantietze.de>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-02-03 12:36:46 -05:00

97 lines
3.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_text_input_dialog.dart';
Widget markdownContextBuilder(
BuildContext context,
EditableTextState editableTextState,
TextEditingController controller,
) {
final value = editableTextState.textEditingValue;
final selectedText = value.selection.textInside(value.text);
final buttonItems = editableTextState.contextMenuButtonItems;
final l10n = L10n.of(context);
return AdaptiveTextSelectionToolbar.buttonItems(
anchors: editableTextState.contextMenuAnchors,
buttonItems: [
...buttonItems,
if (selectedText.isNotEmpty) ...[
ContextMenuButtonItem(
label: l10n.link,
onPressed: () async {
final input = await showTextInputDialog(
context: context,
title: l10n.addLink,
okLabel: l10n.ok,
cancelLabel: l10n.cancel,
validator: (text) {
if (text.isEmpty) {
return l10n.pleaseFillOut;
}
try {
text.startsWith('http') ? Uri.parse(text) : Uri.https(text);
} catch (_) {
return l10n.invalidUrl;
}
return null;
},
hintText: 'www...',
keyboardType: TextInputType.url,
);
final urlString = input;
if (urlString == null) return;
final url = urlString.startsWith('http')
? Uri.parse(urlString)
: Uri.https(urlString);
final selection = controller.selection;
controller.text = controller.text.replaceRange(
selection.start,
selection.end,
'[$selectedText](${url.toString()})',
);
ContextMenuController.removeAny();
},
),
ContextMenuButtonItem(
label: l10n.boldText,
onPressed: () {
final selection = controller.selection;
controller.text = controller.text.replaceRange(
selection.start,
selection.end,
'**$selectedText**',
);
ContextMenuController.removeAny();
},
),
ContextMenuButtonItem(
label: l10n.italicText,
onPressed: () {
final selection = controller.selection;
controller.text = controller.text.replaceRange(
selection.start,
selection.end,
'*$selectedText*',
);
ContextMenuController.removeAny();
},
),
ContextMenuButtonItem(
label: l10n.strikeThrough,
onPressed: () {
final selection = controller.selection;
controller.text = controller.text.replaceRange(
selection.start,
selection.end,
'~~$selectedText~~',
);
ContextMenuController.removeAny();
},
),
],
],
);
}