-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add clipboard hooks - first approach #85
Changes from 2 commits
784bb22
9e016a1
d40fa05
3c529bd
49225f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
export default function useClipboard() { | ||
const checkForPermission = (type: PermissionName): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please prefer using |
||
if (!navigator.permissions) { | ||
resolve('Permission granted.'); | ||
} else { | ||
navigator.permissions | ||
.query({ | ||
name: type, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be put on a single line to improve legibility. |
||
|
||
/* Permission API is still a working draft, and Typescript types for it | ||
are still not correct, hence 'any' type as an argument. Similar situation | ||
in lines where checkForPermission function is invoked. */ | ||
.then((permissionStatus: any) => { | ||
// Will be 'granted', 'denied' or 'prompt': | ||
if (permissionStatus.state === 'granted') { | ||
resolve('Permission granted.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the resolved value should be a boolean, returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes total sense. |
||
} else { | ||
reject('Permission deined'); // eslint-disable-line | ||
} | ||
}) | ||
.catch(error => reject(error)); | ||
} | ||
}); | ||
}; | ||
|
||
const cut = (element: HTMLInputElement) => { | ||
checkForPermission('clipboard-write' as PermissionName) | ||
.then(() => { | ||
element.select(); | ||
document.execCommand('cut'); | ||
element.value = ''; | ||
element.blur(); | ||
}) | ||
.catch((error: string) => console.error(error)); | ||
}; | ||
|
||
const paste = (element: HTMLInputElement) => { | ||
checkForPermission('clipboard-read' as PermissionName) | ||
.then(() => { | ||
element.focus(); | ||
document.execCommand('paste'); | ||
}) | ||
.catch((error: string) => console.error(error)); | ||
}; | ||
|
||
const copy = (text: string) => { | ||
checkForPermission('clipboard-write' as PermissionName) | ||
.then(() => { | ||
if (navigator.clipboard) { | ||
navigator.clipboard.writeText(text); | ||
} else { | ||
const tempInput = document.createElement('input'); | ||
document.body.appendChild(tempInput); | ||
tempInput.setAttribute('id', 'temp-input'); | ||
(document.getElementById( | ||
'temp-input', | ||
) as HTMLInputElement).value = text; | ||
tempInput.select(); | ||
document.execCommand('copy'); | ||
document.body.removeChild(tempInput); | ||
} | ||
}) | ||
.catch((error: string) => console.error(error)); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please prefer using |
||
return { | ||
copy, | ||
cut, | ||
paste, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function may be put outside of the
useClipboard
function (just above it), in order to avoid re-instantiating it on every render.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a second thought,
checkForPermission
may be moved to 'utils.ts', in order to be reused byuseDeviceMotion
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved.