|
8 | 8 | <div class="accordion__form">
|
9 | 9 | <div v-for="(words, index) of currentSortWords" :key="words.english" class="accordion__item">
|
10 | 10 | <label :for="'inputElement' + index">{{ words.russian }} - </label>
|
11 |
| - <input :id="'inputElement' + index" :ref="'inputInfo' + index" type="text" @keyup.enter="checkWord(words, index)" /> |
| 11 | + <input :id="'inputElement' + index" :ref="el => { if (el) inputsInfo[index] = el }" type="text" @keyup.enter="checkWord(words, index)" /> |
12 | 12 | <button v-if="!doneWords.some(wordItem => wordItem.translated == words.english && wordItem.original == words.russian)" @click="checkWord(words, index)">
|
13 | 13 | <img src="@/assets/check.png" />
|
14 | 14 | </button>
|
|
30 | 30 | Перевернуть гармошку
|
31 | 31 | </button>
|
32 | 32 | <router-link :to="{ name: 'Account' }">
|
33 |
| - <button class="accordion__send profile__run" @click="rotateWords"> |
| 33 | + <button class="accordion__send profile__run"> |
34 | 34 | Завершить
|
35 | 35 | </button>
|
36 | 36 | </router-link>
|
|
40 | 40 | </div>
|
41 | 41 | </template>
|
42 | 42 |
|
43 |
| -<script> |
| 43 | +<script lang="ts"> |
44 | 44 | import "./scss/accordion/Accordion.scss"
|
| 45 | + import { SpeechSythesis } from "@/models/speechSythesis" |
| 46 | + import { doneWords } from "./types/accordion.types" |
| 47 | + import { computed, defineComponent, ref } from "vue" |
| 48 | + import { WordInterface } from "@/models/words" |
45 | 49 |
|
46 |
| - export default { |
| 50 | + // isRotate - true - en | !isRotate - false - ru |
| 51 | + export default defineComponent({ |
47 | 52 | name: "Accordion",
|
48 | 53 | props: {
|
49 |
| - currentWords: Array, |
| 54 | + currentWords: Array as WordInterface[] | any |
50 | 55 | },
|
51 |
| - data() { |
52 |
| - return { |
53 |
| - currentInputWord: "", |
54 |
| - doneWords: [], |
55 |
| - errorWords: [], |
56 |
| - isAnswer: [], |
57 |
| - checkedAnswers: [], |
58 |
| - isRotate: false, |
59 |
| - } |
60 |
| - }, |
61 |
| - computed: { |
62 |
| - currentSortWords() { |
63 |
| - const currentWords = [...this.currentWords] |
64 |
| - if (!this.isRotate) { |
65 |
| - currentWords.map(word => { |
| 56 | + setup(props) { |
| 57 | + const currentInputWord = ref("") |
| 58 | + const doneWords = ref<doneWords[]>([]) |
| 59 | + const errorWords = ref([]) |
| 60 | + const isAnswer = ref<string[]>([]) |
| 61 | + const checkedAnswers = ref([]) |
| 62 | + const isRotate = ref(false) |
| 63 | + const inputsInfo = ref([]) |
| 64 | +
|
| 65 | + const currentSortWords = computed(() => { |
| 66 | + const currentWords: WordInterface[] = [...props.currentWords] |
| 67 | + if (!isRotate.value) { |
| 68 | + currentWords.map((word: any) => { |
66 | 69 | const tempRus = word.russian
|
67 | 70 | word.russian = word.english
|
68 | 71 | word.english = tempRus
|
69 | 72 | })
|
70 | 73 | } else {
|
71 |
| - currentWords.map(word => { |
| 74 | + currentWords.map((word: any) => { |
72 | 75 | const tempEn = word.english
|
73 | 76 | word.english = word.russian
|
74 | 77 | word.russian = tempEn
|
75 | 78 | })
|
76 | 79 | }
|
77 | 80 | return currentWords.sort(() => Math.random() - 0.5).reverse()
|
78 |
| - }, |
79 |
| - }, |
80 |
| - methods: { |
81 |
| - addAnswer(words) { |
82 |
| - this.isAnswer.push(words) |
83 |
| - }, |
84 |
| - deleteAnswer(words) { |
85 |
| - const isIndex = this.isAnswer.findIndex(item => item == words) |
86 |
| - if (isIndex != -1) this.isAnswer.splice(isIndex, 1) |
87 |
| - }, |
88 |
| - checkWord(word, index) { |
89 |
| - const value = this.$refs["inputInfo" + index.toString()].value |
90 |
| - if ( |
91 |
| - word.english |
92 |
| - .trimStart() |
93 |
| - .trimEnd() |
94 |
| - .toLowerCase() == |
95 |
| - value |
96 |
| - .trimStart() |
97 |
| - .trimEnd() |
98 |
| - .toLowerCase() |
99 |
| - ) { |
100 |
| - this.$refs["inputInfo" + index.toString()].disabled = true |
101 |
| - const indexError = this.errorWords.indexOf(word.id) |
102 |
| - if (indexError != -1) this.errorWords.splice(indexError, 1) |
| 81 | + }) |
103 | 82 |
|
104 |
| - this.deleteAnswer(word.english) |
105 |
| - this.doneWords.push({ translated: word.english, original: word.russian }) |
106 |
| - } else { |
107 |
| - const indexError = this.errorWords.indexOf(word.id) |
108 |
| - if (indexError == -1) this.errorWords.push(word.id) |
109 |
| - } |
110 |
| - }, |
111 |
| - sendData() { |
112 |
| - const arrayWords = JSON.parse(window.sessionStorage.getItem("words")) |
| 83 | + const addAnswer = (word: string) => isAnswer.value.push(word) |
| 84 | +
|
| 85 | + const deleteAnswer = (word: string) => { |
| 86 | + const isIndex: number = isAnswer.value.findIndex((item: string) => item == word) |
| 87 | + if (isIndex != -1) isAnswer.value.splice(isIndex, 1) |
| 88 | + } |
| 89 | +
|
| 90 | + const sendData = () => { |
| 91 | + // @ts-ignore |
| 92 | + const arrayWords: any = JSON.parse(window.sessionStorage.getItem("words")) |
113 | 93 | if (arrayWords != null) window.sessionStorage.removeItem("words")
|
114 | 94 |
|
115 |
| - const errorWords = JSON.parse(window.sessionStorage.getItem("wordsMistakes")) |
| 95 | + // @ts-ignore |
| 96 | + const errorWords: any = JSON.parse(window.sessionStorage.getItem("wordsMistakes")) |
116 | 97 | if (errorWords != null) window.sessionStorage.removeItem("wordsMistakes")
|
117 | 98 |
|
118 |
| - const successWords = [] |
119 |
| - const failedWords = [] |
| 99 | + const successWords: any[] = [] |
| 100 | + const failedWords: any[] = [] |
120 | 101 |
|
121 |
| - this.doneWords.map(item => { |
122 |
| - this.currentWords.map(wordInfo => { |
| 102 | + doneWords.value.map((item: any) => { |
| 103 | + props.currentWords.map((wordInfo: any) => { |
123 | 104 | if (wordInfo.english == item.translated) successWords.push({ english: wordInfo.russian, id: wordInfo.id })
|
124 | 105 | })
|
125 | 106 | })
|
126 |
| - this.errorWords.map(item => { |
127 |
| - this.currentWords.map(wordInfo => { |
| 107 | + errorWords.map((item: any) => { |
| 108 | + props.currentWords.map((wordInfo: any) => { |
128 | 109 | if (wordInfo.english == item) failedWords.push({ english: item, id: wordInfo.id })
|
129 | 110 | })
|
130 | 111 | })
|
131 | 112 | window.sessionStorage.setItem("words", JSON.stringify(successWords))
|
132 | 113 | window.sessionStorage.setItem("wordsMistakes", JSON.stringify(failedWords))
|
133 |
| - }, |
134 |
| - rotateWords() { |
135 |
| - this.doneWords = [] |
136 |
| - this.errorWords = [] |
137 |
| - this.isAnswer = [] |
138 |
| - this.isRotate = !this.isRotate |
139 |
| - }, |
140 |
| - }, |
141 |
| - } |
| 114 | + } |
| 115 | +
|
| 116 | + const checkWord = (word: WordInterface, index: number) => { |
| 117 | + // @ts-ignore |
| 118 | + const value: any = inputsInfo.value[index.toString()].value |
| 119 | +
|
| 120 | + if (word.english.trimStart().trimEnd().toLowerCase() == value.trimStart().trimEnd().toLowerCase()) { |
| 121 | + // @ts-ignore |
| 122 | + inputsInfo.value[index].disabled = true |
| 123 | + // @ts-ignore |
| 124 | + const indexError = errorWords.value.indexOf(word.id) |
| 125 | + if (indexError != -1) errorWords.value.splice(indexError, 1) |
| 126 | +
|
| 127 | + const speechSythesis = new SpeechSythesis(word.english, isRotate.value ? "en-US" : "ru-RU") |
| 128 | + speechSythesis.render() |
| 129 | + speechSythesis.shooseSpeaker("Whisper") |
| 130 | + speechSythesis.speak() |
| 131 | +
|
| 132 | + deleteAnswer(word.english) |
| 133 | + doneWords.value.push({ translated: word.english, original: word.russian }) |
| 134 | + } else { |
| 135 | + // @ts-ignore |
| 136 | + const indexError = errorWords.value.indexOf(word.id) |
| 137 | + // @ts-ignore |
| 138 | + if (indexError == -1) errorWords.value.push(word.id) |
| 139 | + } |
| 140 | + } |
| 141 | +
|
| 142 | + const rotateWords = () => { |
| 143 | + doneWords.value = [] |
| 144 | + errorWords.value = [] |
| 145 | + isAnswer.value = [] |
| 146 | + isRotate.value = !isRotate.value |
| 147 | + } |
| 148 | +
|
| 149 | + return { |
| 150 | + rotateWords, currentSortWords, currentInputWord, doneWords, |
| 151 | + errorWords, inputsInfo, isAnswer, checkedAnswers, isRotate, |
| 152 | + addAnswer, deleteAnswer, sendData, checkWord |
| 153 | + } |
| 154 | + } |
| 155 | + }) |
142 | 156 | </script>
|
0 commit comments