2024-11-09 03:16:14 +00:00
|
|
|
import 'dart:ui';
|
|
|
|
|
2024-12-07 13:33:01 +00:00
|
|
|
import 'package:dismissible_page/dismissible_page.dart';
|
2024-11-09 03:16:14 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
2024-12-07 13:33:01 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
2024-11-09 03:16:14 +00:00
|
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
|
|
import 'package:markdown/markdown.dart' as markdown;
|
|
|
|
import 'package:styled_widget/styled_widget.dart';
|
2024-12-07 13:33:01 +00:00
|
|
|
import 'package:surface/types/attachment.dart';
|
|
|
|
import 'package:surface/widgets/attachment/attachment_item.dart';
|
2024-11-09 03:16:14 +00:00
|
|
|
import 'package:surface/widgets/universal_image.dart';
|
|
|
|
import 'package:syntax_highlight/syntax_highlight.dart';
|
|
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
|
|
|
import 'package:path/path.dart';
|
2024-12-07 13:33:01 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
2024-11-09 03:16:14 +00:00
|
|
|
|
2024-12-07 15:40:26 +00:00
|
|
|
import 'attachment/attachment_zoom.dart';
|
2024-12-07 13:33:01 +00:00
|
|
|
|
|
|
|
class MarkdownTextContent extends StatelessWidget {
|
2024-11-09 03:16:14 +00:00
|
|
|
final String content;
|
|
|
|
final bool isSelectable;
|
|
|
|
final bool isAutoWarp;
|
2024-12-07 13:33:01 +00:00
|
|
|
final TextScaler? textScaler;
|
|
|
|
final List<SnAttachment?>? attachments;
|
2024-11-09 03:16:14 +00:00
|
|
|
|
|
|
|
const MarkdownTextContent({
|
|
|
|
super.key,
|
|
|
|
required this.content,
|
|
|
|
this.isSelectable = false,
|
|
|
|
this.isAutoWarp = false,
|
2024-12-07 13:33:01 +00:00
|
|
|
this.textScaler,
|
|
|
|
this.attachments,
|
2024-11-09 03:16:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Widget _buildContent(BuildContext context) {
|
|
|
|
return Markdown(
|
|
|
|
shrinkWrap: true,
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
2024-12-07 13:33:01 +00:00
|
|
|
data: content,
|
2024-11-09 03:16:14 +00:00
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
styleSheet: MarkdownStyleSheet.fromTheme(
|
|
|
|
Theme.of(context),
|
|
|
|
).copyWith(
|
2024-12-07 13:33:01 +00:00
|
|
|
textScaler: textScaler,
|
2024-11-09 03:16:14 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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,
|
|
|
|
extensionSet: markdown.ExtensionSet(
|
|
|
|
<markdown.BlockSyntax>[
|
|
|
|
markdown.CodeBlockSyntax(),
|
|
|
|
...markdown.ExtensionSet.gitHubFlavored.blockSyntaxes,
|
|
|
|
],
|
|
|
|
<markdown.InlineSyntax>[
|
2024-12-07 13:33:01 +00:00
|
|
|
if (isAutoWarp) markdown.LineBreakSyntax(),
|
2024-11-09 03:16:14 +00:00
|
|
|
_UserNameCardInlineSyntax(),
|
|
|
|
markdown.AutolinkSyntax(),
|
|
|
|
markdown.AutolinkExtensionSyntax(),
|
|
|
|
markdown.CodeSyntax(),
|
|
|
|
...markdown.ExtensionSet.gitHubFlavored.inlineSyntaxes
|
|
|
|
],
|
|
|
|
),
|
|
|
|
onTapLink: (text, href, title) async {
|
|
|
|
if (href == null) return;
|
|
|
|
if (href.startsWith('solink://')) {
|
2024-12-07 13:33:01 +00:00
|
|
|
final uri = href.replaceFirst('solink://', '');
|
|
|
|
final segments = uri.split('/');
|
|
|
|
switch (segments[0]) {
|
|
|
|
default:
|
2024-12-07 16:40:44 +00:00
|
|
|
GoRouter.of(context).push('/$uri');
|
2024-12-07 13:33:01 +00:00
|
|
|
}
|
2024-11-09 03:16:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await launchUrlString(
|
|
|
|
href,
|
|
|
|
mode: LaunchMode.externalApplication,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
imageBuilder: (uri, title, alt) {
|
|
|
|
var url = uri.toString();
|
|
|
|
double? width, height;
|
|
|
|
BoxFit? fit;
|
|
|
|
if (url.startsWith('solink://')) {
|
2024-12-07 13:33:01 +00:00
|
|
|
final segments = url.replaceFirst('solink://', '').split('/');
|
|
|
|
switch (segments[0]) {
|
|
|
|
case 'attachments':
|
|
|
|
final attachment = attachments?.firstWhere(
|
|
|
|
(ele) => ele?.rid == segments[1],
|
|
|
|
orElse: () => null,
|
|
|
|
);
|
|
|
|
if (attachment != null) {
|
|
|
|
const uuid = Uuid();
|
|
|
|
final heroTag = uuid.v4();
|
|
|
|
return GestureDetector(
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
|
|
|
border: Border.all(
|
|
|
|
color: Theme.of(context).dividerColor,
|
|
|
|
width: 1,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
|
|
|
child: AspectRatio(
|
|
|
|
aspectRatio: attachment.metadata['ratio'] ??
|
|
|
|
switch (attachment.mimetype.split('/').firstOrNull) {
|
|
|
|
'audio' => 16 / 9,
|
|
|
|
'video' => 16 / 9,
|
|
|
|
_ => 1,
|
|
|
|
}
|
|
|
|
.toDouble(),
|
|
|
|
child: AttachmentItem(
|
|
|
|
data: attachment,
|
|
|
|
heroTag: heroTag,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
context.pushTransparentRoute(
|
|
|
|
AttachmentZoomView(
|
|
|
|
data: [attachment],
|
|
|
|
initialIndex: 0,
|
|
|
|
heroTags: [heroTag],
|
|
|
|
),
|
|
|
|
backgroundColor: Colors.black.withOpacity(0.7),
|
|
|
|
rootNavigator: true,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-11-09 03:16:14 +00:00
|
|
|
return const SizedBox.shrink();
|
|
|
|
}
|
|
|
|
return UniversalImage(
|
|
|
|
url,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-12-07 13:33:01 +00:00
|
|
|
if (isSelectable) {
|
2024-11-09 03:16:14 +00:00
|
|
|
return SelectionArea(child: _buildContent(context));
|
|
|
|
}
|
|
|
|
return _buildContent(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UserNameCardInlineSyntax extends markdown.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(
|
2024-12-07 16:40:44 +00:00
|
|
|
'solink://account/${alias.substring(1)}',
|
2024-11-09 03:16:14 +00:00
|
|
|
);
|
|
|
|
parser.addNode(anchor);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = '../../../';
|
2024-12-07 13:33:01 +00:00
|
|
|
final highlightingPath = join(docPath, 'assets/highlighting', language);
|
2024-11-09 03:16:14 +00:00
|
|
|
await Highlighter.initialize([highlightingPath]);
|
|
|
|
return Highlighter(
|
|
|
|
language: highlightingPath,
|
2024-12-07 13:33:01 +00:00
|
|
|
theme: PlatformDispatcher.instance.platformBrightness == Brightness.light
|
2024-11-09 03:16:14 +00:00
|
|
|
? await HighlighterTheme.loadLightTheme()
|
|
|
|
: await HighlighterTheme.loadDarkTheme(),
|
|
|
|
);
|
|
|
|
})(),
|
|
|
|
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(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
).padding(all: 8);
|
|
|
|
}
|
|
|
|
}
|