Repository navigation
feat/#502-live-share-link-copy 라이브 공유 모달 링크 공유 버튼 추가 #503
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,63 @@ | ||
| import { vi } from 'vitest'; | ||
| import { copyToClipboard } from './clipboard'; | ||
|
|
||
| const TEXT = 'https://example.com/live/1'; | ||
|
|
||
| function mockClipboard(writeText?: (text: string) => Promise<void>) { | ||
| Object.defineProperty(navigator, 'clipboard', { | ||
| configurable: true, | ||
| value: writeText ? { writeText } : undefined, | ||
| }); | ||
| } | ||
|
|
||
| function mockExecCommand(result: boolean) { | ||
| const execCommand = vi.fn().mockReturnValue(result); | ||
| Object.defineProperty(document, 'execCommand', { | ||
| configurable: true, | ||
| value: execCommand, | ||
| }); | ||
| return execCommand; | ||
| } | ||
|
|
||
| describe('copyToClipboard', () => { | ||
| afterEach(() => { | ||
| mockClipboard(); | ||
| }); | ||
|
|
||
| test('Clipboard API가 있으면 writeText로 복사한다', async () => { | ||
| const writeText = vi.fn().mockResolvedValue(undefined); | ||
| mockClipboard(writeText); | ||
| const execCommand = mockExecCommand(true); | ||
|
|
||
| await expect(copyToClipboard(TEXT)).resolves.toBe(true); | ||
|
|
||
| expect(writeText).toHaveBeenCalledWith(TEXT); | ||
| expect(execCommand).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('Clipboard API가 없으면 execCommand로 폴백한다', async () => { | ||
| mockClipboard(); | ||
| const execCommand = mockExecCommand(true); | ||
|
|
||
| await expect(copyToClipboard(TEXT)).resolves.toBe(true); | ||
|
|
||
| expect(execCommand).toHaveBeenCalledWith('copy'); | ||
| expect(document.querySelector('textarea')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('writeText가 실패하면 execCommand로 폴백한다', async () => { | ||
| mockClipboard(vi.fn().mockRejectedValue(new Error('denied'))); | ||
| const execCommand = mockExecCommand(true); | ||
|
|
||
| await expect(copyToClipboard(TEXT)).resolves.toBe(true); | ||
|
|
||
| expect(execCommand).toHaveBeenCalledWith('copy'); | ||
| }); | ||
|
|
||
| test('모든 복사 방법이 실패하면 false를 반환한다', async () => { | ||
| mockClipboard(vi.fn().mockRejectedValue(new Error('denied'))); | ||
| mockExecCommand(false); | ||
|
|
||
| await expect(copyToClipboard(TEXT)).resolves.toBe(false); | ||
| }); | ||
| }); |
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,35 @@ | ||
| /** | ||
| * 텍스트를 클립보드에 복사하고 성공 여부를 반환합니다. | ||
| * Clipboard API는 보안 컨텍스트(HTTPS, localhost)에서만 동작하므로, | ||
| * 지원하지 않거나 실패하면 execCommand('copy')로 폴백합니다. | ||
| */ | ||
| export async function copyToClipboard(text: string): Promise<boolean> { | ||
| if (navigator.clipboard?.writeText) { | ||
| try { | ||
| await navigator.clipboard.writeText(text); | ||
| return true; | ||
| } catch { | ||
| // 권한 거부 등으로 실패하면 아래 폴백을 시도합니다. | ||
| } | ||
| } | ||
|
|
||
| return copyWithExecCommand(text); | ||
| } | ||
|
|
||
| function copyWithExecCommand(text: string): boolean { | ||
| const textarea = document.createElement('textarea'); | ||
| textarea.value = text; | ||
| textarea.setAttribute('readonly', ''); | ||
| textarea.style.position = 'fixed'; | ||
| textarea.style.opacity = '0'; | ||
| document.body.appendChild(textarea); | ||
| textarea.select(); | ||
|
|
||
| try { | ||
| return document.execCommand('copy'); | ||
| } catch { | ||
| return false; | ||
| } finally { | ||
| document.body.removeChild(textarea); | ||
| } | ||
| } |
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.