👽 Support server-side truncate content

This commit is contained in:
LittleSheep 2024-10-13 20:36:10 +08:00
parent 1fc84099fe
commit 1809f2557d
4 changed files with 21 additions and 88 deletions

View File

@ -65,7 +65,6 @@ class _PostDetailScreenState extends State<PostDetailScreen> {
isClickable: false, isClickable: false,
isOverrideEmbedClickable: true, isOverrideEmbedClickable: true,
isFullDate: true, isFullDate: true,
isFullContent: true,
isShowReply: false, isShowReply: false,
isContentSelectable: true, isContentSelectable: true,
padding: AppTheme.isLargeScreen(context) padding: AppTheme.isLargeScreen(context)

View File

@ -31,13 +31,11 @@ class PostItem extends StatefulWidget {
final bool isShowEmbed; final bool isShowEmbed;
final bool isOverrideEmbedClickable; final bool isOverrideEmbedClickable;
final bool isFullDate; final bool isFullDate;
final bool isFullContent;
final bool isContentSelectable; final bool isContentSelectable;
final bool showFeaturedReply; final bool showFeaturedReply;
final String? attachmentParent; final String? attachmentParent;
final EdgeInsets? padding; final EdgeInsets? padding;
final Color? backgroundColor;
final Function? onComment; final Function? onComment;
@ -51,12 +49,10 @@ class PostItem extends StatefulWidget {
this.isShowEmbed = true, this.isShowEmbed = true,
this.isOverrideEmbedClickable = false, this.isOverrideEmbedClickable = false,
this.isFullDate = false, this.isFullDate = false,
this.isFullContent = false,
this.isContentSelectable = false, this.isContentSelectable = false,
this.showFeaturedReply = false, this.showFeaturedReply = false,
this.attachmentParent, this.attachmentParent,
this.padding, this.padding,
this.backgroundColor,
this.onComment, this.onComment,
}); });
@ -76,8 +72,6 @@ class _PostItemState extends State<PostItem> {
super.initState(); super.initState();
} }
double _contentHeight = 0;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final List<String> attachments = item.body['attachments'] is List final List<String> attachments = item.body['attachments'] is List
@ -95,18 +89,12 @@ class _PostItemState extends State<PostItem> {
).paddingOnly(bottom: 8), ).paddingOnly(bottom: 8),
_PostHeaderWidget( _PostHeaderWidget(
isCompact: widget.isCompact, isCompact: widget.isCompact,
isFullDate: widget.isFullDate,
item: item, item: item,
).paddingSymmetric(horizontal: 12), ).paddingSymmetric(horizontal: 12),
_PostHeaderDividerWidget(item: item).paddingSymmetric(horizontal: 12), _PostHeaderDividerWidget(item: item).paddingSymmetric(horizontal: 12),
SizedContainer( SizedContainer(
maxWidth: 640, maxWidth: 640,
maxHeight: widget.isFullContent ? double.infinity : 80,
child: _MeasureSize(
onChange: (size) {
setState(() => _contentHeight = size.height);
},
child: SingleChildScrollView(
physics: const NeverScrollableScrollPhysics(),
child: MarkdownTextContent( child: MarkdownTextContent(
parentId: 'p${item.id}', parentId: 'p${item.id}',
content: item.body['content'], content: item.body['content'],
@ -118,9 +106,7 @@ class _PostItemState extends State<PostItem> {
right: 12, right: 12,
bottom: hasAttachment ? 4 : 0, bottom: hasAttachment ? 4 : 0,
), ),
), if (widget.item.body?['content_truncated'] == true)
),
if (_contentHeight >= 80 && !widget.isFullContent)
Opacity( Opacity(
opacity: 0.8, opacity: 0.8,
child: InkWell(child: Text('readMore'.tr)), child: InkWell(child: Text('readMore'.tr)),
@ -165,30 +151,20 @@ class _PostItemState extends State<PostItem> {
children: [ children: [
_PostHeaderWidget( _PostHeaderWidget(
isCompact: widget.isCompact, isCompact: widget.isCompact,
isFullDate: widget.isFullDate,
item: item, item: item,
), ),
_PostHeaderDividerWidget(item: item), _PostHeaderDividerWidget(item: item),
SizedContainer( SizedContainer(
maxWidth: 640, maxWidth: 640,
maxHeight: widget.isFullContent ? double.infinity : 320,
child: _MeasureSize(
onChange: (size) {
setState(() => _contentHeight = size.height);
},
child: SingleChildScrollView(
physics: const NeverScrollableScrollPhysics(),
child: MarkdownTextContent( child: MarkdownTextContent(
parentId: 'p${item.id}-embed', parentId: 'p${item.id}-embed',
content: item.body['content'], content: item.body['content'],
isAutoWarp: item.type == 'story', isAutoWarp: item.type == 'story',
isSelectable: widget.isContentSelectable, isSelectable: widget.isContentSelectable,
isLargeText:
item.type == 'article' && widget.isFullContent,
), ),
), ),
), if (widget.item.body?['content_truncated'] == true)
),
if (_contentHeight >= 320 && !widget.isFullContent)
Opacity( Opacity(
opacity: 0.8, opacity: 0.8,
child: InkWell(child: Text('readMore'.tr)), child: InkWell(child: Text('readMore'.tr)),
@ -593,10 +569,12 @@ class _PostFooterWidget extends StatelessWidget {
class _PostHeaderWidget extends StatelessWidget { class _PostHeaderWidget extends StatelessWidget {
final bool isCompact; final bool isCompact;
final bool isFullDate;
final Post item; final Post item;
const _PostHeaderWidget({ const _PostHeaderWidget({
required this.isCompact, required this.isCompact,
required this.isFullDate,
required this.item, required this.item,
}); });
@ -629,6 +607,7 @@ class _PostHeaderWidget extends StatelessWidget {
if (isCompact) if (isCompact)
RelativeDate( RelativeDate(
item.publishedAt?.toLocal() ?? DateTime.now(), item.publishedAt?.toLocal() ?? DateTime.now(),
isFull: isFullDate,
).paddingOnly(top: 1), ).paddingOnly(top: 1),
], ],
), ),
@ -684,45 +663,3 @@ class _PostThumbnail extends StatelessWidget {
); );
} }
} }
typedef _OnWidgetSizeChange = void Function(Size size);
class _MeasureSizeRenderObject extends RenderProxyBox {
Size? oldSize;
_OnWidgetSizeChange onChange;
_MeasureSizeRenderObject(this.onChange);
@override
void performLayout() {
super.performLayout();
Size newSize = child!.size;
if (oldSize == newSize) return;
oldSize = newSize;
WidgetsBinding.instance.addPostFrameCallback((_) {
onChange(newSize);
});
}
}
class _MeasureSize extends SingleChildRenderObjectWidget {
final _OnWidgetSizeChange onChange;
const _MeasureSize({
required this.onChange,
required Widget super.child,
});
@override
RenderObject createRenderObject(BuildContext context) {
return _MeasureSizeRenderObject(onChange);
}
@override
void updateRenderObject(
BuildContext context, covariant _MeasureSizeRenderObject renderObject) {
renderObject.onChange = onChange;
}
}

View File

@ -86,7 +86,6 @@ class PostListEntryWidget extends StatelessWidget {
isClickable: isNestedClickable, isClickable: isNestedClickable,
showFeaturedReply: showFeaturedReply, showFeaturedReply: showFeaturedReply,
padding: padding, padding: padding,
backgroundColor: backgroundColor,
onComment: () { onComment: () {
AppRouter.instance AppRouter.instance
.pushNamed( .pushNamed(

View File

@ -31,8 +31,6 @@ class PostOwnedListEntry extends StatelessWidget {
isClickable: false, isClickable: false,
isShowReply: false, isShowReply: false,
isReactable: false, isReactable: false,
isFullContent: isFullContent,
backgroundColor: backgroundColor,
).paddingSymmetric(vertical: 8), ).paddingSymmetric(vertical: 8),
], ],
), ),