ChooseTonePreview.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="choosetone-preview">
  4. <div class="stem">
  5. <span class="question-number">{{ data.property.question_number }}.</span>
  6. <span v-html="sanitizeHTML(data.stem)"></span>
  7. </div>
  8. <div v-if="data.property.is_enable_description" class="description">{{ data.description }}</div>
  9. <div class="option-list">
  10. <li v-for="(item, i) in data.option_list" :key="i" :class="['option-item']">
  11. <span>{{ computeOptionMethods[data.option_number_show_mode](i) }}. </span>
  12. <AudioPlay v-if="item.audio_file_id" :file-id="item.audio_file_id" />
  13. <div class="option-content">
  14. <template v-if="data.property.tone_type === 'select'">
  15. <span
  16. v-for="(itemc, indexc) in con_preview[i].item_con"
  17. :key="indexc"
  18. :class="['item-con', active_index_str === i + '-' + indexc ? 'active' : '']"
  19. @click="
  20. con_preview[i].item_active_index = indexc;
  21. active_index_str = i + '-' + indexc;
  22. "
  23. >
  24. {{ itemc }}
  25. </span>
  26. </template>
  27. <template v-else>
  28. <span v-for="(itemc, indexc) in con_preview[i].item_con" :key="indexc" class="items-box">
  29. <span
  30. v-for="(itemi, indexi) in itemc"
  31. :key="indexi"
  32. :class="['items-con', active_index_str === i + '-' + indexc + '-' + indexi ? 'active' : '']"
  33. @click="handleSelectItemTone(i, indexc, indexi, con_preview[i].item_con_yuan[indexc][indexi])"
  34. >{{ itemi }}</span
  35. >
  36. </span>
  37. </template>
  38. </div>
  39. <span
  40. v-for="({ img, value }, j) in toneList"
  41. :key="j"
  42. :class="[
  43. 'tone',
  44. data.property.tone_type === 'select' &&
  45. con_preview[i].user_answer[con_preview[i].item_active_index].select_tone === value
  46. ? 'active'
  47. : data.property.tone_type === 'dimension' &&
  48. con_preview[i].user_answer[con_preview[i].item_active_index].select_tone === value &&
  49. con_preview[i].user_answer[con_preview[i].item_active_index].select_letter === active_letter &&
  50. select_item_index === i
  51. ? 'active'
  52. : '',
  53. ]"
  54. @click="chooseTone(con_preview[i], value, i)"
  55. >
  56. <SvgIcon :icon-class="img" />
  57. </span>
  58. </li>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import { computeOptionMethods } from '@/views/exercise_questions/data/common';
  64. import PreviewMixin from './components/PreviewMixin';
  65. export default {
  66. name: 'ChooseTonePreview',
  67. mixins: [PreviewMixin],
  68. data() {
  69. return {
  70. computeOptionMethods,
  71. toneList: [
  72. { value: '1', label: '一声', img: 'first-tone' },
  73. { value: '2', label: '二声', img: 'second-tone' },
  74. { value: '3', label: '三声', img: 'third-tone' },
  75. { value: '4', label: '四声', img: 'fourth-tone' },
  76. { value: '0', label: '轻声', img: 'neutral-tone' },
  77. ],
  78. con_preview: [],
  79. tone_data: [
  80. ['ā', 'á', 'ǎ', 'à', 'a'],
  81. ['ō', 'ó', 'ǒ', 'ò', 'o'],
  82. ['ē', 'é', 'ě', 'è', 'e'],
  83. ['ī', 'í', 'ǐ', 'ì', 'i'],
  84. ['ū', 'ú', 'ǔ', 'ù', 'u'],
  85. ['ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
  86. ['Ā', 'Á', 'Â', 'À', 'A'],
  87. ['Ō', 'Ó', 'Ô', 'Ò', 'O'],
  88. ['Ē', 'É', 'Ê', 'È', 'E'],
  89. ['Ī', 'Í', 'Î', 'Ì', 'I'],
  90. ['Ū', 'Ú', 'Û', 'Ù', 'U'],
  91. ],
  92. final_con: '',
  93. active_index_str: '', // 高亮索引的字符串
  94. active_letter: '', // 选中字母的值
  95. active_letter_index: 0, // 选择字母索引
  96. select_item_index: 0, // 小题索引
  97. };
  98. },
  99. created() {
  100. this.handleData();
  101. },
  102. methods: {
  103. chooseTone(item, value, i) {
  104. if (!this.active_letter && this.data.property.tone_type === 'dimension') return;
  105. item.user_answer[item.item_active_index].select_tone = value;
  106. if (this.data.property.tone_type === 'dimension') {
  107. item.user_answer[item.item_active_index].select_letter = this.active_letter;
  108. this.active_index_str = `${i}-${item.item_active_index}-${this.active_letter_index}`;
  109. this.handleReplaceTone(this.active_letter + value);
  110. setTimeout(() => {
  111. // item.item_con[item.item_active_index][this.active_letter_index] = this.final_con;
  112. let new_con = item.item_con_yuan[item.item_active_index].split(this.active_letter);
  113. item.item_con[item.item_active_index] = new_con[0] + this.final_con + new_con[1];
  114. // this.active_letter = this.final_con;
  115. this.$forceUpdate();
  116. }, 100);
  117. } else {
  118. this.active_index_str = `${i}-${item.item_active_index}`;
  119. this.handleReplaceTone(item.item_con_yuan[item.item_active_index] + value);
  120. setTimeout(() => {
  121. item.item_con[item.item_active_index] = this.final_con;
  122. this.$forceUpdate();
  123. }, 100);
  124. }
  125. },
  126. // 处理数据
  127. handleData() {
  128. this.con_preview = [];
  129. this.data.option_list.forEach((item) => {
  130. let con_arr = item.content_preview;
  131. let user_answer = [];
  132. con_arr.forEach(() => {
  133. user_answer.push({
  134. select_tone: null,
  135. select_letter: '',
  136. });
  137. });
  138. let obj = {
  139. item_con: con_arr,
  140. item_con_yuan: JSON.parse(JSON.stringify(con_arr)),
  141. mark: item.mark,
  142. user_answer,
  143. item_active_index: 0,
  144. active_letter: '',
  145. };
  146. this.con_preview.push(obj);
  147. });
  148. },
  149. handleReplaceTone(e) {
  150. this.$nextTick(() => {
  151. let value = e;
  152. this.resArr = [];
  153. if (value) {
  154. let reg = /\s+/g;
  155. let valueArr = value.split(reg);
  156. valueArr.forEach((item) => {
  157. this.handleValue(item);
  158. });
  159. let str = '';
  160. setTimeout(() => {
  161. this.resArr.forEach((item) => {
  162. str += ' ';
  163. item.forEach((sItem) => {
  164. if (sItem.number && sItem.con) {
  165. let number = Number(sItem.number);
  166. let con = sItem.con;
  167. let word = this.addTone(number, con);
  168. str += word;
  169. } else if (sItem.number) {
  170. str += sItem.number;
  171. } else if (sItem.con) {
  172. str += ` ${sItem.con} `;
  173. }
  174. });
  175. });
  176. this.final_con = str.trim();
  177. }, 10);
  178. }
  179. });
  180. },
  181. handleValue(valItem) {
  182. let reg = /\d/;
  183. let reg2 = /[A-Za-z]+\d/g;
  184. let numList = [];
  185. let valArr = valItem.split('');
  186. if (reg2.test(valItem)) {
  187. for (let i = 0; i < valArr.length; i++) {
  188. let item = valItem[i];
  189. if (reg.test(item)) {
  190. let numIndex = numList.length === 0 ? 0 : numList[numList.length - 1].index;
  191. let con = valItem.substring(numIndex, i);
  192. con = con.replace(/\d/g, '');
  193. let obj = {
  194. index: i,
  195. number: item,
  196. con,
  197. isTran: true,
  198. };
  199. numList.push(obj);
  200. }
  201. }
  202. } else {
  203. numList = [];
  204. }
  205. if (numList.length === 0) {
  206. this.resArr.push([{ con: valItem }]);
  207. } else {
  208. this.resArr.push(numList);
  209. }
  210. },
  211. addTone(number, con) {
  212. let zmList = ['a', 'o', 'e', 'i', 'u', 'v', 'A', 'O', 'E', 'I', 'U'];
  213. let cons = con;
  214. if (number) {
  215. for (let i = 0; i < zmList.length; i++) {
  216. let zm = zmList[i];
  217. if (con.indexOf(zm) > -1) {
  218. let zm2 = this.tone_data[i][number - 1];
  219. if (con.indexOf('iu') > -1) {
  220. zm2 = this.tone_data[4][number - 1];
  221. cons = con.replace('u', zm2);
  222. } else if (con.indexOf('ui') > -1) {
  223. zm2 = this.tone_data[3][number - 1];
  224. cons = con.replace('i', zm2);
  225. } else if (
  226. con.indexOf('yv') > -1 ||
  227. con.indexOf('jv') > -1 ||
  228. con.indexOf('qv') > -1 ||
  229. con.indexOf('xv') > -1
  230. ) {
  231. zm2 = this.tone_data[4][number - 1];
  232. cons = con.replace('v', zm2);
  233. } else {
  234. cons = con.replace(zm, zm2);
  235. }
  236. break;
  237. }
  238. }
  239. }
  240. return cons;
  241. },
  242. handleSelectItemTone(i, indexc, indexi, itemi) {
  243. this.con_preview[i].item_active_index = indexc;
  244. this.active_index_str = `${i}-${indexc}-${indexi}`;
  245. this.active_letter = itemi;
  246. this.active_letter_index = indexi;
  247. this.select_item_index = i;
  248. },
  249. },
  250. };
  251. </script>
  252. <style lang="scss" scoped>
  253. @use '@/styles/mixin.scss' as *;
  254. .choosetone-preview {
  255. @include preview;
  256. .option-list {
  257. display: flex;
  258. flex-direction: column;
  259. flex-flow: wrap;
  260. row-gap: 16px;
  261. .option-item {
  262. display: flex;
  263. column-gap: 16px;
  264. align-items: center;
  265. width: 45%;
  266. margin-right: 5%;
  267. .option-content {
  268. padding: 12px 24px;
  269. color: #706f78;
  270. background-color: #f9f8f9;
  271. border-radius: 40px;
  272. }
  273. .item-con,
  274. .items-con {
  275. color: #000;
  276. cursor: pointer;
  277. &.active {
  278. color: #2f6fec;
  279. }
  280. }
  281. .items-box {
  282. margin-right: 3px;
  283. }
  284. .tone {
  285. width: 32px;
  286. height: 32px;
  287. padding: 8px;
  288. font-size: 0;
  289. color: #9f9f9f;
  290. text-align: center;
  291. cursor: pointer;
  292. &.active {
  293. color: #2f6fec;
  294. background: #dfe9fd;
  295. border-radius: 16px;
  296. }
  297. }
  298. }
  299. }
  300. }
  301. </style>