forked from rinafcode/teachLink_web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-qr.ts
More file actions
145 lines (132 loc) · 3.98 KB
/
Copy pathgenerate-qr.ts
File metadata and controls
145 lines (132 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* QR Code Generation Utilities
* Provides functions for generating and styling QR codes, with support for custom colors and sizes.
*/
import { createLogger } from '@/lib/logging';
const logger = createLogger('qr-code');
export interface QRCodeOptions {
/** Size of the QR code in pixels */
size?: number;
/** Error correction level: 'L', 'M', 'Q', 'H' */
level?: 'L' | 'M' | 'Q' | 'H';
/** Include margin/quiet zone around QR code */
includeMargin?: boolean;
/** Background color (hex or CSS color) */
bgColor?: string;
/** Foreground/module color (hex or CSS color) */
fgColor?: string;
}
/**
* Default QR code configuration
*/
export const DEFAULT_QR_OPTIONS: QRCodeOptions = {
size: 256,
level: 'H',
includeMargin: true,
bgColor: '#ffffff',
fgColor: '#000000',
};
/**
* Validates a URL for QR code generation
* @param url - The URL to validate
* @returns boolean indicating if URL is valid
*/
export function isValidQRUrl(url: string): boolean {
if (!url || typeof url !== 'string') return false;
try {
// Check if it's a valid URL or a path
new URL(url, 'http://example.com');
return true;
} catch {
return false;
}
}
/**
* Generates a shareable QR code URL
* This can be used to generate QR codes from external services if needed
* @param text - Text or URL to encode
* @param options - QR code options
* @returns Generated QR code data
*/
export function generateQRCodeData(text: string, options: QRCodeOptions = DEFAULT_QR_OPTIONS) {
if (!isValidQRUrl(text)) {
throw new Error('Invalid URL or text for QR code generation');
}
return {
text,
options: {
...DEFAULT_QR_OPTIONS,
...options,
},
};
}
/**
* Downloads a QR code as an image
* @param canvas - Canvas element containing the QR code
* @param filename - Name of the downloaded file
*/
export async function downloadQRCode(canvas: HTMLCanvasElement, filename: string = 'qrcode.png') {
try {
const url = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (error) {
logger.error('Failed to download QR code', { error });
throw new Error('Failed to download QR code');
}
}
/**
* Prints a QR code
* @param canvas - Canvas element containing the QR code
*/
export async function printQRCode(canvas: HTMLCanvasElement) {
try {
const url = canvas.toDataURL('image/png');
const printWindow = window.open();
if (!printWindow) {
throw new Error('Failed to open print window');
}
printWindow.document.write(`<img src="${url}" style="max-width: 100%; margin: auto;" />`);
printWindow.document.close();
printWindow.print();
} catch (error) {
logger.error('Failed to print QR code', { error });
throw new Error('Failed to print QR code');
}
}
/**
* Copies QR code data URL to clipboard
* @param canvas - Canvas element containing the QR code
*/
export async function copyQRCodeToClipboard(canvas: HTMLCanvasElement) {
try {
const url = canvas.toDataURL('image/png');
const blob = await fetch(url).then((res) => res.blob());
await navigator.clipboard.write([
new ClipboardItem({
'image/png': blob,
}),
]);
} catch (error) {
logger.error('Failed to copy QR code to clipboard', { error });
throw new Error('Failed to copy QR code to clipboard');
}
}
/**
* Generates a QR code data URL for sharing
* @param text - Text or URL to encode
* @returns Data URL that can be used for sharing
*/
export function generateQRCodeUrl(text: string): string {
if (!isValidQRUrl(text)) {
throw new Error('Invalid URL or text for QR code generation');
}
// Using a QR code API service as fallback
// You can replace with your preferred QR code generation endpoint
const encodedText = encodeURIComponent(text);
return `https://api.qrserver.com/v1/create-qr-code/?size=256x256&data=${encodedText}`;
}