Skip to content

feat: copyToClipboard util function#37

Merged
prgmr99 merged 2 commits intomainfrom
feature/utils-workspace-yeom
Sep 11, 2025
Merged

feat: copyToClipboard util function#37
prgmr99 merged 2 commits intomainfrom
feature/utils-workspace-yeom

Conversation

@prgmr99
Copy link
Copy Markdown
Member

@prgmr99 prgmr99 commented Sep 11, 2025

Description

  • 웹 애플리케이션에서 텍스트를 클립보드에 복사하는 기능을 제공하는 copyToClipboard 유틸리티 함수를 추가합니다.

  • 최신 Clipboard API를 우선적으로 사용하고, 지원하지 않는 환경에서는 레거시 방식으로 자동 대체하여 높은 브라우저 호환성을 제공합니다.
    또한 대용량 텍스트에 대한 안전장치를 구현하여 안정성을 높였습니다.

@prgmr99 prgmr99 requested a review from klmhyeonwoo September 11, 2025 08:38
@prgmr99 prgmr99 self-assigned this Sep 11, 2025
@prgmr99 prgmr99 added the enhancement New feature or request label Sep 11, 2025
@klmhyeonwoo
Copy link
Copy Markdown
Member

klmhyeonwoo commented Sep 11, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 76.22% 186 / 244
🔵 Statements 76.22% 186 / 244
🔵 Functions 95% 19 / 20
🔵 Branches 91.66% 77 / 84
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
package/commonUtil/copyToClipboard/index.ts 88.46% 83.33% 100% 88.46% 37-40
package/commonUtil/sleep/index.ts 100% 100% 100% 100%
Generated in workflow #58 for commit 8831a28 by the Vitest Coverage Report Action

@prgmr99 prgmr99 force-pushed the feature/utils-workspace-yeom branch from 33b5399 to 8831a28 Compare September 11, 2025 08:39
Copy link
Copy Markdown
Member

@klmhyeonwoo klmhyeonwoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

Comment on lines +20 to +39
const textArea = document.createElement("textarea");
textArea.value = text;

textArea.style.position = "fixed";
textArea.style.top = "-9999px";
textArea.style.left = "-9999px";

document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
const successful = document.execCommand("copy");
document.body.removeChild(textArea);

return successful;
} catch (execError) {
document.body.removeChild(textArea);

return false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우와.. 지원하지 않는 브라우저의 경우이거나 에러가 발생하는 경우에는 execCommand로 처리하셨군요! 굿!

*/
export default async function copyToClipboard(text: string): Promise<boolean> {
try {
if (text.length > 1000000) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1000000은 어떤 기준으로 산정된건가요?!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거는 1,000,000 = 1MB로 제한한 거에요..!!

구현하고, 마지막에 gpt한테 코드에 아쉬운 점을 물어봤었는데요!!

브라우저별로 clipboard api에 제한이 있다는 사실을 알게 되었어요!
chrome이 약 20mb, firefox와 safari가 약 1mb라는 사실을 알려줘서 저렇게 추가했어요!

그런데, 말씀대로 숫자에 대한 정보가 명확하지 않다보니, 주석 + 별도의 처리가 필요해보여서 아래와 같이 수정했어요!

if (text.length > 1024 * 1024) {
  throw new Error("Text too large to copy (max 1MB)");
}

@prgmr99 prgmr99 merged commit 0576f59 into main Sep 11, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants