MarkBoardTrack.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <template>
  2. <div
  3. v-if="store.currentTask"
  4. class="mark-board-track"
  5. :class="[
  6. {
  7. hide: store.isScoreBoardCollapsed && !props.modal,
  8. 'in-dialog': store.isScoreBoardCollapsed && props.modal,
  9. },
  10. ]"
  11. :style="{
  12. height: props.modal ? '100%' : 'auto',
  13. }"
  14. >
  15. <div class="board-header">
  16. <div class="board-header-left">
  17. <div class="board-header-info">
  18. <img src="@/assets/icons/icon-star.svg" />
  19. <span>总分</span>
  20. </div>
  21. <div class="board-header-score">
  22. <transition-group name="score-number-animation" tag="span">
  23. <span :key="store.currentTask.markResult?.markerScore || 0">{{
  24. store.currentTask.markResult?.markerScore
  25. }}</span>
  26. </transition-group>
  27. </div>
  28. </div>
  29. <qm-button
  30. class="board-header-submit"
  31. size="medium"
  32. type="primary"
  33. @click="submit"
  34. >
  35. 提交
  36. </qm-button>
  37. </div>
  38. <div
  39. v-if="store.currentTask && store.currentTask.questionList"
  40. class="board-questions"
  41. :style="{
  42. height: areaHeight ? `${areaHeight}px` : 'auto',
  43. flexGrow: areaHeight ? 0 : 2,
  44. }"
  45. >
  46. <template
  47. v-for="(question, index) in store.currentTask.questionList"
  48. :key="index"
  49. >
  50. <div class="board-question-box">
  51. <div
  52. :id="
  53. store.isScoreBoardCollapsed
  54. ? props.modal
  55. ? ['bq', question.mainNumber, question.subNumber].join('-')
  56. : ''
  57. : ['bq', question.mainNumber, question.subNumber].join('-')
  58. "
  59. :class="[
  60. 'board-question',
  61. { 'is-current': isCurrentQuestion(question) },
  62. ]"
  63. tabindex="0"
  64. outline="0"
  65. hidefocus="true"
  66. @click="chooseQuestion(question)"
  67. @contextmenu="onRightClick($event, index)"
  68. @blur="rightBlur"
  69. >
  70. <div
  71. v-if="
  72. activeRightMenuItem?.id ==
  73. (store.isScoreBoardCollapsed
  74. ? props.modal
  75. ? ['bq', question.mainNumber, question.subNumber].join('-')
  76. : ''
  77. : ['bq', question.mainNumber, question.subNumber].join('-'))
  78. "
  79. class="tw-fixed right-menu-box"
  80. :style="tmpStyle"
  81. >
  82. <div class="right-menu-item" @click="positioning(question)">
  83. 定位
  84. </div>
  85. </div>
  86. <div class="question-info">
  87. <div class="question-title" :title="question.title">
  88. {{ question.title }}
  89. </div>
  90. <div class="question-no">
  91. {{ question.mainNumber }}-{{ question.subNumber }}
  92. </div>
  93. </div>
  94. <!-- 设置高度 避免动画跳动 -->
  95. <div class="question-score">
  96. <transition-group name="score-number-animation" tag="span">
  97. <span
  98. :key="store.currentTask?.markResult?.scoreList[index] || 0"
  99. >
  100. <!-- 特殊的空格符号 -->
  101. <!-- eslint-disable-next-line no-irregular-whitespace -->
  102. {{ store.currentTask?.markResult?.scoreList[index] ?? " " }}
  103. </span>
  104. </transition-group>
  105. </div>
  106. </div>
  107. </div>
  108. </template>
  109. </div>
  110. <div class="board-spliter">
  111. <div class="board-spliter-up" @click="areaHeight = 74">
  112. <img src="@/assets/icons/icon-up.svg" />
  113. <img src="@/assets/icons/icon-up-active.svg" />
  114. </div>
  115. <div ref="dragSpliter" class="board-spliter-bar"></div>
  116. <div class="board-spliter-down" @click="areaHeight = 0">
  117. <img src="@/assets/icons/icon-down.svg" />
  118. <img src="@/assets/icons/icon-down-active.svg" />
  119. </div>
  120. </div>
  121. <div v-if="!questionScoreDisabled" class="board-scores">
  122. <div
  123. v-for="(s, i) in questionScoreSteps.slice(1)"
  124. :key="i"
  125. :class="['board-score', { 'is-current': isCurrentScore(s) }]"
  126. @click="chooseScore(s)"
  127. >
  128. {{ s }}
  129. </div>
  130. <div
  131. class="board-score"
  132. :class="Object.is(store.currentScore, 0) && 'is-current'"
  133. @click="chooseScore(0)"
  134. >
  135. 0
  136. </div>
  137. </div>
  138. <div class="board-footer">
  139. <div>
  140. <qm-button
  141. v-if="!questionScoreDisabled"
  142. class="board-goback"
  143. :clickTimeout="300"
  144. @click="clearLatestMarkOfCurrentQuetion"
  145. >
  146. <template #icon>
  147. <img src="@/assets/icons/icon-goback.svg" />
  148. </template>
  149. 回退
  150. </qm-button>
  151. </div>
  152. <qm-button
  153. class="board-clear"
  154. :clickTimeout="300"
  155. data-test="clear-score"
  156. @click="clearAllMarksOfCurrentQuetion"
  157. >
  158. <template #icon>
  159. <img class="icon-common" src="@/assets/icons/icon-clear.svg" />
  160. <img class="icon-active" src="@/assets/icons/icon-clear-white.svg" />
  161. </template>
  162. 清空
  163. </qm-button>
  164. </div>
  165. </div>
  166. </template>
  167. <script setup lang="ts">
  168. import type { Question } from "@/types";
  169. import { isNumber } from "lodash-es";
  170. import { onMounted, onUnmounted, watch, ref, reactive } from "vue";
  171. import { store } from "@/store/store";
  172. import { autoChooseFirstQuestion } from "./use/autoChooseFirstQuestion";
  173. import { dragSplitPane } from "./use/splitPane";
  174. import { addFocusTrack, removeFocusTrack } from "./use/focusTracks";
  175. import EventBus from "@/plugins/eventBus";
  176. import { cloneDeep } from "lodash-es";
  177. const props = defineProps<{ modal?: boolean; isCheckAnswer?: boolean }>();
  178. const emit = defineEmits(["submit", "unselectiveSubmit"]);
  179. const { dragSpliter, areaHeight } = dragSplitPane();
  180. const activeRightMenuItem = ref<any>(null);
  181. const tmpStyle = reactive<any>({
  182. left: 0,
  183. top: 0,
  184. zIndex: 300,
  185. });
  186. function getParentNode(el: any, c: string): any {
  187. if (el.className?.includes(c)) {
  188. return el;
  189. } else {
  190. if (el.parentNode) {
  191. return getParentNode(el.parentNode, c);
  192. } else {
  193. return null;
  194. }
  195. }
  196. }
  197. const onRightClick = (e: any, index?: any) => {
  198. e.preventDefault();
  199. if (
  200. store.currentTask?.markResult.scoreList[index] ||
  201. store.currentTask?.markResult.scoreList[index] === 0
  202. ) {
  203. tmpStyle.left = e.clientX + "px";
  204. tmpStyle.top = e.clientY + "px";
  205. let parentNode = getParentNode(e.target, "board-question");
  206. if (parentNode) {
  207. activeRightMenuItem.value = parentNode;
  208. }
  209. console.log("activeRightMenuItem:", activeRightMenuItem.value);
  210. }
  211. };
  212. const rightBlur = () => {
  213. activeRightMenuItem.value = null;
  214. removeFocusTrack();
  215. };
  216. const positioning = (question: Question) => {
  217. // let list =
  218. // store.getMarkStatus === "正评" || store.getMarkStatus === "试评"
  219. // ? sliceImagesWithTrackListCopy.value
  220. // : undefined;
  221. // addFocusTrack(undefined, question.mainNumber, question.subNumber, true, list);
  222. console.log(
  223. "sliceImagesWithTrackListCopy:",
  224. sliceImagesWithTrackListCopy.value
  225. );
  226. addFocusTrack(
  227. undefined,
  228. question.mainNumber,
  229. question.subNumber,
  230. true,
  231. sliceImagesWithTrackListCopy.value || []
  232. );
  233. activeRightMenuItem.value = null;
  234. };
  235. const { chooseQuestion } = autoChooseFirstQuestion();
  236. let sliceImagesWithTrackListCopy = ref([]);
  237. EventBus.on("draw-change", (list: any) => {
  238. sliceImagesWithTrackListCopy.value = cloneDeep(list);
  239. });
  240. // 切换题目是清空上一题的分数
  241. watch(
  242. () => store.currentQuestion,
  243. () => {
  244. store.currentScore = undefined;
  245. if (!props.isCheckAnswer) chooseScore(questionScoreSteps[1]);
  246. }
  247. );
  248. watch(
  249. () => store.currentTask,
  250. () => {
  251. if (!props.isCheckAnswer) return;
  252. let currentTaskModifyQuestion = {};
  253. store.currentTask.questionList.forEach((q) => {
  254. const qno = `${q.mainNumber}_${q.subNumber}`;
  255. currentTaskModifyQuestion[qno] = false;
  256. });
  257. store.currentTaskModifyQuestion = currentTaskModifyQuestion;
  258. }
  259. );
  260. const questionScoreDisabled = $computed(() => {
  261. const qno = `${store.currentQuestion.mainNumber}_${store.currentQuestion.subNumber}`;
  262. return props.isCheckAnswer && !store.currentTaskModifyQuestion[qno];
  263. });
  264. const questionScore = $computed(
  265. () =>
  266. store.currentTask &&
  267. store.currentQuestion &&
  268. store.currentTask.markResult?.scoreList[store.currentQuestion.__index]
  269. );
  270. const questionScoreSteps = $computed(() => {
  271. const question = store.currentQuestion;
  272. if (!question) return [];
  273. const remainScore =
  274. Math.round(question.maxScore * 1000 - (questionScore || 0) * 1000) / 1000;
  275. const steps = [];
  276. if (question.intervalScore <= 0) {
  277. console.warn(`question.intervalScore got: ${question.intervalScore}`);
  278. }
  279. for (
  280. let i = 0;
  281. i <= remainScore && question.intervalScore > 0;
  282. i = Math.round(i * 1000 + question.intervalScore * 1000) / 1000
  283. ) {
  284. steps.push(i);
  285. }
  286. if (
  287. Math.round(remainScore * 1000) %
  288. Math.round(question.intervalScore * 1000) !==
  289. 0
  290. ) {
  291. steps.push(remainScore);
  292. }
  293. return steps;
  294. });
  295. function isCurrentQuestion(question: Question) {
  296. return (
  297. store.currentQuestion?.mainNumber === question.mainNumber &&
  298. store.currentQuestion?.subNumber === question.subNumber
  299. );
  300. }
  301. function isCurrentScore(score: number) {
  302. return store.currentScore === score;
  303. }
  304. function chooseScore(score: number) {
  305. if (store.currentScore === score) {
  306. store.currentScore = undefined;
  307. } else {
  308. store.currentScore = score;
  309. store.currentSpecialTag = undefined;
  310. }
  311. store.currentSpecialTagType = undefined;
  312. }
  313. let keyPressTimestamp = 0;
  314. let keys: string[] = [];
  315. function numberKeyListener(event: KeyboardEvent) {
  316. // if (event.target.tagName !== "BODY") return;
  317. if (!store.currentQuestion) return;
  318. if (questionScoreDisabled) return;
  319. if (" jiklc".includes(event.key)) return;
  320. // if (event.key === "#") {
  321. // keys = [];
  322. // store.currentScore = -0;
  323. // return;
  324. // }
  325. function indexOfCurrentQuestion() {
  326. return (
  327. store.currentTask?.questionList.findIndex(
  328. (q) =>
  329. q.mainNumber === store.currentQuestion?.mainNumber &&
  330. q.subNumber === store.currentQuestion.subNumber
  331. ) ?? -1
  332. );
  333. }
  334. // tab 循环答题列表
  335. if (event.key === "Tab") {
  336. const idx = indexOfCurrentQuestion();
  337. if (idx >= 0 && store.currentTask) {
  338. const len = store.currentTask.questionList.length;
  339. chooseQuestion(store.currentTask.questionList[(idx + 1) % len]);
  340. event.preventDefault();
  341. }
  342. return;
  343. }
  344. // 为了cypress可以加速时间
  345. if (Date.now() - keyPressTimestamp > 1 * 1000) {
  346. keys = [];
  347. }
  348. keyPressTimestamp = Date.now();
  349. keys.push(event.key);
  350. if (isNaN(parseFloat(keys.join("")))) {
  351. keys = [];
  352. }
  353. if (event.key === "Escape") {
  354. keys = [];
  355. store.currentScore = undefined;
  356. store.currentSpecialTag = undefined;
  357. return;
  358. }
  359. const score = parseFloat(keys.join(""));
  360. if (isNumber(score) && questionScoreSteps.includes(score)) {
  361. chooseScore(score);
  362. }
  363. }
  364. function submitListener(e: KeyboardEvent) {
  365. // if (event.target.tagName !== "BODY") return;
  366. // if (import.meta.env.DEV && e.ctrlKey && e.key === "Enter") {
  367. if (e.ctrlKey && e.key === "Enter") {
  368. submit();
  369. }
  370. }
  371. onMounted(() => {
  372. document.addEventListener("keydown", numberKeyListener);
  373. document.addEventListener("keydown", submitListener);
  374. });
  375. onUnmounted(() => {
  376. document.removeEventListener("keydown", numberKeyListener);
  377. document.removeEventListener("keydown", submitListener);
  378. });
  379. watch(
  380. () => store.isScoreBoardCollapsed,
  381. () => {
  382. // 此处的逻辑是 MarkBoardTrackDialog 带来的,不然 numberKeyListener 在两个组件中多次触发有问题
  383. if (store.isScoreBoardCollapsed) {
  384. document.removeEventListener("keydown", numberKeyListener);
  385. document.removeEventListener("keydown", submitListener);
  386. } else {
  387. // 重复添加相同的function是没问题,不会重复触发
  388. document.addEventListener("keydown", numberKeyListener);
  389. document.addEventListener("keydown", submitListener);
  390. }
  391. },
  392. { immediate: true }
  393. );
  394. function clearLatestMarkOfCurrentQuetion() {
  395. if (!store.currentTask?.markResult || !store.currentQuestion) return;
  396. const { __index, mainNumber, subNumber } = store.currentQuestion;
  397. const markResult = store.currentTask.markResult;
  398. const ts = markResult.trackList.filter(
  399. (q) => q.mainNumber === mainNumber && q.subNumber === subNumber
  400. );
  401. console.log(ts);
  402. if (ts.length === 0) {
  403. return;
  404. }
  405. const lastMark = ts.splice(-1)[0];
  406. store.removeScoreTracks = [lastMark];
  407. markResult.trackList = markResult.trackList.filter((t) => t !== lastMark);
  408. markResult.scoreList[__index] =
  409. ts.length === 0
  410. ? null
  411. : ts
  412. .map((t) => t.score)
  413. .reduce((acc, v) => (acc += Math.round(v * 1000)), 0) / 1000;
  414. }
  415. function clearAllMarksOfCurrentQuetion() {
  416. if (!store.currentTask?.markResult || !store.currentQuestion) return;
  417. const qno = `${store.currentQuestion.mainNumber}_${store.currentQuestion.subNumber}`;
  418. store.currentTaskModifyQuestion[qno] = true;
  419. const markResult = store.currentTask.markResult;
  420. store.removeScoreTracks = markResult.trackList.filter(
  421. (q) =>
  422. q.mainNumber === store.currentQuestion?.mainNumber &&
  423. q.subNumber === store.currentQuestion?.subNumber
  424. );
  425. markResult.trackList = markResult.trackList.filter(
  426. (q) => !store.removeScoreTracks.includes(q)
  427. );
  428. const { __index } = store.currentQuestion;
  429. markResult.scoreList[__index] = null;
  430. }
  431. function submit() {
  432. emit("submit");
  433. }
  434. </script>
  435. <style lang="less" scoped>
  436. .right-menu-box {
  437. z-index: 10;
  438. background-color: #fff;
  439. border-radius: 4px;
  440. width: 80px;
  441. padding: 2px 0;
  442. box-shadow: 0 0 3px #ddd;
  443. .right-menu-item {
  444. height: 30px;
  445. line-height: 30px;
  446. font-size: 14px;
  447. padding: 0 10px;
  448. color: #333;
  449. &:hover {
  450. background: var(--app-main-bg-color);
  451. }
  452. }
  453. }
  454. </style>