FillPreview.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. v-if="isEnable(data.property.view_pinyin)"
  17. :key="`text-${i}`"
  18. class="content"
  19. :paragraph-list="li.paragraph_list"
  20. :rich-text-list="li.rich_text_list"
  21. :pinyin-position="data.property.pinyin_position"
  22. :is-preview="true"
  23. />
  24. <span v-else :key="`text-${i}`" class="html-content" v-html="renderTextBlockContent(li)"></span>
  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"
  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. </div>
  108. <WriteDialog :visible.sync="writeVisible" @confirm="handleWriteConfirm" />
  109. <PreviewOperation @showAnswerAnalysis="showAnswerAnalysis" @judgeCorrect="judgeCorrect" @retry="retry" />
  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. v-if="isEnable(data.property.view_pinyin)"
  127. :key="`answer-text-${i}`"
  128. class="content"
  129. :paragraph-list="li.paragraph_list"
  130. :rich-text-list="li.rich_text_list"
  131. :pinyin-position="data.property.pinyin_position"
  132. :is-preview="true"
  133. />
  134. <span v-else :key="`answer-text-${i}`" class="html-content" v-html="renderTextBlockContent(li)"></span>
  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. watch: {
  175. 'data.model_essay': {
  176. handler(list) {
  177. if (!list || !Array.isArray(list)) return;
  178. this.modelEssay = JSON.parse(JSON.stringify(list));
  179. this.syncAnswerList(list);
  180. },
  181. deep: true,
  182. immediate: true,
  183. },
  184. modelEssay: {
  185. handler(list) {
  186. if (!list || !Array.isArray(list)) return;
  187. this.syncAnswerList(list);
  188. },
  189. deep: true,
  190. immediate: true,
  191. },
  192. isJudgingRightWrong(val) {
  193. if (!val) return;
  194. this.answer.answer_list.forEach(({ mark, value }) => {
  195. const li = this.modelEssay.find((item) => item.mark === mark);
  196. if (li) {
  197. li.input = value;
  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. renderContent(content) {
  211. return this.convertText(this.sanitizeHTML(content));
  212. },
  213. /**
  214. * 渲染文本块内容
  215. * @param {Object} textBlock 文本块对象,包含纯文本内容和富文本内容
  216. */
  217. renderTextBlockContent(textBlock) {
  218. const richTextList = textBlock?.rich_text_list;
  219. if (Array.isArray(richTextList) && richTextList.length > 0) {
  220. const richHtml = richTextList.map((item) => item?.text || '').join('');
  221. return this.renderContent(richHtml);
  222. }
  223. return this.renderContent(textBlock?.content || '');
  224. },
  225. syncAnswerList(list) {
  226. this.answer.answer_list = list
  227. .map(({ type, input, audio_answer_list, mark }) => {
  228. if (type === 'input') {
  229. return {
  230. value: input,
  231. mark,
  232. audio_answer_list,
  233. write_base64: '',
  234. };
  235. }
  236. return null;
  237. })
  238. .filter((item) => item);
  239. },
  240. /**
  241. * 处理小音频录音
  242. * @param {Object} data 音频数据
  243. * @param {String} mark 选项标识
  244. */
  245. handleMiniWav(data, mark) {
  246. if (!data || !mark) return;
  247. const li = this.modelEssay.find((item) => item?.mark === mark);
  248. if (li) {
  249. this.$set(li, 'audio_answer_list', data);
  250. }
  251. },
  252. /**
  253. * 处理选中词汇
  254. * @param {String} content 选中的词汇内容
  255. * @param {String} mark 选项标识
  256. * @param {Object} li 当前输入框对象
  257. */
  258. handleSelectWord(content, mark, li) {
  259. if (!content || !mark || !li) return;
  260. li.input = content;
  261. this.selectedWordList.push(mark);
  262. },
  263. /**
  264. * 处理书写区确认
  265. * @param {String} data 书写区数据
  266. */
  267. handleWriteConfirm(data) {
  268. if (!data) return;
  269. const li = this.modelEssay.find((item) => item?.mark === this.writeMark);
  270. if (li) {
  271. this.$set(li, 'write_base64', data);
  272. }
  273. },
  274. handleWriteClick(mark) {
  275. this.writeVisible = true;
  276. this.writeMark = mark;
  277. },
  278. getMainStyle() {
  279. const isRow = this.data.property.arrange_type === arrangeTypeList[0].value;
  280. const isFront = this.data.property.audio_position === audioPositionList[0].value;
  281. const isEnableVoice = this.data.property.is_enable_voice_answer === 'true';
  282. const isHasAudio = this.data.audio_file_id.length > 0;
  283. let areaList = [
  284. { name: 'audio', value: '24px' },
  285. { name: 'fill', value: '1fr' },
  286. ];
  287. if (!isHasAudio) {
  288. areaList.shift();
  289. }
  290. if (!isFront && isHasAudio) {
  291. areaList = areaList.reverse();
  292. }
  293. let gridArea = '';
  294. let gridTemplateRows = '';
  295. let gridTemplateColumns = '';
  296. if (isRow) {
  297. const rowAreas = areaList.map(({ name }) => name);
  298. const rowCols = areaList.map(({ value }) => value);
  299. const templateRows = ['auto'];
  300. if (isEnableVoice) {
  301. rowAreas.push('record');
  302. rowCols.push('160px');
  303. }
  304. const areaRows = [`"${rowAreas.join(' ')}"`];
  305. if (this.showLang) {
  306. areaRows.push(`"${Array(rowAreas.length).fill('lang').join(' ')}"`);
  307. templateRows.push('auto');
  308. }
  309. gridArea = areaRows.join(' ');
  310. gridTemplateRows = templateRows.join(' ');
  311. gridTemplateColumns = rowCols.join(' ');
  312. } else {
  313. const areaRows = areaList.map(({ name }) => `"${name}"`);
  314. const templateRows = areaList.map(({ value }) => value);
  315. if (isEnableVoice) {
  316. areaRows.push('"record"');
  317. templateRows.push('32px');
  318. }
  319. if (this.showLang) {
  320. areaRows.push('"lang"');
  321. templateRows.push('auto');
  322. }
  323. gridArea = areaRows.join(' ');
  324. gridTemplateRows = templateRows.join(' ');
  325. gridTemplateColumns = '1fr';
  326. }
  327. return {
  328. 'grid-auto-flow': isRow ? 'column' : 'row',
  329. 'column-gap': isRow && isHasAudio ? '16px' : undefined,
  330. 'row-gap': isRow || !isHasAudio ? undefined : '8px',
  331. 'grid-template-areas': gridArea,
  332. 'grid-template-rows': gridTemplateRows,
  333. 'grid-template-columns': gridTemplateColumns,
  334. };
  335. },
  336. /**
  337. * 计算答题对错选项字体颜色
  338. * @param {string} mark 选项标识
  339. */
  340. computedAnswerClass(mark) {
  341. if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
  342. return '';
  343. }
  344. let selectOption = this.answer.answer_list.find((item) => item.mark === mark);
  345. let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark);
  346. if (!selectOption || !answerOption) return '';
  347. let selectValue = selectOption.value;
  348. let answerValue = answerOption.value;
  349. let answerType = answerOption.type;
  350. let classList = [];
  351. let isRight =
  352. answerType === 'only_one' ? selectValue === answerValue : answerValue.split('/').includes(selectValue);
  353. if (this.isJudgingRightWrong) {
  354. isRight ? classList.push('right') : classList.push('wrong');
  355. }
  356. if (this.isShowRightAnswer && !isRight) {
  357. classList.push('show-right-answer');
  358. }
  359. return classList;
  360. },
  361. /**
  362. * 计算正确答案文本
  363. * @param {string} mark 选项标识
  364. */
  365. computedAnswerText(mark) {
  366. let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark);
  367. if (!answerOption) return '';
  368. let answerValue = answerOption.value;
  369. return `${answerValue}`;
  370. },
  371. // 重做
  372. retry() {
  373. this.modelEssay.forEach((li) => {
  374. if (li.type === 'input') {
  375. li.input = '';
  376. li.write_base64 = '';
  377. }
  378. });
  379. this.selectedWordList = [];
  380. this.isJudgingRightWrong = false;
  381. this.isShowRightAnswer = false;
  382. this.handleWav([]);
  383. },
  384. /**
  385. * 获取无文本内容的数据结构,用于保存为个人模板时的样式模板
  386. */
  387. getNoTextContentData() {
  388. let noTextContentData = JSON.parse(JSON.stringify(this.data));
  389. const resetFieldMap = {
  390. model_essay: [],
  391. content: '',
  392. audio_file_id: '',
  393. file_list: [],
  394. multilingual: [],
  395. analysis_list: [],
  396. answer_list: [],
  397. record_list: [],
  398. };
  399. Object.assign(noTextContentData, resetFieldMap);
  400. if (noTextContentData.answer) {
  401. noTextContentData.answer.answer_list = [];
  402. noTextContentData.answer.reference_answer = '';
  403. }
  404. return noTextContentData;
  405. },
  406. /**
  407. * 处理输入框内容变化
  408. * @description 根据输入框值计算所需长度,动态调整输入框宽度,输入框宽度不小于默认宽度,且不超过组件最大宽度 1000px,需要考虑输入框前面的宽度,保证输入框不会超出组件范围
  409. * @param {String} value 输入框当前值
  410. * @param {String} mark 选项标识
  411. */
  412. handleInput(value, mark) {
  413. if (!mark) return;
  414. const text = `${value || ''}`;
  415. const defaultWidth = Number(this.data?.property?.input_default_width) || 120;
  416. const fontSize = 16;
  417. const fontFamily = this.data?.property?.fill_font || 'arial';
  418. const canvas = document.createElement('canvas');
  419. const context = canvas.getContext('2d');
  420. let textWidth = 0;
  421. if (context) {
  422. context.font = `${fontSize}pt ${fontFamily}`;
  423. textWidth = context.measureText(text || ' ').width;
  424. }
  425. // 额外留出输入框内边距和边框余量,避免刚好卡边。
  426. const contentWidth = Math.ceil(textWidth + 24);
  427. const inputRef = this.$refs[`input-${mark}`];
  428. const inputVm = Array.isArray(inputRef) ? inputRef[0] : inputRef;
  429. const inputEl = inputVm?.$el;
  430. const containerEl = this.$el;
  431. let availableWidth = 1000;
  432. if (inputEl && containerEl) {
  433. const inputRect = inputEl.getBoundingClientRect();
  434. const containerRect = containerEl.getBoundingClientRect();
  435. const rightLimit = containerRect.left + Math.min(containerRect.width, 1000);
  436. availableWidth = Math.floor(rightLimit - inputRect.left - 12);
  437. }
  438. const maxWidth = Math.max(40, availableWidth);
  439. const nextWidth = Math.min(Math.max(contentWidth, defaultWidth), maxWidth);
  440. this.$set(this.inputWidthMap, mark, nextWidth);
  441. },
  442. },
  443. };
  444. </script>
  445. <style lang="scss" scoped>
  446. @use '@/styles/mixin.scss' as *;
  447. .select-preview {
  448. @include preview-base;
  449. .main {
  450. display: grid;
  451. gap: 10px;
  452. align-items: center;
  453. }
  454. .fill-wrapper {
  455. grid-area: fill;
  456. font-size: 16pt;
  457. :deep .pinyin-area .rich-text-container,
  458. :deep .pinyin-area .pinyin-paragraph {
  459. display: inline;
  460. }
  461. p {
  462. margin: 0;
  463. }
  464. .content {
  465. display: inline;
  466. }
  467. .html-content {
  468. :deep p {
  469. display: inline;
  470. }
  471. }
  472. .record-box {
  473. display: inline-flex;
  474. align-items: center;
  475. background-color: #fff;
  476. border-bottom: 1px solid $font-color;
  477. }
  478. .write-click {
  479. display: inline-block;
  480. width: 104px;
  481. height: 32px;
  482. padding: 0 4px;
  483. vertical-align: bottom;
  484. cursor: pointer;
  485. border-bottom: 1px solid $font-color;
  486. img {
  487. width: 100%;
  488. height: 100%;
  489. }
  490. }
  491. .select-content {
  492. display: inline-block;
  493. height: 32px;
  494. margin: 0 10px;
  495. vertical-align: bottom;
  496. cursor: pointer;
  497. border-bottom: 1px solid $font-color;
  498. :deep p {
  499. margin: 0;
  500. }
  501. }
  502. .el-input {
  503. display: inline-flex;
  504. align-items: center;
  505. width: 120px;
  506. margin: 0 10px;
  507. vertical-align: bottom;
  508. &.pinyin :deep input.el-input__inner {
  509. font-family: 'PINYIN-B', sans-serif;
  510. }
  511. &.chinese :deep input.el-input__inner {
  512. font-family: 'arial', sans-serif;
  513. }
  514. &.english :deep input.el-input__inner {
  515. font-family: 'arial', sans-serif;
  516. }
  517. &.right {
  518. :deep input.el-input__inner {
  519. color: $right-color;
  520. }
  521. }
  522. &.wrong {
  523. :deep input.el-input__inner {
  524. color: $error-color;
  525. }
  526. }
  527. :deep .el-input__inner {
  528. padding: 0;
  529. font-size: 16pt;
  530. color: $font-color;
  531. text-align: center;
  532. background-color: #fff;
  533. border-width: 0;
  534. border-bottom: 1px solid $font-color;
  535. border-radius: 0;
  536. }
  537. }
  538. .right-answer {
  539. position: relative;
  540. display: inline-block;
  541. height: 32px;
  542. margin: 0 4px;
  543. line-height: 32px;
  544. vertical-align: bottom;
  545. border-bottom: 1px solid $font-color;
  546. }
  547. }
  548. .record-box {
  549. padding: 6px 12px;
  550. background-color: $fill-color;
  551. :deep .record-time {
  552. width: 100px;
  553. }
  554. }
  555. }
  556. </style>
  557. <style lang="scss" scoped>
  558. .word-list {
  559. display: flex;
  560. flex-wrap: wrap;
  561. gap: 8px;
  562. align-items: center;
  563. .word-item {
  564. cursor: pointer;
  565. }
  566. }
  567. </style>