FillPreview.vue 12 KB

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