2024-10-07 08:29:36 +00:00
|
|
|
import 'dart:ui';
|
|
|
|
|
2024-07-09 14:39:44 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-10-07 08:23:25 +00:00
|
|
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
2024-09-23 14:43:13 +00:00
|
|
|
import 'package:gap/gap.dart';
|
2024-08-03 17:53:52 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-10-07 08:23:25 +00:00
|
|
|
import 'package:google_fonts/google_fonts.dart';
|
2024-07-09 14:39:44 +00:00
|
|
|
import 'package:markdown/markdown.dart' as markdown;
|
2024-10-07 08:23:25 +00:00
|
|
|
import 'package:path/path.dart';
|
2024-08-03 17:53:52 +00:00
|
|
|
import 'package:solian/providers/stickers.dart';
|
2024-08-06 08:24:47 +00:00
|
|
|
import 'package:solian/widgets/attachments/attachment_list.dart';
|
2024-09-10 13:53:05 +00:00
|
|
|
import 'package:solian/widgets/auto_cache_image.dart';
|
2024-10-07 08:23:25 +00:00
|
|
|
import 'package:syntax_highlight/syntax_highlight.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;
|
2024-08-06 08:24:47 +00:00
|
|
|
final String parentId;
|
2024-07-13 10:54:08 +00:00
|
|
|
final bool isSelectable;
|
2024-09-17 13:48:20 +00:00
|
|
|
final bool isLargeText;
|
2024-09-23 14:43:13 +00:00
|
|
|
final bool isAutoWarp;
|
2024-07-09 14:39:44 +00:00
|
|
|
|
2024-07-13 10:54:08 +00:00
|
|
|
const MarkdownTextContent({
|
|
|
|
super.key,
|
|
|
|
required this.content,
|
2024-08-06 08:24:47 +00:00
|
|
|
required this.parentId,
|
2024-07-13 10:54:08 +00:00
|
|
|
this.isSelectable = false,
|
2024-09-17 13:48:20 +00:00
|
|
|
this.isLargeText = false,
|
2024-09-23 14:43:13 +00:00
|
|
|
this.isAutoWarp = false,
|
2024-07-13 10:54:08 +00:00
|
|
|
});
|
2024-07-09 14:39:44 +00:00
|
|
|
|
2024-07-30 06:49:26 +00:00
|
|
|
Widget _buildContent(BuildContext context) {
|
2024-09-23 14:43:13 +00:00
|
|
|
final stickerRegex = RegExp(r':([-\w]+):');
|
2024-08-06 16:52:34 +00:00
|
|
|
|
2024-09-23 14:43:13 +00:00
|
|
|
// Split the content into paragraphs
|
|
|
|
final paragraphs = content.split(RegExp(r'\n\s*\n'));
|
|
|
|
|
|
|
|
// Iterate over each paragraph to process stickers individually
|
|
|
|
List<Widget> contentWidgets = [];
|
|
|
|
for (var idx = 0; idx < paragraphs.length; idx++) {
|
|
|
|
// Getting paragraph
|
|
|
|
var paragraph = paragraphs[idx];
|
|
|
|
|
|
|
|
// Matching stickers
|
|
|
|
final stickerMatch = stickerRegex.allMatches(paragraph);
|
|
|
|
final isOnlySticker =
|
|
|
|
paragraph.replaceAll(stickerRegex, '').trim().isEmpty;
|
|
|
|
|
|
|
|
contentWidgets.add(
|
|
|
|
Markdown(
|
|
|
|
shrinkWrap: true,
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
data: paragraph,
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
styleSheet: MarkdownStyleSheet.fromTheme(
|
|
|
|
Theme.of(context),
|
|
|
|
).copyWith(
|
2024-10-07 08:23:25 +00:00
|
|
|
textScaler: TextScaler.linear(isLargeText ? 1.1 : 1),
|
|
|
|
blockquote: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
blockquoteDecoration: BoxDecoration(
|
|
|
|
color: Theme.of(context).colorScheme.surfaceContainerHigh,
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(4)),
|
|
|
|
),
|
|
|
|
horizontalRuleDecoration: BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
top: BorderSide(
|
|
|
|
width: 1.0,
|
|
|
|
color: Theme.of(context).dividerColor,
|
|
|
|
),
|
2024-08-03 04:29:13 +00:00
|
|
|
),
|
2024-09-23 14:43:13 +00:00
|
|
|
),
|
2024-10-07 08:23:25 +00:00
|
|
|
codeblockDecoration: BoxDecoration(
|
|
|
|
border: Border.all(
|
|
|
|
color: Theme.of(context).dividerColor,
|
|
|
|
width: 0.3,
|
|
|
|
),
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(4)),
|
|
|
|
color: Theme.of(context).colorScheme.surface.withOpacity(0.5),
|
|
|
|
)),
|
|
|
|
builders: {
|
|
|
|
'code': _MarkdownTextCodeElement(),
|
|
|
|
},
|
|
|
|
softLineBreak: true,
|
2024-09-23 14:43:13 +00:00
|
|
|
extensionSet: markdown.ExtensionSet(
|
2024-10-07 08:23:25 +00:00
|
|
|
<markdown.BlockSyntax>[
|
|
|
|
markdown.CodeBlockSyntax(),
|
|
|
|
...markdown.ExtensionSet.commonMark.blockSyntaxes,
|
|
|
|
...markdown.ExtensionSet.gitHubFlavored.blockSyntaxes,
|
|
|
|
],
|
2024-09-23 14:43:13 +00:00
|
|
|
<markdown.InlineSyntax>[
|
2024-10-07 08:23:25 +00:00
|
|
|
if (isAutoWarp) markdown.LineBreakSyntax(),
|
2024-09-23 14:43:13 +00:00
|
|
|
_UserNameCardInlineSyntax(),
|
|
|
|
_CustomEmoteInlineSyntax(),
|
|
|
|
markdown.AutolinkSyntax(),
|
|
|
|
markdown.AutolinkExtensionSyntax(),
|
2024-10-07 08:23:25 +00:00
|
|
|
markdown.CodeSyntax(),
|
|
|
|
...markdown.ExtensionSet.commonMark.inlineSyntaxes,
|
2024-09-23 14:43:13 +00:00
|
|
|
...markdown.ExtensionSet.gitHubFlavored.inlineSyntaxes
|
|
|
|
],
|
|
|
|
),
|
|
|
|
onTapLink: (text, href, title) async {
|
|
|
|
if (href == null) return;
|
|
|
|
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-08-03 04:29:13 +00:00
|
|
|
|
2024-09-23 14:43:13 +00:00
|
|
|
await launchUrlString(
|
|
|
|
href,
|
|
|
|
mode: LaunchMode.externalApplication,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
imageBuilder: (uri, title, alt) {
|
|
|
|
var url = uri.toString();
|
|
|
|
double? width, height;
|
|
|
|
BoxFit? fit;
|
|
|
|
if (url.startsWith('solink://')) {
|
|
|
|
final segments = url.replaceFirst('solink://', '').split('/');
|
|
|
|
switch (segments[0]) {
|
|
|
|
case 'stickers':
|
|
|
|
double radius = 8;
|
|
|
|
final StickerProvider sticker = Get.find();
|
|
|
|
|
|
|
|
// Adjust sticker size based on the sticker count in this paragraph
|
|
|
|
if (stickerMatch.length <= 1 && isOnlySticker) {
|
|
|
|
width = 128;
|
|
|
|
height = 128;
|
|
|
|
} else if (stickerMatch.length <= 3 && isOnlySticker) {
|
|
|
|
width = 32;
|
|
|
|
height = 32;
|
|
|
|
} else {
|
|
|
|
radius = 4;
|
|
|
|
width = 16;
|
|
|
|
height = 16;
|
|
|
|
}
|
|
|
|
fit = BoxFit.contain;
|
|
|
|
return ClipRRect(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(radius)),
|
|
|
|
child: Container(
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
color: Theme.of(context).colorScheme.surfaceContainer,
|
|
|
|
child: FutureBuilder(
|
|
|
|
future: sticker.getStickerByAlias(segments[1]),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData) {
|
|
|
|
return const Center(
|
|
|
|
child: CircularProgressIndicator());
|
|
|
|
}
|
|
|
|
return AutoCacheImage(
|
|
|
|
snapshot.data!.imageUrl,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
noErrorWidget: true,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).paddingSymmetric(vertical: 4);
|
|
|
|
case 'attachments':
|
|
|
|
const radius = BorderRadius.all(Radius.circular(8));
|
|
|
|
return LimitedBox(
|
|
|
|
maxHeight: MediaQuery.of(context).size.width,
|
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: radius,
|
|
|
|
child: AttachmentSelfContainedEntry(
|
|
|
|
isDense: true,
|
|
|
|
parentId: parentId,
|
|
|
|
rid: segments[1],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).paddingSymmetric(vertical: 4);
|
2024-08-06 16:52:34 +00:00
|
|
|
}
|
2024-09-23 14:43:13 +00:00
|
|
|
}
|
|
|
|
return AutoCacheImage(
|
|
|
|
url,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (idx < paragraphs.length - 1) {
|
2024-10-06 15:06:33 +00:00
|
|
|
contentWidgets.add(isAutoWarp ? const Gap(4) : const Gap(8));
|
2024-09-23 14:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-06 08:24:47 +00:00
|
|
|
|
2024-09-23 14:43:13 +00:00
|
|
|
// Return the list of widgets for the paragraphs
|
|
|
|
return Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: contentWidgets,
|
2024-07-09 14:39:44 +00:00
|
|
|
);
|
|
|
|
}
|
2024-07-30 06:49:26 +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
|
|
|
|
2024-10-07 08:23:25 +00:00
|
|
|
class _UserNameCardInlineSyntax extends markdown.InlineSyntax {
|
2024-08-03 04:29:13 +00:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 08:23:25 +00:00
|
|
|
class _CustomEmoteInlineSyntax extends markdown.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) {
|
2024-08-03 17:53:52 +00:00
|
|
|
final StickerProvider sticker = Get.find();
|
2024-08-06 17:47:53 +00:00
|
|
|
final alias = match[1]!.toUpperCase();
|
2024-09-16 13:00:19 +00:00
|
|
|
if (sticker.stickerCache.containsKey(alias) &&
|
|
|
|
sticker.stickerCache[alias] == null) {
|
2024-08-03 17:53:52 +00:00
|
|
|
parser.advanceBy(1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-03 04:29:13 +00:00
|
|
|
final element = markdown.Element.empty('img');
|
2024-08-06 08:24:47 +00:00
|
|
|
element.attributes['src'] = 'solink://stickers/$alias';
|
2024-08-03 04:29:13 +00:00
|
|
|
parser.addNode(element);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 08:23:25 +00:00
|
|
|
|
|
|
|
class _MarkdownTextCodeElement extends MarkdownElementBuilder {
|
|
|
|
@override
|
|
|
|
Widget? visitElementAfter(
|
|
|
|
markdown.Element element,
|
|
|
|
TextStyle? preferredStyle,
|
|
|
|
) {
|
|
|
|
var language = '';
|
|
|
|
|
|
|
|
if (element.attributes['class'] != null) {
|
|
|
|
String lg = element.attributes['class'] as String;
|
|
|
|
language = lg.substring(9).trim();
|
|
|
|
}
|
|
|
|
return SizedBox(
|
|
|
|
child: FutureBuilder(
|
|
|
|
future: (() async {
|
|
|
|
final docPath = '../../../';
|
|
|
|
final highlightingPath =
|
|
|
|
join(docPath, 'assets/highlighting', language);
|
|
|
|
await Highlighter.initialize([highlightingPath]);
|
|
|
|
return Highlighter(
|
|
|
|
language: highlightingPath,
|
2024-10-07 08:29:36 +00:00
|
|
|
theme: PlatformDispatcher.instance.platformBrightness ==
|
|
|
|
Brightness.light
|
|
|
|
? await HighlighterTheme.loadLightTheme()
|
|
|
|
: await HighlighterTheme.loadDarkTheme(),
|
2024-10-07 08:23:25 +00:00
|
|
|
);
|
|
|
|
})(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
final highlighter = snapshot.data!;
|
|
|
|
return Text.rich(
|
|
|
|
highlighter.highlight(element.textContent.trim()),
|
|
|
|
style: GoogleFonts.robotoMono(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Text(
|
|
|
|
element.textContent.trim(),
|
|
|
|
style: GoogleFonts.robotoMono(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
).paddingAll(8);
|
|
|
|
}
|
|
|
|
}
|