From 3640e9bf18e2d99ae4c96a09a68fd620ff8d3408 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Wed, 17 Apr 2024 23:01:46 -0400 Subject: [PATCH] more fixes for embeds --- src/components/messaging/Embed.module.css | 2 - src/components/messaging/EmbedMedia.tsx | 62 +++++++++++++++++++- src/components/messaging/MessageEmbed.tsx | 71 ++++++++--------------- 3 files changed, 83 insertions(+), 52 deletions(-) diff --git a/src/components/messaging/Embed.module.css b/src/components/messaging/Embed.module.css index 713b693..c962f3a 100644 --- a/src/components/messaging/Embed.module.css +++ b/src/components/messaging/Embed.module.css @@ -30,7 +30,6 @@ iframe { border-inline-start-style: solid; padding: 12px; - width: fit-content; background: var(--background-secondary); border-radius: 4px; } @@ -45,7 +44,6 @@ iframe { font-size: 12px; color: var(--text-secondary); font-weight: var(--font-weight-regular); - width: fit-content; } .embedAuthor { diff --git a/src/components/messaging/EmbedMedia.tsx b/src/components/messaging/EmbedMedia.tsx index b312871..f126736 100644 --- a/src/components/messaging/EmbedMedia.tsx +++ b/src/components/messaging/EmbedMedia.tsx @@ -6,6 +6,27 @@ import { modalController } from "../../controllers/modals"; import Icon from "../Icon"; import styles from "./Embed.module.css"; +function getScaledDimensions(originalWidth: number, originalHeight: number, maxWidth: number, maxHeight: number) { + const aspectRatio = originalWidth / originalHeight; + let newWidth = originalWidth; + let newHeight = originalHeight; + + if (newWidth > maxWidth) { + newWidth = maxWidth; + newHeight = newWidth / aspectRatio; + } + + if (newHeight > maxHeight) { + newHeight = maxHeight; + newWidth = newHeight * aspectRatio; + } + + return { width: Math.round(newWidth), height: Math.round(newHeight) }; +} +function shouldScaleImage(originalWidth: number, originalHeight: number, maxWidth: number, maxHeight: number) { + return originalWidth > maxWidth || originalHeight > maxHeight; +} + interface Props { embed: APIEmbed; width?: number; @@ -14,12 +35,49 @@ interface Props { } function EmbedMedia({ embed, width, height, thumbnail }: Props) { + let maxWidth = 400; + let maxHeight = 300; + + if (!width || !height) { + if (embed.video) { + width = embed.video.width; + height = embed.video.height; + } else if (embed.image) { + width = embed.image.width; + height = embed.image.height; + } else if (embed.thumbnail) { + if (embed.type !== EmbedType.Image && embed.provider?.name !== "GitHub") { + maxWidth = 80; + maxHeight = 80; + } + + width = embed.thumbnail.width; + height = embed.thumbnail.height; + } else { + console.log("No media size provided"); + width = 400; + height = 300; + } + } + + const originalWidth = width; + const originalHeight = height; + + // Scale image if it's too large + if (shouldScaleImage(width!, height!, maxWidth, maxHeight)) { + const { width: newWidth, height: newHeight } = getScaledDimensions(width!, height!, maxWidth, maxHeight); + width = newWidth; + height = newHeight; + } + + console.log(`Original size: ${originalWidth}x${originalHeight} - Scaled size: ${width}x${height}`); + switch (embed.provider?.name) { case "YouTube": { if (!embed.video?.url) return null; const url = embed.video.url; - return