MatchingPreview.vue 18 KB

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