Link preview

This commit is contained in:
2024-12-14 14:46:11 +08:00
parent b750cc3c67
commit 1f8d47f6c3
13 changed files with 752 additions and 16 deletions

View File

@ -10,6 +10,7 @@ import 'package:surface/providers/userinfo.dart';
import 'package:surface/types/chat.dart';
import 'package:surface/widgets/account/account_image.dart';
import 'package:surface/widgets/attachment/attachment_list.dart';
import 'package:surface/widgets/link_preview.dart';
import 'package:surface/widgets/markdown_content.dart';
import 'package:swipe_to/swipe_to.dart';
@ -22,6 +23,7 @@ class ChatMessage extends StatelessWidget {
final Function(SnChatMessage)? onReply;
final Function(SnChatMessage)? onEdit;
final Function(SnChatMessage)? onDelete;
const ChatMessage({
super.key,
required this.data,
@ -63,7 +65,7 @@ class ChatMessage extends StatelessWidget {
onReply!(data);
},
),
if (isOwner && onEdit != null)
if (isOwner && data.type == 'messages.new' && onEdit != null)
MenuItem(
label: 'edit'.tr(),
icon: Symbols.edit,
@ -71,7 +73,7 @@ class ChatMessage extends StatelessWidget {
onEdit!(data);
},
),
if (isOwner && onDelete != null)
if (isOwner && data.type == 'messages.new' && onDelete != null)
MenuItem(
label: 'delete'.tr(),
icon: Symbols.delete,
@ -109,9 +111,7 @@ class ChatMessage extends StatelessWidget {
radius: 12,
).padding(right: 6),
Text(
(data.sender.nick?.isNotEmpty ?? false)
? data.sender.nick!
: user?.nick ?? 'unknown',
(data.sender.nick?.isNotEmpty ?? false) ? data.sender.nick! : user?.nick ?? 'unknown',
).bold(),
const Gap(6),
Text(
@ -123,8 +123,7 @@ class ChatMessage extends StatelessWidget {
if (data.preload?.quoteEvent != null)
StyledWidget(Container(
decoration: BoxDecoration(
borderRadius:
const BorderRadius.all(Radius.circular(8)),
borderRadius: const BorderRadius.all(Radius.circular(8)),
border: Border.all(
color: Theme.of(context).dividerColor,
width: 1,
@ -153,6 +152,8 @@ class ChatMessage extends StatelessWidget {
)
],
).opacity(isPending ? 0.5 : 1),
if (data.body['text'] != null && (data.body['text']?.isNotEmpty ?? false))
LinkPreviewWidget(text: data.body['text']!),
if (data.preload?.attachments?.isNotEmpty ?? false)
AttachmentList(
data: data.preload!.attachments!,
@ -161,10 +162,7 @@ class ChatMessage extends StatelessWidget {
maxHeight: 520,
listPadding: const EdgeInsets.only(top: 8),
),
if (!hasMerged && !isCompact)
const Gap(12)
else if (!isCompact)
const Gap(6),
if (!hasMerged && !isCompact) const Gap(12) else if (!isCompact) const Gap(6),
],
),
),
@ -174,6 +172,7 @@ class ChatMessage extends StatelessWidget {
class _ChatMessageText extends StatelessWidget {
final SnChatMessage data;
const _ChatMessageText({super.key, required this.data});
@override
@ -184,6 +183,7 @@ class _ChatMessageText extends StatelessWidget {
children: [
MarkdownTextContent(
content: data.body['text'],
isSelectable: true,
isAutoWarp: true,
),
if (data.updatedAt != data.createdAt)
@ -212,6 +212,7 @@ class _ChatMessageText extends StatelessWidget {
class _ChatMessageSystemNotify extends StatelessWidget {
final SnChatMessage data;
const _ChatMessageSystemNotify({super.key, required this.data});
String _formatDuration(Duration duration) {

View File

@ -0,0 +1,149 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:marquee/marquee.dart';
import 'package:provider/provider.dart';
import 'package:responsive_framework/responsive_framework.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:surface/types/link.dart';
import 'package:surface/widgets/universal_image.dart';
import 'package:url_launcher/url_launcher_string.dart';
import '../providers/link_preview.dart';
class LinkPreviewWidget extends StatefulWidget {
final String text;
const LinkPreviewWidget({super.key, required this.text});
@override
State<LinkPreviewWidget> createState() => _LinkPreviewWidgetState();
}
class _LinkPreviewWidgetState extends State<LinkPreviewWidget> {
final List<SnLinkMeta> _links = List.empty(growable: true);
Future<void> _getLinkMeta() async {
final linkRegex = RegExp(r'https?:\/\/[^\s/$.?#].[^\s]*');
final links = linkRegex.allMatches(widget.text).map((e) => e.group(0)).toSet();
final lp = context.read<SnLinkPreviewProvider>();
final List<Future<SnLinkMeta?>> futures = links.where((e) => e != null).map((e) => lp.getLinkMeta(e!)).toList();
final results = await Future.wait(futures);
_links.addAll(results.where((e) => e != null).map((e) => e!).toList());
if (_links.isNotEmpty && mounted) setState(() {});
}
@override
void initState() {
super.initState();
_getLinkMeta();
}
@override
Widget build(BuildContext context) {
if (_links.isEmpty) return const SizedBox.shrink();
return Wrap(
spacing: 8,
runSpacing: 8,
children: _links
.map(
(e) => Container(
constraints: BoxConstraints(
maxWidth: ResponsiveBreakpoints.of(context).smallerOrEqualTo(MOBILE) ? double.infinity : 480,
),
child: GestureDetector(
child: Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (e.image != null)
Container(
margin: const EdgeInsets.only(bottom: 4),
color: Theme.of(context).colorScheme.surfaceContainer,
child: AspectRatio(
aspectRatio: 16 / 9,
child: ClipRRect(
child: AutoResizeUniversalImage(
e.image!,
fit: BoxFit.contain,
),
),
),
),
SizedBox(
height: 48,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (e.icon != null)
UniversalImage(
e.icon!,
width: 36,
height: 36,
cacheHeight: 36,
cacheWidth: 36,
).padding(all: 4),
const Gap(12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 24,
child: Marquee(
text: e.title ?? 'unknown'.tr(),
style: TextStyle(fontSize: 17, height: 1),
scrollAxis: Axis.horizontal,
showFadingOnlyWhenScrolling: true,
pauseAfterRound: const Duration(seconds: 3),
),
),
if (e.siteName != null)
Text(
e.siteName!,
style: TextStyle(fontSize: 13, height: 0.9),
).fontSize(11),
],
),
),
const Gap(6),
],
).padding(horizontal: 16),
),
Text(
e.description,
maxLines: 3,
overflow: TextOverflow.ellipsis,
).padding(horizontal: 16),
const Gap(8),
Text(
e.url,
style: GoogleFonts.roboto(fontSize: 11, height: 0.9),
maxLines: 1,
overflow: TextOverflow.ellipsis,
).opacity(0.75).padding(horizontal: 16),
const Gap(4),
Text(
'poweredBy'.tr(args: ['HyperNet.Reader']),
style: GoogleFonts.roboto(fontSize: 11, height: 0.9),
).opacity(0.75).padding(horizontal: 16),
const Gap(16),
],
),
),
onTap: () {
launchUrlString(e.url, mode: LaunchMode.externalApplication);
},
),
),
)
.toList(),
);
}
}

View File

@ -132,6 +132,7 @@ class PostItem extends StatelessWidget {
_PostContentHeader(
data: data,
isAuthor: isAuthor,
isRelativeDate: !showFullPost,
onShare: () => _doShare(context),
onShareImage: () => _doShareViaPicture(context),
onDeleted: () {
@ -204,6 +205,7 @@ class PostItem extends StatelessWidget {
children: [
_PostContentHeader(
isAuthor: isAuthor,
isRelativeDate: !showFullPost,
data: data,
showMenu: showMenu,
onShare: () => _doShare(context),
@ -219,6 +221,7 @@ class PostItem extends StatelessWidget {
).padding(horizontal: 16, bottom: 8),
_PostContentBody(
data: data,
isSelectable: showFullPost,
isEnlarge: data.type == 'article' && showFullPost,
).padding(horizontal: 16, bottom: 6),
if (data.repostTo != null)
@ -850,16 +853,19 @@ class _PostContentHeader extends StatelessWidget {
class _PostContentBody extends StatelessWidget {
final SnPost data;
final bool isEnlarge;
final bool isSelectable;
const _PostContentBody({
required this.data,
this.isEnlarge = false,
this.isSelectable = false,
});
@override
Widget build(BuildContext context) {
if (data.body['content'] == null) return const SizedBox.shrink();
return MarkdownTextContent(
isSelectable: isSelectable,
textScaler: isEnlarge ? TextScaler.linear(1.1) : null,
content: data.body['content'],
attachments: data.preload?.attachments,