mirror of
https://github.com/notion-enhancer/notion-enhancer.git
synced 2025-04-09 15:09:02 +00:00
clipboard helpers
This commit is contained in:
parent
db5ec9a934
commit
7da5f65c85
29
api/web.mjs
29
api/web.mjs
@ -143,6 +143,35 @@ export const loadStylesheet = (path) => {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* copy text to the clipboard
|
||||||
|
* @param {string} str - the string to copy
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
export const copyToClipboard = async (str) => {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(str);
|
||||||
|
} catch {
|
||||||
|
const $el = document.createElement('textarea');
|
||||||
|
$el.value = str;
|
||||||
|
$el.setAttribute('readonly', '');
|
||||||
|
$el.style.position = 'absolute';
|
||||||
|
$el.style.left = '-9999px';
|
||||||
|
document.body.appendChild($el);
|
||||||
|
$el.select();
|
||||||
|
document.execCommand('copy');
|
||||||
|
document.body.removeChild($el);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* read text from the clipboard
|
||||||
|
* @returns {Promise<string>}
|
||||||
|
*/
|
||||||
|
export const readFromClipboard = () => {
|
||||||
|
return navigator.clipboard.readText();
|
||||||
|
};
|
||||||
|
|
||||||
document.addEventListener('keyup', (event) => {
|
document.addEventListener('keyup', (event) => {
|
||||||
if (document.activeElement.nodeName === 'INPUT') return;
|
if (document.activeElement.nodeName === 'INPUT') return;
|
||||||
for (const hotkey of _hotkeyEventListeners) {
|
for (const hotkey of _hotkeyEventListeners) {
|
||||||
|
Loading…
Reference in New Issue
Block a user