Surface/lib/screens/post/post_editor.dart

1230 lines
44 KiB
Dart
Raw Normal View History

2025-02-06 15:04:04 +08:00
import 'dart:io';
import 'package:collection/collection.dart';
2024-11-10 12:41:56 +08:00
import 'package:easy_localization/easy_localization.dart';
2025-02-06 15:04:04 +08:00
import 'package:flutter/foundation.dart';
2024-11-10 12:41:56 +08:00
import 'package:flutter/gestures.dart';
2024-11-10 01:34:58 +08:00
import 'package:flutter/material.dart';
2025-02-06 15:04:04 +08:00
import 'package:flutter/services.dart';
import 'package:flutter_context_menu/flutter_context_menu.dart';
2024-11-10 12:41:56 +08:00
import 'package:gap/gap.dart';
2025-02-15 00:36:55 +08:00
import 'package:go_router/go_router.dart';
2025-02-06 15:04:04 +08:00
import 'package:hotkey_manager/hotkey_manager.dart';
2024-11-10 12:41:56 +08:00
import 'package:material_symbols_icons/symbols.dart';
2025-02-06 15:04:04 +08:00
import 'package:pasteboard/pasteboard.dart';
import 'package:responsive_framework/responsive_framework.dart';
2024-11-10 12:41:56 +08:00
import 'package:styled_widget/styled_widget.dart';
import 'package:surface/controllers/post_write_controller.dart';
import 'package:surface/providers/config.dart';
import 'package:surface/providers/sn_attachment.dart';
2024-11-10 12:41:56 +08:00
import 'package:surface/providers/sn_network.dart';
import 'package:surface/types/attachment.dart';
2024-11-10 12:41:56 +08:00
import 'package:surface/types/post.dart';
2025-02-22 01:33:57 +08:00
import 'package:surface/types/realm.dart';
2024-11-10 12:41:56 +08:00
import 'package:surface/widgets/account/account_image.dart';
2025-02-12 23:56:45 +08:00
import 'package:surface/widgets/attachment/attachment_input.dart';
2025-02-10 00:44:52 +08:00
import 'package:surface/widgets/attachment/attachment_item.dart';
import 'package:surface/widgets/attachment/pending_attachment_alt.dart';
import 'package:surface/widgets/attachment/pending_attachment_boost.dart';
2024-11-10 18:37:34 +08:00
import 'package:surface/widgets/loading_indicator.dart';
import 'package:surface/widgets/markdown_content.dart';
import 'package:surface/widgets/navigation/app_scaffold.dart';
2024-11-10 16:41:11 +08:00
import 'package:surface/widgets/post/post_media_pending_list.dart';
2024-11-10 12:41:56 +08:00
import 'package:surface/widgets/post/post_meta_editor.dart';
2024-11-10 16:41:11 +08:00
import 'package:surface/widgets/dialog.dart';
import 'package:provider/provider.dart';
2025-02-13 22:35:53 +08:00
import 'package:surface/widgets/post/post_poll_editor.dart';
2025-02-10 00:44:52 +08:00
import 'package:uuid/uuid.dart';
2025-02-22 01:33:57 +08:00
import '../../providers/sn_realm.dart';
2025-01-31 22:52:21 +08:00
class PostEditorExtra {
2024-12-15 12:59:18 +08:00
final String? text;
final String? title;
final String? description;
final List<PostWriteMedia>? attachments;
2025-01-31 22:52:21 +08:00
const PostEditorExtra({
2024-12-15 12:59:18 +08:00
this.text,
this.title,
this.description,
this.attachments,
});
}
2024-11-10 12:41:56 +08:00
class PostEditorScreen extends StatefulWidget {
final String mode;
2024-11-10 18:37:34 +08:00
final int? postEditId;
final int? postReplyId;
final int? postRepostId;
2025-01-31 22:52:21 +08:00
final PostEditorExtra? extraProps;
2024-12-07 17:43:44 +08:00
2024-11-10 18:37:34 +08:00
const PostEditorScreen({
super.key,
required this.mode,
required this.postEditId,
required this.postReplyId,
required this.postRepostId,
2024-12-15 12:59:18 +08:00
this.extraProps,
2024-11-10 18:37:34 +08:00
});
2024-11-10 12:41:56 +08:00
@override
State<PostEditorScreen> createState() => _PostEditorScreenState();
}
class _PostEditorScreenState extends State<PostEditorScreen> {
late final PostWriteController _writeController = PostWriteController(
doLoadFromTemporary: widget.postEditId == null,
);
2024-11-10 12:41:56 +08:00
bool _isFetching = false;
2024-12-07 17:43:44 +08:00
bool get _isLoading => _isFetching || _writeController.isLoading;
2024-11-10 12:41:56 +08:00
List<SnPublisher>? _publishers;
2025-02-22 01:33:57 +08:00
List<SnRealm>? _realms;
2024-11-10 12:41:56 +08:00
2024-11-10 18:37:34 +08:00
Future<void> _fetchPublishers() async {
setState(() => _isFetching = true);
2024-11-10 16:52:24 +08:00
2024-11-10 12:41:56 +08:00
try {
final sn = context.read<SnNetworkProvider>();
final config = context.read<ConfigProvider>();
final resp = await sn.client.get('/cgi/co/publishers/me');
_publishers = List<SnPublisher>.from(
resp.data?.map((e) => SnPublisher.fromJson(e)) ?? [],
2024-11-10 18:37:34 +08:00
);
final beforeId = config.prefs.getInt('int_last_publisher_id');
2025-02-15 19:43:41 +08:00
_writeController
.setPublisher(_publishers?.where((ele) => ele.id == beforeId).firstOrNull ?? _publishers?.firstOrNull);
2024-11-10 12:41:56 +08:00
} catch (err) {
2024-11-10 16:52:24 +08:00
if (!mounted) return;
2024-11-10 12:41:56 +08:00
context.showErrorDialog(err);
} finally {
setState(() => _isFetching = false);
2024-11-10 12:41:56 +08:00
}
}
2025-02-22 01:33:57 +08:00
Future<void> _fetchRealms() async {
final rels = context.read<SnRealmProvider>();
try {
_realms = await rels.listAvailableRealms();
} catch (err) {
if (!mounted) return;
context.showErrorDialog(err);
}
}
2024-11-10 12:41:56 +08:00
void _updateMeta() {
showModalBottomSheet(
2024-11-10 12:41:56 +08:00
context: context,
builder: (context) => PostMetaEditor(controller: _writeController),
2024-11-10 12:41:56 +08:00
useRootNavigator: true,
);
2024-11-10 12:41:56 +08:00
}
2025-02-06 15:04:04 +08:00
final HotKey _pasteHotKey = HotKey(
key: PhysicalKeyboardKey.keyV,
modifiers: [(!kIsWeb && Platform.isMacOS) ? HotKeyModifier.meta : HotKeyModifier.control],
2025-02-06 15:04:04 +08:00
scope: HotKeyScope.inapp,
);
void _registerHotKey() {
if (kIsWeb || Platform.isAndroid || Platform.isIOS) return;
hotKeyManager.register(_pasteHotKey, keyDownHandler: (_) async {
final imageBytes = await Pasteboard.image;
if (imageBytes == null) return;
_writeController.addAttachments([
PostWriteMedia.fromBytes(
imageBytes,
'attachmentPastedImage'.tr(),
SnMediaType.image,
),
]);
setState(() {});
});
}
2025-02-07 22:35:04 +08:00
void _showPublisherPopup() {
showModalBottomSheet(
context: context,
builder: (context) => _PostPublisherPopup(
controller: _writeController,
publishers: _publishers,
onUpdate: () {
_fetchPublishers();
},
2025-02-07 22:35:04 +08:00
),
);
}
2025-02-22 01:33:57 +08:00
void _showRealmPopup() {
showModalBottomSheet(
context: context,
builder: (context) => _PostRealmPopup(
controller: _writeController,
realms: _realms,
onUpdate: () {
_fetchRealms();
},
),
);
}
2025-02-12 23:56:45 +08:00
void _showPollEditorDialog() async {
final poll = await showDialog<dynamic>(
context: context,
builder: (context) => PollEditorDialog(
poll: _writeController.poll,
),
);
if (poll == null) return;
if (!mounted) return;
if (poll == false) {
_writeController.setPoll(null);
} else {
_writeController.setPoll(poll);
}
}
void _showThumbnailEditorDialog() async {
final attachment = await showDialog<SnAttachment?>(
context: context,
builder: (context) => AttachmentInputDialog(
title: 'postThumbnail'.tr(),
pool: 'interactive',
mediaType: SnMediaType.image,
),
);
if (!context.mounted) return;
if (attachment == null) return;
_writeController.setThumbnail(attachment);
}
2024-11-10 12:41:56 +08:00
@override
void dispose() {
_writeController.dispose();
2025-02-15 19:43:41 +08:00
if (!kIsWeb && !(Platform.isAndroid || Platform.isIOS)) {
2025-02-15 00:36:55 +08:00
hotKeyManager.unregister(_pasteHotKey);
2025-02-15 19:43:41 +08:00
}
2024-11-10 12:41:56 +08:00
super.dispose();
}
@override
void initState() {
super.initState();
2025-02-06 15:04:04 +08:00
_registerHotKey();
if (!PostWriteController.kTitleMap.keys.contains(widget.mode)) {
2024-11-10 12:41:56 +08:00
context.showErrorDialog('Unknown post type');
Navigator.pop(context);
2024-12-07 17:43:44 +08:00
} else {
_writeController.setMode(widget.mode);
2024-11-10 12:41:56 +08:00
}
2025-02-22 01:33:57 +08:00
_fetchRealms();
2024-11-10 12:41:56 +08:00
_fetchPublishers();
_writeController.fetchRelatedPost(
context,
editing: widget.postEditId,
replying: widget.postReplyId,
reposting: widget.postRepostId,
);
2024-12-15 12:59:18 +08:00
if (widget.extraProps != null) {
_writeController.contentController.text = widget.extraProps!.text ?? '';
_writeController.titleController.text = widget.extraProps!.title ?? '';
2025-02-15 19:43:41 +08:00
_writeController.descriptionController.text = widget.extraProps!.description ?? '';
2024-12-15 12:59:18 +08:00
_writeController.addAttachments(widget.extraProps!.attachments ?? []);
}
2024-11-10 12:41:56 +08:00
}
2024-11-10 01:34:58 +08:00
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: _writeController,
builder: (context, _) {
return AppScaffold(
appBar: AppBar(
leading: BackButton(
onPressed: () {
Navigator.pop(context);
},
),
title: RichText(
textAlign: TextAlign.center,
text: TextSpan(children: [
TextSpan(
2025-02-15 19:43:41 +08:00
text: _writeController.title.isNotEmpty ? _writeController.title : 'untitled'.tr(),
style: Theme.of(context).textTheme.titleLarge!.copyWith(
2024-12-15 12:59:18 +08:00
color: Theme.of(context).appBarTheme.foregroundColor!,
),
),
const TextSpan(text: '\n'),
TextSpan(
2024-11-27 00:06:11 +08:00
text: PostWriteController.kTitleMap[widget.mode]!.tr(),
style: Theme.of(context).textTheme.bodySmall!.copyWith(
2024-12-15 12:59:18 +08:00
color: Theme.of(context).appBarTheme.foregroundColor!,
),
),
]),
2025-02-04 22:04:50 +08:00
maxLines: 2,
),
actions: [
IconButton(
icon: const Icon(Symbols.tune),
onPressed: _writeController.isBusy ? null : _updateMeta,
),
2024-11-24 20:23:06 +08:00
const Gap(8),
],
2024-11-10 12:41:56 +08:00
),
body: Column(
children: [
2025-02-07 22:35:04 +08:00
if (_writeController.editingPost != null)
Container(
2025-02-15 19:43:41 +08:00
padding: const EdgeInsets.only(top: 4, bottom: 4, left: 20, right: 20),
2025-02-07 22:35:04 +08:00
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Theme.of(context).dividerColor,
width: 1 / MediaQuery.of(context).devicePixelRatio,
),
),
),
2025-02-07 22:35:04 +08:00
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Icons.edit, size: 16),
const Gap(10),
2025-02-15 19:43:41 +08:00
Text('postEditingNotice').tr(args: ['@${_writeController.editingPost!.publisher.name}']),
],
),
),
if (_writeController.replyingPost != null)
Container(
padding: const EdgeInsets.only(top: 4, bottom: 4, left: 20, right: 20),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Theme.of(context).dividerColor,
width: 1 / MediaQuery.of(context).devicePixelRatio,
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Symbols.reply, size: 16),
const Gap(10),
Text('@${_writeController.replyingPost!.publisher.name}').bold(),
const Gap(4),
Expanded(
child: Text(
_writeController.replyingPost!.body['content'],
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
),
),
if (_writeController.repostingPost != null)
Container(
padding: const EdgeInsets.only(top: 4, bottom: 4, left: 20, right: 20),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Theme.of(context).dividerColor,
width: 1 / MediaQuery.of(context).devicePixelRatio,
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Symbols.forward, size: 16),
const Gap(10),
Text('@${_writeController.repostingPost!.publisher.name}').bold(),
const Gap(4),
Expanded(
child: Text(
_writeController.repostingPost!.body['content'],
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
2025-02-07 22:35:04 +08:00
],
),
2024-11-10 12:41:56 +08:00
),
Expanded(
2025-02-04 22:04:50 +08:00
child: Stack(
children: [
SingleChildScrollView(
padding: EdgeInsets.only(bottom: 160),
2025-02-12 23:56:45 +08:00
child: StyledWidget(switch (_writeController.mode) {
2025-02-07 22:35:04 +08:00
'stories' => _PostStoryEditor(
controller: _writeController,
onTapPublisher: _showPublisherPopup,
2025-02-22 01:33:57 +08:00
onTapRealm: _showRealmPopup,
2025-02-07 22:35:04 +08:00
),
'articles' => _PostArticleEditor(
controller: _writeController,
onTapPublisher: _showPublisherPopup,
2025-02-22 13:13:38 +08:00
onTapRealm: _showRealmPopup,
2025-02-07 22:35:04 +08:00
),
'questions' => _PostQuestionEditor(
controller: _writeController,
onTapPublisher: _showPublisherPopup,
2025-02-22 13:13:38 +08:00
onTapRealm: _showRealmPopup,
),
2025-02-10 00:44:52 +08:00
'videos' => _PostVideoEditor(
controller: _writeController,
onTapPublisher: _showPublisherPopup,
2025-02-22 13:13:38 +08:00
onTapRealm: _showRealmPopup,
2025-02-10 00:44:52 +08:00
),
2025-02-07 22:35:04 +08:00
_ => const Placeholder(),
2025-02-12 23:56:45 +08:00
})
.padding(top: 8),
2025-02-04 22:04:50 +08:00
),
2025-02-15 19:43:41 +08:00
if (_writeController.attachments.isNotEmpty || _writeController.thumbnail != null)
2025-02-04 22:04:50 +08:00
Positioned(
bottom: 0,
left: 0,
right: 0,
child: PostMediaPendingList(
attachments: _writeController.attachments,
isBusy: _writeController.isBusy,
onUpload: (int idx) async {
2025-02-15 19:43:41 +08:00
await _writeController.uploadSingleAttachment(context, idx);
2025-02-04 22:04:50 +08:00
},
onInsertLink: (int idx) async {
_writeController.contentController.text +=
'\n![](solink://attachments/${_writeController.attachments[idx].attachment!.rid})';
},
2025-02-15 19:43:41 +08:00
onUpdate: (int idx, PostWriteMedia updatedMedia) async {
2025-02-04 22:04:50 +08:00
_writeController.setIsBusy(true);
try {
2025-02-15 19:43:41 +08:00
_writeController.setAttachmentAt(idx, updatedMedia);
2025-02-04 22:04:50 +08:00
} finally {
_writeController.setIsBusy(false);
}
},
onRemove: (int idx) async {
_writeController.setIsBusy(true);
try {
_writeController.removeAttachmentAt(idx);
} finally {
_writeController.setIsBusy(false);
}
},
2025-02-15 19:43:41 +08:00
onUpdateBusy: (state) => _writeController.setIsBusy(state),
2025-02-04 22:04:50 +08:00
).padding(bottom: 8),
),
],
),
2024-11-10 12:41:56 +08:00
),
Material(
elevation: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2024-11-10 12:41:56 +08:00
children: [
2025-02-15 19:43:41 +08:00
if (_writeController.isBusy && _writeController.progress != null)
TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: _writeController.progress),
duration: Duration(milliseconds: 300),
2025-02-15 19:43:41 +08:00
builder: (context, value, _) => LinearProgressIndicator(value: value, minHeight: 2),
)
else if (_writeController.isBusy)
const LinearProgressIndicator(value: null, minHeight: 2),
2025-02-12 23:56:45 +08:00
LoadingIndicator(isActive: _isLoading),
const Gap(4),
2024-12-29 22:27:07 +08:00
Container(
child: _writeController.temporaryRestored
2024-12-29 23:11:50 +08:00
? Container(
2025-02-15 19:43:41 +08:00
padding: const EdgeInsets.only(top: 4, bottom: 4, left: 28, right: 22),
2024-12-29 23:11:50 +08:00
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Theme.of(context).dividerColor,
2025-02-15 19:43:41 +08:00
width: 1 / MediaQuery.of(context).devicePixelRatio,
2024-12-29 23:11:50 +08:00
),
2024-12-29 22:27:07 +08:00
),
2024-12-29 23:11:50 +08:00
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Icons.restore, size: 20),
const Gap(8),
2025-02-15 19:43:41 +08:00
Expanded(child: Text('postLocalDraftRestored').tr()),
2024-12-29 23:11:50 +08:00
InkWell(
child: Text('dialogDismiss').tr(),
onTap: () {
_writeController.reset();
},
),
],
))
2024-12-29 22:27:07 +08:00
: const SizedBox.shrink(),
)
2025-02-15 19:43:41 +08:00
.height(_writeController.temporaryRestored ? 32 : 0, animate: true)
.animate(const Duration(milliseconds: 300), Curves.fastLinearToSlowEaseIn),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: ScrollConfiguration(
behavior: _PostEditorActionScrollBehavior(),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Row(
children: [
AddPostMediaButton(
onAdd: (items) {
setState(() {
_writeController.addAttachments(items);
});
},
),
2025-02-12 23:56:45 +08:00
if (_writeController.mode == 'stories')
IconButton(
2025-02-15 19:43:41 +08:00
icon: Icon(Symbols.poll, color: Theme.of(context).colorScheme.primary),
2025-02-12 23:56:45 +08:00
style: ButtonStyle(
2025-02-15 19:43:41 +08:00
backgroundColor: _writeController.poll == null
? null
: WidgetStatePropertyAll(Theme.of(context).colorScheme.surfaceContainer),
2025-02-12 23:56:45 +08:00
),
onPressed: () {
_showPollEditorDialog();
},
),
if (_writeController.mode == 'articles')
IconButton(
2025-02-22 13:13:38 +08:00
icon: Icon(Symbols.full_coverage, color: Theme.of(context).colorScheme.primary),
style: ButtonStyle(
backgroundColor: _writeController.thumbnail == null
? null
: WidgetStatePropertyAll(Theme.of(context).colorScheme.surfaceContainer),
),
onPressed: () {
if (_writeController.thumbnail != null) {
_writeController.setThumbnail(null);
return;
}
_showThumbnailEditorDialog();
},
),
],
2024-11-10 12:41:56 +08:00
),
),
2024-11-10 12:41:56 +08:00
),
),
TextButton.icon(
2025-02-15 19:43:41 +08:00
onPressed: (_writeController.isBusy || _writeController.publisher == null)
? null
2024-11-11 22:43:09 +08:00
: () {
2024-12-22 00:41:41 +08:00
_writeController.sendPost(context).then((_) {
2024-11-11 22:43:09 +08:00
if (!context.mounted) return;
Navigator.pop(context, true);
});
},
icon: const Icon(Symbols.send),
label: Text('postPublish').tr(),
),
],
).padding(horizontal: 16),
2024-11-10 12:41:56 +08:00
],
).padding(
2024-12-08 11:37:03 +08:00
bottom: MediaQuery.of(context).padding.bottom + 8,
),
),
],
2024-11-10 12:41:56 +08:00
),
);
},
2024-11-10 12:41:56 +08:00
);
2024-11-10 01:34:58 +08:00
}
}
2024-11-10 12:41:56 +08:00
class _PostEditorActionScrollBehavior extends MaterialScrollBehavior {
@override
Set<PointerDeviceKind> get dragDevices => {
PointerDeviceKind.touch,
PointerDeviceKind.mouse,
};
}
2025-02-07 22:35:04 +08:00
class _PostPublisherPopup extends StatelessWidget {
final PostWriteController controller;
final List<SnPublisher>? publishers;
final Function onUpdate;
2025-02-07 22:35:04 +08:00
const _PostPublisherPopup({required this.controller, this.publishers, required this.onUpdate});
2025-02-07 22:35:04 +08:00
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Symbols.face, size: 24),
const Gap(16),
2025-02-15 19:43:41 +08:00
Text('accountPublishers', style: Theme.of(context).textTheme.titleLarge).tr(),
2025-02-07 22:35:04 +08:00
],
).padding(horizontal: 20, top: 16, bottom: 12),
2025-02-15 00:36:55 +08:00
ListTile(
leading: const Icon(Symbols.add),
title: Text('publishersNew').tr(),
subtitle: Text('publisherNewSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
onTap: () {
GoRouter.of(context).pushNamed('accountPublisherNew').then((value) {
if (value == true) {
onUpdate();
}
});
2025-02-15 00:36:55 +08:00
},
),
const Divider(height: 1),
2025-02-07 22:35:04 +08:00
Expanded(
child: ListView.builder(
itemCount: publishers?.length ?? 0,
itemBuilder: (context, idx) {
final publisher = publishers![idx];
return ListTile(
title: Text(publisher.nick),
subtitle: Text('@${publisher.name}'),
leading: AccountImage(content: publisher.avatar, radius: 18),
onTap: () {
controller.setPublisher(publisher);
Navigator.pop(context, true);
},
);
},
),
),
],
);
}
}
2025-02-22 01:33:57 +08:00
class _PostRealmPopup extends StatelessWidget {
final PostWriteController controller;
final List<SnRealm>? realms;
final Function onUpdate;
const _PostRealmPopup({required this.controller, this.realms, required this.onUpdate});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Symbols.face, size: 24),
const Gap(16),
Text('accountRealms', style: Theme.of(context).textTheme.titleLarge).tr(),
],
).padding(horizontal: 20, top: 16, bottom: 12),
ListTile(
leading: const Icon(Symbols.close),
title: Text('postInGlobal').tr(),
subtitle: Text('postInGlobalDescription').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
onTap: () {
controller.setRealm(null);
Navigator.pop(context, true);
},
),
const Divider(height: 1),
Expanded(
child: ListView.builder(
itemCount: realms?.length ?? 0,
itemBuilder: (context, idx) {
final realm = realms![idx];
return ListTile(
title: Text(realm.name),
subtitle: Text('@${realm.alias}'),
leading: AccountImage(content: realm.avatar, radius: 18),
onTap: () {
controller.setRealm(realm);
Navigator.pop(context, true);
},
);
},
),
),
],
);
}
}
class _PostStoryEditor extends StatelessWidget {
final PostWriteController controller;
2025-02-07 22:35:04 +08:00
final Function? onTapPublisher;
2025-02-22 01:33:57 +08:00
final Function? onTapRealm;
2025-02-22 01:33:57 +08:00
const _PostStoryEditor({required this.controller, this.onTapPublisher, this.onTapRealm});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12),
constraints: const BoxConstraints(maxWidth: 640),
2025-02-07 22:35:04 +08:00
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2025-02-22 01:33:57 +08:00
Column(
children: [
Material(
elevation: 2,
borderRadius: const BorderRadius.all(Radius.circular(24)),
child: GestureDetector(
onTap: () {
onTapPublisher?.call();
},
child: AccountImage(
content: controller.publisher?.avatar,
),
),
2025-02-07 22:35:04 +08:00
),
2025-02-22 13:13:38 +08:00
const Gap(11),
2025-02-22 01:33:57 +08:00
Material(
elevation: 1,
borderRadius: const BorderRadius.all(Radius.circular(24)),
child: GestureDetector(
onTap: () {
onTapRealm?.call();
},
child: AccountImage(
content: controller.realm?.avatar,
fallbackWidget: const Icon(Symbols.globe, size: 20),
radius: 14,
),
),
),
],
),
2025-02-07 22:35:04 +08:00
Expanded(
child: Column(
children: [
const Gap(6),
TextField(
controller: controller.titleController,
decoration: InputDecoration.collapsed(
hintText: 'fieldPostTitle'.tr(),
border: InputBorder.none,
),
style: Theme.of(context).textTheme.titleLarge,
2025-02-15 19:43:41 +08:00
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
).padding(horizontal: 16),
const Gap(8),
TextField(
controller: controller.contentController,
maxLines: null,
decoration: InputDecoration(
hintText: 'fieldPostContent'.tr(),
hintStyle: TextStyle(fontSize: 14),
isCollapsed: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
),
border: InputBorder.none,
),
2025-02-15 19:43:41 +08:00
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
2025-02-18 00:43:12 +08:00
contentInsertionConfiguration: controller.contentInsertionConfiguration,
2025-02-07 22:35:04 +08:00
),
],
),
2025-02-07 22:35:04 +08:00
),
],
).padding(bottom: 8),
);
}
}
class _PostArticleEditor extends StatelessWidget {
final PostWriteController controller;
2025-02-07 22:35:04 +08:00
final Function? onTapPublisher;
2025-02-22 13:13:38 +08:00
final Function? onTapRealm;
2025-02-22 13:13:38 +08:00
const _PostArticleEditor({required this.controller, this.onTapPublisher, this.onTapRealm});
@override
Widget build(BuildContext context) {
2025-02-07 22:35:04 +08:00
final editorWidgets = <Widget>[
Material(
color: Theme.of(context).colorScheme.surfaceContainerHigh,
child: InkWell(
child: Row(
children: [
AccountImage(content: controller.publisher?.avatar, radius: 20),
const Gap(8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(controller.publisher?.nick ?? 'loading'.tr()).bold(),
Text('@${controller.publisher?.name}'),
],
),
),
2025-02-22 13:13:38 +08:00
Material(
elevation: 1,
borderRadius: const BorderRadius.all(Radius.circular(24)),
child: GestureDetector(
onTap: () {
onTapRealm?.call();
},
child: AccountImage(
content: controller.realm?.avatar,
fallbackWidget: const Icon(Symbols.globe, size: 20),
radius: 14,
),
),
),
const Gap(8),
2025-02-07 22:35:04 +08:00
],
).padding(horizontal: 12, vertical: 8),
onTap: () {
onTapPublisher?.call();
},
),
),
const Gap(16),
2025-02-07 22:35:04 +08:00
TextField(
controller: controller.titleController,
decoration: InputDecoration.collapsed(
hintText: 'fieldPostTitle'.tr(),
2025-02-07 22:35:04 +08:00
border: InputBorder.none,
),
style: Theme.of(context).textTheme.titleLarge,
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
).padding(horizontal: 16),
const Gap(8),
2025-02-07 22:35:04 +08:00
TextField(
controller: controller.descriptionController,
decoration: InputDecoration.collapsed(
hintText: 'fieldPostDescription'.tr(),
2025-02-07 22:35:04 +08:00
border: InputBorder.none,
),
maxLines: null,
keyboardType: TextInputType.multiline,
style: Theme.of(context).textTheme.bodyLarge,
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
2025-02-18 00:43:12 +08:00
contentInsertionConfiguration: controller.contentInsertionConfiguration,
2025-02-07 22:35:04 +08:00
).padding(horizontal: 16),
if (controller.thumbnail != null)
Container(
margin: const EdgeInsets.only(left: 12, right: 12, top: 8, bottom: 4),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Theme.of(context).dividerColor),
),
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: AspectRatio(
aspectRatio: 16 / 9,
child: AttachmentItem(
data: controller.thumbnail!.attachment!,
heroTag: "post-editor-thumbnail-preview",
),
),
),
),
2025-02-07 22:35:04 +08:00
];
if (ResponsiveBreakpoints.of(context).largerThan(MOBILE)) {
return Container(
constraints: const BoxConstraints(maxWidth: 640 * 2 + 8),
2025-02-07 22:35:04 +08:00
child: Column(
children: [
2025-02-07 22:35:04 +08:00
...editorWidgets,
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: TextField(
controller: controller.contentController,
maxLines: null,
decoration: InputDecoration(
hintText: 'fieldPostContent'.tr(),
hintStyle: TextStyle(fontSize: 14),
isCollapsed: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
),
border: InputBorder.none,
),
2025-02-15 19:43:41 +08:00
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
2025-02-18 00:43:12 +08:00
contentInsertionConfiguration: controller.contentInsertionConfiguration,
),
),
2025-02-07 22:35:04 +08:00
const Gap(8),
Expanded(
child: MarkdownTextContent(
content: controller.contentController.text,
).padding(horizontal: 24),
),
],
),
],
),
);
}
2025-02-07 22:35:04 +08:00
return Column(
children: [
...editorWidgets,
Container(
padding: const EdgeInsets.only(top: 8),
constraints: const BoxConstraints(maxWidth: 640),
child: TextField(
controller: controller.contentController,
maxLines: null,
decoration: InputDecoration(
hintText: 'fieldPostContent'.tr(),
hintStyle: TextStyle(fontSize: 14),
isCollapsed: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
),
border: InputBorder.none,
),
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
2025-02-18 00:43:12 +08:00
contentInsertionConfiguration: controller.contentInsertionConfiguration,
),
),
2025-02-07 22:35:04 +08:00
],
);
}
}
class _PostQuestionEditor extends StatelessWidget {
final PostWriteController controller;
final Function? onTapPublisher;
2025-02-22 13:13:38 +08:00
final Function? onTapRealm;
2025-02-22 13:13:38 +08:00
const _PostQuestionEditor({required this.controller, this.onTapPublisher, this.onTapRealm});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12),
constraints: const BoxConstraints(maxWidth: 640),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2025-02-22 13:13:38 +08:00
Column(
children: [
Material(
elevation: 2,
borderRadius: const BorderRadius.all(Radius.circular(24)),
child: GestureDetector(
onTap: () {
onTapPublisher?.call();
},
child: AccountImage(
content: controller.publisher?.avatar,
),
),
),
2025-02-22 13:13:38 +08:00
const Gap(11),
Material(
elevation: 1,
borderRadius: const BorderRadius.all(Radius.circular(24)),
child: GestureDetector(
onTap: () {
onTapRealm?.call();
},
child: AccountImage(
content: controller.realm?.avatar,
fallbackWidget: const Icon(Symbols.globe, size: 20),
radius: 14,
),
),
),
],
),
Expanded(
child: Column(
children: [
const Gap(6),
TextField(
controller: controller.titleController,
decoration: InputDecoration.collapsed(
hintText: 'fieldPostTitle'.tr(),
border: InputBorder.none,
),
style: Theme.of(context).textTheme.titleLarge,
2025-02-15 19:43:41 +08:00
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
).padding(horizontal: 16),
const Gap(8),
TextField(
controller: controller.rewardController,
decoration: InputDecoration(
hintText: 'fieldPostQuestionReward'.tr(),
suffixText: 'walletCurrencyShort'.tr(),
border: InputBorder.none,
isCollapsed: true,
),
2025-02-15 19:43:41 +08:00
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
).padding(horizontal: 16),
const Gap(8),
TextField(
controller: controller.contentController,
maxLines: null,
decoration: InputDecoration(
hintText: 'fieldPostContent'.tr(),
hintStyle: TextStyle(fontSize: 14),
isCollapsed: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
),
border: InputBorder.none,
),
2025-02-15 19:43:41 +08:00
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
2025-02-18 00:43:12 +08:00
contentInsertionConfiguration: controller.contentInsertionConfiguration,
),
],
),
),
],
).padding(top: 8),
);
}
}
2025-02-10 00:44:52 +08:00
class _PostVideoEditor extends StatelessWidget {
final PostWriteController controller;
final Function? onTapPublisher;
2025-02-22 13:13:38 +08:00
final Function? onTapRealm;
2025-02-10 00:44:52 +08:00
2025-02-22 13:13:38 +08:00
const _PostVideoEditor({required this.controller, this.onTapPublisher, this.onTapRealm});
2025-02-10 00:44:52 +08:00
void _selectVideo(BuildContext context) async {
final video = await showDialog<SnAttachment?>(
context: context,
builder: (context) => AttachmentInputDialog(
title: 'postVideoUpload'.tr(),
pool: 'interactive',
mediaType: SnMediaType.video,
),
);
if (!context.mounted) return;
if (video == null) return;
controller.setVideoAttachment(video);
}
void _setAlt(BuildContext context) async {
if (controller.videoAttachment == null) return;
final result = await showDialog<SnAttachment?>(
context: context,
2025-02-15 19:43:41 +08:00
builder: (context) => PendingAttachmentAltDialog(media: PostWriteMedia(controller.videoAttachment)),
);
if (result == null) return;
controller.setVideoAttachment(result);
}
Future<void> _createBoost(BuildContext context) async {
if (controller.videoAttachment == null) return;
final result = await showDialog<SnAttachmentBoost?>(
context: context,
2025-02-15 19:43:41 +08:00
builder: (context) => PendingAttachmentBoostDialog(media: PostWriteMedia(controller.videoAttachment)),
);
if (result == null) return;
final newAttach = controller.videoAttachment!.copyWith(
boosts: [...controller.videoAttachment!.boosts, result],
);
controller.setVideoAttachment(newAttach);
}
void _setThumbnail(BuildContext context) async {
if (controller.videoAttachment == null) return;
final thumbnail = await showDialog<SnAttachment?>(
context: context,
builder: (context) => AttachmentInputDialog(
title: 'attachmentSetThumbnail'.tr(),
pool: 'interactive',
analyzeNow: true,
),
);
if (thumbnail == null) return;
if (!context.mounted) return;
try {
final attach = context.read<SnAttachmentProvider>();
final newAttach = await attach.updateOne(
controller.videoAttachment!,
thumbnailId: thumbnail.id,
);
controller.setVideoAttachment(newAttach);
} catch (err) {
if (!context.mounted) return;
context.showErrorDialog(err);
}
}
Future<void> _deleteAttachment(BuildContext context) async {
if (controller.videoAttachment == null) return;
try {
final sn = context.read<SnNetworkProvider>();
2025-02-15 19:43:41 +08:00
await sn.client.delete('/cgi/uc/attachments/${controller.videoAttachment!.id}');
controller.setVideoAttachment(null);
} catch (err) {
if (!context.mounted) return;
context.showErrorDialog(err);
}
}
2025-02-10 00:44:52 +08:00
@override
Widget build(BuildContext context) {
return Column(
children: [
2025-02-22 13:13:38 +08:00
Column(
children: [
Material(
elevation: 2,
borderRadius: const BorderRadius.all(Radius.circular(24)),
child: GestureDetector(
onTap: () {
onTapPublisher?.call();
},
child: AccountImage(
content: controller.publisher?.avatar,
2025-02-10 00:44:52 +08:00
),
2025-02-22 13:13:38 +08:00
),
),
const Gap(11),
Material(
elevation: 1,
borderRadius: const BorderRadius.all(Radius.circular(24)),
child: GestureDetector(
onTap: () {
onTapRealm?.call();
},
child: AccountImage(
content: controller.realm?.avatar,
fallbackWidget: const Icon(Symbols.globe, size: 20),
radius: 14,
),
),
),
],
2025-02-10 00:44:52 +08:00
),
const Gap(16),
TextField(
controller: controller.titleController,
decoration: InputDecoration.collapsed(
hintText: 'fieldPostTitle'.tr(),
border: InputBorder.none,
),
style: Theme.of(context).textTheme.titleLarge,
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
).padding(horizontal: 16),
const Gap(8),
TextField(
controller: controller.descriptionController,
decoration: InputDecoration.collapsed(
hintText: 'fieldPostDescription'.tr(),
border: InputBorder.none,
),
maxLines: null,
keyboardType: TextInputType.multiline,
style: Theme.of(context).textTheme.bodyLarge,
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
).padding(horizontal: 16),
const Gap(12),
Container(
margin: const EdgeInsets.only(left: 16, right: 16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
border: Border.all(color: Theme.of(context).dividerColor),
),
child: ContextMenuRegion(
contextMenu: ContextMenu(
entries: [
MenuItem(
label: 'attachmentSetAlt'.tr(),
icon: Symbols.description,
onSelected: () {
_setAlt(context);
},
),
MenuItem(
label: 'attachmentBoost'.tr(),
icon: Symbols.bolt,
onSelected: () {
_createBoost(context);
},
),
MenuItem(
label: 'attachmentSetThumbnail'.tr(),
icon: Symbols.image,
onSelected: () {
_setThumbnail(context);
},
),
MenuItem(
label: 'attachmentCopyRandomId'.tr(),
icon: Symbols.content_copy,
onSelected: () {
2025-02-15 19:43:41 +08:00
Clipboard.setData(ClipboardData(text: controller.videoAttachment!.rid));
},
),
MenuItem(
label: 'delete'.tr(),
icon: Symbols.delete,
onSelected: () => _deleteAttachment(context),
),
MenuItem(
label: 'unlink'.tr(),
icon: Symbols.link_off,
onSelected: () {
controller.setVideoAttachment(null);
},
),
],
),
child: InkWell(
borderRadius: BorderRadius.circular(16),
2025-02-15 19:43:41 +08:00
onTap: controller.videoAttachment == null ? () => _selectVideo(context) : null,
child: AspectRatio(
aspectRatio: 16 / 9,
child: controller.videoAttachment == null
? Center(
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.add),
const Gap(4),
Text('postVideoUpload'.tr()),
],
),
)
: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: AttachmentItem(
data: controller.videoAttachment!,
heroTag: const Uuid().v4(),
),
2025-02-10 00:44:52 +08:00
),
),
2025-02-10 00:44:52 +08:00
),
),
),
],
);
}
}