|
18 | 18 | </div> |
19 | 19 | </template> |
20 | 20 |
|
21 | | -<script> |
| 21 | +<script setup lang="ts"> |
22 | 22 | import { t } from '@nextcloud/l10n' |
23 | 23 |
|
24 | 24 | import axios from '@nextcloud/axios' |
25 | 25 | import { getCapabilities } from '@nextcloud/capabilities' |
| 26 | +import { onMounted, ref, watch } from 'vue' |
26 | 27 |
|
27 | 28 | import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon' |
28 | 29 |
|
29 | | -export default { |
| 30 | +type AxiosImageResponse = { |
| 31 | + data: ArrayBuffer | string |
| 32 | + headers: { |
| 33 | + 'content-type': string |
| 34 | + } |
| 35 | +} |
| 36 | +
|
| 37 | +type AxiosConfig = { |
| 38 | + url: string |
| 39 | + method: 'get' |
| 40 | + responseType: 'arraybuffer' |
| 41 | + headers?: Record<string, string> |
| 42 | +} |
| 43 | +
|
| 44 | +defineOptions({ |
30 | 45 | name: 'PreviewSignature', |
31 | | - emits: ['loaded'], |
32 | | - components: { |
33 | | - NcLoadingIcon, |
34 | | - }, |
35 | | - props: { |
36 | | - src: { |
37 | | - type: String, |
38 | | - default: () => '', |
39 | | - required: true, |
40 | | - }, |
41 | | - signRequestUuid: { |
42 | | - type: String, |
43 | | - required: false, |
44 | | - default: '', |
45 | | - }, |
46 | | - alt: { |
47 | | - type: String, |
48 | | - required: false, |
49 | | - // TRANSLATORS Alt text for an image showing the user's handwritten, typed, or uploaded signature. Used as fallback when the parent component does not pass a more specific description, for example "Current signature" or "Confirm your initials". |
50 | | - default: () => t('libresign', 'Signature preview'), |
51 | | - }, |
52 | | - }, |
53 | | - data() { |
54 | | - return { |
55 | | - loading: true, |
56 | | - isLoaded: false, |
57 | | - imageData: '', |
58 | | - width: getCapabilities().libresign.config['sign-elements'].width, |
59 | | - height: getCapabilities().libresign.config['sign-elements'].height, |
| 46 | +}) |
| 47 | +
|
| 48 | +const props = withDefaults(defineProps<{ |
| 49 | + src: string |
| 50 | + signRequestUuid?: string |
| 51 | + alt?: string |
| 52 | +}>(), { |
| 53 | + signRequestUuid: '', |
| 54 | + alt: () => t('libresign', 'Signature preview'), |
| 55 | +}) |
| 56 | +
|
| 57 | +const emit = defineEmits<{ |
| 58 | + (e: 'loaded', status: boolean | Event): void |
| 59 | +}>() |
| 60 | +
|
| 61 | +const loading = ref(true) |
| 62 | +const isLoaded = ref(false) |
| 63 | +const imageData = ref('') |
| 64 | +const width = ref(getCapabilities().libresign.config['sign-elements'].width) |
| 65 | +const height = ref(getCapabilities().libresign.config['sign-elements'].height) |
| 66 | +
|
| 67 | +async function loadImage() { |
| 68 | + if (props.src.startsWith('data:')) { |
| 69 | + imageData.value = props.src |
| 70 | + return |
| 71 | + } |
| 72 | +
|
| 73 | + const config: AxiosConfig = { |
| 74 | + url: props.src, |
| 75 | + method: 'get', |
| 76 | + responseType: 'arraybuffer', |
| 77 | + } |
| 78 | + if (props.signRequestUuid !== '') { |
| 79 | + config.headers = { |
| 80 | + 'libresign-sign-request-uuid': props.signRequestUuid, |
60 | 81 | } |
61 | | - }, |
62 | | - watch: { |
63 | | - src() { |
64 | | - this.loadImage() |
65 | | - }, |
66 | | - }, |
67 | | - mounted() { |
68 | | - this.loadImage() |
69 | | - }, |
70 | | - methods: { |
71 | | - t, |
72 | | - async loadImage() { |
73 | | - if (this.src.startsWith('data:')) { |
74 | | - this.imageData = this.src |
75 | | - return |
76 | | - } |
77 | | - const config = { |
78 | | - url: this.src, |
79 | | - method: 'get', |
80 | | - responseType: 'arraybuffer', |
81 | | - } |
82 | | - if (this.signRequestUuid !== '') { |
83 | | - config.headers = { |
84 | | - 'libresign-sign-request-uuid': this.signRequestUuid, |
85 | | - } |
86 | | - } |
87 | | - await axios(config) |
88 | | - .then(response => { |
89 | | - const buffer = Buffer.from(response.data, 'binary').toString('base64') |
90 | | - this.imageData = 'data:' + response.headers['content-type'] + ';base64,' + buffer |
91 | | - this.onImageLoad(true) |
92 | | - }) |
93 | | - .catch(() => this.onImageLoad(false)) |
94 | | - }, |
95 | | - onImageLoad(status) { |
96 | | - this.loading = false |
97 | | - this.isLoaded = true |
98 | | - this.$emit('loaded', status) |
99 | | - }, |
100 | | - }, |
| 82 | + } |
| 83 | +
|
| 84 | + try { |
| 85 | + const response = await axios(config) as AxiosImageResponse |
| 86 | + const buffer = Buffer.from(response.data, 'binary').toString('base64') |
| 87 | + imageData.value = `data:${response.headers['content-type']};base64,${buffer}` |
| 88 | + onImageLoad(true) |
| 89 | + } catch { |
| 90 | + onImageLoad(false) |
| 91 | + } |
101 | 92 | } |
| 93 | +
|
| 94 | +function onImageLoad(status: boolean | Event) { |
| 95 | + loading.value = false |
| 96 | + isLoaded.value = true |
| 97 | + emit('loaded', status) |
| 98 | +} |
| 99 | +
|
| 100 | +watch(() => props.src, () => { |
| 101 | + void loadImage() |
| 102 | +}) |
| 103 | +
|
| 104 | +onMounted(() => { |
| 105 | + void loadImage() |
| 106 | +}) |
| 107 | +
|
| 108 | +defineExpose({ |
| 109 | + t, |
| 110 | + loading, |
| 111 | + isLoaded, |
| 112 | + imageData, |
| 113 | + width, |
| 114 | + height, |
| 115 | + loadImage, |
| 116 | + onImageLoad, |
| 117 | +}) |
102 | 118 | </script> |
103 | 119 |
|
104 | 120 | <style lang="scss" scoped> |
|
0 commit comments