mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-16 19:47:43 +01:00
53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
|
|
import { useEffect, useState } from "react";
|
||
|
|
|
||
|
|
// 1
|
||
|
|
export async function copyToClipboard(text: string) {
|
||
|
|
try {
|
||
|
|
if (window.__TAURI__) {
|
||
|
|
window.__TAURI__.writeText(text);
|
||
|
|
} else {
|
||
|
|
await navigator.clipboard.writeText(text);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.info("Copy Success");
|
||
|
|
} catch (error) {
|
||
|
|
const textArea = document.createElement("textarea");
|
||
|
|
textArea.value = text;
|
||
|
|
document.body.appendChild(textArea);
|
||
|
|
textArea.focus();
|
||
|
|
textArea.select();
|
||
|
|
try {
|
||
|
|
document.execCommand("copy");
|
||
|
|
console.info("Copy Success");
|
||
|
|
} catch (error) {
|
||
|
|
console.info("Copy Failed");
|
||
|
|
}
|
||
|
|
document.body.removeChild(textArea);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2
|
||
|
|
export function useWindowSize() {
|
||
|
|
const [size, setSize] = useState({
|
||
|
|
width: window.innerWidth,
|
||
|
|
height: window.innerHeight,
|
||
|
|
});
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
const onResize = () => {
|
||
|
|
setSize({
|
||
|
|
width: window.innerWidth,
|
||
|
|
height: window.innerHeight,
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
window.addEventListener("resize", onResize);
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
window.removeEventListener("resize", onResize);
|
||
|
|
};
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return size;
|
||
|
|
}
|