FillPreview.vue 14 KB

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