-
Notifications
You must be signed in to change notification settings - Fork 0
[URH-51] useClipboard 신규 #43
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
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
112ff4b
✨ feat: useClipboard 생성
bicochan cebc1fa
✨ feat: copyImg 함수 작성
bicochan 04a2342
🩹 chore: crossOrigin 옵션 추가
bicochan 7d8ea9e
📝 docs: jsdoc 추가
bicochan c1517c3
🔨 refactor: copied 상태 초기화 로직 수정
bicochan 918149c
🔨 refactor: copy 로직 리팩토링
bicochan ead2442
🔨 refactor: imgToBlob 함수 리팩토링
bicochan 161f029
📝 docs: jsdoc 수정 및 Props, Returns 인터페이스 추가
bicochan 888c3a7
🩹 chore: 불필요한 코드 제거
bicochan a0dfe00
✅ test: useClipboard 테스트 코드 작성중
bicochan 3567401
🩹 chore: 인터페이스 컨벤션 적용
bicochan 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,58 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { imgToBlob } from '../utils'; | ||
|
||
/** | ||
* 클립보드에 텍스트나 이미지를 복사하는 커스텀 훅 | ||
* | ||
* @returns {boolean} `copied`: 복사 작업의 성공 여부를 나타내는 플래그 | ||
* @returns {(string) => Promise<void>} `copyText`: 텍스트를 클립보드에 복사하는 비동기 함수 | ||
* @returns {(string) => void} `copyImg`: 주어진 경로의 이미지를 클립보드에 복사하는 함수 | ||
* | ||
* @description | ||
* 클립보드 API가 지원되지 않는 경우 에러를 발생시킵니다. | ||
*/ | ||
const useClipboard = () => { | ||
const [copied, setCopied] = useState(false); | ||
|
||
if (!navigator.clipboard) { | ||
throw new Error('Clipboard API not supported.'); | ||
} | ||
|
||
const handleCopy = async (copy: () => Promise<void>) => { | ||
setCopied(false); | ||
try { | ||
await copy(); | ||
setCopied(true); | ||
} catch (error) { | ||
setCopied(false); | ||
console.error(error); | ||
throw new Error(`Failed to save to clipboard.`); | ||
} | ||
}; | ||
|
||
const copyText = (text: string) => { | ||
handleCopy(() => navigator.clipboard.writeText(text)); | ||
}; | ||
|
||
const copyImg = (path: string) => { | ||
imgToBlob(path, async (imgBlob) => { | ||
if (!imgBlob) return; | ||
foresec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
handleCopy(() => | ||
navigator.clipboard.write([new ClipboardItem({ 'image/png': imgBlob })]) | ||
); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
if (copied) { | ||
const timer = setTimeout(() => { | ||
setCopied(false); | ||
}, 5000); | ||
suhyeoonn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return () => clearTimeout(timer); | ||
} | ||
}, [copied]); | ||
|
||
return { copied, copyText, copyImg }; | ||
}; | ||
|
||
export default useClipboard; |
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,18 @@ | ||
const img = new Image(); | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
export const imgToBlob = (path: string, func: (blob: Blob | null) => void) => { | ||
img.crossOrigin = 'Anonymous'; | ||
img.onload = function () { | ||
if (this instanceof HTMLImageElement) { | ||
canvas.width = this.naturalWidth; | ||
canvas.height = this.naturalHeight; | ||
ctx?.drawImage(this, 0, 0); | ||
canvas.toBlob((blob) => { | ||
func(blob); | ||
}, 'image/png'); | ||
} | ||
}; | ||
img.src = path; | ||
}; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.