MarkBoardTrack.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <div
  3. v-if="store.currentTask"
  4. :style="{ display: store.MarkBoardTrackCollapse ? 'none' : 'block' }"
  5. class="mark-board-track-container"
  6. >
  7. <div>
  8. <h1 class="tw-text-3xl tw-text-center">
  9. 总分:{{ store.currentMarkResult?.markerScore || 0 }}
  10. </h1>
  11. </div>
  12. <div>
  13. <div class="tw-text-2xl tw-text-center" @click="submit">提交</div>
  14. </div>
  15. <div
  16. v-if="store.currentTask && store.currentTask.questionList"
  17. class="tw-flex tw-gap-1 tw-flex-wrap tw-justify-between"
  18. >
  19. <template
  20. v-for="(question, index) in store.currentTask?.questionList"
  21. :key="index"
  22. >
  23. <div
  24. @click="chooseQuestion(question)"
  25. class="question tw-rounded tw-p-1"
  26. :class="isCurrentQuestion(question) && 'current-question'"
  27. >
  28. <div>
  29. {{ question.title }} {{ question.mainNumber }}-{{
  30. question.subNumber
  31. }}
  32. </div>
  33. <div class="tw-text-center">
  34. {{ question.score || 0 }}
  35. </div>
  36. </div>
  37. </template>
  38. </div>
  39. <div class="tw-flex tw-gap-1 tw-flex-wrap tw-mt-5">
  40. <div
  41. v-for="(s, i) in questionScoreSteps"
  42. :key="i"
  43. @click="chooseScore(s)"
  44. class="single-score"
  45. :class="isCurrentScore(s) && 'current-score'"
  46. >
  47. {{ s }}
  48. </div>
  49. </div>
  50. <div class="tw-flex tw-justify-between tw-mt-4">
  51. <div @click="clearLatestMarkOfCurrentQuetion">回退</div>
  52. <div @click="clearAllMarksOfCurrentQuetion">清除本题</div>
  53. </div>
  54. </div>
  55. </template>
  56. <script lang="ts">
  57. import { Question } from "@/types";
  58. import { isNumber } from "lodash";
  59. import { computed, defineComponent, onMounted, onUnmounted, watch } from "vue";
  60. import { store } from "./store";
  61. import { autoChooseFirstQuestion } from "./use/autoChooseFirstQuestion";
  62. export default defineComponent({
  63. name: "MarkBoardTrack",
  64. emits: ["submit"],
  65. setup(props, { emit }) {
  66. const { chooseQuestion } = autoChooseFirstQuestion();
  67. const questionScoreSteps = computed(() => {
  68. const question = store.currentQuestion;
  69. if (!question) return [];
  70. const remainScore = question.maxScore - (question.score || 0);
  71. const steps = [];
  72. for (let i = 0; i <= remainScore; i += question.intervalScore) {
  73. steps.push(i);
  74. }
  75. if (remainScore % question.intervalScore !== 0) {
  76. steps.push(remainScore);
  77. }
  78. return steps;
  79. });
  80. function isCurrentQuestion(question: Question) {
  81. return (
  82. store.currentQuestion?.mainNumber === question.mainNumber &&
  83. store.currentQuestion?.subNumber === question.subNumber
  84. );
  85. }
  86. watch(
  87. () => store.currentQuestion,
  88. () => {
  89. store.currentScore = undefined;
  90. }
  91. );
  92. function isCurrentScore(score: number) {
  93. return store.currentScore === score;
  94. }
  95. function chooseScore(score: number) {
  96. store.currentScore = score;
  97. }
  98. let keyPressTimestamp = 0;
  99. let keys: string[] = [];
  100. function numberKeyListener(event: KeyboardEvent) {
  101. // console.log(event);
  102. if (!store.currentQuestion) return;
  103. function indexOfCurrentQuestion() {
  104. return store.currentTask?.questionList.findIndex(
  105. (q) =>
  106. q.mainNumber === store.currentQuestion?.mainNumber &&
  107. q.subNumber === store.currentQuestion.subNumber
  108. );
  109. }
  110. // tab 循环答题列表
  111. if (event.key === "Tab") {
  112. const idx = indexOfCurrentQuestion() as number;
  113. if (idx >= 0 && store.currentTask) {
  114. const len = store.currentTask.questionList.length;
  115. chooseQuestion(store.currentTask.questionList[(idx + 1) % len]);
  116. event.preventDefault();
  117. }
  118. return;
  119. }
  120. if (event.timeStamp - keyPressTimestamp > 1.5 * 1000) {
  121. keys = [];
  122. }
  123. keyPressTimestamp = event.timeStamp;
  124. keys.push(event.key);
  125. if (isNaN(parseFloat(keys.join("")))) {
  126. keys = [];
  127. }
  128. if (event.key === "Escape") {
  129. keys = [];
  130. }
  131. const score = parseFloat(keys.join(""));
  132. if (isNumber(score) && questionScoreSteps.value.includes(score)) {
  133. chooseScore(score);
  134. }
  135. }
  136. onMounted(() => {
  137. document.addEventListener("keydown", numberKeyListener);
  138. });
  139. onUnmounted(() => {
  140. document.removeEventListener("keydown", numberKeyListener);
  141. });
  142. function clearLatestMarkOfCurrentQuetion() {
  143. if (!store.currentMarkResult || !store.currentQuestion) return;
  144. const ts = store.currentMarkResult?.trackList.filter(
  145. (q) =>
  146. q.mainNumber === store.currentQuestion?.mainNumber &&
  147. q.subNumber === store.currentQuestion?.subNumber
  148. );
  149. if (ts.length === 0) return;
  150. const maxNumber = Math.max(...ts.map((q) => q.number));
  151. const idx = store.currentMarkResult.trackList.findIndex(
  152. (q) =>
  153. q.mainNumber === store.currentQuestion?.mainNumber &&
  154. q.subNumber === store.currentQuestion?.subNumber &&
  155. q.number === maxNumber
  156. );
  157. store.removeScoreTracks = store.currentMarkResult.trackList.splice(
  158. idx,
  159. 1
  160. );
  161. }
  162. function clearAllMarksOfCurrentQuetion() {
  163. if (!store.currentMarkResult || !store.currentQuestion) return;
  164. store.removeScoreTracks = store.currentMarkResult?.trackList.filter(
  165. (q) =>
  166. q.mainNumber === store.currentQuestion?.mainNumber &&
  167. q.subNumber === store.currentQuestion?.subNumber
  168. );
  169. store.currentMarkResult.trackList = store.currentMarkResult?.trackList.filter(
  170. (q) =>
  171. !(
  172. q.mainNumber === store.currentQuestion?.mainNumber &&
  173. q.subNumber === store.currentQuestion?.subNumber
  174. )
  175. );
  176. }
  177. function submit() {
  178. const errors: any = [];
  179. store.currentTask?.questionList.forEach((question, index) => {
  180. if (!isNumber(question.score)) {
  181. errors.push({ question, index, error: "没有赋分不能提交" });
  182. }
  183. });
  184. if (errors.length === 0) {
  185. emit("submit");
  186. } else {
  187. console.log(errors);
  188. }
  189. }
  190. return {
  191. store,
  192. isCurrentQuestion,
  193. chooseQuestion,
  194. isCurrentScore,
  195. chooseScore,
  196. questionScoreSteps,
  197. clearLatestMarkOfCurrentQuetion,
  198. clearAllMarksOfCurrentQuetion,
  199. submit,
  200. };
  201. },
  202. });
  203. </script>
  204. <style scoped>
  205. .mark-board-track-container {
  206. max-width: 250px;
  207. min-width: 250px;
  208. border-left: 1px solid grey;
  209. padding-left: 6px;
  210. padding-right: 6px;
  211. }
  212. .question {
  213. min-width: 100px;
  214. border: 1px solid grey;
  215. }
  216. .current-question {
  217. border: 1px solid yellowgreen;
  218. background-color: lightblue;
  219. }
  220. .single-score {
  221. width: 30px;
  222. height: 30px;
  223. display: grid;
  224. place-content: center;
  225. border: 1px solid black;
  226. border-radius: 5px;
  227. }
  228. .current-score {
  229. border: 1px solid yellowgreen;
  230. background-color: lightblue;
  231. }
  232. </style>