-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefinition.vue
More file actions
148 lines (122 loc) · 3.52 KB
/
Copy pathDefinition.vue
File metadata and controls
148 lines (122 loc) · 3.52 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<template>
<div
class="rounded-md px-6 py-7 relative flex items-start space-x-8 bg-white dark:bg-gray-900 text-gray-800 dark:text-gray-100"
>
<!-- ICON
-->
<div>
<translate-button class="" v-model="showTranslate" />
</div>
<section class="flex-1">
<p class="font-medium text-4xl" :dir="dir">{{ definition }}</p>
<Divider class="my-5" />
<p class="italic text-2xl mt-3" :dir="dir">{{ example }}</p>
<Divider class="my-10" v-if="antonyms.length || synonyms.length" />
<div v-if="antonyms.length">
<p class="text-2xl">
<span class="font-bold">Antonyms: </span><span>{{ antonyms }}</span>
</p>
</div>
<div v-if="synonyms.length">
<p class="text-2xl">
<span class="font-bold">Synonyms: </span><span>{{ synonyms }}</span>
</p>
</div>
</section>
</div>
</template>
<script lang="ts">
import { defineComponent, PropType } from "@vue/runtime-core";
import { getDir, rtls } from "../../common/helper/text";
import { CLOSE_ICON, TRANSLATE_ICON } from "../../common/icons/icons";
import { TranslateService } from "../../common/services/translate.service";
import { Definition } from "../../common/types/dictionaryapi.type";
import { analytic } from "../../plugins/mixpanel";
import Divider from "../../common/components/Divider.vue";
export default defineComponent({
components: {
Divider,
},
props: {
data: { type: Object as PropType<Definition>, required: true },
},
data() {
return {
sourceLanguage: "en",
showTranslate: false,
translatedDefinition: "",
translatedExample: "",
};
},
watch: {
showTranslate(value) {
if (value == true && !this.translatedDefinition.length) {
this.translate();
}
},
},
computed: {
translateIcon() {
return TRANSLATE_ICON;
},
closeIcon() {
return CLOSE_ICON;
},
definition() {
if (this.showTranslate && this.translatedDefinition.length) {
return this.translatedDefinition;
} else return this.data?.definition;
},
example() {
if (this.showTranslate && this.translatedExample.length) {
return this.translatedExample;
} else return this.data?.example;
},
antonyms() {
let antonyms = "";
if (this.data?.antonyms && this.data?.antonyms.length) {
antonyms = this.data?.antonyms.join(", ");
}
return antonyms;
},
synonyms() {
let synonyms = "";
if (this.data?.synonyms && this.data?.synonyms.length) {
synonyms = this.data?.synonyms.join(", ");
}
return synonyms;
},
dir() {
if (this.showTranslate && this.translatedDefinition.length) {
return getDir();
} else return this.sourceDir;
},
sourceDir() {
let dir = rtls.indexOf(this.sourceLanguage) != -1 ? "rtl" : "ltr";
return dir;
},
},
methods: {
translate() {
let lines = [this.data?.definition, this.data?.example];
TranslateService.instance
.fetchSimpleTranslation(lines)
.then(({ list, lang }: any) => {
this.sourceLanguage = lang;
lines.forEach((result, i) => {
// Translated line
if (i == 0) {
this.translatedDefinition = list[i];
}
// Translated Word
else {
this.translatedExample = list[i] || "";
}
});
});
analytic.track("definition_translated");
},
},
});
</script>
<style></style>