FillPreview.vue 11 KB

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