FillPreview.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="select-preview" :style="[getAreaStyle(), getComponentStyle()]">
  4. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  5. <div class="main" :style="getMainStyle()">
  6. <AudioFill
  7. v-if="data.audio_file_id.length > 0"
  8. :color="data.unified_attrib?.topic_color"
  9. :file-id="data.audio_file_id"
  10. />
  11. <div class="fill-wrapper">
  12. <p v-for="(item, i) in modelEssay" :key="i">
  13. <template v-for="(li, j) in item">
  14. <template v-if="li.type === 'text'">
  15. <PinyinText
  16. v-if="isEnable(data.property.view_pinyin)"
  17. :key="`${i}-${j}`"
  18. class="content"
  19. :paragraph-list="li.paragraph_list"
  20. :pinyin-position="data.property.pinyin_position"
  21. :is-preview="true"
  22. />
  23. <span v-else :key="j" v-html="convertText(sanitizeHTML(li.content))"></span>
  24. </template>
  25. <template v-if="li.type === 'input'">
  26. <template v-if="data.property.fill_type === fillTypeList[0].value">
  27. <el-input
  28. :key="j"
  29. v-model="li.content"
  30. :disabled="disabled"
  31. :class="[data.property.fill_font, ...computedAnswerClass(li.mark)]"
  32. :style="[{ width: Math.max(80, li.content.length * 21.3) + 'px' }]"
  33. />
  34. </template>
  35. <template v-else-if="data.property.fill_type === fillTypeList[1].value">
  36. <el-popover :key="j" placement="top" trigger="click">
  37. <div class="word-list">
  38. <span
  39. v-for="{ content, mark } in data.word_list"
  40. :key="mark"
  41. class="word-item"
  42. @click="handleSelectWord(content, mark, li)"
  43. >
  44. {{ content }}
  45. </span>
  46. </div>
  47. <el-input
  48. slot="reference"
  49. v-model="li.content"
  50. :readonly="true"
  51. :class="[data.property.fill_font, ...computedAnswerClass(li.mark)]"
  52. class="pinyin"
  53. :style="[{ width: Math.max(80, li.content.length * 21.3) + 'px' }]"
  54. />
  55. </el-popover>
  56. </template>
  57. <template v-else-if="data.property.fill_type === fillTypeList[2].value">
  58. <span :key="j" class="write-click" @click="handleWriteClick(li.mark)">
  59. <img
  60. v-show="li.write_base64"
  61. style="background-color: #f4f4f4"
  62. :src="li.write_base64"
  63. alt="write-show"
  64. />
  65. </span>
  66. </template>
  67. <template v-else-if="data.property.fill_type === fillTypeList[3].value">
  68. <SoundRecordBox
  69. ref="record"
  70. :key="j"
  71. type="mini"
  72. :many-times="false"
  73. class="record-box"
  74. :attrib="data.unified_attrib"
  75. :answer-record-list="data.audio_answer_list"
  76. :task-model="isJudgingRightWrong ? 'ANSWER' : ''"
  77. @handleWav="handleMiniWav($event, li.mark)"
  78. />
  79. </template>
  80. </template>
  81. </template>
  82. </p>
  83. </div>
  84. <SoundRecord
  85. v-if="isEnable(data.property.is_enable_voice_answer)"
  86. ref="record"
  87. type="normal"
  88. class="record-box"
  89. :attrib="data.unified_attrib"
  90. :answer-record-list="data.record_list"
  91. :task-model="isJudgingRightWrong ? 'ANSWER' : ''"
  92. @handleWav="handleWav"
  93. />
  94. <div v-if="showLang" class="lang">
  95. {{ data.multilingual.find((item) => item.type === getLang())?.translation }}
  96. </div>
  97. </div>
  98. <WriteDialog :visible.sync="writeVisible" @confirm="handleWriteConfirm" />
  99. <PreviewOperation @showAnswerAnalysis="showAnswerAnalysis" @retry="retry" />
  100. <AnswerAnalysis
  101. :visible.sync="visibleAnswerAnalysis"
  102. :answer-list="data.answer_list"
  103. :analysis-list="data.analysis_list"
  104. @closeAnswerAnalysis="closeAnswerAnalysis"
  105. >
  106. <div slot="right-answer" class="fill-wrapper">
  107. <p v-for="(item, i) in modelEssay" :key="i">
  108. <template v-for="(li, j) in item">
  109. <template v-if="li.type === 'text'">
  110. <PinyinText
  111. v-if="isEnable(data.property.view_pinyin)"
  112. :key="`${i}-${j}`"
  113. class="content"
  114. :paragraph-list="li.paragraph_list"
  115. :pinyin-position="data.property.pinyin_position"
  116. :is-preview="true"
  117. />
  118. <span v-else :key="j" v-html="convertText(sanitizeHTML(li.content))"></span>
  119. </template>
  120. <template v-if="li.type === 'input'">
  121. <template v-if="data.property.fill_type === fillTypeList[0].value">
  122. <el-input
  123. :key="j"
  124. v-model="li.content"
  125. :disabled="disabled"
  126. :class="[data.property.fill_font, ...computedAnswerClass(li.mark)]"
  127. :style="[{ width: Math.max(80, li.content.length * 21.3) + 'px' }]"
  128. />
  129. </template>
  130. <template v-else-if="data.property.fill_type === fillTypeList[1].value">
  131. <el-popover :key="j" placement="top" trigger="click">
  132. <div class="word-list">
  133. <span
  134. v-for="{ content, mark } in data.word_list"
  135. :key="mark"
  136. class="word-item"
  137. @click="handleSelectWord(content, mark, li)"
  138. >
  139. {{ content }}
  140. </span>
  141. </div>
  142. <el-input
  143. slot="reference"
  144. v-model="li.content"
  145. :readonly="true"
  146. :class="[data.property.fill_font, ...computedAnswerClass(li.mark)]"
  147. class="pinyin"
  148. :style="[{ width: Math.max(80, li.content.length * 21.3) + 'px' }]"
  149. />
  150. </el-popover>
  151. </template>
  152. <template v-else-if="data.property.fill_type === fillTypeList[2].value">
  153. <span :key="j" class="write-click" @click="handleWriteClick(li.mark)">
  154. <img
  155. v-show="li.write_base64"
  156. style="background-color: #f4f4f4"
  157. :src="li.write_base64"
  158. alt="write-show"
  159. />
  160. </span>
  161. </template>
  162. <template v-else-if="data.property.fill_type === fillTypeList[3].value">
  163. <SoundRecordBox
  164. ref="record"
  165. :key="j"
  166. type="mini"
  167. :many-times="false"
  168. class="record-box"
  169. :attrib="data.unified_attrib"
  170. :answer-record-list="data.audio_answer_list"
  171. :task-model="isJudgingRightWrong ? 'ANSWER' : ''"
  172. @handleWav="handleMiniWav($event, li.mark)"
  173. />
  174. </template>
  175. <span v-show="computedAnswerText(li.mark).length > 0" :key="`answer-${j}`" class="right-answer">
  176. {{ computedAnswerText(li.mark) }}
  177. </span>
  178. </template>
  179. </template>
  180. </p>
  181. </div>
  182. </AnswerAnalysis>
  183. </div>
  184. </template>
  185. <script>
  186. import {
  187. getFillData,
  188. fillFontList,
  189. fillTypeList,
  190. arrangeTypeList,
  191. audioPositionList,
  192. } from '@/views/book/courseware/data/fill';
  193. import PreviewMixin from '../common/PreviewMixin';
  194. import AudioFill from './components/AudioFillPlay.vue';
  195. import SoundRecord from '../../common/SoundRecord.vue';
  196. import SoundRecordBox from '@/views/book/courseware/preview/components/record_input/SoundRecord.vue';
  197. import WriteDialog from './components/WriteDialog.vue';
  198. export default {
  199. name: 'FillPreview',
  200. components: {
  201. AudioFill,
  202. SoundRecord,
  203. SoundRecordBox,
  204. WriteDialog,
  205. },
  206. mixins: [PreviewMixin],
  207. data() {
  208. return {
  209. data: getFillData(),
  210. fillTypeList,
  211. modelEssay: [],
  212. selectedWordList: [], // 用于存储选中的词汇
  213. writeVisible: false,
  214. writeMark: '',
  215. };
  216. },
  217. computed: {
  218. fontFamily() {
  219. return fillFontList.find(({ value }) => this.data.property.fill_font === value).font;
  220. },
  221. },
  222. watch: {
  223. 'data.model_essay': {
  224. handler(list) {
  225. if (!list || !Array.isArray(list)) return;
  226. this.modelEssay = JSON.parse(JSON.stringify(list));
  227. this.answer.answer_list = list
  228. .map((item) => {
  229. return item
  230. .map(({ type, content, audio_answer_list, mark }) => {
  231. if (type === 'input') {
  232. return {
  233. value: content,
  234. mark,
  235. audio_answer_list,
  236. write_base64: '',
  237. };
  238. }
  239. })
  240. .filter((item) => item);
  241. })
  242. .flat();
  243. },
  244. deep: true,
  245. immediate: true,
  246. },
  247. modelEssay: {
  248. handler(list) {
  249. if (!list || !Array.isArray(list)) return;
  250. this.answer.answer_list = list
  251. .map((item) => {
  252. return item
  253. .map(({ type, content, audio_answer_list, mark }) => {
  254. if (type === 'input') {
  255. return {
  256. value: content,
  257. mark,
  258. audio_answer_list,
  259. write_base64: '',
  260. };
  261. }
  262. })
  263. .filter((item) => item);
  264. })
  265. .flat();
  266. },
  267. deep: true,
  268. immediate: true,
  269. },
  270. isJudgingRightWrong(val) {
  271. if (!val) return;
  272. this.answer.answer_list.forEach(({ mark, value }) => {
  273. this.modelEssay.forEach((item) => {
  274. item.forEach((li) => {
  275. if (li.mark === mark) {
  276. li.content = value;
  277. }
  278. });
  279. });
  280. });
  281. this.handleWav(this.answer.record_list);
  282. },
  283. 'data.record_list'(val) {
  284. this.answer.record_list = val;
  285. },
  286. },
  287. methods: {
  288. handleWav(data) {
  289. this.data.record_list = data;
  290. },
  291. /**
  292. * 处理小音频录音
  293. * @param {Object} data 音频数据
  294. * @param {String} mark 选项标识
  295. */
  296. handleMiniWav(data, mark) {
  297. if (!data || !mark) return;
  298. for (const item of this.modelEssay) {
  299. const li = item.find((li) => li?.mark === mark);
  300. if (li) {
  301. this.$set(li, 'audio_answer_list', data);
  302. break;
  303. }
  304. }
  305. },
  306. /**
  307. * 处理选中词汇
  308. * @param {String} content 选中的词汇内容
  309. * @param {String} mark 选项标识
  310. * @param {Object} li 当前输入框对象
  311. */
  312. handleSelectWord(content, mark, li) {
  313. if (!content || !mark || !li) return;
  314. li.content = content;
  315. this.selectedWordList.push(mark);
  316. },
  317. /**
  318. * 处理书写区确认
  319. * @param {String} data 书写区数据
  320. */
  321. handleWriteConfirm(data) {
  322. if (!data) return;
  323. for (const item of this.modelEssay) {
  324. const li = item.find((li) => li?.mark === this.writeMark);
  325. if (li) {
  326. this.$set(li, 'write_base64', data);
  327. break;
  328. }
  329. }
  330. },
  331. handleWriteClick(mark) {
  332. this.writeVisible = true;
  333. this.writeMark = mark;
  334. },
  335. getMainStyle() {
  336. const isRow = this.data.property.arrange_type === arrangeTypeList[0].value;
  337. const isFront = this.data.property.audio_position === audioPositionList[0].value;
  338. const isEnableVoice = this.data.property.is_enable_voice_answer === 'true';
  339. const isHasAudio = this.data.audio_file_id.length > 0;
  340. let _list = [
  341. { name: 'audio', value: '24px' },
  342. { name: 'fill', value: '1fr' },
  343. ];
  344. if (!isHasAudio) {
  345. _list[0].value = '0px';
  346. }
  347. if (!isFront) {
  348. _list = _list.reverse();
  349. }
  350. let gridArea = '';
  351. let gridTemplateRows = '';
  352. let gridTemplateColumns = '';
  353. if (isRow) {
  354. gridArea = `"${_list[0].name} ${_list[1].name}${isEnableVoice ? ' record' : ''}" ${this.showLang ? ' "lang lang lang"' : ''}`;
  355. gridTemplateRows = `auto ${this.showLang ? 'auto' : ''}`;
  356. gridTemplateColumns = `${_list[0].value} ${_list[1].value}${isEnableVoice ? ' 160px' : ''}`;
  357. } else {
  358. gridArea = `"${_list[0].name}" "${_list[1].name}" ${isEnableVoice ? `" record" ` : ''} ${this.showLang ? ' "lang"' : ''}`;
  359. gridTemplateRows = `${_list[0].value} ${_list[1].value} ${isEnableVoice ? ' 32px' : ''} ${this.showLang ? 'auto' : ''}`;
  360. gridTemplateColumns = '1fr';
  361. }
  362. let style = {
  363. 'grid-auto-flow': isRow ? 'column' : 'row',
  364. 'column-gap': isRow && isHasAudio ? '16px' : undefined,
  365. 'row-gap': isRow || !isHasAudio ? undefined : '8px',
  366. 'grid-template-areas': gridArea,
  367. 'grid-template-rows': gridTemplateRows,
  368. 'grid-template-columns': gridTemplateColumns,
  369. };
  370. return style;
  371. },
  372. /**
  373. * 计算答题对错选项字体颜色
  374. * @param {string} mark 选项标识
  375. */
  376. computedAnswerClass(mark) {
  377. if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
  378. return '';
  379. }
  380. let selectOption = this.answer.answer_list.find((item) => item.mark === mark);
  381. let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark);
  382. if (!selectOption) return '';
  383. let selectValue = selectOption.value;
  384. let answerValue = answerOption.value;
  385. let answerType = answerOption.type;
  386. let classList = [];
  387. let isRight =
  388. answerType === 'only_one' ? selectValue === answerValue : answerValue.split('/').includes(selectValue);
  389. if (this.isJudgingRightWrong) {
  390. isRight ? classList.push('right') : classList.push('wrong');
  391. }
  392. if (this.isShowRightAnswer && !isRight) {
  393. classList.push('show-right-answer');
  394. }
  395. return classList;
  396. },
  397. /**
  398. * 计算正确答案文本
  399. * @param {string} mark 选项标识
  400. */
  401. computedAnswerText(mark) {
  402. if (!this.isShowRightAnswer) return '';
  403. let selectOption = this.answer.answer_list.find((item) => item.mark === mark);
  404. let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark);
  405. if (!selectOption) return '';
  406. let selectValue = selectOption.value;
  407. let answerValue = answerOption.value;
  408. let isRight = selectValue === answerValue;
  409. if (isRight) return '';
  410. return `(${answerValue})`;
  411. },
  412. // 重做
  413. retry() {
  414. this.modelEssay.forEach((item) => {
  415. item.forEach((li) => {
  416. if (li.type === 'input') {
  417. li.content = '';
  418. li.write_base64 = '';
  419. }
  420. });
  421. });
  422. this.selectedWordList = [];
  423. this.handleWav([]);
  424. },
  425. },
  426. };
  427. </script>
  428. <style lang="scss" scoped>
  429. @use '@/styles/mixin.scss' as *;
  430. .select-preview {
  431. @include preview-base;
  432. .main {
  433. display: grid;
  434. align-items: center;
  435. }
  436. .fill-wrapper {
  437. grid-area: fill;
  438. font-size: 16pt;
  439. p {
  440. margin: 0;
  441. }
  442. .content {
  443. display: inline-block;
  444. }
  445. .record-box {
  446. display: inline-flex;
  447. align-items: center;
  448. background-color: #fff;
  449. border-bottom: 1px solid $font-color;
  450. }
  451. .write-click {
  452. display: inline-block;
  453. width: 104px;
  454. height: 32px;
  455. padding: 0 4px;
  456. vertical-align: bottom;
  457. cursor: pointer;
  458. border-bottom: 1px solid $font-color;
  459. img {
  460. width: 100%;
  461. height: 100%;
  462. }
  463. }
  464. .el-input {
  465. display: inline-flex;
  466. align-items: center;
  467. width: 120px;
  468. margin: 0 2px;
  469. vertical-align: bottom;
  470. &.pinyin :deep input.el-input__inner {
  471. font-family: 'PINYIN-B', sans-serif;
  472. }
  473. &.chinese :deep input.el-input__inner {
  474. font-family: 'arial', sans-serif;
  475. }
  476. &.english :deep input.el-input__inner {
  477. font-family: 'arial', sans-serif;
  478. }
  479. &.right {
  480. :deep input.el-input__inner {
  481. color: $right-color;
  482. }
  483. }
  484. &.wrong {
  485. :deep input.el-input__inner {
  486. color: $error-color;
  487. }
  488. }
  489. & + .right-answer {
  490. position: relative;
  491. left: -4px;
  492. display: inline-block;
  493. height: 32px;
  494. line-height: 28px;
  495. vertical-align: bottom;
  496. border-bottom: 1px solid $font-color;
  497. }
  498. :deep input.el-input__inner {
  499. padding: 0;
  500. font-size: 16pt;
  501. color: $font-color;
  502. text-align: center;
  503. background-color: #fff;
  504. border-width: 0;
  505. border-bottom: 1px solid $font-color;
  506. border-radius: 0;
  507. }
  508. }
  509. .popper-list {
  510. & + .right-answer {
  511. position: relative;
  512. left: -4px;
  513. display: inline-block;
  514. height: 32px;
  515. line-height: 28px;
  516. vertical-align: bottom;
  517. border-bottom: 1px solid $font-color;
  518. }
  519. }
  520. }
  521. .record-box {
  522. padding: 6px 12px;
  523. background-color: $fill-color;
  524. :deep .record-time {
  525. width: 100px;
  526. }
  527. }
  528. }
  529. </style>
  530. <style lang="scss" scoped>
  531. .word-list {
  532. display: flex;
  533. flex-wrap: wrap;
  534. gap: 8px;
  535. align-items: center;
  536. .word-item {
  537. cursor: pointer;
  538. }
  539. }
  540. </style>