MarkBoardKeyBoard.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div
  3. v-if="store.currentTask"
  4. style="
  5. max-width: 250px;
  6. min-width: 250px;
  7. border: 1px solid grey;
  8. padding-left: 6px;
  9. padding-right: 6px;
  10. "
  11. >
  12. <div>
  13. <h1 class="text-3xl text-center" @click="toggleKeyMouse">键盘给分</h1>
  14. </div>
  15. <div>
  16. <h1 class="text-3xl text-center">
  17. 总分:{{ store.currentMarkResult?.markerScore || 0 }}
  18. </h1>
  19. </div>
  20. <div v-if="store.currentTask && store.currentTask.questionList">
  21. <template
  22. v-for="(question, index) in store.currentTask?.questionList"
  23. :key="index"
  24. >
  25. <div
  26. @click="chooseQuestion(question)"
  27. class="question rounded p-1 mb-2"
  28. :class="isCurrentQuestion(question) && 'current-question'"
  29. >
  30. <div class="flex justify-between">
  31. <div>
  32. <div>
  33. {{ question.title }} {{ question.mainNumber }}-{{
  34. question.subNumber
  35. }}
  36. </div>
  37. <div class="text-center text-3xl">
  38. {{ question.score || 0 }}
  39. </div>
  40. </div>
  41. <div>
  42. <div class="text-center">间隔{{ question.intervalScore }}分</div>
  43. <div class="text-3xl">
  44. {{ question.minScore }} ~ {{ question.maxScore }}
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </template>
  50. </div>
  51. </div>
  52. </template>
  53. <script lang="ts">
  54. import { Question } from "@/types";
  55. import { isNumber } from "lodash";
  56. import { computed, defineComponent, onMounted, onUnmounted, watch } from "vue";
  57. import { findCurrentTaskMarkResult, store } from "./store";
  58. export default defineComponent({
  59. name: "MarkBoardKeyBoard",
  60. emits: ["submit"],
  61. setup(props, { emit }) {
  62. function toggleKeyMouse() {
  63. if (store.setting.uiSetting["normal.mode"] === "keyboard") {
  64. store.setting.uiSetting["normal.mode"] = "mouse";
  65. } else {
  66. store.setting.uiSetting["normal.mode"] = "keyboard";
  67. }
  68. }
  69. const markResult = findCurrentTaskMarkResult();
  70. const questionScoreSteps = computed(() => {
  71. const question = store.currentQuestion;
  72. if (!question) return [];
  73. const steps = [];
  74. for (
  75. let i = 0;
  76. i <= question.maxScore - question.score;
  77. i += question.intervalScore
  78. ) {
  79. steps.push(i);
  80. }
  81. if ((question.maxScore - question.score) % question.intervalScore !== 0) {
  82. steps.push(question.maxScore - question.score);
  83. }
  84. return steps;
  85. });
  86. function isCurrentQuestion(question: Question) {
  87. return (
  88. store.currentQuestion?.mainNumber === question.mainNumber &&
  89. store.currentQuestion?.subNumber === question.subNumber
  90. );
  91. }
  92. watch(
  93. () => store.currentTask,
  94. () => {
  95. store.currentQuestion = undefined;
  96. store.currentScore = undefined;
  97. }
  98. );
  99. watch(
  100. () => store.currentQuestion,
  101. () => {
  102. store.currentScore = undefined;
  103. }
  104. );
  105. function chooseQuestion(question: Question) {
  106. store.currentQuestion = question;
  107. }
  108. let keyPressTimestamp = 0;
  109. let keys: string[] = [];
  110. function numberKeyListener(event: KeyboardEvent) {
  111. // console.log(event);
  112. if (!store.currentQuestion || !store.currentTask) return;
  113. // 处理Enter跳下一题或submit
  114. if (event.key === "Enter") {
  115. if (!isNumber(store.currentQuestion.score)) {
  116. // 当前题赋分不通过,Enter无效
  117. return;
  118. }
  119. const idx = store.currentTask?.questionList.findIndex(
  120. (q) =>
  121. q.mainNumber === store.currentQuestion?.mainNumber &&
  122. q.subNumber === store.currentQuestion.subNumber
  123. );
  124. if (idx + 1 === store.currentTask?.questionList.length) {
  125. submit();
  126. } else {
  127. chooseQuestion(store.currentTask.questionList[idx + 1]);
  128. }
  129. keys = [];
  130. return;
  131. }
  132. if (event.timeStamp - keyPressTimestamp > 1.5 * 1000) {
  133. keys = [];
  134. }
  135. keyPressTimestamp = event.timeStamp;
  136. keys.push(event.key);
  137. if (isNaN(parseFloat(keys.join("")))) {
  138. keys = [];
  139. }
  140. if (event.key === "Escape") {
  141. keys = [];
  142. }
  143. const score = parseFloat(keys.join(""));
  144. if (isNumber(score) && questionScoreSteps.value.includes(score)) {
  145. store.currentQuestion.score = score;
  146. }
  147. }
  148. onMounted(() => {
  149. document.addEventListener("keydown", numberKeyListener);
  150. });
  151. onUnmounted(() => {
  152. document.removeEventListener("keydown", numberKeyListener);
  153. });
  154. function submit() {
  155. emit("submit");
  156. }
  157. return {
  158. store,
  159. toggleKeyMouse,
  160. markResult,
  161. isCurrentQuestion,
  162. chooseQuestion,
  163. questionScoreSteps,
  164. submit,
  165. };
  166. },
  167. });
  168. </script>
  169. <style scoped>
  170. .question {
  171. min-width: 80px;
  172. border: 1px solid grey;
  173. }
  174. .current-question {
  175. border: 1px solid yellowgreen;
  176. background-color: lightblue;
  177. }
  178. .single-score {
  179. width: 30px;
  180. height: 30px;
  181. display: grid;
  182. place-content: center;
  183. border: 1px solid black;
  184. border-radius: 5px;
  185. }
  186. .current-score {
  187. border: 1px solid yellowgreen;
  188. background-color: lightblue;
  189. }
  190. </style>