FillPreview.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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>
  13. <template v-for="(li, i) in modelEssay">
  14. <template v-if="li.type === 'text'">
  15. <PinyinText
  16. :key="`text-${i}`"
  17. class="content"
  18. :paragraph-list="li.paragraph_list"
  19. :rich-text-list="li.rich_text_list"
  20. :pinyin-position="data.property.pinyin_position"
  21. :body-styles="pinyinBodyStyles"
  22. :is-show-pinyin="isEnable(data.property.view_pinyin)"
  23. :is-preview="true"
  24. />
  25. </template>
  26. <template v-if="li.type === 'input'">
  27. <!-- 输入填空 -->
  28. <template v-if="data.property.fill_type === fillTypeList[0].value">
  29. <el-input
  30. :key="`input-${i}`"
  31. :ref="`input-${li.mark}`"
  32. v-model="li.input"
  33. :disabled="disabled"
  34. :class="[...computedAnswerClass(li.mark)]"
  35. :style="[
  36. {
  37. fontFamily: data.property.fill_font,
  38. width: (inputWidthMap[li.mark] || data.property.input_default_width) + 'px',
  39. },
  40. ]"
  41. @input="handleInput(li.input, li.mark)"
  42. />
  43. </template>
  44. <!-- 选词填空 -->
  45. <template v-else-if="data.property.fill_type === fillTypeList[1].value">
  46. <el-popover :key="`popover-${i}`" placement="top" trigger="click">
  47. <div class="word-list">
  48. <span
  49. v-for="{ content, mark } in data.word_list"
  50. :key="mark"
  51. class="word-item"
  52. @click="handleSelectWord(content, mark, li)"
  53. v-html="sanitizeHTML(content)"
  54. >
  55. </span>
  56. </div>
  57. <span
  58. slot="reference"
  59. class="select-content"
  60. :style="[{ minWidth: data.property.input_default_width + 'px' }]"
  61. v-html="sanitizeHTML(li.input)"
  62. ></span>
  63. </el-popover>
  64. </template>
  65. <!-- 手写填空 -->
  66. <template v-else-if="data.property.fill_type === fillTypeList[2].value">
  67. <span :key="`write-${i}`" class="write-click" @click="handleWriteClick(li.mark)">
  68. <img
  69. v-show="li.write_base64"
  70. style="background-color: #f4f4f4"
  71. :src="li.write_base64"
  72. alt="write-show"
  73. />
  74. </span>
  75. </template>
  76. <!-- 语音填空 -->
  77. <template v-else-if="data.property.fill_type === fillTypeList[3].value">
  78. <SoundRecordBox
  79. ref="record"
  80. :key="`record-${i}`"
  81. type="mini"
  82. :many-times="false"
  83. class="record-box fill"
  84. :attrib="data.unified_attrib"
  85. :answer-record-list="data.audio_answer_list"
  86. :task-model="isJudgingRightWrong ? 'ANSWER' : ''"
  87. @handleWav="handleMiniWav($event, li.mark)"
  88. />
  89. </template>
  90. </template>
  91. </template>
  92. </p>
  93. </div>
  94. <SoundRecord
  95. v-if="isEnable(data.property.is_enable_voice_answer)"
  96. ref="record"
  97. type="normal"
  98. class="record-box"
  99. :attrib="data.unified_attrib"
  100. :answer-record-list="data.record_list"
  101. :task-model="isJudgingRightWrong ? 'ANSWER' : ''"
  102. @handleWav="handleWav"
  103. />
  104. <div v-if="showLang" class="lang">
  105. {{ data.multilingual.find((item) => item.type === getLang())?.translation }}
  106. </div>
  107. <PreviewOperation @showAnswerAnalysis="showAnswerAnalysis" @judgeCorrect="judgeCorrect" @retry="retry" />
  108. </div>
  109. <WriteDialog :visible.sync="writeVisible" @confirm="handleWriteConfirm" />
  110. <AnswerCorrect
  111. :visible.sync="visibleAnswerCorrect"
  112. :is-check-correct="isCheckCorrect"
  113. @closeAnswerCorrect="closeAnswerCorrect"
  114. />
  115. <AnswerAnalysis
  116. :visible.sync="visibleAnswerAnalysis"
  117. :answer-list="data.answer_list"
  118. :analysis-list="data.analysis_list"
  119. @closeAnswerAnalysis="closeAnswerAnalysis"
  120. >
  121. <div slot="right-answer" class="fill-wrapper">
  122. <p>
  123. <template v-for="(li, i) in modelEssay">
  124. <template v-if="li.type === 'text'">
  125. <PinyinText
  126. :key="`answer-text-${i}`"
  127. :is-show-pinyin="isEnable(data.property.view_pinyin)"
  128. class="content"
  129. :paragraph-list="li.paragraph_list"
  130. :rich-text-list="li.rich_text_list"
  131. :body-styles="pinyinBodyStyles"
  132. :pinyin-position="data.property.pinyin_position"
  133. :is-preview="true"
  134. />
  135. </template>
  136. <template v-if="li.type === 'input'">
  137. <span v-show="computedAnswerText(li.mark).length > 0" :key="`answer-${i}`" class="right-answer">
  138. {{ computedAnswerText(li.mark) }}
  139. </span>
  140. </template>
  141. </template>
  142. </p>
  143. </div>
  144. </AnswerAnalysis>
  145. </div>
  146. </template>
  147. <script>
  148. import PreviewMixin from '../common/PreviewMixin';
  149. import AudioFill from './components/AudioFillPlay.vue';
  150. import SoundRecord from '../../common/SoundRecord.vue';
  151. import SoundRecordBox from '@/views/book/courseware/preview/components/record_input/SoundRecord.vue';
  152. import WriteDialog from './components/WriteDialog.vue';
  153. import { getFillData, fillTypeList, arrangeTypeList, audioPositionList } from '@/views/book/courseware/data/fill';
  154. export default {
  155. name: 'FillPreview',
  156. components: {
  157. AudioFill,
  158. SoundRecord,
  159. SoundRecordBox,
  160. WriteDialog,
  161. },
  162. mixins: [PreviewMixin],
  163. data() {
  164. return {
  165. data: getFillData(),
  166. fillTypeList,
  167. modelEssay: [],
  168. inputWidthMap: {},
  169. selectedWordList: [], // 用于存储选中的词汇
  170. writeVisible: false,
  171. writeMark: '',
  172. };
  173. },
  174. computed: {
  175. pinyinBodyStyles() {
  176. const unifiedAttrib = this.data?.unified_attrib || {};
  177. return {
  178. fontFamily: unifiedAttrib.font || undefined,
  179. fontSize: unifiedAttrib.font_size || undefined,
  180. color: unifiedAttrib.text_color || undefined,
  181. };
  182. },
  183. },
  184. watch: {
  185. 'data.model_essay': {
  186. handler(list) {
  187. if (!list || !Array.isArray(list)) return;
  188. this.modelEssay = JSON.parse(JSON.stringify(list));
  189. this.syncAnswerList(list);
  190. },
  191. deep: true,
  192. immediate: true,
  193. },
  194. modelEssay: {
  195. handler(list) {
  196. if (!list || !Array.isArray(list)) return;
  197. this.syncAnswerList(list);
  198. },
  199. deep: true,
  200. immediate: true,
  201. },
  202. isJudgingRightWrong(val) {
  203. if (!val) return;
  204. this.answer.answer_list.forEach(({ mark, value }) => {
  205. const li = this.modelEssay.find((item) => item.mark === mark);
  206. if (li) {
  207. li.input = value;
  208. }
  209. });
  210. this.handleWav(this.answer.record_list);
  211. },
  212. 'data.record_list'(val) {
  213. this.answer.record_list = val;
  214. },
  215. },
  216. methods: {
  217. handleWav(data) {
  218. this.data.record_list = data;
  219. },
  220. syncAnswerList(list) {
  221. this.answer.answer_list = list
  222. .map(({ type, input, audio_answer_list, mark }) => {
  223. if (type === 'input') {
  224. return {
  225. value: input,
  226. mark,
  227. audio_answer_list,
  228. write_base64: '',
  229. };
  230. }
  231. return null;
  232. })
  233. .filter((item) => item);
  234. },
  235. /**
  236. * 处理小音频录音
  237. * @param {Object} data 音频数据
  238. * @param {String} mark 选项标识
  239. */
  240. handleMiniWav(data, mark) {
  241. if (!data || !mark) return;
  242. const li = this.modelEssay.find((item) => item?.mark === mark);
  243. if (li) {
  244. this.$set(li, 'audio_answer_list', data);
  245. }
  246. },
  247. /**
  248. * 处理选中词汇
  249. * @param {String} content 选中的词汇内容
  250. * @param {String} mark 选项标识
  251. * @param {Object} li 当前输入框对象
  252. */
  253. handleSelectWord(content, mark, li) {
  254. if (!content || !mark || !li) return;
  255. li.input = content;
  256. this.selectedWordList.push(mark);
  257. },
  258. /**
  259. * 处理书写区确认
  260. * @param {String} data 书写区数据
  261. */
  262. handleWriteConfirm(data) {
  263. if (!data) return;
  264. const li = this.modelEssay.find((item) => item?.mark === this.writeMark);
  265. if (li) {
  266. this.$set(li, 'write_base64', data);
  267. }
  268. },
  269. handleWriteClick(mark) {
  270. this.writeVisible = true;
  271. this.writeMark = mark;
  272. },
  273. getMainStyle() {
  274. const isRow = this.data.property.arrange_type === arrangeTypeList[0].value;
  275. const isFront = this.data.property.audio_position === audioPositionList[0].value;
  276. const isEnableVoice = this.data.property.is_enable_voice_answer === 'true';
  277. const isHasAudio = this.data.audio_file_id.length > 0;
  278. let areaList = [
  279. { name: 'audio', value: '24px' },
  280. { name: 'fill', value: '1fr' },
  281. ];
  282. if (!isHasAudio) {
  283. areaList.shift();
  284. }
  285. if (!isFront && isHasAudio) {
  286. areaList = areaList.reverse();
  287. }
  288. let gridArea = '';
  289. let gridTemplateRows = '';
  290. let gridTemplateColumns = '';
  291. if (isRow) {
  292. const rowAreas = areaList.map(({ name }) => name);
  293. const rowCols = areaList.map(({ value }) => value);
  294. const templateRows = ['auto'];
  295. if (isEnableVoice) {
  296. rowAreas.push('record');
  297. rowCols.push('160px');
  298. }
  299. const areaRows = [`"${rowAreas.join(' ')}"`];
  300. if (this.showLang) {
  301. areaRows.push(`"${Array(rowAreas.length).fill('lang').join(' ')}"`);
  302. templateRows.push('auto');
  303. }
  304. gridArea = areaRows.join(' ');
  305. gridTemplateRows = templateRows.join(' ');
  306. gridTemplateColumns = rowCols.join(' ');
  307. } else {
  308. const areaRows = areaList.map(({ name }) => `"${name}"`);
  309. const templateRows = areaList.map(({ value }) => value);
  310. if (isEnableVoice) {
  311. areaRows.push('"record"');
  312. templateRows.push('32px');
  313. }
  314. if (this.showLang) {
  315. areaRows.push('"lang"');
  316. templateRows.push('auto');
  317. }
  318. gridArea = areaRows.join(' ');
  319. gridTemplateRows = templateRows.join(' ');
  320. gridTemplateColumns = '1fr';
  321. }
  322. return {
  323. 'grid-auto-flow': isRow ? 'column' : 'row',
  324. 'column-gap': isRow && isHasAudio ? '16px' : undefined,
  325. 'row-gap': isRow || !isHasAudio ? undefined : '8px',
  326. 'grid-template-areas': gridArea,
  327. 'grid-template-rows': gridTemplateRows,
  328. 'grid-template-columns': gridTemplateColumns,
  329. };
  330. },
  331. /**
  332. * 计算答题对错选项字体颜色
  333. * @param {string} mark 选项标识
  334. */
  335. computedAnswerClass(mark) {
  336. if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
  337. return '';
  338. }
  339. let selectOption = this.answer.answer_list.find((item) => item.mark === mark);
  340. let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark);
  341. if (!selectOption || !answerOption) return '';
  342. let selectValue = selectOption.value;
  343. let answerValue = answerOption.value;
  344. let answerType = answerOption.type;
  345. let classList = [];
  346. let isRight =
  347. answerType === 'only_one' ? selectValue === answerValue : answerValue.split('/').includes(selectValue);
  348. if (this.isJudgingRightWrong) {
  349. isRight ? classList.push('right') : classList.push('wrong');
  350. }
  351. if (this.isShowRightAnswer && !isRight) {
  352. classList.push('show-right-answer');
  353. }
  354. return classList;
  355. },
  356. /**
  357. * 计算正确答案文本
  358. * @param {string} mark 选项标识
  359. */
  360. computedAnswerText(mark) {
  361. let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark);
  362. if (!answerOption) return '';
  363. let answerValue = answerOption.value;
  364. return `${answerValue}`;
  365. },
  366. // 重做
  367. retry() {
  368. this.modelEssay.forEach((li) => {
  369. if (li.type === 'input') {
  370. li.input = '';
  371. li.write_base64 = '';
  372. }
  373. });
  374. this.selectedWordList = [];
  375. this.isJudgingRightWrong = false;
  376. this.isShowRightAnswer = false;
  377. this.handleWav([]);
  378. },
  379. /**
  380. * 获取无文本内容的数据结构,用于保存为个人模板时的样式模板
  381. */
  382. getNoTextContentData() {
  383. let noTextContentData = JSON.parse(JSON.stringify(this.data));
  384. const resetFieldMap = {
  385. model_essay: [],
  386. content: '',
  387. audio_file_id: '',
  388. file_list: [],
  389. multilingual: [],
  390. analysis_list: [],
  391. answer_list: [],
  392. record_list: [],
  393. };
  394. Object.assign(noTextContentData, resetFieldMap);
  395. if (noTextContentData.answer) {
  396. noTextContentData.answer.answer_list = [];
  397. noTextContentData.answer.reference_answer = '';
  398. }
  399. return noTextContentData;
  400. },
  401. /**
  402. * 处理输入框内容变化
  403. * @description 根据输入框值计算所需长度,动态调整输入框宽度,输入框宽度不小于默认宽度,且不超过组件最大宽度 1000px,需要考虑输入框前面的宽度,保证输入框不会超出组件范围
  404. * @param {String} value 输入框当前值
  405. * @param {String} mark 选项标识
  406. */
  407. handleInput(value, mark) {
  408. if (!mark) return;
  409. const text = `${value || ''}`;
  410. const defaultWidth = Number(this.data?.property?.input_default_width) || 120;
  411. const fontSize = 16;
  412. const fontFamily = this.data?.property?.fill_font || 'arial';
  413. const canvas = document.createElement('canvas');
  414. const context = canvas.getContext('2d');
  415. let textWidth = 0;
  416. if (context) {
  417. context.font = `${fontSize}pt ${fontFamily}`;
  418. textWidth = context.measureText(text || ' ').width;
  419. }
  420. // 额外留出输入框内边距和边框余量,避免刚好卡边。
  421. const contentWidth = Math.ceil(textWidth + 24);
  422. const inputRef = this.$refs[`input-${mark}`];
  423. const inputVm = Array.isArray(inputRef) ? inputRef[0] : inputRef;
  424. const inputEl = inputVm?.$el;
  425. const containerEl = this.$el;
  426. let availableWidth = 1000;
  427. if (inputEl && containerEl) {
  428. const inputRect = inputEl.getBoundingClientRect();
  429. const containerRect = containerEl.getBoundingClientRect();
  430. const rightLimit = containerRect.left + Math.min(containerRect.width, 1000);
  431. availableWidth = Math.floor(rightLimit - inputRect.left - 12);
  432. }
  433. const maxWidth = Math.max(40, availableWidth);
  434. const nextWidth = Math.min(Math.max(contentWidth, defaultWidth), maxWidth);
  435. this.$set(this.inputWidthMap, mark, nextWidth);
  436. },
  437. },
  438. };
  439. </script>
  440. <style lang="scss" scoped>
  441. @use '@/styles/mixin.scss' as *;
  442. .select-preview {
  443. @include preview-base;
  444. .main {
  445. display: grid;
  446. gap: 10px;
  447. align-items: center;
  448. }
  449. .fill-wrapper {
  450. grid-area: fill;
  451. font-size: 16pt;
  452. :deep .pinyin-area .rich-text-container,
  453. :deep .pinyin-area .pinyin-paragraph {
  454. display: inline;
  455. }
  456. p {
  457. margin: 0;
  458. }
  459. .content {
  460. display: inline;
  461. }
  462. .record-box.fill {
  463. display: inline-flex;
  464. align-items: center;
  465. background-color: #fff;
  466. border-bottom: 1px solid $font-color;
  467. :deep(.playBack) {
  468. width: 20px;
  469. height: 20px;
  470. }
  471. }
  472. .write-click {
  473. display: inline-block;
  474. width: 104px;
  475. height: 32px;
  476. padding: 0 4px;
  477. vertical-align: bottom;
  478. cursor: pointer;
  479. border-bottom: 1px solid $font-color;
  480. img {
  481. width: 100%;
  482. height: 100%;
  483. }
  484. }
  485. .select-content {
  486. display: inline-block;
  487. height: 32px;
  488. margin: 0 10px;
  489. vertical-align: bottom;
  490. cursor: pointer;
  491. border-bottom: 1px solid $font-color;
  492. :deep p {
  493. margin: 0;
  494. }
  495. }
  496. .el-input {
  497. display: inline-flex;
  498. align-items: center;
  499. width: 120px;
  500. margin: 0 10px;
  501. vertical-align: bottom;
  502. &.pinyin :deep input.el-input__inner {
  503. font-family: 'PINYIN-B', sans-serif;
  504. }
  505. &.chinese :deep input.el-input__inner {
  506. font-family: 'arial', sans-serif;
  507. }
  508. &.english :deep input.el-input__inner {
  509. font-family: 'arial', sans-serif;
  510. }
  511. &.right {
  512. :deep input.el-input__inner {
  513. color: $right-color;
  514. }
  515. }
  516. &.wrong {
  517. :deep input.el-input__inner {
  518. color: $error-color;
  519. }
  520. }
  521. :deep .el-input__inner {
  522. padding: 0;
  523. font-size: 16pt;
  524. color: $font-color;
  525. text-align: center;
  526. background-color: #fff;
  527. border-width: 0;
  528. border-bottom: 1px solid $font-color;
  529. border-radius: 0;
  530. }
  531. }
  532. .right-answer {
  533. position: relative;
  534. display: inline-block;
  535. height: 32px;
  536. margin: 0 4px;
  537. line-height: 32px;
  538. vertical-align: bottom;
  539. border-bottom: 1px solid $font-color;
  540. }
  541. }
  542. .record-box {
  543. padding: 6px 12px;
  544. background-color: $fill-color;
  545. :deep .record-time {
  546. width: 100px;
  547. }
  548. }
  549. }
  550. </style>
  551. <style lang="scss" scoped>
  552. .word-list {
  553. display: flex;
  554. flex-wrap: wrap;
  555. gap: 8px;
  556. align-items: center;
  557. .word-item {
  558. cursor: pointer;
  559. }
  560. }
  561. </style>