TablePreview.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="table-preview" :style="getAreaStyle()">
  4. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  5. <div class="main">
  6. <div
  7. class="table-box"
  8. :style="{
  9. width: isMobile ? '100%' : data.property.width + 'px',
  10. height: data.property.height + 'px',
  11. }"
  12. >
  13. <table
  14. :style="{
  15. width: isMobile ? '100%' : table_width + 'px',
  16. height: data.property.height + 'px',
  17. }"
  18. >
  19. <colgroup>
  20. <col v-for="(item, i) in data.col_width" :key="`col-${i}`" :style="{ width: `${item.value}px` }" />
  21. </colgroup>
  22. <tr v-for="(row, i) in data.option_list" :key="`tr-${i}`">
  23. <template v-for="(col, j) in row">
  24. <td
  25. :key="col.mark"
  26. :style="{
  27. borderTop: i === 0 ? '1px solid ' + data.property.border_color : '',
  28. borderBottom: '1px solid ' + data.property.border_color,
  29. borderLeft:
  30. i === 0 && data.property.first_line_color
  31. ? '1px solid ' + data.property.border_color
  32. : j === 0
  33. ? '2px solid ' + data.property.decoration_color
  34. : '1px dotted ' + data.property.border_color,
  35. borderRight:
  36. i === 0 && data.property.first_line_color
  37. ? '1px solid ' + data.property.border_color
  38. : j === row.length - 1
  39. ? '2px solid ' + data.property.decoration_color
  40. : '1px dotted ' + data.property.border_color,
  41. borderRadius: i === 0 && data.property.first_line_color ? '5px ' : '0',
  42. background:
  43. i === 0 && data.property.first_line_color
  44. ? data.property.first_line_color
  45. : j === 0
  46. ? data.property.first_column_color
  47. : data.mode === 'short' && data.styles.bgColor
  48. ? data.styles.bgColor
  49. : '',
  50. }"
  51. >
  52. <div :style="[tdStyle, computedRichStyle(col.content)]" class="cell-wrap">
  53. <template v-if="isEnable(data.property.view_pinyin)">
  54. <div
  55. v-for="(item, index) in col.model_pinyin"
  56. :key="index"
  57. class="pinyin-text"
  58. :class="[index === 0 ? 'pinyin-text-left' : '']"
  59. >
  60. <template v-if="item.type === 'input'">
  61. <template v-if="data.property.fill_type === fillTypeList[0].value">
  62. <el-input
  63. :key="index"
  64. v-model="item.value"
  65. :disabled="disabled"
  66. :class="[...computedAnswerClass(item, i, j)]"
  67. :style="[{ width: Math.max(40, item.value.length * 21.3) + 'px' }]"
  68. />
  69. </template>
  70. <template v-else-if="data.property.fill_type === fillTypeList[1].value">
  71. <el-popover :key="index" placement="top" trigger="click">
  72. <div class="word-list">
  73. <span
  74. v-for="{ content, mark } in data.word_list"
  75. :key="mark"
  76. class="word-item"
  77. @click="handleSelectWord(content, mark, item)"
  78. >
  79. {{ content }}
  80. </span>
  81. </div>
  82. <el-input
  83. slot="reference"
  84. v-model="item.value"
  85. :readonly="true"
  86. :class="[...computedAnswerClass(item, i, j)]"
  87. :style="[{ width: Math.max(40, item.value.length * 21.3) + 'px' }]"
  88. />
  89. </el-popover>
  90. </template>
  91. <template v-else-if="data.property.fill_type === fillTypeList[2].value">
  92. <span :key="j" class="write-click" @click="handleWriteClick(item)">
  93. <img
  94. v-show="item.write_base64"
  95. style="background-color: #f4f4f4"
  96. :src="item.write_base64"
  97. alt="write-show"
  98. />
  99. </span>
  100. </template>
  101. <template v-else-if="data.property.fill_type === fillTypeList[3].value">
  102. <SoundRecordBox
  103. ref="record"
  104. :key="j"
  105. type="mini"
  106. :many-times="false"
  107. class="record-box"
  108. :answer-record-list="data.audio_answer_list"
  109. :task-model="isJudgingRightWrong ? 'ANSWER' : ''"
  110. :attrib="data.unified_attrib"
  111. @handleWav="handleMiniWav($event, item)"
  112. />
  113. </template>
  114. <span v-if="data.property.pinyin_position === 'bottom'" class="pinyin">&nbsp;</span>
  115. </template>
  116. <template v-else>
  117. <span v-if="data.property.pinyin_position === 'top'" class="pinyin">
  118. {{ item.pinyin.replace(/\s+/g, '') }}
  119. </span>
  120. <span :style="{ ...item.activeTextStyle }">
  121. {{ item.text }}
  122. </span>
  123. <span v-if="data.property.pinyin_position === 'bottom'" class="pinyin">{{
  124. item.pinyin.replace(/\s+/g, '')
  125. }}</span>
  126. </template>
  127. </div>
  128. </template>
  129. <template v-else>
  130. <div v-for="(item, index) in col.model_essay" :key="index" :style="[tdStyle]">
  131. <span
  132. v-if="item.type === 'text'"
  133. :key="index"
  134. :style="{
  135. fontSize:
  136. data.unified_attrib && data.unified_attrib.font_size ? data.unified_attrib.font_size : '',
  137. fontFamily: data.unified_attrib && data.unified_attrib.font ? data.unified_attrib.font : '',
  138. }"
  139. v-html="sanitizeHTML(item.value)"
  140. ></span>
  141. <template v-if="item.type === 'input'">
  142. <template v-if="data.property.fill_type === fillTypeList[0].value">
  143. <el-input
  144. :key="index"
  145. v-model="item.value"
  146. :disabled="disabled"
  147. :class="[...computedAnswerClass(item, i, j)]"
  148. :style="[{ width: Math.max(40, item.value.length * 21.3) + 'px' }]"
  149. />
  150. </template>
  151. <template v-else-if="data.property.fill_type === fillTypeList[1].value">
  152. <el-popover :key="index" placement="top" trigger="click">
  153. <div class="word-list">
  154. <span
  155. v-for="{ content, mark } in data.word_list"
  156. :key="mark"
  157. class="word-item"
  158. @click="handleSelectWord(content, mark, item)"
  159. >
  160. {{ content }}
  161. </span>
  162. </div>
  163. <el-input
  164. slot="reference"
  165. v-model="item.value"
  166. :readonly="true"
  167. :class="[...computedAnswerClass(item, i, j)]"
  168. :style="[{ width: Math.max(40, item.value.length * 21.3) + 'px' }]"
  169. />
  170. </el-popover>
  171. </template>
  172. <template v-else-if="data.property.fill_type === fillTypeList[2].value">
  173. <span :key="j" class="write-click" @click="handleWriteClick(item)">
  174. <img
  175. v-show="item.write_base64"
  176. style="background-color: #f4f4f4"
  177. :src="item.write_base64"
  178. alt="write-show"
  179. />
  180. </span>
  181. </template>
  182. <template v-else-if="data.property.fill_type === fillTypeList[3].value">
  183. <SoundRecordBox
  184. ref="record"
  185. :key="j"
  186. type="mini"
  187. :many-times="false"
  188. class="record-box"
  189. :answer-record-list="data.audio_answer_list"
  190. :task-model="isJudgingRightWrong ? 'ANSWER' : ''"
  191. :attrib="data.unified_attrib"
  192. @handleWav="handleMiniWav($event, item)"
  193. />
  194. </template>
  195. <span
  196. v-show="computedAnswerText(item, i, j).length > 0"
  197. :key="`answer-${j}`"
  198. class="right-answer"
  199. >
  200. {{ computedAnswerText(item, i, j) }}
  201. </span>
  202. </template>
  203. </div>
  204. </template>
  205. </div>
  206. <span v-if="showLang" class="multilingual" :style="[tdStyle, computedRichStyle(col.content)]">
  207. {{
  208. multilingualTextList[getLang()] &&
  209. multilingualTextList[getLang()][i] &&
  210. multilingualTextList[getLang()][i][j]
  211. ? multilingualTextList[getLang()][i][j]
  212. : ''
  213. }}
  214. </span>
  215. </td>
  216. </template>
  217. </tr>
  218. </table>
  219. </div>
  220. </div>
  221. <WriteDialog :visible.sync="writeVisible" @confirm="handleWriteConfirm" />
  222. </div>
  223. </template>
  224. <script>
  225. import { getTableData, fillTypeList } from '@/views/book/courseware/data/table';
  226. import PreviewMixin from '../common/PreviewMixin';
  227. import WriteDialog from '../fill/components/WriteDialog.vue';
  228. import SoundRecordBox from '@/views/book/courseware/preview/components/record_input/SoundRecord.vue';
  229. export default {
  230. name: 'TablePreview',
  231. components: { SoundRecordBox, WriteDialog },
  232. mixins: [PreviewMixin],
  233. props: {
  234. isMobile: {
  235. type: Boolean,
  236. default: false,
  237. },
  238. },
  239. data() {
  240. return {
  241. data: getTableData(),
  242. table_width: 0,
  243. multilingualTextList: {}, // 多语言对应的切割后的翻译
  244. fillTypeList,
  245. selectedWordList: [], // 用于存储选中的词汇
  246. writeVisible: false,
  247. writeMark: '',
  248. };
  249. },
  250. computed: {
  251. tdStyle() {
  252. let obj = {};
  253. if (this.data.mode === 'short') {
  254. let styles = this.data.styles;
  255. obj = {
  256. fontFamily: styles.fontFamily,
  257. fontSize: styles.fontSize,
  258. color: styles.fontColor,
  259. textDecoration: styles.isUnderline ? 'underline' : styles.isStrikethrough ? 'line-through' : '',
  260. fontWeight: styles.isBold ? 'bold' : '',
  261. fontStyle: styles.isItalic ? 'italic' : '',
  262. justifyContent: styles.textAlign,
  263. textAlign: styles.textAlign,
  264. };
  265. }
  266. return obj;
  267. },
  268. },
  269. watch: {
  270. 'data.col_width': {
  271. handler(val) {
  272. this.handleData();
  273. },
  274. },
  275. isJudgingRightWrong(val) {
  276. if (!val) return;
  277. this.data.answer_list = this.answer.answer_list;
  278. // this.answer.answer_list.forEach(({ mark, value }) => {
  279. // this.modelEssay.forEach((item) => {
  280. // item.forEach((li) => {
  281. // if (li.mark === mark) {
  282. // li.content = value;
  283. // }
  284. // });
  285. // });
  286. // });
  287. },
  288. },
  289. created() {
  290. this.handleData();
  291. },
  292. methods: {
  293. handleData() {
  294. this.table_width = 0;
  295. this.data.col_width.forEach((item) => {
  296. this.table_width += Number(item.value);
  297. });
  298. this.data.multilingual.forEach((item) => {
  299. let trans_arr = item.translation.split('\n');
  300. let chunkSize = this.data.property.column_count;
  301. let chunkedArr = trans_arr.reduce((acc, curr, index) => {
  302. // 当索引是chunkSize的倍数时,开始一个新的子数组
  303. if (index % chunkSize === 0) {
  304. acc.push([curr]); // 开始新的子数组并添加当前元素
  305. } else {
  306. acc[acc.length - 1].push(curr); // 将当前元素添加到最后一个子数组中
  307. }
  308. return acc;
  309. }, []);
  310. this.$set(this.multilingualTextList, item.type, chunkedArr);
  311. });
  312. if (!this.isJudgingRightWrong) {
  313. this.answer.answer_list = this.data.answer_list;
  314. }
  315. },
  316. computedAnswerText(item, i, j) {
  317. if (!this.isShowRightAnswer) return '';
  318. let answerOption =
  319. this.data.answer_list[i] && this.data.answer_list[i][j] ? this.data.answer_list[i][j].answer : '';
  320. let answerOptionList = answerOption.split('\n');
  321. if (!item) return '';
  322. let selectValue = item.value ? item.value.trim() : '';
  323. let answerValue = answerOptionList[item.inputIndex] ? answerOptionList[item.inputIndex].split('/') : '';
  324. let isRight = answerValue && answerValue.includes(selectValue);
  325. if (isRight || !answerValue) return '';
  326. return `(${answerValue.join('/')})`;
  327. },
  328. /**
  329. * 计算答题对错选项字体颜色
  330. * @param {string} mark 选项标识
  331. */
  332. computedAnswerClass(item, i, j) {
  333. if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
  334. return '';
  335. }
  336. let answerOption =
  337. this.data.answer_list[i] && this.data.answer_list[i][j] ? this.data.answer_list[i][j].answer : '';
  338. let answerOptionList = answerOption.split('\n');
  339. if (!item) return '';
  340. let selectValue = item.value ? item.value.trim() : '';
  341. let answerValue = answerOptionList[item.inputIndex] ? answerOptionList[item.inputIndex].split('/') : '';
  342. let classList = [];
  343. let isRight = answerValue && answerValue.includes(selectValue);
  344. if (this.isJudgingRightWrong && answerValue) {
  345. isRight ? classList.push('right') : classList.push('wrong');
  346. }
  347. if (this.isShowRightAnswer && !isRight) {
  348. classList.push('show-right-answer');
  349. }
  350. return classList;
  351. },
  352. /**
  353. * 处理小音频录音
  354. * @param {Object} data 音频数据
  355. * @param {String} mark 选项标识
  356. */
  357. handleMiniWav(data, mark) {
  358. if (!data || !mark) return;
  359. this.$set(mark, 'audio_answer_list', data);
  360. },
  361. /**
  362. * 处理选中词汇
  363. * @param {String} content 选中的词汇内容
  364. * @param {String} mark 选项标识
  365. * @param {Object} li 当前输入框对象
  366. */
  367. handleSelectWord(content, mark, li) {
  368. if (!content || !mark || !li || this.disabled) return;
  369. li.value = content;
  370. this.selectedWordList.push(mark);
  371. },
  372. /**
  373. * 处理书写区确认
  374. * @param {String} data 书写区数据
  375. */
  376. handleWriteConfirm(data) {
  377. if (!data) return;
  378. this.$set(this.writeMark, 'write_base64', data);
  379. },
  380. handleWriteClick(mark) {
  381. this.writeVisible = true;
  382. this.writeMark = mark;
  383. },
  384. computedRichStyle(content) {
  385. if (this.data.mode === 'short') return {};
  386. if (!content) return {};
  387. // 提取首个标签的 style 样式属性
  388. let styleMatch = content.match(/<\w+\s+[^>]*style=["']([^"']*)["'][^>]*>/i);
  389. if (styleMatch && styleMatch[1]) {
  390. let styleString = styleMatch[1];
  391. let styleArray = styleString.split(';').filter((s) => s.trim() !== '');
  392. let styleObject = {};
  393. styleArray.forEach((style) => {
  394. let [key, value] = style.split(':');
  395. if (key && value) {
  396. styleObject[key.trim()] = value.trim();
  397. }
  398. });
  399. return styleObject;
  400. }
  401. return {};
  402. },
  403. },
  404. };
  405. </script>
  406. <style lang="scss" scoped>
  407. @use '@/styles/mixin.scss' as *;
  408. $select-color: #1890ff;
  409. $border-color: #e6e6e6;
  410. .table-preview {
  411. @include preview-base;
  412. .main {
  413. color: #262626;
  414. .table-box {
  415. margin: 0 auto;
  416. overflow: auto;
  417. }
  418. table {
  419. // border-spacing: 3px;
  420. border-collapse: separate;
  421. }
  422. .cell-wrap {
  423. display: flex;
  424. flex-flow: wrap;
  425. grid-area: fill;
  426. align-items: end;
  427. p {
  428. flex-grow: 1; // 为了常规模式时设置居中显示撑满整个单元格
  429. max-width: 100%;
  430. margin: 0;
  431. }
  432. .record-box {
  433. display: inline-flex;
  434. align-items: center;
  435. background-color: #fff;
  436. border-bottom: 1px solid $font-color;
  437. }
  438. .write-click {
  439. display: inline-block;
  440. width: 104px;
  441. height: 32px;
  442. padding: 0 4px;
  443. vertical-align: bottom;
  444. cursor: pointer;
  445. border-bottom: 1px solid $font-color;
  446. img {
  447. width: 100%;
  448. height: 100%;
  449. }
  450. }
  451. .el-input {
  452. display: inline-flex;
  453. align-items: center;
  454. width: 120px;
  455. margin: 0 2px;
  456. font-size: 16px;
  457. &.pinyin :deep input.el-input__inner {
  458. font-family: 'PINYIN-B', sans-serif;
  459. }
  460. &.chinese :deep input.el-input__inner {
  461. font-family: 'arial', sans-serif;
  462. }
  463. &.english :deep input.el-input__inner {
  464. font-family: 'arial', sans-serif;
  465. }
  466. &.right {
  467. :deep input.el-input__inner {
  468. color: $right-color;
  469. }
  470. }
  471. &.wrong {
  472. :deep input.el-input__inner {
  473. color: $error-color;
  474. }
  475. }
  476. & + .right-answer {
  477. position: relative;
  478. left: -4px;
  479. display: inline-block;
  480. height: 32px;
  481. line-height: 28px;
  482. vertical-align: bottom;
  483. border-bottom: 1px solid $font-color;
  484. }
  485. :deep input.el-input__inner {
  486. padding: 0;
  487. // font-size: 16pt;
  488. color: $font-color;
  489. text-align: center;
  490. background-color: #fff;
  491. border-width: 0;
  492. border-bottom: 1px solid $font-color;
  493. border-radius: 0;
  494. }
  495. }
  496. &-normal {
  497. p {
  498. width: 100%; // 为了解决换行问题
  499. }
  500. }
  501. }
  502. .multilingual {
  503. display: block;
  504. word-break: break-word;
  505. }
  506. .pinyin-text {
  507. display: flex;
  508. flex-direction: column;
  509. padding: 0 1px;
  510. font-size: 16px;
  511. span {
  512. text-align: center;
  513. }
  514. .pinyin {
  515. height: 18px;
  516. font-family: 'League';
  517. font-size: 12px;
  518. }
  519. &-left {
  520. span {
  521. // text-align: left;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. .word-list {
  528. display: flex;
  529. flex-wrap: wrap;
  530. gap: 8px;
  531. align-items: center;
  532. .word-item {
  533. cursor: pointer;
  534. }
  535. }
  536. </style>