ChooseTonePreview.vue 11 KB

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