TablePreview.vue 9.8 KB

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