-
Notifications
You must be signed in to change notification settings - Fork 1
채팅 이미지 미리보기 리팩토링 #221
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
Open
0Jaemin0
wants to merge
6
commits into
develop
Choose a base branch
from
refactor/#219-ChatRoom-Image
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
채팅 이미지 미리보기 리팩토링 #221
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6f933b
feat: 이미지 미리보기 util 추가
0Jaemin0 4a2f667
feat: 채팅 이미지 메시지 컴포넌트 추가
0Jaemin0 3df755e
Merge branch 'develop' of https://github.com/98OO/colla-frontend into…
0Jaemin0 34a9b42
fix: 이미지 클릭 기본 동작 보정
0Jaemin0 7ea7222
refactor: 이미지 로드 핸들러 명칭 정리
0Jaemin0 a7304b7
fix: 채팅 읽음 처리 전송 안전화
0Jaemin0 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/components/Chat/ChatImageMessage/ChatImageMessage.styled.ts
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,42 @@ | ||
| import styled from 'styled-components'; | ||
|
|
||
| type Orientation = 'landscape' | 'portrait' | 'square'; | ||
|
|
||
| const IMAGE_CARD_SIZE: Record<Orientation, { width: string; height: string }> = { | ||
| landscape: { | ||
| width: '260px', | ||
| height: '174px', | ||
| }, | ||
| portrait: { | ||
| width: '260px', | ||
| height: '325px', | ||
| }, | ||
| square: { | ||
| width: '260px', | ||
| height: '260px', | ||
| }, | ||
| }; | ||
|
|
||
| export const ImageLink = styled.a<{ | ||
| $isLoaded: boolean; | ||
| $orientation: Orientation; | ||
| }>` | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| position: relative; | ||
| overflow: hidden; | ||
|
|
||
| width: ${(props) => IMAGE_CARD_SIZE[props.$orientation].width}; | ||
| height: ${(props) => IMAGE_CARD_SIZE[props.$orientation].height}; | ||
| `; | ||
|
|
||
| export const PreviewImage = styled.img<{ $isLoaded: boolean }>` | ||
| display: block; | ||
| width: auto; | ||
| height: auto; | ||
| max-width: 100%; | ||
| max-height: 100%; | ||
| object-fit: contain; | ||
| opacity: ${(props) => (props.$isLoaded ? 1 : 0)}; | ||
| `; |
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,53 @@ | ||
| import { useState } from 'react'; | ||
| import { openImagePreviewWindow } from '@utils/openImagePreviewWindow'; | ||
| import * as S from './ChatImageMessage.styled'; | ||
|
|
||
| type Orientation = 'landscape' | 'portrait' | 'square'; | ||
|
|
||
| interface ChatImageMessageProps { | ||
| src: string; | ||
| alt: string; | ||
| } | ||
|
|
||
| const ChatImageMessage = ({ src, alt }: ChatImageMessageProps) => { | ||
| const [isLoaded, setIsLoaded] = useState(false); | ||
| const [orientation, setOrientation] = useState<Orientation>('portrait'); | ||
|
|
||
| const handleImageLoad = (event: React.SyntheticEvent<HTMLImageElement>) => { | ||
| const { naturalWidth, naturalHeight } = event.currentTarget; | ||
|
|
||
| if (naturalWidth > naturalHeight) setOrientation('landscape'); | ||
| else if (naturalWidth < naturalHeight) setOrientation('portrait'); | ||
| else setOrientation('square'); | ||
|
|
||
| setIsLoaded(true); | ||
| }; | ||
|
|
||
| const handleImageClick = (event: React.MouseEvent<HTMLAnchorElement>) => { | ||
| if (event.ctrlKey || event.metaKey || event.shiftKey) return; | ||
|
|
||
| event.preventDefault(); | ||
| openImagePreviewWindow({ src, alt }); | ||
| }; | ||
|
|
||
| return ( | ||
| <S.ImageLink | ||
| href={src} | ||
| target='_blank' | ||
| rel='noopener noreferrer' | ||
| aria-label={`${alt} 이미지 새 탭에서 보기`} | ||
| onClick={handleImageClick} | ||
| $isLoaded={isLoaded} | ||
| $orientation={orientation}> | ||
| <S.PreviewImage | ||
| src={src} | ||
| alt={alt} | ||
| loading='lazy' | ||
| onLoad={handleImageLoad} | ||
| $isLoaded={isLoaded} | ||
| /> | ||
| </S.ImageLink> | ||
| ); | ||
| }; | ||
|
|
||
| export default ChatImageMessage; | ||
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
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,29 @@ | ||
| interface OpenImagePreviewWindowParams { | ||
| src: string; | ||
| alt: string; | ||
| } | ||
|
|
||
| export const openImagePreviewWindow = ({ src, alt }: OpenImagePreviewWindowParams) => { | ||
| const imageWindow = window.open('', '_blank'); | ||
|
|
||
| if (!imageWindow) return; | ||
|
|
||
| imageWindow.opener = null; | ||
| imageWindow.document.title = alt; | ||
| imageWindow.document.body.style.margin = '0'; | ||
| imageWindow.document.body.style.backgroundColor = '#0f1115'; | ||
| imageWindow.document.body.style.display = 'flex'; | ||
| imageWindow.document.body.style.alignItems = 'center'; | ||
| imageWindow.document.body.style.justifyContent = 'center'; | ||
| imageWindow.document.body.style.minHeight = '100vh'; | ||
|
|
||
| const previewImage = imageWindow.document.createElement('img'); | ||
|
|
||
| previewImage.src = src; | ||
| previewImage.alt = alt; | ||
| previewImage.style.maxWidth = '100vw'; | ||
| previewImage.style.maxHeight = '100vh'; | ||
| previewImage.style.objectFit = 'contain'; | ||
|
|
||
| imageWindow.document.body.replaceChildren(previewImage); | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.