ChooseTonePreview.vue 10.0 KB

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