MarkBoardKeyBoard.vue 6.1 KB

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