-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathindex.ts
52 lines (45 loc) · 1.1 KB
/
index.ts
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
import QRious from 'qrious'
import { type PropType, defineComponent, h, ref, watch } from 'vue'
export const LEVELS = ['L', 'M', 'Q', 'H'] as const
export type Level = (typeof LEVELS)[number]
export default defineComponent({
props: {
value: {
type: String,
required: true,
},
background: String,
backgroundAlpha: Number,
foreground: String,
foregroundAlpha: Number,
level: {
type: String as PropType<Level>,
validator: (level: Level) => LEVELS.includes(level),
},
mime: String,
padding: Number,
size: Number,
},
setup(props, { attrs, emit }) {
const qrious = new QRious(props)
const toDataURL = () => {
try {
const dataUrl = qrious.toDataURL(props.mime)
emit('change', dataUrl)
return dataUrl
} catch (err) {
emit('error', err)
}
}
const dataUrlRef = ref<string | undefined>(toDataURL())
watch(props, () => {
qrious.set(props)
dataUrlRef.value = toDataURL()
})
return () =>
h('img', {
...attrs,
src: dataUrlRef.value,
})
},
})