forked from tng10/vue3-signature-pad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.vue
87 lines (77 loc) · 2.26 KB
/
serve.vue
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
<script lang="ts">
import { defineComponent, reactive, ref, Ref, toRefs } from 'vue';
import { ImageTypesEnum, SaveOutputsEnum } from '@/utils';
import { ISignaturePad, SignaturePadSavedSignatureDataURL, SignaturePadSavedSignatureFile } from '@/types';
import SignaturePad from '@/signature-pad.vue';
export default defineComponent({
name: 'ServeDev',
components: {
SignaturePad
},
setup() {
// state
const state = reactive({
customStyle: { border: "black 3px solid" },
saveType: ImageTypesEnum.SVG,
saveOutput: SaveOutputsEnum.FILE,
});
const signatureDataURL: Ref<string | null> = ref(null);
const signatureFile: Ref<File | null> = ref(null);
const signaturePad: Ref<ISignaturePad | null> = ref(null);
// methods
function getSignaturePad(): ISignaturePad {
if (!signaturePad.value) {
throw new Error("No signature pad ref could be found");
}
return signaturePad.value;
}
function clearSignature(): void {
getSignaturePad().clearSignature();
}
function saveSignature(): SignaturePadSavedSignatureDataURL | SignaturePadSavedSignatureFile {
const signature: SignaturePadSavedSignatureDataURL | SignaturePadSavedSignatureFile = getSignaturePad().saveSignature();
return signature;
}
function onInput(value: string | File | null) {
console.log('calling on input', value)
if (!value) {
signatureDataURL.value = null;
signatureFile.value = null;
} else if (value instanceof File) {
signatureDataURL.value = null;
signatureFile.value = value
} else {
signatureDataURL.value = value
signatureFile.value = null;
}
}
return {
// state
...toRefs(state),
signaturePad,
signatureDataURL,
signatureFile,
// methods
clearSignature,
saveSignature,
onInput,
};
},
});
</script>
<template>
<div id="app">
<signature-pad
:modelValue="signatureFile"
@input="onInput"
:width="500"
:height="300"
:customStyle="customStyle"
:saveType="saveType"
:saveOutput="saveOutput"
ref="signaturePad"
>
</signature-pad>
<button @click="saveSignature">Save signature</button>
</div>
</template>