ChooseTonePreview.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.select_item_index !== i && this.data.property.answer_mode === 'label') return;
  107. if (!this.active_letter && this.data.property.answer_mode === 'label') return;
  108. item.user_answer[item.item_active_index].select_tone = value;
  109. if (this.data.property.answer_mode === 'label') {
  110. item.user_answer[item.item_active_index].select_letter = this.active_letter;
  111. this.active_index_str = `${i}-${item.item_active_index}-${this.active_letter_index}`;
  112. this.handleReplaceTone(this.active_letter + value);
  113. setTimeout(() => {
  114. // item.item_con[item.item_active_index][this.active_letter_index] = this.final_con;
  115. let new_con = item.item_con_yuan[item.item_active_index].split(this.active_letter);
  116. item.item_con[item.item_active_index] = new_con[0] + this.final_con + new_con[1];
  117. // this.active_letter = this.final_con;
  118. this.$forceUpdate();
  119. }, 100);
  120. } else {
  121. this.active_index_str = `${i}-${item.item_active_index}`;
  122. this.handleReplaceTone(item.item_con_yuan[item.item_active_index] + value);
  123. setTimeout(() => {
  124. item.item_con[item.item_active_index] = this.final_con;
  125. this.$forceUpdate();
  126. }, 100);
  127. }
  128. },
  129. // 处理数据
  130. handleData() {
  131. this.con_preview = [];
  132. this.data.option_list.forEach((item) => {
  133. let con_arr = JSON.parse(JSON.stringify(item.content_view));
  134. let user_answer = [];
  135. con_arr.forEach(() => {
  136. user_answer.push({
  137. select_tone: null,
  138. select_letter: '',
  139. select_index: '',
  140. });
  141. });
  142. let obj = {
  143. item_con: con_arr,
  144. item_con_yuan: JSON.parse(JSON.stringify(con_arr)),
  145. mark: item.mark,
  146. user_answer,
  147. item_active_index: 0,
  148. active_letter: '',
  149. };
  150. this.con_preview.push(obj);
  151. });
  152. },
  153. handleReplaceTone(e) {
  154. this.$nextTick(() => {
  155. let value = e;
  156. this.resArr = [];
  157. if (value) {
  158. let reg = /\s+/g;
  159. let valueArr = value.split(reg);
  160. valueArr.forEach((item) => {
  161. this.handleValue(item);
  162. });
  163. let str = '';
  164. setTimeout(() => {
  165. this.resArr.forEach((item) => {
  166. str += ' ';
  167. item.forEach((sItem) => {
  168. if (sItem.number && sItem.con) {
  169. let number = Number(sItem.number);
  170. let con = sItem.con;
  171. let word = this.addTone(number, con);
  172. str += word;
  173. } else if (sItem.number) {
  174. str += sItem.number;
  175. } else if (sItem.con) {
  176. str += ` ${sItem.con} `;
  177. }
  178. });
  179. });
  180. this.final_con = str.trim();
  181. }, 10);
  182. }
  183. });
  184. },
  185. handleValue(valItem) {
  186. let reg = /\d/;
  187. let reg2 = /[A-Za-z]+\d/g;
  188. let numList = [];
  189. let valArr = valItem.split('');
  190. if (reg2.test(valItem)) {
  191. for (let i = 0; i < valArr.length; i++) {
  192. let item = valItem[i];
  193. if (reg.test(item)) {
  194. let numIndex = numList.length === 0 ? 0 : numList[numList.length - 1].index;
  195. let con = valItem.substring(numIndex, i);
  196. con = con.replace(/\d/g, '');
  197. let obj = {
  198. index: i,
  199. number: item,
  200. con,
  201. isTran: true,
  202. };
  203. numList.push(obj);
  204. }
  205. }
  206. } else {
  207. numList = [];
  208. }
  209. if (numList.length === 0) {
  210. this.resArr.push([{ con: valItem }]);
  211. } else {
  212. this.resArr.push(numList);
  213. }
  214. },
  215. addTone(number, con) {
  216. let zmList = ['a', 'o', 'e', 'i', 'u', 'v', 'A', 'O', 'E', 'I', 'U'];
  217. let cons = con;
  218. if (number) {
  219. for (let i = 0; i < zmList.length; i++) {
  220. let zm = zmList[i];
  221. if (con.indexOf(zm) > -1) {
  222. let zm2 = this.tone_data[i][number - 1];
  223. if (con.indexOf('iu') > -1) {
  224. zm2 = this.tone_data[4][number - 1];
  225. cons = con.replace('u', zm2);
  226. } else if (con.indexOf('ui') > -1) {
  227. zm2 = this.tone_data[3][number - 1];
  228. cons = con.replace('i', zm2);
  229. } else if (
  230. con.indexOf('yv') > -1 ||
  231. con.indexOf('jv') > -1 ||
  232. con.indexOf('qv') > -1 ||
  233. con.indexOf('xv') > -1
  234. ) {
  235. zm2 = this.tone_data[4][number - 1];
  236. cons = con.replace('v', zm2);
  237. } else {
  238. cons = con.replace(zm, zm2);
  239. }
  240. break;
  241. }
  242. }
  243. }
  244. return cons;
  245. },
  246. handleSelectItemTone(i, indexc, indexi, itemi) {
  247. this.con_preview[i].item_active_index = indexc;
  248. this.con_preview[i].user_answer[indexc].select_index = indexi;
  249. this.active_index_str = `${i}-${indexc}-${indexi}`;
  250. this.active_letter = itemi;
  251. this.active_letter_index = indexi;
  252. this.select_item_index = i;
  253. },
  254. },
  255. };
  256. </script>
  257. <style lang="scss" scoped>
  258. @use '@/styles/mixin.scss' as *;
  259. .choosetone-preview {
  260. @include preview;
  261. .option-list {
  262. display: flex;
  263. flex-wrap: wrap;
  264. row-gap: 16px;
  265. .option-item {
  266. display: flex;
  267. column-gap: 16px;
  268. align-items: center;
  269. width: 45%;
  270. margin-right: 5%;
  271. .option-content {
  272. padding: 12px 24px;
  273. color: #706f78;
  274. background-color: #f9f8f9;
  275. border-radius: 40px;
  276. }
  277. .item-con,
  278. .items-con {
  279. font-family: 'League';
  280. color: #000;
  281. cursor: pointer;
  282. &.active {
  283. color: #2f6fec;
  284. }
  285. }
  286. .items-box {
  287. margin-right: 3px;
  288. }
  289. .tone {
  290. width: 32px;
  291. height: 32px;
  292. padding: 8px;
  293. font-size: 0;
  294. color: #9f9f9f;
  295. text-align: center;
  296. cursor: pointer;
  297. &.active {
  298. color: #2f6fec;
  299. background: #dfe9fd;
  300. border-radius: 16px;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. </style>