forked from Easterok/telegram-onboarding-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sticker.vue
95 lines (78 loc) · 1.99 KB
/
Sticker.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
88
89
90
91
92
93
94
95
<template>
<div v-bind="props" :class="$style.container">
<component
v-if="loaded"
v-bind="props"
:is="TelegramSticker"
:json="loaded"
:class="$style.sticker"
/>
</div>
</template>
<script setup lang="ts">
import { computed, defineAsyncComponent, ref, toRefs, watch } from 'vue';
import type { StickerPresetProps } from './Media.preset.props';
// We have to do it this way because Lottie-web's bundled size is around 80kb
const TelegramSticker = defineAsyncComponent(() =>
import('@tok/telegram-ui/components/Sticker').then((m) => m.Sticker)
);
const props = withDefaults(defineProps<StickerPresetProps>(), {
src: null,
});
const { src, size } = toRefs(props);
const loaded = ref<TelegramStickerJson | undefined>();
const computedSize = computed(() => {
const _size = size?.value;
if (!_size) {
return {
width: '100%',
height: '100%',
};
}
if (typeof _size === 'number') {
return {
width: `${_size}px`,
height: `${_size}px`,
};
}
return {
width: `${_size[0]}px`,
height: `${_size[1]}px`,
};
});
const width = computed(() => computedSize.value.width);
const height = computed(() => computedSize.value.height);
const loadStickerJson = (loader: Promise<typeof import('*.tgs')>) => {
loader.then((m) => {
loaded.value = (m as { default: TelegramStickerJson }).default;
});
};
watch(
src,
(value) => {
if (value && typeof value === 'string') {
console.error(
'Sticker Preset: The sticker src is string. You need to use import like that: `import("./assets/.tgs")`. Otherwise it won\'t loaded'
);
return;
}
if (value && typeof value === 'object' && 'then' in value) {
loadStickerJson(value);
}
},
{ immediate: true }
);
</script>
<style lang="scss" module>
.container {
height: v-bind(height);
aspect-ratio: 1/1;
max-height: 40vw;
}
.sticker {
width: v-bind(width);
height: v-bind(height);
max-height: inherit;
margin: 0 auto;
}
</style>