-
Notifications
You must be signed in to change notification settings - Fork 60
Add gallery support #1019
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
Merged
Merged
Add gallery support #1019
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fcf2765
Add gallery support
nikiwastaken 23926d9
Add changeset
nikiwastaken 4db5a42
Make items keep aspect ratio
nushea 2d3b5af
WIP css changes
nikiwastaken 2948963
Change gallery to be a grid
nikiwastaken b55cca1
Fix re-rendering
nikiwastaken 7ebf12f
Rendering changes
nikiwastaken d800ae3
Prefer body over filename in tooltip
nikiwastaken f2683fe
Redesign uploads menu
nikiwastaken 38b9729
Add the ability to send individual attachment text as caption
nikiwastaken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| default: minor | ||
| --- | ||
|
|
||
| # Added a gallery support as per msc4274 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { recipe } from '@vanilla-extract/recipes'; | ||
| import { style } from '@vanilla-extract/css'; | ||
| import { DefaultReset, config, toRem } from 'folds'; | ||
|
|
||
| export const GalleryHolder = style({ | ||
| marginTop: config.space.S200, | ||
| }); | ||
|
|
||
| export const GalleryImageGrid = recipe({ | ||
| base: [ | ||
| DefaultReset, | ||
| { | ||
| display: 'grid', | ||
| gap: '0.5rem', | ||
| maxWidth: toRem(600), | ||
| height: '100%', | ||
| width: '100%', | ||
| gridAutoColumns: toRem(100), | ||
| }, | ||
| ], | ||
| variants: { | ||
| type: { | ||
| ThreeItems: { | ||
| gridTemplateColumns: '1fr 1fr', | ||
| gridTemplateRows: `repeat(2, 1fr)`, | ||
| }, | ||
| ThreeByThree: { | ||
| gridTemplateColumns: 'repeat(3,minmax(0,1fr))', | ||
| }, | ||
| TwoByTwo: { | ||
| gridTemplateColumns: 'repeat(2,minmax(0,1fr))', | ||
| }, | ||
| OneByOne: { | ||
| maxHeight: toRem(300), | ||
| gridTemplateColumns: '1fr', | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export const GalleryItem = recipe({ | ||
| base: [ | ||
| DefaultReset, | ||
| { | ||
| borderRadius: config.radii.R300, | ||
| overflow: 'hidden', | ||
| width: '100%', | ||
| height: '100%', | ||
| aspectRatio: '1/1', | ||
| selectors: { | ||
| [`${GalleryImageGrid.classNames.variants.type.ThreeItems} &:nth-child(1)`]: { | ||
| gridRow: 'span 2', | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { type ReactNode } from 'react'; | ||
| import { Box } from 'folds'; | ||
| import { MsgType, type IContent } from 'matrix-js-sdk'; | ||
| import type { | ||
| IGalleryContent, | ||
| IGalleryImageItem, | ||
| IGalleryItem, | ||
| IGalleryVideoItem, | ||
| } from '$types/matrix/common'; | ||
| import * as css from './MGallery.css'; | ||
|
|
||
| function galleryItemToContent(item: IGalleryItem): IContent { | ||
| const { itemtype, ...rest } = item; | ||
| return { ...rest, msgtype: itemtype } as IContent; | ||
| } | ||
|
|
||
| type MGalleryProps = { | ||
| content: IGalleryContent; | ||
| renderItem: (content: IContent, index: number) => ReactNode; | ||
| }; | ||
|
|
||
| type PartitionedMediaItem = { | ||
| items: (IGalleryImageItem | IGalleryVideoItem)[]; | ||
| type: 'ThreeByThree' | 'TwoByTwo' | 'OneByOne' | 'ThreeItems'; | ||
| }; | ||
|
|
||
| export function MGallery({ content, renderItem }: MGalleryProps) { | ||
| const items = content.itemtypes; | ||
|
|
||
| let mediaItems = items.filter( | ||
| (item) => item.itemtype == MsgType.Video || item.itemtype == MsgType.Image | ||
| ); | ||
| const columnItems = items.filter( | ||
| (item) => item.itemtype == MsgType.File || item.itemtype == MsgType.Audio | ||
| ); | ||
|
|
||
| let partitionedMediaItems: PartitionedMediaItem[] = []; | ||
| while (mediaItems.length > 0) { | ||
| if (mediaItems.length >= 9) { | ||
| partitionedMediaItems.unshift({ | ||
| items: mediaItems.slice(-9), | ||
| type: 'ThreeByThree', | ||
| }); | ||
| mediaItems = mediaItems.slice(0, mediaItems.length - 9); | ||
| continue; | ||
| } | ||
| if (mediaItems.length >= 6) { | ||
| partitionedMediaItems.unshift({ | ||
| items: mediaItems.slice(-6), | ||
| type: 'ThreeByThree', | ||
| }); | ||
| mediaItems = mediaItems.slice(0, mediaItems.length - 6); | ||
| continue; | ||
| } | ||
| if (mediaItems.length >= 4) { | ||
| partitionedMediaItems.unshift({ | ||
| items: mediaItems.slice(-4), | ||
| type: 'TwoByTwo', | ||
| }); | ||
| mediaItems = mediaItems.slice(0, mediaItems.length - 4); | ||
| continue; | ||
| } | ||
| if (mediaItems.length > 3) { | ||
| partitionedMediaItems.unshift({ | ||
| items: mediaItems.slice(-3), | ||
| type: 'ThreeByThree', | ||
| }); | ||
| mediaItems = mediaItems.slice(0, mediaItems.length - 3); | ||
| continue; | ||
| } | ||
| if (mediaItems.length == 3) { | ||
| partitionedMediaItems.unshift({ | ||
| items: mediaItems.slice(-3), | ||
| type: 'ThreeItems', | ||
| }); | ||
| mediaItems = mediaItems.slice(0, mediaItems.length - 3); | ||
| continue; | ||
| } | ||
| if (mediaItems.length >= 2) { | ||
| partitionedMediaItems.unshift({ | ||
| items: mediaItems.slice(-2), | ||
| type: 'TwoByTwo', | ||
| }); | ||
| mediaItems = mediaItems.slice(0, mediaItems.length - 2); | ||
| continue; | ||
| } | ||
| if (mediaItems.length >= 1) { | ||
| partitionedMediaItems.unshift({ | ||
| items: mediaItems.slice(-1), | ||
| type: 'OneByOne', | ||
| }); | ||
| mediaItems = mediaItems.slice(0, mediaItems.length - 1); | ||
| continue; | ||
| } | ||
| } | ||
| return ( | ||
| <Box display="Flex" direction="Column" gap="400"> | ||
| {partitionedMediaItems.length > 0 && ( | ||
| <Box direction="Column" gap="200"> | ||
| {partitionedMediaItems.map((item) => ( | ||
| <div | ||
| key={`${item.type}-${item.items[0]?.url ?? item.items[0]?.file?.url}`} | ||
| className={css.GalleryImageGrid({ | ||
| type: item.type, | ||
| })} | ||
| > | ||
| {item.items.map((mediaItem, index) => ( | ||
| <div key={mediaItem.url ?? mediaItem.file?.url} className={css.GalleryItem()}> | ||
| {renderItem(galleryItemToContent(mediaItem), index)} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ))} | ||
| </Box> | ||
| )} | ||
| {columnItems.length > 0 && ( | ||
| <Box alignItems="Start" gap="200" direction="Column"> | ||
| {columnItems.map((item, index) => ( | ||
| <div key={item.url ?? item.file?.url ?? index}> | ||
| {renderItem(galleryItemToContent(item), index)} | ||
| </div> | ||
| ))} | ||
| </Box> | ||
| )} | ||
| </Box> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not like this, can we set it for gallery only somehow?