Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message embed image resizing #58

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@widgetbot/message-renderer",
"version": "v2.2.0",
"version": "v2.3.1",
"description": "",
"module": "dist/message-renderer.mjs",
"files": [
Expand Down
4 changes: 4 additions & 0 deletions src/Content/Attachment/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const ImageAttachment = styled.withConfig({
displayName: "image-attachment",
componentId: commonComponentId,
})(LazyLoadImage, {
maxWidth: "100%",
height: "auto",
variants: {
clickable: {
true: {
Expand All @@ -30,6 +32,8 @@ export const LazyImagePlaceholder = styled.withConfig({
display: "flex",
alignItems: "center",
justifyContent: "center",
maxWidth: "100%",
height: "auto",
});

export const VideoAttachmentContainer = styled.withConfig({
Expand Down
4 changes: 2 additions & 2 deletions src/Content/Embed/EmbedVideo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ interface EmbedVideoProps
extends Required<Pick<APIEmbedVideo, "width" | "height">> {
thumbnail?: APIEmbedThumbnail["url"];
url: APIEmbedVideo["url"] | undefined;
proxyUrl: APIEmbedVideo["proxy_url"] | undefined;
proxyUrl: APIEmbedVideo["proxy_url"] | undefined | null;
}

function EmbedVideo(props: EmbedVideoProps) {
if (props.proxyUrl !== undefined)
if (props.proxyUrl)
return (
<ThumbnailWrapper
thumbnail={props.thumbnail}
Expand Down
13 changes: 11 additions & 2 deletions src/Content/Embed/EmbeddedImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ComponentProps } from "react";
import React from "react";
import { useConfig } from "../../core/ConfigContext";
import type { APIEmbedImage } from "discord-api-types/v10";
import useSize from "src/Content/Attachment/useSize";

interface Props extends ComponentProps<typeof Styles.Image> {
embedImage: APIEmbedImage;
Expand All @@ -14,14 +15,22 @@ interface Props extends ComponentProps<typeof Styles.Image> {
function EmbeddedImage({ width, height, embedImage, ...rest }: Props) {
const { embedImageOnClick } = useConfig();

const { width: widthEmbed, height: heightEmbed } = useSize(
width ?? 1,
height ?? 1
);

const actualWidth = widthEmbed ?? width;
const actualHeight = heightEmbed ?? height;

return (
<Styles.Image
{...rest}
src={embedImage.proxy_url ?? embedImage.url}
clickable={embedImageOnClick !== undefined}
onClick={() => embedImageOnClick?.(embedImage)}
width={width}
height={height}
width={actualWidth}
height={actualHeight}
/>
);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Content/Embed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import * as Styles from "./style";
import { colorIntToRgba } from "../../utils/colorIntToCss";
import moment from "moment";
import { LinkMarkdown, parseEmbedTitle } from "../../markdown/render";
import useSize from "./useSize";
import EmbedVideo from "./EmbedVideo";
import React, { useMemo } from "react";
import type { APIEmbed, APIEmbedImage } from "discord-api-types/v10";
import { EmbedType } from "discord-api-types/v10";
import EmbeddedImage from "./EmbeddedImage";
import ExternalLink from "../../ExternalLink";
import { error } from "../../utils/error";
import useSize from "src/Content/Embed/useSize";

export interface EmbedProps {
embed: APIEmbed;
Expand All @@ -38,7 +38,7 @@ function Embed({ embed, images }: EmbedProps) {
? colorIntToRgba(embed.color)
: undefined;

const { width: widthImage, height: heightImage } = useSize(
const { width: widthImage } = useSize(
embed.type,
embed.image,
"EmbedImage",
Expand Down Expand Up @@ -140,8 +140,11 @@ function Embed({ embed, images }: EmbedProps) {
{(images === undefined || images?.length === 0) && embed.image && (
<EmbeddedImage
embedImage={embed.image}
width={widthImage ?? undefined}
height={heightImage ?? undefined}
withMargin
image={embed.image}
width={embed.image.width}
height={embed.image.height}
type="EmbedImage"
/>
)}
{images && images.length > 0 && (
Expand Down
14 changes: 11 additions & 3 deletions src/Content/Embed/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const Embed = styled.withConfig({
display: "flex",
flexDirection: "column",
maxWidth: 520,

variants: {
thin: {
true: {
Expand All @@ -35,9 +34,10 @@ export const ContentAndThumbnail = styled.withConfig({
displayName: "embed-content-and-thumbnail",
componentId: commonComponentId,
})("div", {
display: "flex",
display: "grid",
gridAutoColumns: "auto",
gap: theme.space.xxl,

maxWidth: "100%",
variants: {
hasLargeThumbnail: {
true: {
Expand All @@ -54,6 +54,8 @@ export const Content = styled.withConfig({
})("div", {
display: "grid",
gap: theme.space.large,
gridColumnStart: "1",
gridColumnEnd: "1",
});

export const Provider = styled.withConfig({
Expand Down Expand Up @@ -192,6 +194,10 @@ export const Image = styled.withConfig({
componentId: commonComponentId,
})("img", {
borderRadius: 3,
flexShrink: 0,
gridColumnStart: "2",
maxWidth: "100%",
height: "auto",

variants: {
clickable: {
Expand Down Expand Up @@ -279,6 +285,8 @@ export const MediaEmbed = styled.withConfig({
})("img", {
borderRadius: 3,
cursor: "pointer",
maxWidth: "100%",
height: "auto",
});

export const VideoIframe = styled.withConfig({
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Reactions/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const Reaction = styled.withConfig({
display: "flex",
flexDirection: "row",
alignItems: "center",
padding: `${theme.space.small} ${theme.space.medium}`,
padding: `${theme.space.xs} ${theme.space.medium}`,
borderRadius: 8,
cursor: "not-allowed",
backgroundColor: theme.colors.backgroundSecondary,
Expand Down
1 change: 1 addition & 0 deletions src/Stitches/stitches.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const stitches = createStitches({
messageLeftPadding: "72px",
threadButton: "34px",
messageTypeIcon: "16px",
embedThumbnail: "80px",
},
borderWidths: {
spines: "2px",
Expand Down