TablePreview.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. }"
  49. >
  50. <div :style="[tdStyle]" class="cell-wrap">
  51. <p v-for="(item, index) in col.model_essay" :key="index">
  52. <span v-if="item.type === 'text'" :key="index" v-html="sanitizeHTML(item.value)"></span>
  53. <template v-if="item.type === 'input'">
  54. <el-input
  55. :key="index"
  56. v-model="item.value"
  57. :disabled="disabled"
  58. :style="[{ width: Math.max(80, item.value.length * 21.3) + 'px' }]"
  59. />
  60. <!-- <span v-show="computedAnswerText(li.mark).length > 0" :key="`answer-${indexs}`" class="right-answer">
  61. {{ computedAnswerText(li.mark) }}
  62. </span> -->
  63. </template>
  64. </p>
  65. </div>
  66. <span class="multilingual" v-if="showLang">
  67. {{
  68. multilingualTextList[getLang()] &&
  69. multilingualTextList[getLang()][i] &&
  70. multilingualTextList[getLang()][i][j]
  71. ? multilingualTextList[getLang()][i][j]
  72. : ''
  73. }}
  74. </span>
  75. </td>
  76. </template>
  77. </tr>
  78. </table>
  79. </div>
  80. </div>
  81. </div>
  82. </template>
  83. <script>
  84. import { getTableData } from '@/views/book/courseware/data/table';
  85. import PreviewMixin from '../common/PreviewMixin';
  86. export default {
  87. name: 'TablePreview',
  88. components: {},
  89. mixins: [PreviewMixin],
  90. data() {
  91. return {
  92. data: getTableData(),
  93. table_width: 0,
  94. multilingualTextList: {}, // 多语言对应的切割后的翻译
  95. };
  96. },
  97. computed: {
  98. tdStyle() {
  99. let obj = {};
  100. if (this.data.mode === 'short') {
  101. let styles = this.data.styles;
  102. obj = {
  103. fontFamily: styles.fontFamily,
  104. fontSize: styles.fontSize,
  105. fontColor: styles.fontColor,
  106. backgroundColor: styles.bgColor,
  107. textDecoration: styles.isUnderline ? 'underline' : styles.isStrikethrough ? 'line-through' : '',
  108. fontWeight: styles.isBold ? 'bold' : '',
  109. fontStyle: styles.isItalic ? 'italic' : '',
  110. justifyContent: styles.textAlign,
  111. };
  112. }
  113. return obj;
  114. },
  115. },
  116. watch: {
  117. 'data.col_width': {
  118. handler(val) {
  119. this.handleData();
  120. },
  121. },
  122. },
  123. created() {
  124. this.handleData();
  125. },
  126. mounted() {},
  127. beforeDestroy() {},
  128. methods: {
  129. handleData() {
  130. this.table_width = 0;
  131. this.data.col_width.forEach((item) => {
  132. this.table_width += Number(item.value);
  133. });
  134. if (this.showLang) {
  135. this.data.multilingual.forEach((item) => {
  136. let trans_arr = item.translation.split('\n');
  137. let chunkSize = this.data.property.column_count;
  138. let chunkedArr = trans_arr.reduce((acc, curr, index) => {
  139. // 当索引是chunkSize的倍数时,开始一个新的子数组
  140. if (index % chunkSize === 0) {
  141. acc.push([curr]); // 开始新的子数组并添加当前元素
  142. } else {
  143. acc[acc.length - 1].push(curr); // 将当前元素添加到最后一个子数组中
  144. }
  145. return acc;
  146. }, []);
  147. this.$set(this.multilingualTextList, item.type, chunkedArr);
  148. });
  149. }
  150. },
  151. computedAnswerText(mark) {
  152. if (!this.isShowRightAnswer) return '';
  153. let selectOption = this.answer.answer_list.find((item) => item.mark === mark);
  154. let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark);
  155. if (!selectOption) return '';
  156. let selectValue = selectOption.value;
  157. let answerValue = answerOption.value;
  158. let isRight = selectValue === answerValue;
  159. if (isRight) return '';
  160. return `(${answerValue})`;
  161. },
  162. },
  163. };
  164. </script>
  165. <style lang="scss" scoped>
  166. @use '@/styles/mixin.scss' as *;
  167. $select-color: #1890ff;
  168. $border-color: #e6e6e6;
  169. .table-preview {
  170. @include preview-base;
  171. .main {
  172. color: #262626;
  173. .table-box {
  174. overflow: auto;
  175. }
  176. table {
  177. // border-spacing: 3px;
  178. border-collapse: separate;
  179. }
  180. .cell-wrap {
  181. display: flex;
  182. grid-area: fill;
  183. p {
  184. margin: 0;
  185. }
  186. .el-input {
  187. display: inline-flex;
  188. align-items: center;
  189. width: 120px;
  190. margin: 0 2px;
  191. &.pinyin :deep input.el-input__inner {
  192. font-family: 'PINYIN-B', sans-serif;
  193. }
  194. &.chinese :deep input.el-input__inner {
  195. font-family: 'arial', sans-serif;
  196. }
  197. &.english :deep input.el-input__inner {
  198. font-family: 'arial', sans-serif;
  199. }
  200. &.right {
  201. :deep input.el-input__inner {
  202. color: $right-color;
  203. }
  204. }
  205. &.wrong {
  206. :deep input.el-input__inner {
  207. color: $error-color;
  208. }
  209. }
  210. & + .right-answer {
  211. position: relative;
  212. left: -4px;
  213. display: inline-block;
  214. height: 32px;
  215. line-height: 28px;
  216. vertical-align: bottom;
  217. border-bottom: 1px solid $font-color;
  218. }
  219. :deep input.el-input__inner {
  220. padding: 0;
  221. font-size: 16pt;
  222. color: $font-color;
  223. text-align: center;
  224. background-color: #fff;
  225. border-width: 0;
  226. border-bottom: 1px solid $font-color;
  227. border-radius: 0;
  228. }
  229. }
  230. }
  231. .multilingual {
  232. word-break: break-word;
  233. }
  234. }
  235. }
  236. </style>