FillPreview.vue 13 KB

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