qrdx 0.0.45 → 0.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +353 -300
- package/dist/index.js +27 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -7
- package/dist/index.mjs.map +1 -1
- package/dist/qr-code-canvas.js +12 -0
- package/dist/qr-code-canvas.js.map +1 -0
- package/dist/qr-code-canvas.mjs +12 -0
- package/dist/qr-code-canvas.mjs.map +1 -0
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/art-trace.d.ts +46 -0
- package/dist/src/art-trace.d.ts.map +1 -0
- package/dist/src/art.d.ts +138 -0
- package/dist/src/art.d.ts.map +1 -0
- package/dist/src/body-patterns.d.ts +34 -0
- package/dist/src/body-patterns.d.ts.map +1 -0
- package/dist/src/codegen.d.ts +5 -5
- package/dist/src/codegen.d.ts.map +1 -1
- package/dist/src/codeword-map.d.ts +40 -0
- package/dist/src/codeword-map.d.ts.map +1 -0
- package/dist/src/constants.d.ts +4 -7
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts +3 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts +5 -2
- package/dist/src/corner-eye.d.ts.map +1 -1
- package/dist/src/detection.d.ts.map +1 -1
- package/dist/src/download.d.ts +22 -5
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/eps.d.ts +62 -0
- package/dist/src/eps.d.ts.map +1 -0
- package/dist/src/frame-icons.d.ts +11 -0
- package/dist/src/frame-icons.d.ts.map +1 -0
- package/dist/src/frame-import.d.ts +61 -0
- package/dist/src/frame-import.d.ts.map +1 -0
- package/dist/src/frame-spec.d.ts +24 -0
- package/dist/src/frame-spec.d.ts.map +1 -0
- package/dist/src/frame-styles.d.ts +50 -0
- package/dist/src/frame-styles.d.ts.map +1 -0
- package/dist/src/frame.d.ts +126 -0
- package/dist/src/frame.d.ts.map +1 -0
- package/dist/src/frames/builtin.d.ts +8 -0
- package/dist/src/frames/builtin.d.ts.map +1 -0
- package/dist/src/frames/official.d.ts +18 -0
- package/dist/src/frames/official.d.ts.map +1 -0
- package/dist/src/helpers.d.ts +15 -2
- package/dist/src/helpers.d.ts.map +1 -1
- package/dist/src/index.d.ts +24 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/logo-budget.d.ts +54 -0
- package/dist/src/logo-budget.d.ts.map +1 -0
- package/dist/src/logo-cleanup.d.ts +25 -0
- package/dist/src/logo-cleanup.d.ts.map +1 -0
- package/dist/src/logo.d.ts +28 -0
- package/dist/src/logo.d.ts.map +1 -0
- package/dist/src/print.d.ts +73 -0
- package/dist/src/print.d.ts.map +1 -0
- package/dist/src/qr-code-canvas.d.ts +13 -0
- package/dist/src/qr-code-canvas.d.ts.map +1 -0
- package/dist/src/qr-code.d.ts +4 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/qr-outline.d.ts +41 -0
- package/dist/src/qr-outline.d.ts.map +1 -0
- package/dist/src/qr-shape.d.ts +72 -0
- package/dist/src/qr-shape.d.ts.map +1 -0
- package/dist/src/scan-risk.d.ts +40 -0
- package/dist/src/scan-risk.d.ts.map +1 -0
- package/dist/src/scan-score.d.ts +154 -0
- package/dist/src/scan-score.d.ts.map +1 -0
- package/dist/src/shapes.d.ts +36 -0
- package/dist/src/shapes.d.ts.map +1 -0
- package/dist/src/silhouette.d.ts +28 -0
- package/dist/src/silhouette.d.ts.map +1 -0
- package/dist/src/utils/color.d.ts.map +1 -1
- package/dist/src/utils/fmt.d.ts +24 -0
- package/dist/src/utils/fmt.d.ts.map +1 -0
- package/dist/src/utils/paint.d.ts +80 -0
- package/dist/src/utils/paint.d.ts.map +1 -0
- package/dist/src/utils/parse-color.d.ts +21 -0
- package/dist/src/utils/parse-color.d.ts.map +1 -0
- package/dist/src/utils/path-data.d.ts +24 -0
- package/dist/src/utils/path-data.d.ts.map +1 -0
- package/dist/src/utils/png.d.ts +16 -0
- package/dist/src/utils/png.d.ts.map +1 -0
- package/dist/src/utils/polygon-clip.d.ts +26 -0
- package/dist/src/utils/polygon-clip.d.ts.map +1 -0
- package/dist/src/utils/print-shared.d.ts +20 -0
- package/dist/src/utils/print-shared.d.ts.map +1 -0
- package/dist/src/utils/svg-geometry.d.ts +49 -0
- package/dist/src/utils/svg-geometry.d.ts.map +1 -0
- package/dist/src/utils/svg-gradient.d.ts +59 -0
- package/dist/src/utils/svg-gradient.d.ts.map +1 -0
- package/dist/src/utils/svg-painter.d.ts +152 -0
- package/dist/src/utils/svg-painter.d.ts.map +1 -0
- package/dist/src/utils.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/types/art.d.ts +64 -0
- package/dist/types/art.d.ts.map +1 -0
- package/dist/types/color.d.ts +125 -7
- package/dist/types/color.d.ts.map +1 -1
- package/dist/types/corner-dot.d.ts +52 -2
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +43 -2
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/frame-spec.d.ts +3509 -0
- package/dist/types/frame-spec.d.ts.map +1 -0
- package/dist/types/frame.d.ts +68 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/image-pattern.d.ts +15 -1
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +6 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/qr.d.ts +1684 -4
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/template.d.ts +2 -2
- package/dist/types/template.d.ts.map +1 -1
- package/package.json +63 -20
- package/src/api.tsx +67 -17
- package/src/art-trace.ts +554 -0
- package/src/art.tsx +2226 -0
- package/src/body-patterns.tsx +389 -0
- package/src/codegen.ts +10 -5
- package/src/codeword-map.ts +265 -0
- package/src/constants.ts +4 -8
- package/src/corner-dot.tsx +209 -33
- package/src/corner-eye.ts +328 -60
- package/src/detection.ts +5 -11
- package/src/download.tsx +123 -52
- package/src/eps.ts +701 -0
- package/src/frame-icons.ts +325 -0
- package/src/frame-import.ts +1198 -0
- package/src/frame-spec.ts +518 -0
- package/src/frame-styles.ts +1095 -0
- package/src/frame.tsx +630 -0
- package/src/frames/builtin/banner-bottom.json +58 -0
- package/src/frames/builtin/circle.json +81 -0
- package/src/frames/builtin/rounded-border.json +60 -0
- package/src/frames/builtin/ticket.json +51 -0
- package/src/frames/builtin.ts +20 -0
- package/src/frames/official/attendance.json +110 -0
- package/src/frames/official/audio-guide.json +95 -0
- package/src/frames/official/book-now.json +103 -0
- package/src/frames/official/check-in.json +110 -0
- package/src/frames/official/christmas.json +131 -0
- package/src/frames/official/donate.json +77 -0
- package/src/frames/official/event-ticket.json +77 -0
- package/src/frames/official/feedback-bubble.json +73 -0
- package/src/frames/official/follow-us.json +83 -0
- package/src/frames/official/get-the-app.json +107 -0
- package/src/frames/official/halloween.json +80 -0
- package/src/frames/official/new-year.json +94 -0
- package/src/frames/official/order-at-table.json +86 -0
- package/src/frames/official/product-info.json +119 -0
- package/src/frames/official/register-product.json +77 -0
- package/src/frames/official/review-us.json +127 -0
- package/src/frames/official/rsvp-card.json +112 -0
- package/src/frames/official/save-contact.json +117 -0
- package/src/frames/official/scan-to-pay.json +94 -0
- package/src/frames/official/shop-window.json +81 -0
- package/src/frames/official/sign-rider.json +81 -0
- package/src/frames/official/table-tent.json +75 -0
- package/src/frames/official/wifi-card.json +89 -0
- package/src/frames/official/winter.json +142 -0
- package/src/frames/official.ts +77 -0
- package/src/helpers.ts +168 -11
- package/src/index.tsx +141 -172
- package/src/logo-budget.ts +226 -0
- package/src/logo-cleanup.ts +146 -0
- package/src/logo.tsx +137 -0
- package/src/print.ts +794 -0
- package/src/qr-code-canvas.tsx +241 -0
- package/src/qr-code.tsx +51 -16
- package/src/qr-outline.ts +511 -0
- package/src/qr-shape.ts +255 -0
- package/src/scan-risk.ts +197 -0
- package/src/scan-score.ts +1629 -0
- package/src/shapes.ts +169 -0
- package/src/silhouette.ts +334 -0
- package/src/utils/color.ts +10 -1
- package/src/utils/fmt.ts +59 -0
- package/src/utils/paint.ts +317 -0
- package/src/utils/parse-color.ts +135 -0
- package/src/utils/path-data.ts +182 -0
- package/src/utils/png.ts +175 -0
- package/src/utils/polygon-clip.ts +163 -0
- package/src/utils/print-shared.ts +74 -0
- package/src/utils/svg-geometry.ts +838 -0
- package/src/utils/svg-gradient.ts +407 -0
- package/src/utils/svg-painter.ts +946 -0
- package/src/utils.tsx +794 -385
- package/types/art.ts +74 -0
- package/types/color.ts +132 -12
- package/types/corner-dot.ts +41 -4
- package/types/corner-eye.ts +31 -4
- package/types/frame-spec.ts +748 -0
- package/types/frame.ts +67 -0
- package/types/image-pattern.ts +15 -0
- package/types/index.ts +106 -4
- package/types/qr.ts +180 -2
- package/types/template.ts +2 -2
- package/dist/cli.js +0 -4231
package/src/download.tsx
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import type { QRProps } from "../types";
|
|
2
2
|
import { DEFAULT_SIZE } from "./constants";
|
|
3
|
+
import { epsToDataUrl, getQRAsEPS } from "./eps";
|
|
4
|
+
import { getFrameDimensions } from "./frame";
|
|
3
5
|
import { getQRAsCanvas, getQRAsSVGDataUri } from "./index";
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
/** "eps" is vector PostScript: `size.width` becomes points (72 per inch). */
|
|
8
|
+
export type DownloadFormat = "png" | "jpg" | "svg" | "pdf" | "eps";
|
|
6
9
|
|
|
7
|
-
export
|
|
10
|
+
export interface DownloadSize {
|
|
8
11
|
width: number;
|
|
9
12
|
height: number;
|
|
10
|
-
}
|
|
13
|
+
}
|
|
11
14
|
|
|
12
15
|
export const PRESET_SIZES: Record<string, DownloadSize> = {
|
|
13
16
|
small: { width: 200, height: 200 },
|
|
@@ -18,11 +21,11 @@ export const PRESET_SIZES: Record<string, DownloadSize> = {
|
|
|
18
21
|
"3xl": { width: 2000, height: 2000 },
|
|
19
22
|
};
|
|
20
23
|
|
|
21
|
-
export
|
|
24
|
+
export interface DownloadOptions {
|
|
22
25
|
format: DownloadFormat;
|
|
23
26
|
size: DownloadSize;
|
|
24
27
|
filename?: string;
|
|
25
|
-
}
|
|
28
|
+
}
|
|
26
29
|
|
|
27
30
|
/**
|
|
28
31
|
* Validates if a size value is within acceptable bounds
|
|
@@ -61,10 +64,36 @@ export function validateSize(
|
|
|
61
64
|
return { isValid: true };
|
|
62
65
|
}
|
|
63
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Returns props rendered at `size`. A logo laid out for the props' own `size`
|
|
69
|
+
* (as `getQRData` does on a 1024px canvas) is scaled along with the code.
|
|
70
|
+
*/
|
|
71
|
+
export function withQRSize(qrProps: QRProps, size: number): QRProps {
|
|
72
|
+
const { imageSettings, size: fromSize } = qrProps;
|
|
73
|
+
if (!(imageSettings && fromSize) || fromSize === size) {
|
|
74
|
+
return { ...qrProps, size };
|
|
75
|
+
}
|
|
76
|
+
const ratio = size / fromSize;
|
|
77
|
+
return {
|
|
78
|
+
...qrProps,
|
|
79
|
+
size,
|
|
80
|
+
imageSettings: {
|
|
81
|
+
...imageSettings,
|
|
82
|
+
width: imageSettings.width * ratio,
|
|
83
|
+
height: imageSettings.height * ratio,
|
|
84
|
+
...(imageSettings.x != null && { x: imageSettings.x * ratio }),
|
|
85
|
+
...(imageSettings.y != null && { y: imageSettings.y * ratio }),
|
|
86
|
+
...(imageSettings.padding != null && {
|
|
87
|
+
padding: imageSettings.padding * ratio,
|
|
88
|
+
}),
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
64
93
|
/**
|
|
65
94
|
* Downloads a file to the user's device
|
|
66
95
|
*/
|
|
67
|
-
function downloadFile(dataUrl: string, filename: string) {
|
|
96
|
+
export function downloadFile(dataUrl: string, filename: string) {
|
|
68
97
|
const link = document.createElement("a");
|
|
69
98
|
link.href = dataUrl;
|
|
70
99
|
link.download = filename;
|
|
@@ -86,6 +115,8 @@ function getMimeType(format: DownloadFormat): string {
|
|
|
86
115
|
return "image/svg+xml";
|
|
87
116
|
case "pdf":
|
|
88
117
|
return "application/pdf";
|
|
118
|
+
case "eps":
|
|
119
|
+
return "application/postscript";
|
|
89
120
|
default:
|
|
90
121
|
return "image/png";
|
|
91
122
|
}
|
|
@@ -98,11 +129,53 @@ function getFileExtension(format: DownloadFormat): string {
|
|
|
98
129
|
return format;
|
|
99
130
|
}
|
|
100
131
|
|
|
132
|
+
/** Vector files: they have no pixel size, so their names state none. */
|
|
133
|
+
const VECTOR_FORMATS: ReadonlySet<DownloadFormat> = new Set(["svg", "eps"]);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The default file name of a download: for PNG and JPG (and the image a
|
|
137
|
+
* PDF holds) the exact pixel size of what is saved, which a frame makes
|
|
138
|
+
* bigger than the code (a 400 px code in a bottom banner is 496 × 568);
|
|
139
|
+
* SVG and EPS have no pixel size and name none.
|
|
140
|
+
*/
|
|
141
|
+
export function getDownloadFilename(
|
|
142
|
+
format: DownloadFormat,
|
|
143
|
+
size: DownloadSize,
|
|
144
|
+
frame?: QRProps["frame"]
|
|
145
|
+
): string {
|
|
146
|
+
const extension = getFileExtension(format);
|
|
147
|
+
if (VECTOR_FORMATS.has(format)) {
|
|
148
|
+
return `qr-code.${extension}`;
|
|
149
|
+
}
|
|
150
|
+
// The canvas size getQRAsCanvas and getQRAsPDF draw on
|
|
151
|
+
const { width, height } = getFrameDimensions(size.width, frame);
|
|
152
|
+
return `qr-code-${Math.round(width)}x${Math.round(height)}.${extension}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// One CSS pixel = one PostScript point (72 per inch).
|
|
156
|
+
const MM_PER_POINT = 25.4 / 72;
|
|
157
|
+
|
|
158
|
+
/** EPS of the code at `size` points wide, as a data URL. */
|
|
159
|
+
async function getQRAsEPSDataUrl(qrProps: QRProps): Promise<string> {
|
|
160
|
+
const eps = await getQRAsEPS(qrProps, {
|
|
161
|
+
sizeMm: (qrProps.size ?? DEFAULT_SIZE) * MM_PER_POINT,
|
|
162
|
+
});
|
|
163
|
+
return epsToDataUrl(eps);
|
|
164
|
+
}
|
|
165
|
+
|
|
101
166
|
/**
|
|
102
167
|
* Convert QR code to PDF format
|
|
103
168
|
*/
|
|
104
169
|
async function getQRAsPDF(qrProps: QRProps): Promise<string> {
|
|
105
|
-
|
|
170
|
+
// jspdf is an optional peer dependency, loaded only when a PDF is requested.
|
|
171
|
+
let jsPDF: typeof import("jspdf").jsPDF;
|
|
172
|
+
try {
|
|
173
|
+
({ jsPDF } = await import("jspdf"));
|
|
174
|
+
} catch (error) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
`PDF export needs the optional "jspdf" package. Install it with \`npm install jspdf\`. (${error})`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
106
179
|
|
|
107
180
|
// Get the SVG as a data URI
|
|
108
181
|
const svgDataUri = await getQRAsSVGDataUri(qrProps);
|
|
@@ -123,14 +196,18 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
|
|
|
123
196
|
throw new Error(`Failed to encode SVG for PDF conversion: ${error}`);
|
|
124
197
|
}
|
|
125
198
|
|
|
199
|
+
const { width, height } = getFrameDimensions(
|
|
200
|
+
qrProps.size || DEFAULT_SIZE,
|
|
201
|
+
qrProps.frame
|
|
202
|
+
);
|
|
203
|
+
|
|
126
204
|
// Get canvas from SVG using base64 encoding
|
|
127
205
|
const canvas = await new Promise<HTMLCanvasElement>((resolve, reject) => {
|
|
128
206
|
const img = new Image();
|
|
129
207
|
const tempCanvas = document.createElement("canvas");
|
|
130
|
-
const size = qrProps.size || DEFAULT_SIZE;
|
|
131
208
|
|
|
132
|
-
tempCanvas.width =
|
|
133
|
-
tempCanvas.height =
|
|
209
|
+
tempCanvas.width = Math.round(width);
|
|
210
|
+
tempCanvas.height = Math.round(height);
|
|
134
211
|
|
|
135
212
|
const ctx = tempCanvas.getContext("2d");
|
|
136
213
|
if (!ctx) {
|
|
@@ -151,7 +228,7 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
|
|
|
151
228
|
img.onload = () => {
|
|
152
229
|
clearTimeout(timeout);
|
|
153
230
|
try {
|
|
154
|
-
ctx.drawImage(img, 0, 0,
|
|
231
|
+
ctx.drawImage(img, 0, 0, tempCanvas.width, tempCanvas.height);
|
|
155
232
|
resolve(tempCanvas);
|
|
156
233
|
} catch (error) {
|
|
157
234
|
clearTimeout(timeout);
|
|
@@ -171,16 +248,17 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
|
|
|
171
248
|
|
|
172
249
|
// Create PDF with the canvas
|
|
173
250
|
const imgData = canvas.toDataURL("image/png", 1.0);
|
|
174
|
-
|
|
175
|
-
const
|
|
251
|
+
// Convert pixels to mm (assuming 96 DPI)
|
|
252
|
+
const mmWidth = width * 0.264_583;
|
|
253
|
+
const mmHeight = height * 0.264_583;
|
|
176
254
|
|
|
177
255
|
const pdf = new jsPDF({
|
|
178
|
-
orientation: "portrait",
|
|
256
|
+
orientation: mmWidth > mmHeight ? "landscape" : "portrait",
|
|
179
257
|
unit: "mm",
|
|
180
|
-
format: [
|
|
258
|
+
format: [mmWidth, mmHeight],
|
|
181
259
|
});
|
|
182
260
|
|
|
183
|
-
pdf.addImage(imgData, "PNG", 0, 0,
|
|
261
|
+
pdf.addImage(imgData, "PNG", 0, 0, mmWidth, mmHeight);
|
|
184
262
|
|
|
185
263
|
canvas.remove();
|
|
186
264
|
|
|
@@ -203,14 +281,11 @@ export async function downloadQRCode(
|
|
|
203
281
|
throw new Error(validation.error);
|
|
204
282
|
}
|
|
205
283
|
|
|
206
|
-
// Create props with the custom size
|
|
207
|
-
const propsWithSize =
|
|
208
|
-
...qrProps,
|
|
209
|
-
size: size.width, // QR codes are square, so we just use width
|
|
210
|
-
};
|
|
284
|
+
// Create props with the custom size (QR codes are square, so use width)
|
|
285
|
+
const propsWithSize = withQRSize(qrProps, size.width);
|
|
211
286
|
|
|
212
|
-
const
|
|
213
|
-
|
|
287
|
+
const finalFilename =
|
|
288
|
+
filename || getDownloadFilename(format, size, qrProps.frame);
|
|
214
289
|
|
|
215
290
|
try {
|
|
216
291
|
if (format === "svg") {
|
|
@@ -221,6 +296,8 @@ export async function downloadQRCode(
|
|
|
221
296
|
// Download as PDF
|
|
222
297
|
const pdfDataUrl = await getQRAsPDF(propsWithSize);
|
|
223
298
|
downloadFile(pdfDataUrl, finalFilename);
|
|
299
|
+
} else if (format === "eps") {
|
|
300
|
+
downloadFile(await getQRAsEPSDataUrl(propsWithSize), finalFilename);
|
|
224
301
|
} else {
|
|
225
302
|
// Download as PNG or JPG
|
|
226
303
|
const mimeType = getMimeType(format);
|
|
@@ -252,10 +329,7 @@ export async function getQRCodeDataUrl(
|
|
|
252
329
|
}
|
|
253
330
|
|
|
254
331
|
// Create props with the custom size
|
|
255
|
-
const propsWithSize =
|
|
256
|
-
...qrProps,
|
|
257
|
-
size: size.width,
|
|
258
|
-
};
|
|
332
|
+
const propsWithSize = withQRSize(qrProps, size.width);
|
|
259
333
|
|
|
260
334
|
if (format === "svg") {
|
|
261
335
|
return await getQRAsSVGDataUri(propsWithSize);
|
|
@@ -265,6 +339,10 @@ export async function getQRCodeDataUrl(
|
|
|
265
339
|
return await getQRAsPDF(propsWithSize);
|
|
266
340
|
}
|
|
267
341
|
|
|
342
|
+
if (format === "eps") {
|
|
343
|
+
return await getQRAsEPSDataUrl(propsWithSize);
|
|
344
|
+
}
|
|
345
|
+
|
|
268
346
|
const mimeType = getMimeType(format);
|
|
269
347
|
const result = await getQRAsCanvas(propsWithSize, mimeType);
|
|
270
348
|
|
|
@@ -289,37 +367,30 @@ export async function copyQRCodeToClipboard(
|
|
|
289
367
|
throw new Error(validation.error);
|
|
290
368
|
}
|
|
291
369
|
|
|
292
|
-
const propsWithSize =
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
370
|
+
const propsWithSize = withQRSize(qrProps, size.width);
|
|
371
|
+
|
|
372
|
+
// Render inside a promise handed straight to ClipboardItem: Safari rejects
|
|
373
|
+
// clipboard writes that start after an await (outside the user gesture).
|
|
374
|
+
const imageBlob = getQRAsCanvas(propsWithSize, "image/png", true).then(
|
|
375
|
+
(result) =>
|
|
376
|
+
new Promise<Blob>((resolve, reject) => {
|
|
377
|
+
const canvas = result as HTMLCanvasElement;
|
|
378
|
+
canvas.toBlob((canvasBlob) => {
|
|
379
|
+
canvas.remove();
|
|
380
|
+
if (canvasBlob) {
|
|
381
|
+
resolve(canvasBlob);
|
|
382
|
+
} else {
|
|
383
|
+
reject(new Error("Failed to create blob from canvas"));
|
|
384
|
+
}
|
|
385
|
+
}, "image/png");
|
|
386
|
+
})
|
|
387
|
+
);
|
|
296
388
|
|
|
297
|
-
// Get canvas
|
|
298
|
-
const canvas = (await getQRAsCanvas(
|
|
299
|
-
propsWithSize,
|
|
300
|
-
"image/png",
|
|
301
|
-
true
|
|
302
|
-
)) as HTMLCanvasElement;
|
|
303
|
-
|
|
304
|
-
// Convert canvas to blob
|
|
305
|
-
const imageBlob = await new Promise<Blob>((resolve, reject) => {
|
|
306
|
-
canvas.toBlob((canvasBlob) => {
|
|
307
|
-
if (canvasBlob) {
|
|
308
|
-
resolve(canvasBlob);
|
|
309
|
-
} else {
|
|
310
|
-
reject(new Error("Failed to create blob from canvas"));
|
|
311
|
-
}
|
|
312
|
-
}, "image/png");
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
// Copy to clipboard
|
|
316
389
|
await navigator.clipboard.write([
|
|
317
390
|
new ClipboardItem({
|
|
318
391
|
"image/png": imageBlob,
|
|
319
392
|
}),
|
|
320
393
|
]);
|
|
321
|
-
|
|
322
|
-
canvas.remove();
|
|
323
394
|
} catch (error) {
|
|
324
395
|
console.error("Error copying to clipboard:", error);
|
|
325
396
|
throw error;
|