qrdx 0.0.2-beta.2 → 0.0.42
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/dist/cli.js +4231 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/constants.d.ts +5 -3
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts +1 -1
- 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 +1 -1
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/utils/color.d.ts +12 -0
- package/dist/src/utils/color.d.ts.map +1 -0
- package/dist/src/utils.d.ts +1 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/types/color.d.ts +92 -0
- package/dist/types/color.d.ts.map +1 -0
- package/dist/types/corner-dot.d.ts +9 -2
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +7 -2
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/image-pattern.d.ts +4 -3
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/qr.d.ts +90 -12
- package/dist/types/qr.d.ts.map +1 -1
- package/package.json +22 -17
- package/src/api.tsx +7 -2
- package/src/constants.ts +5 -3
- package/src/corner-dot.tsx +83 -2
- package/src/corner-eye.ts +170 -23
- package/src/detection.ts +0 -3
- package/src/download.tsx +102 -2
- package/src/index.tsx +95 -29
- package/src/utils/color.ts +34 -0
- package/src/utils.tsx +557 -64
- package/types/color.ts +133 -0
- package/types/corner-dot.ts +8 -1
- package/types/corner-eye.ts +5 -0
- package/types/image-pattern.ts +2 -1
- package/types/index.ts +24 -5
- package/types/qr.ts +9 -8
package/src/index.tsx
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { type JSX, useEffect, useRef, useState } from "react";
|
|
14
14
|
import type { QRProps, QRPropsCanvas } from "../types";
|
|
15
|
+
import { normalizeColorConfig } from "../types/color";
|
|
15
16
|
import qrcodegen from "./codegen";
|
|
16
17
|
import {
|
|
17
18
|
DEFAULT_BGCOLOR,
|
|
@@ -104,10 +105,20 @@ export function QRCodeCanvas(props: QRPropsCanvas) {
|
|
|
104
105
|
ctx.scale(scale, scale);
|
|
105
106
|
|
|
106
107
|
// Draw solid background, only paint dark modules.
|
|
107
|
-
|
|
108
|
+
// Extract solid color from bgColor (canvas doesn't support gradient objects)
|
|
109
|
+
const bgColorNormalized = normalizeColorConfig(bgColor, DEFAULT_BGCOLOR);
|
|
110
|
+
ctx.fillStyle =
|
|
111
|
+
bgColorNormalized.type === "solid"
|
|
112
|
+
? bgColorNormalized.color
|
|
113
|
+
: bgColorNormalized.stops[0]?.color || DEFAULT_BGCOLOR;
|
|
108
114
|
ctx.fillRect(0, 0, numCells, numCells);
|
|
109
115
|
|
|
110
|
-
|
|
116
|
+
// Extract solid color from fgColor (canvas doesn't support gradient objects)
|
|
117
|
+
const fgColorNormalized = normalizeColorConfig(fgColor, DEFAULT_FGCOLOR);
|
|
118
|
+
ctx.fillStyle =
|
|
119
|
+
fgColorNormalized.type === "solid"
|
|
120
|
+
? fgColorNormalized.color
|
|
121
|
+
: fgColorNormalized.stops[0]?.color || DEFAULT_FGCOLOR;
|
|
111
122
|
if (SUPPORTS_PATH2D) {
|
|
112
123
|
// $FlowFixMe: Path2D c'tor doesn't support args yet.
|
|
113
124
|
ctx.fill(new Path2D(generatePath(cells, margin)));
|
|
@@ -183,9 +194,11 @@ export async function getQRAsSVGDataUri(props: QRProps) {
|
|
|
183
194
|
src: base64Image,
|
|
184
195
|
},
|
|
185
196
|
};
|
|
186
|
-
} catch (
|
|
187
|
-
|
|
188
|
-
//
|
|
197
|
+
} catch (_error) {
|
|
198
|
+
// Remove the image from settings if it fails to load
|
|
199
|
+
// This prevents SVG from containing external URLs that will fail during canvas conversion
|
|
200
|
+
const { imageSettings: _removed, ...propsWithoutImage } = props;
|
|
201
|
+
updatedProps = propsWithoutImage;
|
|
189
202
|
}
|
|
190
203
|
}
|
|
191
204
|
|
|
@@ -204,6 +217,7 @@ export async function getQRAsSVGDataUri(props: QRProps) {
|
|
|
204
217
|
templateId: updatedProps.templateId, // Ensure templateId is passed
|
|
205
218
|
customTemplate: updatedProps.customTemplate, // Ensure customTemplate is passed
|
|
206
219
|
});
|
|
220
|
+
|
|
207
221
|
const svgString = renderToStaticMarkup(svgElement);
|
|
208
222
|
|
|
209
223
|
return `data:image/svg+xml,${encodeURIComponent(svgString)}`;
|
|
@@ -211,26 +225,47 @@ export async function getQRAsSVGDataUri(props: QRProps) {
|
|
|
211
225
|
|
|
212
226
|
const getBase64Image = (imgUrl: string) =>
|
|
213
227
|
new Promise((resolve, reject) => {
|
|
228
|
+
// If already a data URL, return as is
|
|
229
|
+
if (imgUrl.startsWith("data:")) {
|
|
230
|
+
resolve(imgUrl);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
214
234
|
const img = new Image();
|
|
215
|
-
|
|
216
|
-
img.
|
|
235
|
+
// Set crossOrigin before setting src
|
|
236
|
+
img.crossOrigin = "anonymous";
|
|
217
237
|
|
|
218
238
|
img.onload = () => {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
239
|
+
try {
|
|
240
|
+
const canvas = document.createElement("canvas");
|
|
241
|
+
canvas.width = img.naturalWidth || img.width;
|
|
242
|
+
canvas.height = img.naturalHeight || img.height;
|
|
243
|
+
|
|
244
|
+
const ctx = canvas.getContext("2d");
|
|
245
|
+
if (!ctx) {
|
|
246
|
+
reject(new Error("Could not get canvas context"));
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
223
249
|
|
|
224
|
-
|
|
225
|
-
|
|
250
|
+
ctx.drawImage(img, 0, 0);
|
|
251
|
+
const dataURL = canvas.toDataURL("image/png");
|
|
226
252
|
|
|
227
|
-
|
|
228
|
-
|
|
253
|
+
canvas.remove();
|
|
254
|
+
resolve(dataURL);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
reject(new Error(`Failed to convert image to base64: ${error}`));
|
|
257
|
+
}
|
|
229
258
|
};
|
|
230
259
|
|
|
231
260
|
img.onerror = () => {
|
|
232
|
-
reject(
|
|
261
|
+
reject(
|
|
262
|
+
new Error(
|
|
263
|
+
`Could not load image from ${imgUrl.substring(0, 100)}... Check CORS settings or use a data URL.`
|
|
264
|
+
)
|
|
265
|
+
);
|
|
233
266
|
};
|
|
267
|
+
|
|
268
|
+
img.src = imgUrl;
|
|
234
269
|
});
|
|
235
270
|
|
|
236
271
|
// function waitUntilImageLoaded(img: HTMLImageElement, src: string) {
|
|
@@ -258,25 +293,56 @@ export async function getQRAsCanvas(
|
|
|
258
293
|
svgDataUri.replace("data:image/svg+xml,", "")
|
|
259
294
|
);
|
|
260
295
|
|
|
296
|
+
// Convert SVG to base64 for better compatibility
|
|
297
|
+
let base64DataUri: string;
|
|
298
|
+
try {
|
|
299
|
+
// Use modern approach instead of deprecated unescape
|
|
300
|
+
const utf8Bytes = new TextEncoder().encode(svgString);
|
|
301
|
+
const binaryString = Array.from(utf8Bytes, (byte) =>
|
|
302
|
+
String.fromCharCode(byte)
|
|
303
|
+
).join("");
|
|
304
|
+
const base64SVG = btoa(binaryString);
|
|
305
|
+
base64DataUri = `data:image/svg+xml;base64,${base64SVG}`;
|
|
306
|
+
} catch (error) {
|
|
307
|
+
throw new Error(`Failed to encode SVG for canvas conversion: ${error}`);
|
|
308
|
+
}
|
|
309
|
+
|
|
261
310
|
return new Promise((resolve, reject) => {
|
|
262
311
|
const canvas = document.createElement("canvas");
|
|
263
312
|
const ctx = canvas.getContext("2d");
|
|
313
|
+
|
|
314
|
+
if (!ctx) {
|
|
315
|
+
reject(new Error("Could not get canvas context"));
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
264
319
|
const img = new Image();
|
|
265
320
|
|
|
266
321
|
// Set canvas size to match the QR code
|
|
267
|
-
|
|
268
|
-
canvas.
|
|
322
|
+
const size = props.size || DEFAULT_SIZE;
|
|
323
|
+
canvas.width = size;
|
|
324
|
+
canvas.height = size;
|
|
325
|
+
|
|
326
|
+
// Add timeout to prevent hanging
|
|
327
|
+
const timeout = setTimeout(() => {
|
|
328
|
+
canvas.remove();
|
|
329
|
+
reject(
|
|
330
|
+
new Error(
|
|
331
|
+
"SVG load timeout - image may be too large or contain invalid data"
|
|
332
|
+
)
|
|
333
|
+
);
|
|
334
|
+
}, 30_000); // 30 second timeout
|
|
269
335
|
|
|
270
336
|
img.onload = () => {
|
|
271
|
-
|
|
272
|
-
|
|
337
|
+
clearTimeout(timeout);
|
|
338
|
+
try {
|
|
273
339
|
// Fill white background for JPG
|
|
274
340
|
if (type === "image/jpeg") {
|
|
275
341
|
ctx.fillStyle = "white";
|
|
276
342
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
277
343
|
}
|
|
278
344
|
|
|
279
|
-
ctx.drawImage(img, 0, 0);
|
|
345
|
+
ctx.drawImage(img, 0, 0, size, size);
|
|
280
346
|
|
|
281
347
|
if (getCanvas) {
|
|
282
348
|
resolve(canvas);
|
|
@@ -285,21 +351,21 @@ export async function getQRAsCanvas(
|
|
|
285
351
|
canvas.remove();
|
|
286
352
|
resolve(dataUrl);
|
|
287
353
|
}
|
|
288
|
-
}
|
|
289
|
-
|
|
354
|
+
} catch (error) {
|
|
355
|
+
clearTimeout(timeout);
|
|
356
|
+
canvas.remove();
|
|
357
|
+
reject(new Error(`Failed to draw SVG to canvas: ${error}`));
|
|
290
358
|
}
|
|
291
359
|
};
|
|
292
360
|
|
|
293
361
|
img.onerror = () => {
|
|
294
|
-
|
|
362
|
+
clearTimeout(timeout);
|
|
363
|
+
canvas.remove();
|
|
295
364
|
reject(new Error("Could not load SVG"));
|
|
296
365
|
};
|
|
297
366
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
});
|
|
301
|
-
const url = URL.createObjectURL(svgBlob);
|
|
302
|
-
img.src = url;
|
|
367
|
+
// Use base64-encoded data URI for maximum compatibility
|
|
368
|
+
img.src = base64DataUri;
|
|
303
369
|
});
|
|
304
370
|
}
|
|
305
371
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ColorConfig } from "../../types/color";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get solid color representation from ColorConfig
|
|
5
|
+
* For gradients, returns the first stop color as representative
|
|
6
|
+
* This is useful for cases where you need a single color value (e.g., CSS backgrounds, color displays)
|
|
7
|
+
*
|
|
8
|
+
* @param colorConfig - The color configuration (can be string, solid, linear, or radial gradient)
|
|
9
|
+
* @param fallback - Fallback color if colorConfig is undefined or invalid
|
|
10
|
+
* @returns A solid color hex string
|
|
11
|
+
*/
|
|
12
|
+
export function getSolidColor(
|
|
13
|
+
colorConfig: ColorConfig | undefined,
|
|
14
|
+
fallback = "#000000"
|
|
15
|
+
): string {
|
|
16
|
+
if (!colorConfig) {
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof colorConfig === "string") {
|
|
21
|
+
return colorConfig;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (colorConfig.type === "solid") {
|
|
25
|
+
return colorConfig.color;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// For gradients, return the first stop color as representative
|
|
29
|
+
if (colorConfig.type === "linear" || colorConfig.type === "radial") {
|
|
30
|
+
return colorConfig.stops[0]?.color || fallback;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return fallback;
|
|
34
|
+
}
|