Skip to content

[URH-62] useClipboard hook 사용 시 Image 관련 에러가 발생하는 이슈 #119

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 2 commits into from
Nov 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/.nojekyll

This file was deleted.

1 change: 1 addition & 0 deletions docs/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-undef */
import nextra from 'nextra';

const withNextra = nextra('nextra-theme-docs', './theme.config.js');
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
"start": "NODE_ENV=test npm run build && npx serve -s out"
},
"keywords": [],
"author": "",
655 changes: 465 additions & 190 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
"install:docs": "npm install --prefix ./docs",
"dev:docs": "npm run dev --prefix ./docs",
"dev": "vite",
"build": "rm -rf node_modules/.tmp && tsc -b --verbose",
"build": "rm -rf node_modules/.tmp && tsc -b --verbose && tsc-alias -p tsconfig.app.json",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"prepare": "if [ -d .git ] && command -v husky >/dev/null 2>&1; then husky install; else echo 'Skipping husky install'; fi",
@@ -99,6 +99,7 @@
"prettier": "^3.3.2",
"ts-jest": "^29.2.2",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.10",
"typescript": "^5.2.2",
"vite": "^5.3.1"
},
15 changes: 10 additions & 5 deletions src/hooks/useClipboard/useClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from 'react';
import { imgToBlob } from '@/utils';
import { imgToBlob, hasNavigator } from '@/utils';
import { UseClipboardProps, UseClipboardReturns } from './type';

/**
@@ -21,12 +21,17 @@ const useClipboard = ({
}: UseClipboardProps = {}): UseClipboardReturns => {
const [copied, setCopied] = useState(false);

if (!navigator.clipboard) {
throw new Error('Clipboard API not supported.');
}

const handleCopy = async (copy: () => Promise<void>) => {
if (!hasNavigator()) {
throw new Error('Navigator not supported.');
}

if (!navigator.clipboard) {
throw new Error('Clipboard API not supported.');
}

setCopied(false);

try {
await copy();
setCopied(true);
7 changes: 3 additions & 4 deletions src/utils/imgToBlob.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const img = new Image();
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

export const imgToBlob = (path: string): Promise<Blob> => {
return new Promise((resolve, reject) => {
const img = new Image();
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
img.crossOrigin = 'Anonymous';
img.onload = function () {
if (this instanceof HTMLImageElement) {
Loading