Article attachments

This commit is contained in:
LittleSheep 2025-07-03 01:18:07 +08:00
parent 23321171f3
commit 594ac39e3d
3 changed files with 38 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@ -6,11 +7,14 @@ import 'package:flutter_highlight/themes/a11y-dark.dart';
import 'package:flutter_highlight/themes/a11y-light.dart'; import 'package:flutter_highlight/themes/a11y-light.dart';
import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:island/models/file.dart';
import 'package:island/pods/config.dart'; import 'package:island/pods/config.dart';
import 'package:island/widgets/alert.dart'; import 'package:island/widgets/alert.dart';
import 'package:island/widgets/content/cloud_files.dart';
import 'package:island/widgets/content/markdown_latex.dart'; import 'package:island/widgets/content/markdown_latex.dart';
import 'package:markdown/markdown.dart' as markdown; import 'package:markdown/markdown.dart' as markdown;
import 'package:markdown_widget/markdown_widget.dart'; import 'package:markdown_widget/markdown_widget.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'image.dart'; import 'image.dart';
@ -23,6 +27,7 @@ class MarkdownTextContent extends HookConsumerWidget {
final TextStyle? linkStyle; final TextStyle? linkStyle;
final EdgeInsets? linesMargin; final EdgeInsets? linesMargin;
final bool isSelectable; final bool isSelectable;
final List<SnCloudFile>? attachments;
const MarkdownTextContent({ const MarkdownTextContent({
super.key, super.key,
@ -33,6 +38,7 @@ class MarkdownTextContent extends HookConsumerWidget {
this.linkStyle, this.linkStyle,
this.isSelectable = false, this.isSelectable = false,
this.linesMargin, this.linesMargin,
this.attachments,
}); });
@override @override
@ -109,6 +115,29 @@ class MarkdownTextContent extends HookConsumerWidget {
final uri = Uri.parse(url); final uri = Uri.parse(url);
if (uri.scheme == 'solian') { if (uri.scheme == 'solian') {
switch (uri.host) { switch (uri.host) {
case 'files':
final file = attachments?.firstWhereOrNull(
(file) => file.id == uri.pathSegments[0],
);
if (file == null) {
return const SizedBox.shrink();
}
return ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainer,
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
),
child: CloudFileWidget(
item: file,
fit: BoxFit.cover,
).clipRRect(all: 8),
),
);
case 'stickers': case 'stickers':
final size = doesEnlargeSticker ? 96.0 : 24.0; final size = doesEnlargeSticker ? 96.0 : 24.0;
return ClipRRect( return ClipRRect(
@ -132,9 +161,9 @@ class MarkdownTextContent extends HookConsumerWidget {
); );
} }
} }
final content = UniversalImage( final content = ConstrainedBox(
uri: uri.toString(), constraints: BoxConstraints(maxHeight: 360),
fit: BoxFit.cover, child: UniversalImage(uri: uri.toString(), fit: BoxFit.contain),
); );
return content; return content;
}, },

View File

@ -480,8 +480,7 @@ class ComposeLogic {
return; return;
} }
final cloudFile = attachment.data as SnCloudFile; final cloudFile = attachment.data as SnCloudFile;
final baseUrl = ref.read(serverUrlProvider); final markdown = '![${cloudFile.name}](solian://files/${cloudFile.id})';
final markdown = '![${cloudFile.name}]($baseUrl/files/${cloudFile.id})';
final controller = state.contentController; final controller = state.contentController;
final text = controller.text; final text = controller.text;
final selection = controller.selection; final selection = controller.selection;

View File

@ -331,6 +331,7 @@ class PostItem extends HookConsumerWidget {
item.type == 0 item.type == 0
? EdgeInsets.only(bottom: 8) ? EdgeInsets.only(bottom: 8)
: null, : null,
attachments: item.attachments,
), ),
], ],
// Render tags and categories if they exist // Render tags and categories if they exist
@ -689,6 +690,7 @@ Widget _buildReferencePost(BuildContext context, SnPost item) {
referencePost.type == 0 referencePost.type == 0
? EdgeInsets.only(bottom: 4) ? EdgeInsets.only(bottom: 4)
: null, : null,
attachments: item.attachments,
).padding(bottom: 4), ).padding(bottom: 4),
// Truncation hint for referenced post // Truncation hint for referenced post
if (referencePost.isTruncated) if (referencePost.isTruncated)
@ -696,7 +698,8 @@ Widget _buildReferencePost(BuildContext context, SnPost item) {
isCompact: true, isCompact: true,
margin: const EdgeInsets.only(top: 4, bottom: 8), margin: const EdgeInsets.only(top: 4, bottom: 8),
), ),
if (referencePost.attachments.isNotEmpty && referencePost.type != 1) if (referencePost.attachments.isNotEmpty &&
referencePost.type != 1)
Row( Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
@ -1030,6 +1033,7 @@ class _ArticlePostDisplay extends StatelessWidget {
MarkdownTextContent( MarkdownTextContent(
content: item.content!, content: item.content!,
textStyle: Theme.of(context).textTheme.bodyLarge, textStyle: Theme.of(context).textTheme.bodyLarge,
attachments: item.attachments,
), ),
], ],
); );