From 5dc6d9ee227fa714f39957ff6e744e3079a3e57d Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 2 Dec 2024 16:01:47 +0800 Subject: [PATCH] feat(social): improve image gallery grid Signed-off-by: Innei --- apps/renderer/src/components/ui/media.tsx | 6 +- .../entry-column/Items/social-media-item.tsx | 205 ++++++++++++------ 2 files changed, 146 insertions(+), 65 deletions(-) diff --git a/apps/renderer/src/components/ui/media.tsx b/apps/renderer/src/components/ui/media.tsx index a0b4e5257..2eb685a86 100644 --- a/apps/renderer/src/components/ui/media.tsx +++ b/apps/renderer/src/components/ui/media.tsx @@ -257,7 +257,11 @@ const MediaImpl: FC = ({ ) } else { return ( -
+
{ const entry = useEntry(entryId) || entryPreview - const previewMedia = usePreviewMedia() const asRead = useAsRead(entry) const feed = useFeedById(entry?.feedId) @@ -110,69 +110,7 @@ export const SocialMediaItem: EntryListItemFC = ({ entryId, entryPreview, transl {!!entry.collections && }
- {!!media?.length && ( -
- {media.map((media, i, mediaList) => { - const style: Partial<{ - width: string - height: string - }> = {} - const boundsWidth = jotaiStore.get(socialMediaContentWidthAtom) - if (media.height && media.width) { - // has 1 picture, max width is container width, but max height is less than window height: 2/3 - if (mediaList.length === 1) { - style.width = `${boundsWidth}px` - style.height = `${(boundsWidth * media.height) / media.width}px` - if (Number.parseInt(style.height) > (window.innerHeight * 2) / 3) { - style.height = `${(window.innerHeight * 2) / 3}px` - style.width = `${(Number.parseInt(style.height) * media.width) / media.height}px` - } - } - // has 2 pictures, max width is container half width, and - gap 8px - else if (mediaList.length === 2) { - style.width = `${(boundsWidth - 8) / 2}px` - style.height = `${(((boundsWidth - 8) / 2) * media.height) / media.width}px` - } - // has over 2 pictures, max width is container 1/3 width - else if (mediaList.length > 2) { - style.width = `${boundsWidth / 3}px` - style.height = `${((boundsWidth / 3) * media.height) / media.width}px` - } - } - - const proxySize = { - width: Number.parseInt(style.width || "0") * 2 || 0, - height: Number.parseInt(style.height || "0") * 2 || 0, - } - return ( - { - e.stopPropagation() - previewMedia( - mediaList.map((m) => ({ - url: m.url, - type: m.type, - blurhash: m.blurhash, - fallbackUrl: - m.preview_image_url ?? getImageProxyUrl({ url: m.url, ...proxySize }), - })), - i, - ) - }} - /> - ) - })} -
- )} + {!!media?.length && } {showAction && ( @@ -278,3 +216,142 @@ export const SocialMediaItemSkeleton = ( ) + +const SocialMediaGallery = ({ media }: { media: MediaModel[] }) => { + const previewMedia = usePreviewMedia() + + const isAllMediaSameRatio = useMemo(() => { + let ratio = 0 + for (const m of media) { + if (m.height && m.width) { + const currentRatio = m.height / m.width + if (ratio === 0) { + ratio = currentRatio + } else if (ratio !== currentRatio) { + return false + } + } else { + return false + } + } + return true + }, [media]) + + // all media has same ratio, use horizontal layout + if (isAllMediaSameRatio) { + return ( +
+ {media.map((media, i, mediaList) => { + const style: Partial<{ + width: string + height: string + }> = {} + const boundsWidth = jotaiStore.get(socialMediaContentWidthAtom) + if (media.height && media.width) { + // has 1 picture, max width is container width, but max height is less than window height: 2/3 + if (mediaList.length === 1) { + style.width = `${boundsWidth}px` + style.height = `${(boundsWidth * media.height) / media.width}px` + if (Number.parseInt(style.height) > (window.innerHeight * 2) / 3) { + style.height = `${(window.innerHeight * 2) / 3}px` + style.width = `${(Number.parseInt(style.height) * media.width) / media.height}px` + } + } + // has 2 pictures, max width is container half width, and - gap 8px + else if (mediaList.length === 2) { + style.width = `${(boundsWidth - 8) / 2}px` + style.height = `${(((boundsWidth - 8) / 2) * media.height) / media.width}px` + } + // has over 2 pictures, max width is container 1/3 width + else if (mediaList.length > 2) { + style.width = `${boundsWidth / 3}px` + style.height = `${((boundsWidth / 3) * media.height) / media.width}px` + } + } + + const proxySize = { + width: Number.parseInt(style.width || "0") * 2 || 0, + height: Number.parseInt(style.height || "0") * 2 || 0, + } + return ( + { + e.stopPropagation() + previewMedia( + mediaList.map((m) => ({ + url: m.url, + type: m.type, + blurhash: m.blurhash, + fallbackUrl: + m.preview_image_url ?? getImageProxyUrl({ url: m.url, ...proxySize }), + })), + i, + ) + }} + /> + ) + })} +
+ ) + } + + // all media has different ratio, use grid layout + return ( +
+
= 5 && "grid-cols-3", + )} + > + {media.map((m, i) => { + const proxySize = { + width: 400, + height: 400, + } + + const style = media.length === 3 && i === 2 ? { gridRow: "span 2" } : {} + + return ( + { + e.stopPropagation() + previewMedia( + media.map((m) => ({ + url: m.url, + type: m.type, + blurhash: m.blurhash, + fallbackUrl: + m.preview_image_url ?? getImageProxyUrl({ url: m.url, ...proxySize }), + })), + i, + ) + }} + /> + ) + })} +
+
+ ) +}