Solian/lib/widgets/markdown_text_content.dart

187 lines
5.7 KiB
Dart
Raw Normal View History

2024-07-09 14:39:44 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_markdown_selectionarea/flutter_markdown.dart';
import 'package:get/get.dart';
2024-07-09 14:39:44 +00:00
import 'package:markdown/markdown.dart' as markdown;
2024-08-03 04:29:13 +00:00
import 'package:markdown/markdown.dart';
import 'package:solian/providers/stickers.dart';
import 'package:solian/widgets/attachments/attachment_list.dart';
import 'package:solian/widgets/auto_cache_image.dart';
2024-07-09 14:39:44 +00:00
import 'package:url_launcher/url_launcher_string.dart';
2024-08-03 04:29:13 +00:00
import 'account/account_profile_popup.dart';
2024-07-11 16:44:57 +00:00
class MarkdownTextContent extends StatelessWidget {
2024-07-09 14:39:44 +00:00
final String content;
final String parentId;
final bool isSelectable;
2024-07-09 14:39:44 +00:00
const MarkdownTextContent({
super.key,
required this.content,
required this.parentId,
this.isSelectable = false,
});
2024-07-09 14:39:44 +00:00
Widget _buildContent(BuildContext context) {
2024-08-06 17:47:53 +00:00
final emojiRegex = RegExp(r':([-\w]+):');
final emojiMatch = emojiRegex.allMatches(content);
final isOnlyEmoji = content.replaceAll(emojiRegex, '').trim().isEmpty;
2024-08-06 16:52:34 +00:00
2024-07-09 14:39:44 +00:00
return Markdown(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
data: content,
padding: EdgeInsets.zero,
2024-07-31 17:21:27 +00:00
styleSheet: MarkdownStyleSheet.fromTheme(
Theme.of(context),
).copyWith(
2024-07-11 16:44:57 +00:00
horizontalRuleDecoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 1.0,
color: Theme.of(context).dividerColor,
),
),
),
),
2024-07-09 14:39:44 +00:00
extensionSet: markdown.ExtensionSet(
markdown.ExtensionSet.gitHubFlavored.blockSyntaxes,
<markdown.InlineSyntax>[
2024-08-03 04:29:13 +00:00
_UserNameCardInlineSyntax(),
_CustomEmoteInlineSyntax(),
2024-07-09 14:39:44 +00:00
markdown.EmojiSyntax(),
markdown.AutolinkSyntax(),
2024-07-09 14:39:44 +00:00
markdown.AutolinkExtensionSyntax(),
...markdown.ExtensionSet.gitHubFlavored.inlineSyntaxes
],
),
onTapLink: (text, href, title) async {
if (href == null) return;
2024-08-03 04:29:13 +00:00
if (href.startsWith('solink://')) {
final segments = href.replaceFirst('solink://', '').split('/');
switch (segments[0]) {
case 'users':
showModalBottomSheet(
useRootNavigator: true,
isScrollControlled: true,
backgroundColor: Theme.of(context).colorScheme.surface,
context: context,
builder: (context) => AccountProfilePopup(
name: segments[1],
),
);
}
return;
}
2024-07-09 14:39:44 +00:00
await launchUrlString(
href,
mode: LaunchMode.externalApplication,
);
},
imageBuilder: (uri, title, alt) {
var url = uri.toString();
double? width, height;
2024-08-06 16:52:34 +00:00
BoxFit? fit;
if (url.startsWith('solink://')) {
final segments = url.replaceFirst('solink://', '').split('/');
switch (segments[0]) {
case 'stickers':
2024-08-07 11:11:52 +00:00
double radius = 8;
final StickerProvider sticker = Get.find();
2024-08-06 17:47:53 +00:00
url = sticker.aliasImageMapping[segments[1].toUpperCase()]!;
2024-08-06 16:52:34 +00:00
if (emojiMatch.length <= 1 && isOnlyEmoji) {
2024-08-07 11:11:52 +00:00
width = 128;
height = 128;
2024-08-06 16:52:34 +00:00
} else if (emojiMatch.length <= 3 && isOnlyEmoji) {
2024-08-07 11:11:52 +00:00
width = 32;
height = 32;
2024-08-06 16:52:34 +00:00
} else {
2024-08-07 11:11:52 +00:00
radius = 4;
width = 16;
height = 16;
2024-08-06 16:52:34 +00:00
}
2024-08-06 17:20:23 +00:00
fit = BoxFit.contain;
2024-08-07 11:11:52 +00:00
return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(radius)),
child: Container(
color: Theme.of(context).colorScheme.surfaceContainer,
child: AutoCacheImage(
url,
width: width,
height: height,
fit: fit,
),
2024-08-07 11:11:52 +00:00
),
).paddingSymmetric(vertical: 4);
case 'attachments':
const radius = BorderRadius.all(Radius.circular(8));
return LimitedBox(
2024-08-06 17:20:23 +00:00
maxHeight: MediaQuery.of(context).size.width,
child: ClipRRect(
borderRadius: radius,
child: AttachmentSelfContainedEntry(
isDense: true,
parentId: parentId,
rid: segments[1],
),
),
).paddingSymmetric(vertical: 4);
}
}
return AutoCacheImage(
url,
width: width,
height: height,
fit: fit,
);
},
2024-07-09 14:39:44 +00:00
);
}
@override
Widget build(BuildContext context) {
if (isSelectable) {
return SelectionArea(child: _buildContent(context));
}
return _buildContent(context);
}
2024-07-09 14:39:44 +00:00
}
2024-08-03 04:29:13 +00:00
class _UserNameCardInlineSyntax extends InlineSyntax {
_UserNameCardInlineSyntax() : super(r'@[a-zA-Z0-9_]+');
@override
bool onMatch(markdown.InlineParser parser, Match match) {
final alias = match[0]!;
final anchor = markdown.Element.text('a', alias)
..attributes['href'] = Uri.encodeFull(
'solink://users/${alias.substring(1)}',
);
parser.addNode(anchor);
return true;
}
}
class _CustomEmoteInlineSyntax extends InlineSyntax {
2024-08-06 17:47:53 +00:00
_CustomEmoteInlineSyntax() : super(r':([-\w]+):');
2024-08-03 04:29:13 +00:00
@override
bool onMatch(markdown.InlineParser parser, Match match) {
final StickerProvider sticker = Get.find();
2024-08-06 17:47:53 +00:00
final alias = match[1]!.toUpperCase();
if (sticker.aliasImageMapping[alias] == null) {
parser.advanceBy(1);
return false;
}
2024-08-03 04:29:13 +00:00
final element = markdown.Element.empty('img');
element.attributes['src'] = 'solink://stickers/$alias';
2024-08-03 04:29:13 +00:00
parser.addNode(element);
return true;
}
}