Table.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <ModuleBase :type="data.type">
  3. <template #content>
  4. <div class="fun-type">
  5. <a
  6. v-for="{ value, label } in tableTypeList"
  7. :key="value"
  8. :class="[data.mode === value ? 'active' : '']"
  9. @click="data.mode = value"
  10. >{{ label }}</a
  11. >
  12. </div>
  13. <template v-if="data.mode === 'short'">
  14. <div class="table-rich-toolbar">
  15. <el-select v-model="data.styles.fontFamily" placeholder="请选择" style="width: 130px">
  16. <el-option v-for="{ value, label } in fontFamilyList" :key="value" :label="label" :value="value" />
  17. </el-select>
  18. <el-select v-model="data.styles.fontSize" placeholder="请选择" style="width: 130px">
  19. <el-option
  20. v-for="(value, index) in 16"
  21. :key="index"
  22. :label="6 + value * 2 + 'pt'"
  23. :value="6 + value * 2 + 'pt'"
  24. />
  25. </el-select>
  26. <el-form :model="property" inline>
  27. <el-form-item label="文字颜色">
  28. <el-color-picker v-model="data.styles.fontColor" />
  29. </el-form-item>
  30. <el-form-item label="背景色">
  31. <el-color-picker v-model="data.styles.bgColor" />
  32. </el-form-item>
  33. </el-form>
  34. <span
  35. :class="[data.styles.isUnderline ? 'active' : '']"
  36. @click="data.styles.isUnderline = !data.styles.isUnderline"
  37. >
  38. <SvgIcon icon-class="underline" size="20" />
  39. </span>
  40. <span :class="[data.styles.isBold ? 'active' : '']" @click="data.styles.isBold = !data.styles.isBold">
  41. <SvgIcon icon-class="font-bold" size="20" />
  42. </span>
  43. <span :class="[data.styles.isItalic ? 'active' : '']" @click="data.styles.isItalic = !data.styles.isItalic">
  44. <SvgIcon icon-class="font-italic" size="20" />
  45. </span>
  46. <span
  47. :class="[data.styles.isStrikethrough ? 'active' : '']"
  48. @click="data.styles.isStrikethrough = !data.styles.isStrikethrough"
  49. >
  50. <SvgIcon icon-class="strikethrough" size="20" />
  51. </span>
  52. <span :class="[data.styles.textAlign === 'start' ? 'active' : '']" @click="data.styles.textAlign = 'left'">
  53. <SvgIcon icon-class="align-left" size="20" />
  54. </span>
  55. <span :class="[data.styles.textAlign === 'center' ? 'active' : '']" @click="data.styles.textAlign = 'center'">
  56. <SvgIcon icon-class="align-center" size="20" />
  57. </span>
  58. <span :class="[data.styles.textAlign === 'end' ? 'active' : '']" @click="data.styles.textAlign = 'right'">
  59. <SvgIcon icon-class="align-right" size="20" />
  60. </span>
  61. </div>
  62. </template>
  63. <div class="option-list">
  64. <div v-for="(item, i) in data.option_list" :key="i" class="table-node">
  65. <div v-for="li in item" :key="li.mark" class="table-item">
  66. <!-- eslint-disable max-len -->
  67. <RichText
  68. v-if="data.mode === 'normal'"
  69. ref="richText"
  70. v-model="li.content"
  71. :font-size="data?.unified_attrib?.font_size"
  72. :font-family="data?.unified_attrib?.font"
  73. :inline="true"
  74. toolbar="fontselect fontsizeselect forecolor backcolor | underline | bold italic strikethrough alignleft aligncenter alignright"
  75. @handleRichTextBlur="handleBlurCon"
  76. />
  77. <el-input v-else v-model="li.content" @blur="handleBlurCon" />
  78. </div>
  79. </div>
  80. <div class="table-node">
  81. <div v-for="(value, index) in data.col_width" :key="index" class="table-item">
  82. <el-input v-model="value.value"> <template slot="prepend">列宽:</template></el-input>
  83. </div>
  84. </div>
  85. </div>
  86. <el-input
  87. v-if="data.property.fill_type === fillTypeList[1].value"
  88. v-model="data.vocabulary"
  89. type="textarea"
  90. :autosize="{ minRows: 2, maxRows: 4 }"
  91. resize="none"
  92. placeholder="请输入词汇,用于选词填空"
  93. />
  94. <p class="tips">在需要作答的单元格内输入三个以上下划线“___”</p>
  95. <el-button @click="identifyText">识别</el-button>
  96. <el-button @click="handleMultilingual">多语言</el-button>
  97. <template v-if="isEnable(data.has_identify)">
  98. <p class="tips">在需要作答的单元格内录入标准答案,多个填空答案用换行录入,同一个填空有多个答案用斜线“/”隔开</p>
  99. <div class="option-list">
  100. <div v-for="(item, i) in data.answer_list" :key="i" class="table-node">
  101. <div v-for="(li, j) in item" :key="i + 'col' + j" class="table-item">
  102. <el-input v-model="li.answer" type="textarea" />
  103. </div>
  104. </div>
  105. </div>
  106. </template>
  107. <MultilingualFill
  108. :visible.sync="multilingualVisible"
  109. :text="multilingualText"
  110. :translations="data.multilingual"
  111. @SubmitTranslation="handleMultilingualTranslation"
  112. />
  113. <el-divider v-if="isEnable(data.property.view_pinyin)" content-position="left">拼音效果</el-divider>
  114. <PinyinText
  115. v-if="isEnable(data.property.view_pinyin)"
  116. :id="'table_pinyin_text'"
  117. ref="PinyinText"
  118. :paragraph-list="data.paragraph_list"
  119. :pinyin-position="data.property.pinyin_position"
  120. :pinyin-size="data?.unified_attrib?.pinyin_size"
  121. :font-size="data?.unified_attrib?.font_size"
  122. :font-family="data?.unified_attrib?.font"
  123. @fillCorrectPinyin="fillCorrectPinyin"
  124. />
  125. </template>
  126. </ModuleBase>
  127. </template>
  128. <script>
  129. import { isEnable } from '@/views/book/courseware/data/common';
  130. import ModuleMixin from '../../common/ModuleMixin';
  131. import { CrateParsedTextInfo_Pinyin } from '@/api/book';
  132. import { getRandomNumber } from '@/utils';
  133. import PinyinText from '@/components/PinyinText.vue';
  134. import {
  135. getTableData,
  136. getOption,
  137. tableTypeList,
  138. fontFamilyList,
  139. getAnswerOption,
  140. fillTypeList,
  141. } from '@/views/book/courseware/data/table';
  142. export default {
  143. name: 'TablePage',
  144. components: {
  145. PinyinText,
  146. },
  147. mixins: [ModuleMixin],
  148. data() {
  149. return {
  150. isEnable,
  151. data: getTableData(),
  152. tableTypeList,
  153. fontFamilyList,
  154. multilingualText: '',
  155. isViewExplanatoryNoteDialog: false,
  156. oldRichData: {},
  157. fillTypeList,
  158. };
  159. },
  160. watch: {
  161. 'data.property.row_count': {
  162. handler(val) {
  163. if (val < this.data.option_list.length) {
  164. this.data.option_list = this.data.option_list.slice(0, val);
  165. this.data.answer_list = this.data.answer_list.slice(0, val);
  166. } else {
  167. const diff = val - this.data.option_list.length;
  168. for (let i = 0; i < diff; i++) {
  169. this.data.option_list.push(Array.from({ length: this.data.property.column_count }, getOption));
  170. this.data.answer_list.push(Array.from({ length: this.data.property.column_count }, getAnswerOption));
  171. }
  172. }
  173. },
  174. },
  175. 'data.property.column_count': {
  176. handler(val) {
  177. const diff = val - this.data.option_list[0].length;
  178. for (let i = 0; i < diff; i++) {
  179. this.data.col_width.push({
  180. value: 100,
  181. });
  182. }
  183. this.data.option_list = this.data.option_list.map((row) => {
  184. if (val < row.length) {
  185. this.data.col_width = this.data.col_width.slice(0, val);
  186. return row.slice(0, val);
  187. }
  188. const diff = val - row.length;
  189. return row.concat(Array.from({ length: diff }, getOption));
  190. });
  191. this.data.answer_list = this.data.answer_list.map((row) => {
  192. if (val < row.length) {
  193. return row.slice(0, val);
  194. }
  195. const diff = val - row.length;
  196. return row.concat(Array.from({ length: diff }, getAnswerOption));
  197. });
  198. },
  199. },
  200. 'data.property.view_pinyin': {
  201. handler(val) {
  202. let text = '';
  203. this.data.option_list.forEach((item) => {
  204. item.forEach((items) => {
  205. text += `${items.content.replace(/<[^>]+>/g, '')}\n`;
  206. });
  207. });
  208. if (!isEnable(val)) {
  209. this.data.paragraph_list = [];
  210. return;
  211. }
  212. if (isEnable(val) && text && this.data.paragraph_list.length <= 0) {
  213. this.data.paragraph_list_parameter.text = text;
  214. this.data.paragraph_list_parameter.is_first_sentence_first_hz_pinyin_first_char_upper_case =
  215. this.data.property.is_first_sentence_first_hz_pinyin_first_char_upper_case;
  216. this.createParsedTextInfoPinyin(text);
  217. }
  218. },
  219. deep: true,
  220. },
  221. 'data.property.is_first_sentence_first_hz_pinyin_first_char_upper_case': {
  222. handler(val, oldVal) {
  223. if (val === oldVal) return;
  224. let text = '';
  225. this.data.option_list.forEach((item) => {
  226. item.forEach((items) => {
  227. text += `${items.content.replace(/<[^>]+>/g, '')}\n`;
  228. });
  229. });
  230. if (!isEnable(this.data.property.view_pinyin)) {
  231. this.data.paragraph_list = [];
  232. return;
  233. }
  234. if (text && isEnable(this.data.property.view_pinyin)) {
  235. this.data.paragraph_list_parameter.text = text;
  236. this.data.paragraph_list_parameter.is_first_sentence_first_hz_pinyin_first_char_upper_case = val;
  237. this.createParsedTextInfoPinyin(text);
  238. }
  239. },
  240. deep: true,
  241. },
  242. 'data.vocabulary': {
  243. handler(val) {
  244. if (!val) return;
  245. this.data.word_list = val
  246. .split(/[\n\r]+/)
  247. .map((item) => item.split(' ').filter((s) => s))
  248. .flat()
  249. .map((content) => {
  250. return {
  251. content,
  252. mark: getRandomNumber(),
  253. };
  254. });
  255. },
  256. },
  257. },
  258. methods: {
  259. // 识别文本
  260. identifyText() {
  261. let text = '';
  262. this.data.has_identify = 'true';
  263. this.data.option_list.forEach((item, index) => {
  264. item.forEach((items, indexs) => {
  265. items.model_essay = [];
  266. // this.data.answer_list[index][indexs].answer_list = [];
  267. if (items.content) {
  268. let inputIndex = 0;
  269. items.content
  270. .split(/<(p|div)[^>]*>(.*?)<\/(p|div)>/g)
  271. .filter((s) => s && !s.match(/^(p|div)$/))
  272. .forEach((itemss) => {
  273. if (itemss.charCodeAt() === 10) return;
  274. let strArr = itemss.split(/_{3,}/g);
  275. strArr.forEach((itemS, index) => {
  276. items.model_essay.push({
  277. value: itemS,
  278. type: 'text',
  279. });
  280. if (index !== strArr.length - 1) {
  281. items.model_essay.push({
  282. value: '',
  283. type: 'input',
  284. mark: getRandomNumber(),
  285. inputIndex,
  286. write_base64: '',
  287. audio_answer_list: [],
  288. });
  289. inputIndex += 1;
  290. }
  291. });
  292. // // 去除所有的 font-size 样式
  293. // .replace(/font-size:\s*\d+(\.\d+)?px;/gi, '')
  294. // // 匹配 class 名为 rich-fill 的 span 标签和三个以上的_,并将它们组成数组
  295. // .replace(/<span class="rich-fill".*?>(.*?)<\/span>|([_]{3,})/gi, '###$1$2###');
  296. });
  297. }
  298. text += `${items.content.replace(/<[^>]+>/g, '')}\n`;
  299. });
  300. });
  301. if (isEnable(this.data.property.view_pinyin)) {
  302. this.createParsedTextInfoPinyin(text);
  303. }
  304. },
  305. // 获取拼音解析文本
  306. createParsedTextInfoPinyin(text) {
  307. if (text === '') {
  308. this.data.paragraph_list_parameter.pinyin_proofread_word_list = [];
  309. return;
  310. }
  311. this.data.paragraph_list_parameter.text = text.replace(/<[^>]+>/g, '');
  312. this.data.paragraph_list_parameter.is_first_sentence_first_hz_pinyin_first_char_upper_case =
  313. this.data.property.is_first_sentence_first_hz_pinyin_first_char_upper_case;
  314. CrateParsedTextInfo_Pinyin(this.data.paragraph_list_parameter).then((res) => {
  315. if (res.parsed_text) {
  316. const mergedData = res.parsed_text.paragraph_list.map((outerArr, i) =>
  317. outerArr.map((innerArr, j) =>
  318. innerArr.map((newItem, k) => {
  319. // 从 originalData 中找到对应的项
  320. const originalItem = this.data.paragraph_list[i]?.[j]?.[k];
  321. // 如果 originalItem 有 activeTextStyle,就合并到 newItem
  322. if (originalItem?.activeTextStyle) {
  323. return {
  324. ...newItem,
  325. activeTextStyle: originalItem.activeTextStyle,
  326. };
  327. }
  328. // 否则直接返回 newItem
  329. return newItem;
  330. }),
  331. ),
  332. );
  333. this.data.paragraph_list = mergedData;
  334. let pinyin_index = 0;
  335. this.data.option_list.forEach((item, index) => {
  336. item.forEach((items, indexs) => {
  337. items.model_pinyin = [];
  338. let inputIndex = 0;
  339. if (items.content && mergedData[pinyin_index] && mergedData[pinyin_index][0]) {
  340. mergedData[pinyin_index][0].forEach((itemP) => {
  341. let isUnderline = /^_{3,}$/.test(itemP.text);
  342. if (isUnderline) {
  343. items.model_pinyin.push({
  344. value: '',
  345. type: 'input',
  346. mark: getRandomNumber(),
  347. inputIndex,
  348. });
  349. inputIndex++;
  350. } else {
  351. items.model_pinyin.push(itemP);
  352. }
  353. });
  354. pinyin_index++;
  355. }
  356. });
  357. });
  358. }
  359. });
  360. },
  361. // 填充校对后的拼音
  362. fillCorrectPinyin({ selectContent: { text, pinyin, activeTextStyle }, i, j, k }) {
  363. this.data.paragraph_list_parameter.pinyin_proofread_word_list.push({
  364. paragraph_index: i,
  365. sentence_index: j,
  366. word_index: k,
  367. word: text,
  368. pinyin,
  369. });
  370. if (pinyin) this.data.paragraph_list[i][j][k].pinyin = pinyin;
  371. if (activeTextStyle) this.data.paragraph_list[i][j][k].activeTextStyle = activeTextStyle;
  372. },
  373. // 思维导图数据
  374. handleMindMap() {
  375. let node_list = [];
  376. this.data.option_list.forEach((item) => {
  377. item.forEach((items) => {
  378. node_list.push({
  379. name: items.content.replace(/<[^>]*>?/gm, ''),
  380. id: Math.random().toString(36).substring(2, 12),
  381. });
  382. });
  383. });
  384. this.data.mind_map.node_list = node_list;
  385. },
  386. handleBlurCon() {
  387. this.handleMindMap();
  388. },
  389. handleMultilingual() {
  390. this.multilingualText = '';
  391. this.data.option_list.forEach((item) => {
  392. item.forEach((items) => {
  393. this.multilingualText += items.content ? `<p>${items.content}</p>` : '<p>&nbsp;</p>';
  394. });
  395. });
  396. this.multilingualVisible = true;
  397. },
  398. },
  399. };
  400. </script>
  401. <style lang="scss" scoped>
  402. .option-list {
  403. margin-bottom: 12px;
  404. .table-node {
  405. display: flex;
  406. row-gap: 16px;
  407. .table-item {
  408. flex: 1;
  409. min-width: 75px;
  410. padding: 8px;
  411. border: 1px solid $border-color;
  412. &:not(:last-child) {
  413. border-right: 0;
  414. }
  415. :deep p {
  416. margin: 0;
  417. }
  418. }
  419. }
  420. }
  421. .fun-type {
  422. display: flex;
  423. gap: 5px;
  424. width: 100%;
  425. padding-bottom: 10px;
  426. border-bottom: 1px solid #e5e6eb;
  427. a {
  428. padding: 5px 10px;
  429. font-weight: normal;
  430. color: #1d2129;
  431. cursor: pointer;
  432. background: #f2f3f5;
  433. border: 1px solid #f2f3f5;
  434. border-radius: 2px;
  435. &.active {
  436. color: #165dff;
  437. background: #e7eeff;
  438. border-color: #165dff;
  439. }
  440. }
  441. }
  442. .table-rich-toolbar {
  443. display: flex;
  444. gap: 5px;
  445. padding: 5px 0;
  446. /* align-items: center; */
  447. :deep .el-form-item--small.el-form-item {
  448. margin: 0 5px 0 0;
  449. }
  450. :deep .el-form-item__label {
  451. padding-right: 3px;
  452. }
  453. > span {
  454. height: 30px;
  455. padding: 5px 6px;
  456. color: #222f3e;
  457. cursor: pointer;
  458. border-radius: 5px;
  459. &:hover {
  460. background: #cce2fa;
  461. }
  462. &.active {
  463. background: #a6ccf7;
  464. }
  465. }
  466. }
  467. .tips {
  468. font-size: 12px;
  469. color: #999;
  470. }
  471. </style>