add media on mobile

This commit is contained in:
plebeius.eth
2024-03-29 20:43:58 +01:00
parent b888b9bf68
commit a8adb2a80e
5 changed files with 47 additions and 58 deletions
+18 -11
View File
@@ -1,23 +1,29 @@
.thumbnail {
.thumbnailBig {
width: var(--width);
height: var(--height);
float: left;
padding: 3px 20px 5px 0;
width: var(--width, 250px);
height: var(--height, 250px);
}
.thumbnailHidden {
display: none;
.thumbnailSmall {
width: var(--width);
height: var(--height);
float: left;
padding: 3px 10px 5px 5px;
}
.thumbnailVisible {
display: block;
}
.thumbnailWrapper {
background-color: var(--background-thumbnail);
.thumbnailWrapperBig {
background-color: var(--post-thumbnail-background-color);
display: inline-block;
height: 250px;
width: 250px;
}
.thumbnailWrapperSmall {
background-color: var(--post-thumbnail-background-color);
display: inline-block;
height: 150px;
width: 150px;
}
.transparentThumbnailWrapper {
@@ -26,6 +32,7 @@
}
.thumbnail img, .thumbnail video {
object-fit: contain;
width: 100%;
height: 100%;
}
+12 -36
View File
@@ -1,48 +1,34 @@
import styles from './media.module.css';
import { Link } from 'react-router-dom';
import { CommentMediaInfo } from '../../lib/utils/media-utils';
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
interface ThumbnailProps {
cid?: string;
commentMediaInfo?: CommentMediaInfo;
expanded?: boolean;
isMobile: boolean;
isReply: boolean;
link: string;
linkHeight?: number;
linkWidth?: number;
subplebbitAddress?: string;
toggleExpanded?: () => void;
}
export const Thumbnail = ({
cid,
commentMediaInfo,
expanded = false,
isReply = false,
link,
linkHeight,
linkWidth,
subplebbitAddress,
toggleExpanded,
}: ThumbnailProps) => {
export const Thumbnail = ({ commentMediaInfo, isMobile, isReply, linkHeight, linkWidth }: ThumbnailProps) => {
const iframeThumbnail = commentMediaInfo?.patternThumbnailUrl || commentMediaInfo?.thumbnail;
let displayWidth, displayHeight, hasLinkDimensions;
const routeOrLink = isReply ? link : `/p/${subplebbitAddress}/c/${cid}`;
const thumbnailClass = expanded ? styles.thumbnailHidden : styles.thumbnailVisible;
const maxThumbnailSize = isMobile || isReply ? 125 : 250;
if (linkWidth && linkHeight) {
let scale = Math.min(1, 250 / Math.max(linkWidth, linkHeight));
hasLinkDimensions = true;
let scale = Math.min(1, maxThumbnailSize / Math.max(linkWidth, linkHeight));
displayWidth = `${linkWidth * scale}px`;
displayHeight = `${linkHeight * scale}px`;
hasLinkDimensions = true;
} else {
displayWidth = '250px';
displayHeight = '250px';
hasLinkDimensions = false;
displayWidth = `${maxThumbnailSize}px`;
displayHeight = `${maxThumbnailSize}px`;
}
const style = hasLinkDimensions ? ({ '--width': displayWidth, '--height': displayHeight } as React.CSSProperties) : {};
const thumbnailStyle = hasLinkDimensions ? ({ '--width': displayWidth, '--height': displayHeight } as React.CSSProperties) : {};
const thumbnailWrapperClass = hasLinkDimensions ? styles.transparentThumbnailWrapper : isMobile ? styles.thumbnailWrapperSmall : styles.thumbnailWrapperBig;
let mediaComponent = null;
const gifFrameUrl = useFetchGifFirstFrame(commentMediaInfo?.type === 'gif' ? commentMediaInfo.url : undefined);
@@ -60,19 +46,9 @@ export const Thumbnail = ({
}
return (
<span className={`${styles.thumbnail} ${thumbnailClass}`} style={style}>
<span className={hasLinkDimensions ? styles.transparentThumbnailWrapper : styles.thumbnailWrapper}>
<Link
to={routeOrLink}
onClick={(e) => {
if (e.button === 0 && isReply) {
e.preventDefault();
toggleExpanded && toggleExpanded();
}
}}
>
{mediaComponent}
</Link>
<span className={styles.thumbnail} style={thumbnailStyle}>
<span className={isMobile || isReply ? styles.thumbnailSmall : styles.thumbnailBig}>
<span className={thumbnailWrapperClass}>{mediaComponent}</span>
</span>
</span>
);
+10 -10
View File
@@ -40,15 +40,7 @@ const PostDesktop = ({ post }: Comment) => {
</div>
{hasThumbnail && (
<span className={styles.fileThumb}>
<Thumbnail
cid={cid}
commentMediaInfo={commentMediaInfo}
isReply={false}
link={link}
linkHeight={linkHeight}
linkWidth={linkWidth}
subplebbitAddress={subplebbitAddress}
/>
<Thumbnail commentMediaInfo={commentMediaInfo} isMobile={false} isReply={false} linkHeight={linkHeight} linkWidth={linkWidth} />
</span>
)}
</div>
@@ -101,12 +93,15 @@ const PostDesktop = ({ post }: Comment) => {
};
const PostMobile = ({ post }: Comment) => {
const { author, cid, content, replyCount, shortCid, subplebbitAddress, timestamp, title } = post || {};
const { author, cid, content, link, linkHeight, linkWidth, replyCount, shortCid, subplebbitAddress, timestamp, title } = post || {};
const { address, displayName, shortAddress } = author || {};
const linkCount = countLinksInCommentReplies(post);
const displayTitle = title && title.length > 30 ? title.slice(0, 30) + '(...)' : title;
const displayContent = content && content.length > 1000 ? content.slice(0, 1000) : content;
const commentMediaInfo = getCommentMediaInfoMemoized(post);
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
return (
<div className={styles.postMobile}>
<div className={styles.hrWrapper}>
@@ -134,6 +129,11 @@ const PostMobile = ({ post }: Comment) => {
<span className={styles.replyToPost}>{shortCid}</span>
</span>
</div>
{hasThumbnail && (
<span className={styles.fileThumb}>
<Thumbnail commentMediaInfo={commentMediaInfo} isMobile={true} isReply={false} linkHeight={linkHeight} linkWidth={linkWidth} />
</span>
)}
{content && (
<blockquote className={`${styles.postMessage} ${styles.clampLines}`}>
<Markdown content={displayContent} />