More conditions for show/hide captions

- Remove unused code
- Refactor and memoize the long/short calculation too
This commit is contained in:
Lim Chee Aun 2023-10-02 18:58:42 +08:00
parent f05267b216
commit 7e993704cc
3 changed files with 19 additions and 29 deletions

View file

@ -1,4 +1,5 @@
import { getBlurHashAverageColor } from 'fast-blurhash'; import { getBlurHashAverageColor } from 'fast-blurhash';
import mem from 'mem';
import { Fragment } from 'preact'; import { Fragment } from 'preact';
import { import {
useCallback, useCallback,
@ -53,6 +54,12 @@ const AltBadge = (props) => {
}; };
const MEDIA_CAPTION_LIMIT = 140; const MEDIA_CAPTION_LIMIT = 140;
export const isMediaCaptionLong = mem((caption) =>
caption?.length
? caption.length > MEDIA_CAPTION_LIMIT ||
/[\n\r].*[\n\r]/.test(caption.trim())
: false,
);
function Media({ function Media({
media, media,
@ -172,15 +179,9 @@ function Media({
aspectRatio: `${width} / ${height}`, aspectRatio: `${width} / ${height}`,
}; };
const multilineDesc = const longDesc = isMediaCaptionLong(description);
!!description && description.trim().split('\n').length > 2;
const longDesc = description?.length > MEDIA_CAPTION_LIMIT || multilineDesc;
const showInlineDesc = const showInlineDesc =
!!showCaption && !!showCaption && !showOriginal && !!description && !longDesc;
!showOriginal &&
!!description &&
!longDesc &&
!multilineDesc;
const Figure = !showInlineDesc const Figure = !showInlineDesc
? Fragment ? Fragment
: (props) => { : (props) => {
@ -189,9 +190,7 @@ function Media({
<figure {...restProps}> <figure {...restProps}>
{children} {children}
<figcaption <figcaption
class={`media-caption media-caption-${ class="media-caption"
longDesc ? 'long' : 'short'
}`}
lang={lang} lang={lang}
dir="auto" dir="auto"
onClick={(e) => { onClick={(e) => {
@ -202,11 +201,6 @@ function Media({
lang, lang,
}; };
}} }}
title={
description.length > MEDIA_CAPTION_LIMIT
? description
: undefined
}
> >
{description} {description}
</figcaption> </figcaption>

View file

@ -725,17 +725,6 @@
white-space: pre-line; white-space: pre-line;
flex-basis: 15em; flex-basis: 15em;
flex-grow: 1; flex-grow: 1;
&.media-caption-long {
overflow: hidden;
white-space: normal;
display: -webkit-box;
display: box;
-webkit-box-orient: vertical;
box-orient: vertical;
-webkit-line-clamp: 3;
line-clamp: 3;
}
} }
} }

View file

@ -55,6 +55,7 @@ import Avatar from './avatar';
import Icon from './icon'; import Icon from './icon';
import Link from './link'; import Link from './link';
import Media from './media'; import Media from './media';
import { isMediaCaptionLong } from './media';
import MenuLink from './menu-link'; import MenuLink from './menu-link';
import RelativeTime from './relative-time'; import RelativeTime from './relative-time';
import TranslationBlock from './translation-block'; import TranslationBlock from './translation-block';
@ -1269,12 +1270,17 @@ function Status({
lang={language} lang={language}
enabled={ enabled={
mediaAttachments.length > 1 && mediaAttachments.length > 1 &&
mediaAttachments.some((media) => !!media.description) mediaAttachments.some(
(media) =>
!!media.description &&
!isMediaCaptionLong(media.description),
)
} }
captionChildren={() => { captionChildren={() => {
return mediaAttachments.map( return mediaAttachments.map(
(media, i) => (media, i) =>
!!media.description && ( !!media.description &&
!isMediaCaptionLong(media.description) && (
<div <div
key={media.id} key={media.id}
onClick={(e) => { onClick={(e) => {
@ -1315,6 +1321,7 @@ function Status({
altIndex={ altIndex={
mediaAttachments.length > 1 && mediaAttachments.length > 1 &&
!!media.description && !!media.description &&
!isMediaCaptionLong(media.description) &&
i + 1 i + 1
} }
to={`/${instance}/s/${id}?${ to={`/${instance}/s/${id}?${