TablePreview.vue 19 KB

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