TableFillQuestion.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <QuestionBase>
  3. <template #content>
  4. <div v-show="isMask" class="mask"></div>
  5. <div class="stem">
  6. <RichText v-model="data.stem" :font-size="18" placeholder="输入题干" />
  7. </div>
  8. <div class="content">
  9. <div class="option-wrapper" :style="computedOptionStyle()">
  10. <template v-for="i in data.property.column_number">
  11. <div :key="i" class="fill-form">
  12. <!-- 头部 -->
  13. <span class="form-header">
  14. <span v-if="i === 1 && isEnable(data.property.is_enable_number_column)" class="header-serial-number">
  15. #
  16. </span>
  17. <el-input v-model="data.option_header_list[i - 1].text" placeholder="请输入" />
  18. </span>
  19. <span v-for="j in data.property.row_number" :key="j" :class="['form-item', `form-item-${i}-${j}`]">
  20. <span v-if="i === 1 && isEnable(data.property.is_enable_number_column)" class="serial-number">
  21. {{ j }}
  22. </span>
  23. <div class="rich">
  24. <RichText
  25. ref="modelEssay"
  26. v-model="data.option_list[j - 1][i - 1].text"
  27. :is-fill="true"
  28. :toolbar="false"
  29. :font-size="12"
  30. :wordlimit-num="false"
  31. placeholder="请输入"
  32. />
  33. </div>
  34. </span>
  35. </div>
  36. <span
  37. v-if="i !== data.property.column_number"
  38. :key="`resize-${i}`"
  39. class="resize"
  40. @mousedown="resize($event, i)"
  41. ></span>
  42. </template>
  43. </div>
  44. <div class="tips">在输入框最前插入##,代表输入框中内容作为正确答案在作答时隐藏。</div>
  45. </div>
  46. </template>
  47. <template #property>
  48. <el-form :model="data.property">
  49. <el-form-item label="题号">
  50. <el-input v-model="data.property.question_number" />
  51. </el-form-item>
  52. <el-form-item label-width="45px">
  53. <el-radio
  54. v-for="{ value, label } in questionNumberTypeList"
  55. :key="value"
  56. v-model="data.other.question_number_type"
  57. :label="value"
  58. >
  59. {{ label }}
  60. </el-radio>
  61. </el-form-item>
  62. <el-form-item label="分值">
  63. <el-radio
  64. v-for="{ value, label } in scoreTypeList"
  65. :key="value"
  66. v-model="data.property.score_type"
  67. :label="value"
  68. >
  69. {{ label }}
  70. </el-radio>
  71. </el-form-item>
  72. <el-form-item label-width="45px">
  73. <el-input-number
  74. v-model="data.property.score"
  75. :min="0"
  76. :step="data.property.score_type === scoreTypeList[0].value ? 1 : 0.1"
  77. />
  78. </el-form-item>
  79. <el-form-item label="序号列">
  80. <el-radio
  81. v-for="{ value, label } in switchOption"
  82. :key="value"
  83. v-model="data.property.is_enable_number_column"
  84. :label="value"
  85. >
  86. {{ label }}
  87. </el-radio>
  88. </el-form-item>
  89. <el-form-item label="列表宽度">
  90. <el-input-number v-model="data.property.form_width" :min="100" :max="3000" :step="100" />
  91. </el-form-item>
  92. <el-form-item label="行数">
  93. <el-input-number v-model="data.property.row_number" :min="1" :max="30" />
  94. </el-form-item>
  95. <el-form-item label="列数">
  96. <el-input-number v-model="data.property.column_number" :min="2" :max="5" />
  97. </el-form-item>
  98. </el-form>
  99. </template>
  100. </QuestionBase>
  101. </template>
  102. <script>
  103. import QuestionMixin from '../common/QuestionMixin.js';
  104. import { getRandomNumber } from '@/utils';
  105. import { getTableFillData, getOption } from '@/views/exercise_questions/data/tableFill.js';
  106. export default {
  107. name: 'TableFillQuestion',
  108. mixins: [QuestionMixin],
  109. data() {
  110. return {
  111. isMask: false,
  112. data: getTableFillData(),
  113. };
  114. },
  115. computed: {
  116. optionList() {
  117. return JSON.parse(JSON.stringify(this.data.option_list));
  118. },
  119. },
  120. watch: {
  121. 'data.property.row_number': {
  122. handler(val) {
  123. if (val > this.data.option_list.length) {
  124. this.data.option_list.push(Array.from({ length: this.data.property.column_number }, () => getOption()));
  125. } else if (val < this.data.option_list.length) {
  126. this.data.option_list.pop();
  127. }
  128. },
  129. immediate: true,
  130. },
  131. 'data.property.column_number': {
  132. handler(val) {
  133. // 列数变化时,需要重新计算每列的宽度
  134. if (val > this.data.option_header_list.length) {
  135. let width = 0;
  136. this.data.option_header_list.forEach((item) => {
  137. // 平均分配宽度,最小宽度为10
  138. let w = Math.max((item.width / val) * this.data.option_header_list.length, 10);
  139. width += item.width - w;
  140. item.width = w;
  141. });
  142. this.data.option_header_list.push({ mark: getRandomNumber(), text: '', width });
  143. } else if (val < this.data.option_header_list.length) {
  144. let width = this.data.option_header_list[val].width;
  145. this.data.option_header_list.pop();
  146. this.data.option_header_list.forEach((item) => {
  147. item.width += width / val;
  148. });
  149. }
  150. this.data.option_list = this.data.option_list.map((item) => {
  151. const arr = [];
  152. for (let i = 0; i < val; i++) {
  153. arr.push(item[i] || getOption());
  154. }
  155. return arr;
  156. });
  157. },
  158. immediate: true,
  159. },
  160. optionList: {
  161. handler(newVal, oldVal) {
  162. if (newVal.length === 0 || !oldVal) return;
  163. if (newVal.length !== oldVal.length || newVal[0].length !== oldVal[0].length) return;
  164. newVal.forEach((item, i) => {
  165. item.forEach((li, j) => {
  166. let newValue = newVal[i][j];
  167. let oldValue = oldVal[i][j];
  168. if (newValue.text === oldValue.text) return;
  169. this.handleOptionChange(newValue, oldValue, i, j);
  170. });
  171. });
  172. },
  173. immediate: true,
  174. deep: true,
  175. },
  176. },
  177. methods: {
  178. /**
  179. *鼠标拖动更改列宽
  180. * @param {MouseEvent} e 鼠标事件
  181. * @param {number} i 选项列表的索引
  182. */
  183. resize(e, i) {
  184. let target = e.target;
  185. let startX = e.clientX;
  186. this.isMask = true;
  187. // 鼠标拖动事件
  188. document.onmousemove = (e) => {
  189. const endX = e.clientX;
  190. let list = this.data.option_header_list;
  191. // 计算拖动的距离与总宽度的比例
  192. let w = ((endX - startX) / this.data.property.form_width) * 100;
  193. // 限制最小宽度为10,最大宽度为总宽度-10
  194. list[i - 1].width = Math.min(Math.max(10, list[i - 1].width + w), list[i - 1].width + list[i].width - 10);
  195. list[i].width = Math.min(Math.max(10, list[i].width - w), list[i - 1].width + list[i].width - 10);
  196. startX = endX;
  197. };
  198. // 鼠标松开事件
  199. document.onmouseup = () => {
  200. this.isMask = false;
  201. document.onmousemove = null;
  202. document.onmouseup = null;
  203. target.releaseCapture && target.releaseCapture(); // 当不在需要继续获得鼠标消息就要应该调用 ReleaseCapture() 释放掉
  204. };
  205. target.setCapture && target.setCapture(); // 该函数在属于当前线程的指定窗口里设置鼠标捕获
  206. return false;
  207. },
  208. // 计算选项样式
  209. computedOptionStyle() {
  210. let gridTemplateColumns = this.data.option_header_list.reduce((acc, { width }, i) => {
  211. if (i === this.data.option_header_list.length - 1) {
  212. return `${acc}${width}%`;
  213. }
  214. return `${acc}${width}% 2px `;
  215. }, '');
  216. return {
  217. gridTemplateColumns,
  218. width: `${this.data.property.form_width}px`,
  219. };
  220. },
  221. /**
  222. * 处理选项内容变化
  223. * @param {object} newValue 新值
  224. * @param {object} oldValue 旧值
  225. * @param {number} i 选项列表的索引
  226. * @param {number} j 选项列表中的索引
  227. */
  228. handleOptionChange(newValue, oldValue, i, j) {
  229. let arr = newValue.text.split(/<p>(.*?)<\/p>/gi).filter((item) => item);
  230. // 答案数组中的索引
  231. let answerIndex = this.data.answer.answer_list.findIndex(({ mark }) => mark === newValue.mark);
  232. if (arr.length === 0) {
  233. if (answerIndex !== -1) {
  234. this.data.answer.answer_list.splice(answerIndex, 1);
  235. answerIndex = -1;
  236. }
  237. }
  238. let isStart = /^##/.test(arr[0]); // 是否以 ## 开头
  239. if (isStart) {
  240. this.handleSpecialCharacterStart(arr, answerIndex, newValue.mark, i, j);
  241. } else {
  242. let str = arr.join().replace(/<span class="rich-fill".*?>(.*?)<\/span>/gi, '###$1###');
  243. this.handleFill(str, answerIndex, newValue.mark, i, j);
  244. }
  245. },
  246. /**
  247. * 处理填空或其他情况
  248. * @param {string} str 处理后的字符串
  249. * @param {number} answerIndex 答案数组的索引
  250. * @param {string} newValueMark 新值的标识
  251. * @param {number} i 选项列表的索引
  252. * @param {number} j 选项列表中的索引
  253. */
  254. handleFill(str, answerIndex, newValueMark, i, j) {
  255. let _str = str;
  256. let start = 0;
  257. let index = 0;
  258. let arr = [];
  259. let matchNum = 0;
  260. while (index !== -1) {
  261. index = _str.indexOf('###', start);
  262. if (index === -1) break;
  263. matchNum += 1;
  264. arr.push({ content: _str.slice(start, index), type: 'text', mark: '' });
  265. if (matchNum % 2 === 0 && arr.length > 0) {
  266. arr[arr.length - 1].type = 'input';
  267. let mark = getRandomNumber();
  268. arr[arr.length - 1].mark = mark;
  269. }
  270. start = index + 3;
  271. }
  272. let last = _str.slice(start);
  273. if (last) {
  274. arr.push({ content: last, type: 'text' });
  275. }
  276. let value_list = arr
  277. .filter(({ type }) => type === 'input')
  278. .map(({ content, mark }) => ({ value: content, mark }));
  279. if (answerIndex === -1 && value_list.length > 0) {
  280. this.data.answer.answer_list.push({
  281. value_list,
  282. mark: newValueMark,
  283. });
  284. } else if (answerIndex !== -1) {
  285. this.data.answer.answer_list[answerIndex].value_list = value_list;
  286. }
  287. this.data.option_list[i][j].content_list = arr.map(({ content, type, mark }) => {
  288. return {
  289. type,
  290. mark,
  291. content: type === 'input' ? '' : content,
  292. };
  293. });
  294. },
  295. /**
  296. * 处理特殊字符开头
  297. * @param {array} arr 文本数组
  298. * @param {number} answerIndex 答案数组的索引
  299. * @param {string} newValueMark 新值的标识
  300. * @param {number} i 选项列表的索引
  301. * @param {number} j 选项列表中的索引
  302. */
  303. handleSpecialCharacterStart(arr, answerIndex, newValueMark, i, j) {
  304. // 去除第一个元素的 ##
  305. arr[0] = arr[0].slice(2);
  306. let _arr = arr.map((item) => {
  307. return item.replace(/<span class="rich-fill".*?>(.*?)<\/span>/gi, '$1');
  308. });
  309. let mark = getRandomNumber();
  310. if (answerIndex === -1) {
  311. this.data.answer.answer_list.push({
  312. value_list: [{ mark, value: _arr.join('') }],
  313. mark: newValueMark,
  314. });
  315. } else {
  316. this.data.answer.answer_list[answerIndex].value_list = [{ mark, value: _arr.join('') }];
  317. }
  318. this.data.option_list[i][j].content_list = [
  319. {
  320. type: 'fill',
  321. mark,
  322. content: '',
  323. },
  324. ];
  325. },
  326. },
  327. };
  328. </script>
  329. <style lang="scss" scoped>
  330. .mask {
  331. position: absolute;
  332. top: 0;
  333. left: 0;
  334. z-index: 1;
  335. width: 100%;
  336. height: 100%;
  337. cursor: col-resize;
  338. }
  339. .content {
  340. width: calc(100vw - 645px);
  341. overflow: auto;
  342. .option-wrapper {
  343. display: grid;
  344. grid-auto-flow: column;
  345. .fill-form {
  346. display: flex;
  347. flex-direction: column;
  348. .form-header {
  349. display: flex;
  350. align-items: center;
  351. height: 40px;
  352. background-color: $fill-color;
  353. .header-serial-number {
  354. padding: 8px 16px;
  355. font-weight: bold;
  356. }
  357. :deep .el-input__inner {
  358. font-weight: bold;
  359. }
  360. }
  361. .form-item {
  362. display: flex;
  363. height: 55px;
  364. border-bottom: $border;
  365. .serial-number {
  366. width: 40px;
  367. min-width: 40px;
  368. line-height: 55px;
  369. text-align: center;
  370. }
  371. .rich {
  372. position: relative;
  373. width: 100%;
  374. overflow: auto;
  375. }
  376. :deep .tox-tinymce {
  377. border-width: 0;
  378. }
  379. :deep .tox .tox-sidebar-wrap {
  380. border-width: 0;
  381. }
  382. :deep .tox-editor-header {
  383. display: none;
  384. }
  385. }
  386. }
  387. .resize {
  388. width: 2px;
  389. height: 100%;
  390. cursor: col-resize;
  391. background-color: #165dff;
  392. border-radius: 4px;
  393. }
  394. }
  395. .tips {
  396. margin-top: 8px;
  397. font-size: 14px;
  398. color: #999;
  399. }
  400. }
  401. .property {
  402. .el-input-number {
  403. width: 200px;
  404. }
  405. }
  406. </style>