From 0dbb5a7fe2fa6b786d01d08fbeca4f16747660fa Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sun, 24 Dec 2023 15:47:48 -0500 Subject: [PATCH] rework image scaling to work better on mobile --- .../messaging/MessageAttachment.tsx | 45 +++++++++++++------ src/components/messaging/MessageList.tsx | 1 + 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/components/messaging/MessageAttachment.tsx b/src/components/messaging/MessageAttachment.tsx index fd36021..55f99f3 100644 --- a/src/components/messaging/MessageAttachment.tsx +++ b/src/components/messaging/MessageAttachment.tsx @@ -2,29 +2,45 @@ import { APIAttachment } from "@spacebarchat/spacebar-api-types/v9"; import styled from "styled-components"; import { modalController } from "../../controllers/modals"; import useLogger from "../../hooks/useLogger"; -import { calculateImageRatio, calculateScaledDimensions } from "../../utils/Message"; import { getFileDetails, zoomFit } from "../../utils/Utils"; import Audio from "../media/Audio"; import File from "../media/File"; import Video from "../media/Video"; +const MAX_ATTACHMENT_HEIGHT = 350; + +function adjustDimensions(width: number, height: number): { adjustedWidth: number; adjustedHeight: number } { + const aspectRatio = width / height; + + let adjustedWidth: number = width * aspectRatio; + let adjustedHeight: number = height * aspectRatio; + + // Ensure the adjusted height does not exceed the maximum height + if (adjustedHeight > MAX_ATTACHMENT_HEIGHT) { + const scale = MAX_ATTACHMENT_HEIGHT / adjustedHeight; + adjustedWidth *= scale; + adjustedHeight = MAX_ATTACHMENT_HEIGHT; + } + + return { adjustedWidth: Math.floor(adjustedWidth), adjustedHeight: Math.floor(adjustedHeight) }; +} + const Attachment = styled.div<{ withPointer?: boolean }>` cursor: ${(props) => (props.withPointer ? "pointer" : "default")}; padding: 2px 0; - width: min-content; `; const Image = styled.img` border-radius: 4px; + width: 100%; + height: auto; `; interface AttachmentProps { attachment: APIAttachment; - maxWidth?: number; - maxHeight?: number; } -export default function MessageAttachment({ attachment, maxWidth, maxHeight }: AttachmentProps) { +export default function MessageAttachment({ attachment }: AttachmentProps) { const logger = useLogger("MessageAttachment"); const url = attachment.proxy_url && attachment.proxy_url.length > 0 ? attachment.proxy_url : attachment.url; @@ -32,16 +48,17 @@ export default function MessageAttachment({ attachment, maxWidth, maxHeight }: A const details = getFileDetails(attachment); let finalElement: JSX.Element = <>; if (details.isImage && details.isEmbeddable) { - const ratio = calculateImageRatio(attachment.width!, attachment.height!, maxWidth, maxHeight); - const { scaledWidth, scaledHeight } = calculateScaledDimensions( - attachment.width!, - attachment.height!, - ratio, - maxWidth, - maxHeight, - ); + const width = attachment.width!; + const height = attachment.height!; + const { adjustedWidth, adjustedHeight } = adjustDimensions(width, height); + finalElement = ( - {attachment.filename} + {attachment.filename} ); } else if (details.isVideo && details.isEmbeddable) { finalElement =