Table.vue 16 KB

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