MatchingPreview.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="matching-preview" :style="[getAreaStyle(), getComponentStyle()]">
  4. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  5. <div class="main">
  6. <ul ref="list" class="option-list">
  7. <li v-for="(item, i) in data.option_list" :key="i" class="list-item">
  8. <div
  9. v-for="({ content, mark, multilingual, paragraph_list }, j) in item"
  10. :key="mark"
  11. :class="['item-wrapper', `item-${mark}`, computedAnswerClass(mark, i, j), { isMobile: isMobile }]"
  12. :style="{
  13. cursor: disabled ? 'default' : 'pointer',
  14. flex: isMobile ? '0 1 auto' : '',
  15. width: isMobile ? 'calc(50% - 8px)' : '',
  16. overflow: isMobile ? 'auto' : '',
  17. }"
  18. @mousedown="mousedown($event, i, j, mark)"
  19. @mouseup="mouseup($event, i, j, mark)"
  20. @click="handleClickConnection($event, i, j, mark)"
  21. >
  22. <PinyinText
  23. v-if="isEnable(data.property.view_pinyin)"
  24. class="content"
  25. :paragraph-list="paragraph_list"
  26. :pinyin-position="data.property.pinyin_position"
  27. :is-preview="true"
  28. />
  29. <span v-else class="content rich-text" v-html="sanitizeHTML(content)"></span>
  30. <div v-if="showLang" class="lang">
  31. {{ multilingual.find((item) => item.type === getLang())?.translation }}
  32. </div>
  33. </div>
  34. </li>
  35. </ul>
  36. <div v-if="isShowRightAnswer" class="right-answer">
  37. <div class="title">正确答案</div>
  38. <ul ref="answerList" class="option-list">
  39. <li v-for="(item, i) in data.option_list" :key="i" class="list-item">
  40. <div v-for="{ content, mark } in item" :key="mark" :class="['item-wrapper', `answer-item-${mark}`]">
  41. <span class="content rich-text" v-html="sanitizeHTML(content)"></span>
  42. </div>
  43. </li>
  44. </ul>
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. import { getMatchingData, svgNS } from '@/views/book/courseware/data/matching';
  51. import { arraysIntersect } from '@/utils/validate';
  52. import PreviewMixin from '../common/PreviewMixin';
  53. export default {
  54. name: 'MatchingPreview',
  55. mixins: [PreviewMixin],
  56. data() {
  57. return {
  58. data: getMatchingData(),
  59. answerList: [], // 答案列表
  60. curConnectionPoint: { i: -1, j: -1, mark: '' }, // 当前连线点
  61. // 拖拽相关
  62. drag: false,
  63. mouseEvent: {
  64. clientX: 0,
  65. clientY: 0,
  66. },
  67. };
  68. },
  69. watch: {
  70. 'data.option_list': {
  71. handler(val) {
  72. this.clearLine();
  73. if (!val) return;
  74. let list = val.map((item) => {
  75. return item.map(({ mark }) => {
  76. return { mark, preMark: [], nextMark: [] };
  77. });
  78. });
  79. this.$set(this, 'answerList', list);
  80. },
  81. immediate: true,
  82. },
  83. answerList: {
  84. handler(list) {
  85. let arr = [];
  86. list.forEach((item) => {
  87. let markArr = [];
  88. item.forEach(({ mark, nextMark }, j) => {
  89. if (j === 0) {
  90. markArr.push(mark);
  91. markArr.push([...nextMark]);
  92. } else if (item.length === 3 && j === 1) {
  93. markArr.push([...nextMark]);
  94. }
  95. });
  96. arr.push(markArr);
  97. });
  98. // 答案数组为 第一个值为固定字符串,后面为连接的值数组,如:['A', ['B', 'C'], ['1', '2']],根据此结构来判断对错、绘制连线
  99. this.answer.answer_list = arr;
  100. },
  101. deep: true,
  102. },
  103. isShowRightAnswer(cur) {
  104. if (cur) {
  105. this.$nextTick(() => {
  106. this.circulateAnswerList();
  107. });
  108. }
  109. },
  110. },
  111. created() {
  112. document.addEventListener('click', this.handleEventConnection);
  113. document.addEventListener('mousemove', this.documentMousemouse);
  114. document.addEventListener('mouseup', this.documentMouseup);
  115. },
  116. beforeDestroy() {
  117. document.removeEventListener('click', this.handleEventConnection);
  118. document.removeEventListener('mousemove', this.documentMousemouse);
  119. document.removeEventListener('mouseup', this.documentMouseup);
  120. },
  121. methods: {
  122. /* 用 mouse 事件模拟拖拽 开始*/
  123. documentMousemouse(e) {
  124. if (!this.drag) return;
  125. // 使用 svg 绘制跟随鼠标移动的连接线
  126. let svg = document.querySelector('.move-connection');
  127. let list = this.$refs.list.getBoundingClientRect(); // 列表的位置
  128. let { clientX, clientY } = e;
  129. let isLeft = clientX < this.mouseEvent.clientX; // 鼠标是否向左移动
  130. // 计算 svg 的宽度,宽度不能超过列表的宽度
  131. let width = Math.min(list.width, isLeft ? list.width - clientX + list.left - 1 : clientX - list.left - 1);
  132. if (svg) {
  133. svg.setAttribute(
  134. 'style',
  135. `position: absolute; ${isLeft ? 'right: 0;' : 'left: 0;'} width: ${width}px; height: 100%;`,
  136. );
  137. } else {
  138. svg = document.createElementNS(svgNS, 'svg');
  139. svg.classList.add('move-connection');
  140. svg.setAttribute('style', `position: absolute; width: ${width}px; height: 100%;`);
  141. let path = document.createElementNS(svgNS, 'path');
  142. this.setPathAttr(path);
  143. svg.appendChild(path);
  144. this.$refs.list.appendChild(svg);
  145. }
  146. let top = this.mouseEvent.clientY - list.top;
  147. let left = isLeft
  148. ? this.mouseEvent.clientX - list.left - Math.abs(width - list.width)
  149. : this.mouseEvent.clientX - list.left;
  150. let mouseX = isLeft ? clientX - list.left - Math.abs(width - list.width) : clientX - list.left;
  151. let mouseY = clientY - list.top;
  152. let path = svg.querySelector('path');
  153. path.setAttribute('d', `M ${left} ${top} L ${mouseX} ${mouseY}`);
  154. },
  155. documentMouseup() {
  156. if (!this.drag) return;
  157. this.drag = false;
  158. document.querySelector('.move-connection')?.remove();
  159. document.body.style.userSelect = 'auto'; // 允许选中文本
  160. this.mousePointer = { i: -1, j: -1, mark: '' };
  161. this.mouseEvent = { clientX: 0, clientY: 0 };
  162. },
  163. /**
  164. * 鼠标按下事件,设置当前连线点
  165. * @param {PointerEvent} e 事件对象
  166. * @param {number} i 选项列表索引
  167. * @param {number} j 选项索引
  168. * @param {string} mark 选项标识
  169. */
  170. mousedown(e, i, j, mark) {
  171. if (this.disabled) return;
  172. this.drag = true;
  173. document.body.style.userSelect = 'none'; // 禁止选中文本
  174. this.mouseEvent = { clientX: e.clientX, clientY: e.clientY };
  175. this.mousePointer = { i, j, mark };
  176. },
  177. /**
  178. * 鼠标抬起事件,如果是一个合适的连接点,则创建连接线
  179. * @param {PointerEvent} e 事件对象
  180. * @param {Number} i 选项列表索引
  181. * @param {Number} j 选项索引
  182. * @param {String} mark 选项标识
  183. */
  184. mouseup(e, i, j, mark) {
  185. if (this.disabled || !this.drag) return;
  186. let { i: curI, j: curJ, mark: curMark } = this.mousePointer;
  187. if (curI === -1 && curJ === -1) return;
  188. if (Math.abs(curJ - j) > 1 || mark === curMark) return;
  189. this.changeConnectionList(mark, j, true);
  190. this.createLine(mark, true);
  191. },
  192. /* 用 mouse 事件模拟拖拽 结束 */
  193. // 重置当前连线点
  194. resetCurConnectionPoint() {
  195. this.curConnectionPoint = { i: -1, j: -1, mark: '' };
  196. },
  197. /**
  198. * 当点击的不是连线点时,清除所有连线点的选中状态
  199. * @param {PointerEvent} e
  200. */
  201. handleEventConnection(e) {
  202. let currentNode = e.target;
  203. while (currentNode !== null) {
  204. if (currentNode.classList && currentNode.classList.contains('item-wrapper')) {
  205. break;
  206. }
  207. currentNode = currentNode.parentNode;
  208. }
  209. if (currentNode) return;
  210. Array.from(document.getElementsByClassName('item-wrapper')).forEach((item) => {
  211. item.classList.remove('focus');
  212. });
  213. this.resetCurConnectionPoint();
  214. },
  215. /**
  216. * 处理点击连线
  217. * @param {PointerEvent} e 事件对象
  218. * @param {Number} i 选项列表索引
  219. * @param {Number} j 选项索引
  220. * @param {String} mark 选项标识
  221. */
  222. handleClickConnection(e, i, j, mark) {
  223. if (this.disabled) return;
  224. let { i: curI, j: curJ, mark: curMark } = this.curConnectionPoint;
  225. // 获取 item-wrapper 元素
  226. let currentNode = e.target;
  227. while (currentNode !== null) {
  228. if (currentNode.classList && currentNode.classList.contains('item-wrapper')) {
  229. break;
  230. }
  231. currentNode = currentNode.parentNode;
  232. }
  233. // 如果当前连线点不存在或就是当前连线点,则设置当前连线点
  234. if ((curI === -1 && curJ === -1) || mark === curMark) {
  235. this.curConnectionPoint = { i, j, mark };
  236. currentNode.classList.add('focus');
  237. return;
  238. }
  239. // 如果当前连线点存在,清除所有连线点的选中状态
  240. Array.from(this.$refs.list.getElementsByClassName('item-wrapper')).forEach((item) => {
  241. item.classList.remove('focus');
  242. });
  243. // 如果当前连线点与上一个连线点不在相邻的位置,则设置点击的连接点为当前连线点
  244. if (Math.abs(curJ - j) > 1 || mark === curMark || (curJ === j && curI !== i)) {
  245. this.curConnectionPoint = { i, j, mark };
  246. currentNode.classList.add('focus');
  247. return;
  248. }
  249. this.changeConnectionList(mark, j);
  250. // 如果当前连线点与上一个连线点在相邻的位置,则创建连接线
  251. this.createLine(mark);
  252. },
  253. /**
  254. * @description 循环答案列表
  255. */
  256. circulateAnswerList() {
  257. this.data.answer.answer_list.forEach((item, index) => {
  258. let preMark = ''; // 上一个连线点标识
  259. item.forEach(({ mark }, j) => {
  260. if (j === 0) {
  261. preMark = mark;
  262. return;
  263. }
  264. if (mark.length <= 0) return;
  265. if (j === 2) {
  266. preMark = this.data.option_list[index][j - 1].mark;
  267. }
  268. let cur = { i: -1, j: -1, mark: '' }; // 当前连线点
  269. // 根据 preMark 查找当前连线点的位置
  270. this.data.option_list.find((list, i) => {
  271. return list.find((li, idx) => {
  272. if (li.mark === preMark) {
  273. cur = { i, j: idx };
  274. return true;
  275. }
  276. });
  277. });
  278. // 循环 mark 数组,创建连接线
  279. mark.forEach((m) => {
  280. this.curConnectionPoint = { i: cur.i, j: cur.j, mark: preMark };
  281. this.createLine(m, false, true);
  282. });
  283. });
  284. });
  285. },
  286. /**
  287. * 创建连接线
  288. * @param {String} mark 选项标识
  289. * @param {Boolean} isDrag 是否是拖拽
  290. * @param {Boolean} isShowRightAnswer 是否是显示正确答案
  291. */
  292. createLine(mark, isDrag = false, isShowRightAnswer = false) {
  293. const { offsetWidth, offsetLeft, offsetTop, offsetHeight } = document.getElementsByClassName(
  294. `${isShowRightAnswer ? 'answer-' : ''}item-${mark}`,
  295. )[0];
  296. const { curOffsetWidth, curOffsetLeft, curOffsetTop, curOffsetHeight, curMark } = this.computedCurConnectionPoint(
  297. isDrag,
  298. isShowRightAnswer,
  299. );
  300. let top = Math.min(offsetTop + offsetHeight / 2, curOffsetTop + curOffsetHeight / 2);
  301. // 判断是否是同一行
  302. const isSameRow = Math.abs(offsetTop + offsetHeight / 2 - (curOffsetTop + curOffsetHeight / 2)) <= 2;
  303. let left = Math.min(offsetLeft + offsetWidth, curOffsetLeft + curOffsetWidth);
  304. let width = Math.abs(
  305. offsetLeft > curOffsetLeft
  306. ? curOffsetLeft - offsetLeft + offsetWidth
  307. : offsetLeft - curOffsetLeft + curOffsetWidth,
  308. );
  309. let height = 0;
  310. if (!isSameRow) {
  311. height =
  312. curOffsetTop > offsetTop
  313. ? Math.abs(offsetTop + offsetHeight / 2 - (curOffsetTop + curOffsetHeight / 2))
  314. : Math.abs(offsetTop - curOffsetTop + offsetHeight / 2 - curOffsetHeight / 2);
  315. }
  316. let size = offsetLeft > curOffsetLeft ? offsetTop > curOffsetTop : offsetTop < curOffsetTop; // 判断是左上还是右下
  317. // 创建一个空的SVG元素
  318. let svg = document.createElementNS(svgNS, 'svg');
  319. svg.setAttribute(
  320. 'style',
  321. `position:absolute; width: 74px; height: ${Math.max(8, height)}px; top: ${top}px; left: ${left}px;overflow: visible;`,
  322. );
  323. svg.classList.add('connection-line', `svg-${mark}-${curMark}`); // 添加类名
  324. // 向SVG元素添加 path 元素
  325. let path = document.createElementNS(svgNS, 'path');
  326. path.setAttribute('d', `M ${size ? 0 : width} 0 L ${size ? width : 0} ${height}`); // 设置路径数据
  327. this.setPathAttr(path);
  328. svg.appendChild(path);
  329. // 在 svg 元素中的 path 元素上两端添加圆形标记
  330. let circleStart = document.createElementNS(svgNS, 'circle');
  331. circleStart.setAttribute('cx', size ? '0' : width); // 设置圆心的 x 坐标
  332. circleStart.setAttribute('cy', '0'); // 设置圆心的 y 坐标
  333. circleStart.setAttribute('r', '4'); // 设置半径
  334. const assistColor = this.data?.unified_attrib?.topic_color || '#306eff';
  335. circleStart.setAttribute('fill', assistColor); // 设置填充颜色
  336. svg.appendChild(circleStart);
  337. let circleEnd = document.createElementNS(svgNS, 'circle');
  338. circleEnd.setAttribute('cx', size ? width : '0'); // 设置圆心的 x 坐标
  339. circleEnd.setAttribute('cy', height); // 设置圆心的 y 坐标
  340. circleEnd.setAttribute('r', '4'); // 设置半径
  341. circleEnd.setAttribute('fill', assistColor); // 设置填充颜色
  342. svg.appendChild(circleEnd);
  343. this.$refs[isShowRightAnswer ? 'answerList' : 'list'].appendChild(svg); // 将SVG元素插入到文档中
  344. // 清除当前连线点
  345. this.resetCurConnectionPoint();
  346. },
  347. // 设置 path 公用属性
  348. setPathAttr(path) {
  349. path.setAttribute('stroke-width', '2'); // 设置线条宽度
  350. path.setAttribute('stroke-linecap', 'round'); // 设置线段的两端样式
  351. const assistColor = this.data?.unified_attrib?.topic_color || '#306eff';
  352. path.setAttribute('stroke', assistColor); // 设置填充颜色
  353. },
  354. /**
  355. * 计算当前连线点的位置
  356. * @param {Boolean} isDrag 是否是拖拽
  357. * @param {Boolean} isShowRightAnswer 是否是显示正确答案
  358. */
  359. computedCurConnectionPoint(isDrag = false, isShowRightAnswer = false) {
  360. let { mark } = isDrag ? this.mousePointer : this.curConnectionPoint;
  361. let type = Object.prototype.toString.call(mark);
  362. if (type === '[object String]') {
  363. type = 'string';
  364. } else if (type === '[object Object]') {
  365. type = 'object';
  366. }
  367. if (type === 'object') {
  368. mark = mark.mark;
  369. }
  370. const dom = document.getElementsByClassName(`${isShowRightAnswer ? 'answer-' : ''}item-${mark}`)[0];
  371. return {
  372. curOffsetWidth: dom.offsetWidth,
  373. curOffsetLeft: dom.offsetLeft,
  374. curOffsetTop: dom.offsetTop,
  375. curOffsetHeight: dom.offsetHeight,
  376. curMark: mark,
  377. };
  378. },
  379. // 清除所有连接线
  380. clearLine() {
  381. document.querySelectorAll('svg.connection-line').forEach((item) => {
  382. item.remove();
  383. });
  384. },
  385. /**
  386. * 修改连接列表
  387. * @param {String} mark 选项标识
  388. * @param {Number} j 当前选项索引
  389. * @param {Boolean} isDrag 是否是拖拽
  390. */
  391. changeConnectionList(mark, j, isDrag = false) {
  392. const { mark: curMark, j: curJ } = isDrag ? this.mousePointer : this.curConnectionPoint;
  393. this.changeAnswerList(curMark, mark, curJ < j);
  394. this.changeAnswerList(mark, curMark, curJ > j);
  395. },
  396. /**
  397. * 改变答案列表
  398. * @param {String} curMark 当前选项标识
  399. * @param {String} mark 选项标识
  400. */
  401. changeAnswerList(curMark, mark, isPre) {
  402. this.answerList.find((item) =>
  403. item.find((li) => {
  404. if (li.mark === curMark) {
  405. if (isPre && !li.preMark.includes(mark)) {
  406. li.nextMark.push(mark);
  407. } else if (!isPre && !li.nextMark.includes(mark)) {
  408. li.preMark.push(mark);
  409. }
  410. return true;
  411. }
  412. }),
  413. );
  414. },
  415. /**
  416. * 根据 mark 查找 nextMark 或 preMark
  417. * @param {Array} list 答案列表
  418. * @param {String} mark 标记
  419. * @param {'pre'|'next'} type 类型
  420. * @returns {String} 返回 nextMark 或 preMark
  421. */
  422. findMark(list, mark, type) {
  423. let fMark = '';
  424. list.find((item) => {
  425. return item.find((li) => {
  426. if (mark === li.mark) {
  427. fMark = type === 'pre' ? li.preMark : li.nextMark;
  428. return true;
  429. }
  430. });
  431. });
  432. return fMark;
  433. },
  434. /**
  435. * 计算答题对错选项class
  436. * @param {string} mark 选项标识
  437. * @param {number} i 选项列表索引
  438. * @param {number} j 选项索引
  439. * @returns {string} 返回 class 名称
  440. */
  441. computedAnswerClass(mark, i, j) {
  442. if (!this.isJudgingRightWrong) return '';
  443. if (j === 0) {
  444. return this.judgeFirstAnswerRightWrong(i);
  445. }
  446. let answer = this.data.answer.answer_list.find((item) => {
  447. return item.some((li) => Array.isArray(li.mark) && li.mark.includes(mark));
  448. }); // 正确答案列
  449. let userAnswer = this.answer.answer_list.find((item) => {
  450. return item.some((li) => Array.isArray(li) && li.includes(mark));
  451. }); // 选项对应的用户答案列
  452. if (answer === undefined || userAnswer === undefined) return 'wrong';
  453. if (answer.length === 0 && userAnswer.length === 0) return '';
  454. if (answer.length === 0 || userAnswer.length === 0) return 'wrong';
  455. let isRight = true;
  456. if (j === 1) {
  457. // 判断第二行答案对错,answer[0].mark 和 userAnswer[0] 是否相等
  458. isRight = answer[0].mark === userAnswer[0];
  459. } else if (j === 2) {
  460. // 判断第三行答案对错,answer[1].mark 和 userAnswer[1] 是否相交
  461. isRight = arraysIntersect(answer[1].mark, userAnswer[1]);
  462. }
  463. return isRight ? 'right' : 'wrong';
  464. },
  465. /**
  466. * 判断第一列答案对错,是否有个一个选项连接正确
  467. * @param {number} i 选项列表索引
  468. */
  469. judgeFirstAnswerRightWrong(i) {
  470. let answer = this.data.answer.answer_list[i]; // 正确答案列
  471. let userAnswer = this.answer.answer_list[i]; // 选项对应的用户答案列
  472. if (answer === undefined || userAnswer === undefined) return 'wrong';
  473. if (answer.length === 0 && userAnswer.length === 0) return '';
  474. if (answer.length === 0 || userAnswer.length === 0) return 'wrong';
  475. let isRight = true;
  476. // 判断 answer[1].mark 和 userAnswer[1] 是否相交
  477. const answerSet = new Set(answer[1].mark);
  478. isRight = userAnswer[1].some((x) => answerSet.has(x));
  479. return isRight ? 'right' : 'wrong';
  480. },
  481. },
  482. };
  483. </script>
  484. <style lang="scss" scoped>
  485. @use '@/styles/mixin.scss' as *;
  486. .matching-preview {
  487. @include preview-base;
  488. overflow: auto;
  489. .option-list {
  490. position: relative;
  491. display: flex;
  492. flex-direction: column;
  493. row-gap: 16px;
  494. .list-item {
  495. display: flex;
  496. column-gap: 8px;
  497. align-items: stretch;
  498. padding: 1px;
  499. .item-wrapper {
  500. position: relative;
  501. display: flex;
  502. flex: 1;
  503. flex-wrap: wrap;
  504. column-gap: 24px;
  505. align-items: center;
  506. min-height: 48px;
  507. padding: 12px 24px;
  508. background-color: $content-color;
  509. border-radius: 4px;
  510. &:not(:last-child, .isMobile) {
  511. margin-right: 64px;
  512. }
  513. &.focus {
  514. background-color: #dcdbdd;
  515. }
  516. &.right {
  517. background-color: $right-bc-color;
  518. }
  519. &.wrong {
  520. box-shadow: 0 0 0 1px $error-color;
  521. }
  522. .content {
  523. flex: 1;
  524. }
  525. .lang {
  526. width: 100%;
  527. }
  528. }
  529. }
  530. }
  531. .right-answer {
  532. .title {
  533. margin: 24px 0;
  534. }
  535. }
  536. }
  537. </style>