TablePreview.vue 19 KB

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