FillPreview.vue 18 KB

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