forked from rajun/ar-test
feat: add clipboard, download and image browser helpers
This commit is contained in:
parent
f61c26af33
commit
2f02dd1ae5
38
app/lib/browser.ts
Normal file
38
app/lib/browser.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { toast } from 'sonner';
|
||||||
|
|
||||||
|
export async function copyToClipboard(value: string, label = 'Link') {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(value);
|
||||||
|
toast.success(`${label} copied`);
|
||||||
|
} catch {
|
||||||
|
toast.error(`Could not copy ${label.toLowerCase()}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function downloadBlob(blob: Blob, fileName: string) {
|
||||||
|
const objectUrl = URL.createObjectURL(blob);
|
||||||
|
const anchor = document.createElement('a');
|
||||||
|
anchor.href = objectUrl;
|
||||||
|
anchor.download = fileName;
|
||||||
|
anchor.click();
|
||||||
|
URL.revokeObjectURL(objectUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadImage(src: string): Promise<HTMLImageElement> {
|
||||||
|
const image = new Image();
|
||||||
|
image.crossOrigin = 'anonymous';
|
||||||
|
image.src = src;
|
||||||
|
return image.decode().then(() => image);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function blobToDataUrl(blob: Blob): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = () => resolve(String(reader.result));
|
||||||
|
reader.onerror = () => reject(reader.error);
|
||||||
|
reader.readAsDataURL(blob);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads a theme colour token (e.g. "--color-logo-chip-dark") for canvas/SVG drawing
|
||||||
|
export const themeColor = (token: string) => getComputedStyle(document.documentElement).getPropertyValue(token).trim();
|
||||||
Loading…
x
Reference in New Issue
Block a user