ChooseTonePreview.vue 11 KB

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