@vue-solana/vue 4.1.7 → 4.1.9
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/index.mjs +1 -1
- package/dist/shared/vue.D0MQeVy2.mjs +200 -0
- package/dist/useSolanaQrScanner.mjs +1 -1
- package/package.json +3 -2
- package/dist/shared/vue.Btxdm--M.mjs +0 -130
package/dist/index.mjs
CHANGED
|
@@ -18,7 +18,7 @@ export { u as useSignAndSendTransactions } from './shared/vue.sPcsjyKU.mjs';
|
|
|
18
18
|
export { u as useSignatureStatus } from './shared/vue.Bu2cQ690.mjs';
|
|
19
19
|
export { u as useSignIn } from './shared/vue.SeuJipQ8.mjs';
|
|
20
20
|
export { t as tryUseSolana, u as useSolana } from './shared/vue.CcOZ9858.mjs';
|
|
21
|
-
export { u as useSolanaQrScanner } from './shared/vue.
|
|
21
|
+
export { u as useSolanaQrScanner } from './shared/vue.D0MQeVy2.mjs';
|
|
22
22
|
export { u as useSolanaQrCode } from './shared/vue.Bo4nAja_.mjs';
|
|
23
23
|
export { u as useSolanaPay } from './shared/vue.gvgOc5rc.mjs';
|
|
24
24
|
export { u as useSolanaPayConfirmation } from './shared/vue.MN-kcudq.mjs';
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { ref, shallowRef, onUnmounted } from 'vue';
|
|
2
|
+
import { parseURL } from '@vue-solana/core/solana-pay';
|
|
3
|
+
|
|
4
|
+
const DECODE_SIZE = 720;
|
|
5
|
+
async function createQrCodeDetector() {
|
|
6
|
+
const BarcodeDetectorCtor = globalThis.BarcodeDetector;
|
|
7
|
+
if (!BarcodeDetectorCtor) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
const formats = await BarcodeDetectorCtor.getSupportedFormats?.();
|
|
12
|
+
if (formats && !formats.includes("qr_code")) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return new BarcodeDetectorCtor({ formats: ["qr_code"] });
|
|
16
|
+
} catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function loadQrDecoder() {
|
|
21
|
+
try {
|
|
22
|
+
const mod = await import('jsqr');
|
|
23
|
+
const jsQR = mod.default ?? mod;
|
|
24
|
+
if (typeof jsQR !== "function") {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return (data, width, height) => jsQR(data, width, height, { inversionAttempts: "dontInvert" })?.data || null;
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function useSolanaQrScanner() {
|
|
33
|
+
const scanning = ref(false);
|
|
34
|
+
const result = shallowRef(null);
|
|
35
|
+
const parsed = shallowRef(null);
|
|
36
|
+
const error = ref(null);
|
|
37
|
+
let mediaStream = null;
|
|
38
|
+
let videoElement = null;
|
|
39
|
+
let scanTimer = null;
|
|
40
|
+
let barcodeDetector = null;
|
|
41
|
+
let qrDecoder = null;
|
|
42
|
+
let frameCanvas = null;
|
|
43
|
+
let stopped = false;
|
|
44
|
+
function stopTracks() {
|
|
45
|
+
if (mediaStream) {
|
|
46
|
+
for (const track of mediaStream.getTracks()) {
|
|
47
|
+
try {
|
|
48
|
+
track.stop();
|
|
49
|
+
} catch {
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
mediaStream = null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function clearTimer() {
|
|
56
|
+
if (scanTimer) {
|
|
57
|
+
clearTimeout(scanTimer);
|
|
58
|
+
scanTimer = null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function scanFromText(text) {
|
|
62
|
+
try {
|
|
63
|
+
result.value = text;
|
|
64
|
+
parsed.value = parseURL(text);
|
|
65
|
+
error.value = null;
|
|
66
|
+
return parsed.value;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
error.value = e;
|
|
69
|
+
throw e;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function detectWithBrowser(video) {
|
|
73
|
+
if (!barcodeDetector) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const codes = await barcodeDetector.detect(video);
|
|
78
|
+
return codes[0]?.rawValue || codes[0]?.raw || codes[0]?.value || null;
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function decodeFrame(video) {
|
|
84
|
+
const side = Math.min(video.videoWidth, video.videoHeight);
|
|
85
|
+
if (!qrDecoder || !side) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const size = Math.min(side, DECODE_SIZE);
|
|
89
|
+
frameCanvas ??= document.createElement("canvas");
|
|
90
|
+
frameCanvas.width = size;
|
|
91
|
+
frameCanvas.height = size;
|
|
92
|
+
const context = frameCanvas.getContext("2d", { willReadFrequently: true });
|
|
93
|
+
if (!context) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
context.drawImage(
|
|
97
|
+
video,
|
|
98
|
+
(video.videoWidth - side) / 2,
|
|
99
|
+
(video.videoHeight - side) / 2,
|
|
100
|
+
side,
|
|
101
|
+
side,
|
|
102
|
+
0,
|
|
103
|
+
0,
|
|
104
|
+
size,
|
|
105
|
+
size
|
|
106
|
+
);
|
|
107
|
+
return qrDecoder(context.getImageData(0, 0, size, size).data, size, size);
|
|
108
|
+
}
|
|
109
|
+
async function scanFrame() {
|
|
110
|
+
if (!videoElement || videoElement.readyState < 2) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const text = await detectWithBrowser(videoElement) ?? decodeFrame(videoElement);
|
|
114
|
+
if (!text) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
await scanFromText(text);
|
|
119
|
+
return true;
|
|
120
|
+
} catch {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function start(options = {}) {
|
|
125
|
+
if (typeof window === "undefined") {
|
|
126
|
+
const err = new Error("Scanner can only run in the browser");
|
|
127
|
+
error.value = err;
|
|
128
|
+
throw err;
|
|
129
|
+
}
|
|
130
|
+
if (scanning.value) return;
|
|
131
|
+
stopped = false;
|
|
132
|
+
error.value = null;
|
|
133
|
+
videoElement = options.video ?? null;
|
|
134
|
+
const intervalMs = options.scanIntervalMs ?? 500;
|
|
135
|
+
try {
|
|
136
|
+
if (!navigator?.mediaDevices?.getUserMedia) {
|
|
137
|
+
throw new Error("Camera not available");
|
|
138
|
+
}
|
|
139
|
+
[barcodeDetector, qrDecoder] = await Promise.all([createQrCodeDetector(), loadQrDecoder()]);
|
|
140
|
+
if (!barcodeDetector && !qrDecoder) {
|
|
141
|
+
throw new Error("This browser cannot detect QR codes. Paste the payment URL instead.");
|
|
142
|
+
}
|
|
143
|
+
mediaStream = await navigator.mediaDevices.getUserMedia({
|
|
144
|
+
video: { facingMode: "environment", width: { ideal: 1280 }, height: { ideal: 720 } }
|
|
145
|
+
});
|
|
146
|
+
if (videoElement) {
|
|
147
|
+
videoElement.srcObject = mediaStream;
|
|
148
|
+
await videoElement.play().catch(() => {
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
scanning.value = true;
|
|
152
|
+
clearTimer();
|
|
153
|
+
const scheduleScan = () => {
|
|
154
|
+
scanTimer = setTimeout(async () => {
|
|
155
|
+
if (stopped || !scanning.value) return;
|
|
156
|
+
if (await scanFrame()) {
|
|
157
|
+
stop();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (!stopped && scanning.value) {
|
|
161
|
+
scheduleScan();
|
|
162
|
+
}
|
|
163
|
+
}, intervalMs);
|
|
164
|
+
};
|
|
165
|
+
scheduleScan();
|
|
166
|
+
} catch (e) {
|
|
167
|
+
error.value = e;
|
|
168
|
+
stopTracks();
|
|
169
|
+
clearTimer();
|
|
170
|
+
scanning.value = false;
|
|
171
|
+
throw e;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function stop() {
|
|
175
|
+
stopped = true;
|
|
176
|
+
scanning.value = false;
|
|
177
|
+
clearTimer();
|
|
178
|
+
stopTracks();
|
|
179
|
+
if (videoElement) {
|
|
180
|
+
try {
|
|
181
|
+
videoElement.srcObject = null;
|
|
182
|
+
} catch {
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function reset() {
|
|
187
|
+
stop();
|
|
188
|
+
result.value = null;
|
|
189
|
+
parsed.value = null;
|
|
190
|
+
error.value = null;
|
|
191
|
+
barcodeDetector = null;
|
|
192
|
+
videoElement = null;
|
|
193
|
+
}
|
|
194
|
+
onUnmounted(() => {
|
|
195
|
+
stop();
|
|
196
|
+
});
|
|
197
|
+
return { scanning, result, parsed, error, scanFromText, start, stop, reset };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export { useSolanaQrScanner as u };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { u as useSolanaQrScanner } from './shared/vue.
|
|
1
|
+
export { u as useSolanaQrScanner } from './shared/vue.D0MQeVy2.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vue-solana/vue",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.9",
|
|
4
4
|
"description": "Vue plugin and composables for Solana applications.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -194,7 +194,8 @@
|
|
|
194
194
|
"access": "public"
|
|
195
195
|
},
|
|
196
196
|
"dependencies": {
|
|
197
|
-
"
|
|
197
|
+
"jsqr": "^1.4.0",
|
|
198
|
+
"@vue-solana/core": "4.1.9"
|
|
198
199
|
},
|
|
199
200
|
"peerDependencies": {
|
|
200
201
|
"vue": "^3.5.0"
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { ref, shallowRef, onUnmounted } from 'vue';
|
|
2
|
-
import { parseURL } from '@vue-solana/core/solana-pay';
|
|
3
|
-
|
|
4
|
-
function useSolanaQrScanner() {
|
|
5
|
-
const scanning = ref(false);
|
|
6
|
-
const result = shallowRef(null);
|
|
7
|
-
const parsed = shallowRef(null);
|
|
8
|
-
const error = ref(null);
|
|
9
|
-
let mediaStream = null;
|
|
10
|
-
let videoElement = null;
|
|
11
|
-
let scanInterval = null;
|
|
12
|
-
let barcodeDetector = null;
|
|
13
|
-
let stopped = false;
|
|
14
|
-
function stopTracks() {
|
|
15
|
-
if (mediaStream) {
|
|
16
|
-
for (const track of mediaStream.getTracks()) {
|
|
17
|
-
try {
|
|
18
|
-
track.stop();
|
|
19
|
-
} catch {
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
mediaStream = null;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function clearTimer() {
|
|
26
|
-
if (scanInterval) {
|
|
27
|
-
clearInterval(scanInterval);
|
|
28
|
-
scanInterval = null;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
async function scanFromText(text) {
|
|
32
|
-
try {
|
|
33
|
-
result.value = text;
|
|
34
|
-
parsed.value = parseURL(text);
|
|
35
|
-
error.value = null;
|
|
36
|
-
return parsed.value;
|
|
37
|
-
} catch (e) {
|
|
38
|
-
error.value = e;
|
|
39
|
-
throw e;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
async function start(options = {}) {
|
|
43
|
-
if (typeof window === "undefined") {
|
|
44
|
-
const err = new Error("Scanner can only run in the browser");
|
|
45
|
-
error.value = err;
|
|
46
|
-
throw err;
|
|
47
|
-
}
|
|
48
|
-
if (scanning.value) return;
|
|
49
|
-
stopped = false;
|
|
50
|
-
error.value = null;
|
|
51
|
-
videoElement = options.video ?? null;
|
|
52
|
-
const intervalMs = options.scanIntervalMs ?? 500;
|
|
53
|
-
try {
|
|
54
|
-
const BarcodeDetectorCtor = globalThis.BarcodeDetector;
|
|
55
|
-
if (BarcodeDetectorCtor) {
|
|
56
|
-
try {
|
|
57
|
-
barcodeDetector = new BarcodeDetectorCtor({
|
|
58
|
-
formats: ["qr_code"]
|
|
59
|
-
});
|
|
60
|
-
} catch {
|
|
61
|
-
barcodeDetector = null;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (!navigator?.mediaDevices?.getUserMedia) {
|
|
65
|
-
throw new Error("Camera not available");
|
|
66
|
-
}
|
|
67
|
-
mediaStream = await navigator.mediaDevices.getUserMedia({
|
|
68
|
-
video: { facingMode: "environment" }
|
|
69
|
-
});
|
|
70
|
-
if (videoElement) {
|
|
71
|
-
videoElement.srcObject = mediaStream;
|
|
72
|
-
await videoElement.play().catch(() => {
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
scanning.value = true;
|
|
76
|
-
clearTimer();
|
|
77
|
-
scanInterval = setInterval(async () => {
|
|
78
|
-
if (stopped || !scanning.value) return;
|
|
79
|
-
try {
|
|
80
|
-
if (barcodeDetector && videoElement) {
|
|
81
|
-
const codes = await barcodeDetector.detect(videoElement);
|
|
82
|
-
if (codes && codes.length > 0) {
|
|
83
|
-
const text = codes[0]?.rawValue || codes[0]?.raw || codes[0]?.value;
|
|
84
|
-
if (text) {
|
|
85
|
-
try {
|
|
86
|
-
await scanFromText(text);
|
|
87
|
-
} catch {
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
} catch (e) {
|
|
93
|
-
error.value = e;
|
|
94
|
-
}
|
|
95
|
-
}, intervalMs);
|
|
96
|
-
} catch (e) {
|
|
97
|
-
error.value = e;
|
|
98
|
-
stopTracks();
|
|
99
|
-
clearTimer();
|
|
100
|
-
scanning.value = false;
|
|
101
|
-
throw e;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
function stop() {
|
|
105
|
-
stopped = true;
|
|
106
|
-
scanning.value = false;
|
|
107
|
-
clearTimer();
|
|
108
|
-
stopTracks();
|
|
109
|
-
if (videoElement) {
|
|
110
|
-
try {
|
|
111
|
-
videoElement.srcObject = null;
|
|
112
|
-
} catch {
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function reset() {
|
|
117
|
-
stop();
|
|
118
|
-
result.value = null;
|
|
119
|
-
parsed.value = null;
|
|
120
|
-
error.value = null;
|
|
121
|
-
barcodeDetector = null;
|
|
122
|
-
videoElement = null;
|
|
123
|
-
}
|
|
124
|
-
onUnmounted(() => {
|
|
125
|
-
stop();
|
|
126
|
-
});
|
|
127
|
-
return { scanning, result, parsed, error, scanFromText, start, stop, reset };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export { useSolanaQrScanner as u };
|