TablePreview.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. :attrib="data.unified_attrib"
  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. v-html="sanitizeHTML(item.value)"
  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. ></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. @handleWav="handleMiniWav($event, item)"
  191. :attrib="data.unified_attrib"
  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. data() {
  233. return {
  234. data: getTableData(),
  235. table_width: 0,
  236. multilingualTextList: {}, // 多语言对应的切割后的翻译
  237. fillTypeList,
  238. selectedWordList: [], // 用于存储选中的词汇
  239. writeVisible: false,
  240. writeMark: '',
  241. };
  242. },
  243. computed: {
  244. tdStyle() {
  245. let obj = {};
  246. if (this.data.mode === 'short') {
  247. let styles = this.data.styles;
  248. obj = {
  249. fontFamily: styles.fontFamily,
  250. fontSize: styles.fontSize,
  251. color: styles.fontColor,
  252. textDecoration: styles.isUnderline ? 'underline' : styles.isStrikethrough ? 'line-through' : '',
  253. fontWeight: styles.isBold ? 'bold' : '',
  254. fontStyle: styles.isItalic ? 'italic' : '',
  255. justifyContent: styles.textAlign,
  256. textAlign: styles.textAlign,
  257. };
  258. }
  259. return obj;
  260. },
  261. },
  262. watch: {
  263. 'data.col_width': {
  264. handler(val) {
  265. this.handleData();
  266. },
  267. },
  268. isJudgingRightWrong(val) {
  269. if (!val) return;
  270. // this.answer.answer_list.forEach(({ mark, value }) => {
  271. // this.modelEssay.forEach((item) => {
  272. // item.forEach((li) => {
  273. // if (li.mark === mark) {
  274. // li.content = value;
  275. // }
  276. // });
  277. // });
  278. // });
  279. },
  280. },
  281. created() {
  282. this.handleData();
  283. },
  284. methods: {
  285. handleData() {
  286. this.table_width = 0;
  287. this.data.col_width.forEach((item) => {
  288. this.table_width += Number(item.value);
  289. });
  290. if (this.showLang) {
  291. this.data.multilingual.forEach((item) => {
  292. let trans_arr = item.translation.split('\n');
  293. let chunkSize = this.data.property.column_count;
  294. let chunkedArr = trans_arr.reduce((acc, curr, index) => {
  295. // 当索引是chunkSize的倍数时,开始一个新的子数组
  296. if (index % chunkSize === 0) {
  297. acc.push([curr]); // 开始新的子数组并添加当前元素
  298. } else {
  299. acc[acc.length - 1].push(curr); // 将当前元素添加到最后一个子数组中
  300. }
  301. return acc;
  302. }, []);
  303. this.$set(this.multilingualTextList, item.type, chunkedArr);
  304. });
  305. }
  306. if (!this.isJudgingRightWrong) {
  307. }
  308. },
  309. computedAnswerText(item, i, j) {
  310. if (!this.isShowRightAnswer) return '';
  311. let answerOption =
  312. this.data.answer_list[i] && this.data.answer_list[i][j] ? this.data.answer_list[i][j].answer : '';
  313. let answerOptionList = answerOption.split('\n');
  314. if (!item) return '';
  315. let selectValue = item.value ? item.value.trim() : '';
  316. let answerValue = answerOptionList[item.inputIndex] ? answerOptionList[item.inputIndex].split('/') : '';
  317. let isRight = answerValue && answerValue.includes(selectValue);
  318. if (isRight || !answerValue) return '';
  319. return `(${answerValue.join('/')})`;
  320. },
  321. /**
  322. * 计算答题对错选项字体颜色
  323. * @param {string} mark 选项标识
  324. */
  325. computedAnswerClass(item, i, j) {
  326. if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
  327. return '';
  328. }
  329. let answerOption =
  330. this.data.answer_list[i] && this.data.answer_list[i][j] ? this.data.answer_list[i][j].answer : '';
  331. let answerOptionList = answerOption.split('\n');
  332. if (!item) return '';
  333. let selectValue = item.value ? item.value.trim() : '';
  334. let answerValue = answerOptionList[item.inputIndex] ? answerOptionList[item.inputIndex].split('/') : '';
  335. let classList = [];
  336. let isRight = answerValue && answerValue.includes(selectValue);
  337. if (this.isJudgingRightWrong && answerValue) {
  338. isRight ? classList.push('right') : classList.push('wrong');
  339. }
  340. if (this.isShowRightAnswer && !isRight) {
  341. classList.push('show-right-answer');
  342. }
  343. return classList;
  344. },
  345. /**
  346. * 处理小音频录音
  347. * @param {Object} data 音频数据
  348. * @param {String} mark 选项标识
  349. */
  350. handleMiniWav(data, mark) {
  351. if (!data || !mark) return;
  352. this.$set(mark, 'audio_answer_list', data);
  353. },
  354. /**
  355. * 处理选中词汇
  356. * @param {String} content 选中的词汇内容
  357. * @param {String} mark 选项标识
  358. * @param {Object} li 当前输入框对象
  359. */
  360. handleSelectWord(content, mark, li) {
  361. if (!content || !mark || !li || this.disabled) return;
  362. li.value = content;
  363. this.selectedWordList.push(mark);
  364. },
  365. /**
  366. * 处理书写区确认
  367. * @param {String} data 书写区数据
  368. */
  369. handleWriteConfirm(data) {
  370. if (!data) return;
  371. this.$set(this.writeMark, 'write_base64', data);
  372. },
  373. handleWriteClick(mark) {
  374. this.writeVisible = true;
  375. this.writeMark = mark;
  376. },
  377. computedRichStyle(content) {
  378. if (this.data.mode === 'short') return {};
  379. if (!content) return {};
  380. // 提取首个标签的 style 样式属性
  381. let styleMatch = content.match(/<\w+\s+[^>]*style=["']([^"']*)["'][^>]*>/i);
  382. if (styleMatch && styleMatch[1]) {
  383. let styleString = styleMatch[1];
  384. let styleArray = styleString.split(';').filter((s) => s.trim() !== '');
  385. let styleObject = {};
  386. styleArray.forEach((style) => {
  387. let [key, value] = style.split(':');
  388. if (key && value) {
  389. styleObject[key.trim()] = value.trim();
  390. }
  391. });
  392. return styleObject;
  393. }
  394. return {};
  395. },
  396. },
  397. };
  398. </script>
  399. <style lang="scss" scoped>
  400. @use '@/styles/mixin.scss' as *;
  401. $select-color: #1890ff;
  402. $border-color: #e6e6e6;
  403. .table-preview {
  404. @include preview-base;
  405. .main {
  406. color: #262626;
  407. .table-box {
  408. margin: 0 auto;
  409. overflow: auto;
  410. }
  411. table {
  412. // border-spacing: 3px;
  413. border-collapse: separate;
  414. }
  415. .cell-wrap {
  416. display: flex;
  417. flex-flow: wrap;
  418. grid-area: fill;
  419. align-items: end;
  420. p {
  421. width: 100%;
  422. margin: 0;
  423. }
  424. .record-box {
  425. display: inline-flex;
  426. align-items: center;
  427. background-color: #fff;
  428. border-bottom: 1px solid $font-color;
  429. }
  430. .write-click {
  431. display: inline-block;
  432. width: 104px;
  433. height: 32px;
  434. padding: 0 4px;
  435. vertical-align: bottom;
  436. cursor: pointer;
  437. border-bottom: 1px solid $font-color;
  438. img {
  439. width: 100%;
  440. height: 100%;
  441. }
  442. }
  443. .el-input {
  444. display: inline-flex;
  445. align-items: center;
  446. width: 120px;
  447. margin: 0 2px;
  448. font-size: 16px;
  449. &.pinyin :deep input.el-input__inner {
  450. font-family: 'PINYIN-B', sans-serif;
  451. }
  452. &.chinese :deep input.el-input__inner {
  453. font-family: 'arial', sans-serif;
  454. }
  455. &.english :deep input.el-input__inner {
  456. font-family: 'arial', sans-serif;
  457. }
  458. &.right {
  459. :deep input.el-input__inner {
  460. color: $right-color;
  461. }
  462. }
  463. &.wrong {
  464. :deep input.el-input__inner {
  465. color: $error-color;
  466. }
  467. }
  468. & + .right-answer {
  469. position: relative;
  470. left: -4px;
  471. display: inline-block;
  472. height: 32px;
  473. line-height: 28px;
  474. vertical-align: bottom;
  475. border-bottom: 1px solid $font-color;
  476. }
  477. :deep input.el-input__inner {
  478. padding: 0;
  479. // font-size: 16pt;
  480. color: $font-color;
  481. text-align: center;
  482. background-color: #fff;
  483. border-width: 0;
  484. border-bottom: 1px solid $font-color;
  485. border-radius: 0;
  486. }
  487. }
  488. }
  489. .multilingual {
  490. word-break: break-word;
  491. }
  492. .pinyin-text {
  493. display: flex;
  494. flex-direction: column;
  495. padding: 0 1px;
  496. font-size: 16px;
  497. span {
  498. text-align: center;
  499. }
  500. .pinyin {
  501. height: 18px;
  502. font-family: 'League';
  503. font-size: 12px;
  504. }
  505. &-left {
  506. span {
  507. // text-align: left;
  508. }
  509. }
  510. }
  511. }
  512. }
  513. .word-list {
  514. display: flex;
  515. flex-wrap: wrap;
  516. gap: 8px;
  517. align-items: center;
  518. .word-item {
  519. cursor: pointer;
  520. }
  521. }
  522. </style>