MarkBoardKeyBoard.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <div v-if="store.currentTask" class="mark-board-track-container">
  3. <div class="tw-my-2 tw-flex tw-place-content-center">
  4. <a-dropdown>
  5. <template #overlay>
  6. <a-menu>
  7. <a-menu-item key="1" @click="toggleKeyMouse">
  8. 鼠标给分
  9. </a-menu-item>
  10. </a-menu>
  11. </template>
  12. <a-button>
  13. 键盘给分
  14. <DownOutlined style="display: inline-flex" />
  15. </a-button>
  16. </a-dropdown>
  17. </div>
  18. <div>
  19. <h1 class="tw-text-3xl tw-text-center">
  20. 总分:{{ store.currentMarkResult?.markerScore || 0 }}
  21. </h1>
  22. </div>
  23. <div v-if="store.currentTask && store.currentTask.questionList">
  24. <template
  25. v-for="(question, index) in store.currentTask?.questionList"
  26. :key="index"
  27. >
  28. <div
  29. @click="chooseQuestion(question)"
  30. class="question tw-rounded tw-p-1 tw-mb-2"
  31. :class="isCurrentQuestion(question) && 'current-question'"
  32. >
  33. <div class="tw-flex tw-justify-between">
  34. <div>
  35. <div>
  36. {{ question.title }} {{ question.mainNumber }}-{{
  37. question.subNumber
  38. }}
  39. </div>
  40. <div class="tw-text-center tw-text-3xl">
  41. <!-- {{ showScore(question) }} -->
  42. {{ question.__updateScore }}
  43. </div>
  44. </div>
  45. <div>
  46. <div class="tw-text-center">
  47. 间隔{{ question.intervalScore }}分
  48. </div>
  49. <div class="tw-flex tw-text-3xl" style="width: 80px">
  50. <span class="tw-flex-1">{{ question.minScore }}</span>
  51. <span class="tw-flex-1">~</span>
  52. <span class="tw-flex-1 tw-text-center">{{
  53. question.maxScore
  54. }}</span>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. </div>
  61. </div>
  62. </template>
  63. <script lang="ts">
  64. import { Question } from "@/types";
  65. import { isNumber } from "lodash";
  66. import {
  67. computed,
  68. defineComponent,
  69. onMounted,
  70. onUnmounted,
  71. reactive,
  72. watch,
  73. } from "vue";
  74. import { store } from "./store";
  75. import { keyMouse } from "./use/keyboardAndMouse";
  76. import { autoChooseFirstQuestion } from "./use/autoChooseFirstQuestion";
  77. import { message } from "ant-design-vue";
  78. import { DownOutlined } from "@ant-design/icons-vue";
  79. export default defineComponent({
  80. name: "MarkBoardKeyBoard",
  81. emits: ["submit"],
  82. components: { DownOutlined },
  83. setup(props, { emit }) {
  84. const { toggleKeyMouse } = keyMouse();
  85. const { chooseQuestion } = autoChooseFirstQuestion();
  86. const questionScoreSteps = computed(() => {
  87. const question = store.currentQuestion;
  88. if (!question) return [];
  89. const remainScore = Math.round(
  90. question.maxScore * 100 - (question.score || 0) * 100
  91. );
  92. const steps = [];
  93. for (
  94. let i = 0;
  95. i <= remainScore;
  96. i = Math.round(i * 100 + question.intervalScore * 100) / 100
  97. ) {
  98. steps.push(i);
  99. }
  100. if (
  101. Math.round(remainScore * 100) %
  102. Math.round(question.intervalScore * 100) !==
  103. 0
  104. ) {
  105. steps.push(remainScore);
  106. }
  107. return steps;
  108. });
  109. function isCurrentQuestion(question: Question) {
  110. return (
  111. store.currentQuestion?.mainNumber === question.mainNumber &&
  112. store.currentQuestion?.subNumber === question.subNumber
  113. );
  114. }
  115. let keyPressTimestamp = 0;
  116. let keys = reactive([] as Array<String>);
  117. function numberKeyListener(event: KeyboardEvent) {
  118. // console.log(event);
  119. if (!store.currentQuestion || !store.currentTask) return;
  120. function indexOfCurrentQuestion() {
  121. return store.currentTask?.questionList.findIndex(
  122. (q) =>
  123. q.mainNumber === store.currentQuestion?.mainNumber &&
  124. q.subNumber === store.currentQuestion.subNumber
  125. );
  126. }
  127. // 处理Enter跳下一题或submit
  128. if (event.key === "Enter") {
  129. // if (!isNumber(store.currentQuestion.score)) {
  130. // // 当前题赋分不通过,Enter无效
  131. // return;
  132. // }
  133. // 有bug,移除当前题,再回来就出错了
  134. if (keys.length === 0) {
  135. message.error({ content: "请输入分数", duration: 100 });
  136. console.log("请输入分数");
  137. return;
  138. }
  139. const score = parseFloat(keys.join(""));
  140. if (!isNumber(score)) {
  141. message.error({ content: "非数字输入", duration: 10 });
  142. console.log("非数字输入");
  143. return;
  144. }
  145. if (!questionScoreSteps.value.includes(score)) {
  146. message.error({ content: "输入的分数不在有效间隔内", duration: 10 });
  147. console.log("输入的分数不在有效间隔内");
  148. return;
  149. }
  150. store.currentQuestion.score = score;
  151. const idx = indexOfCurrentQuestion() as number;
  152. if (idx + 1 === store.currentTask?.questionList.length) {
  153. submit();
  154. } else {
  155. chooseQuestion(store.currentTask.questionList[idx + 1]);
  156. }
  157. keys = [];
  158. return;
  159. }
  160. if (event.key === "ArrowLeft") {
  161. const idx = indexOfCurrentQuestion() as number;
  162. if (idx > 0) {
  163. chooseQuestion(store.currentTask.questionList[idx - 1]);
  164. }
  165. keys = [];
  166. return;
  167. }
  168. if (event.key === "ArrowRight") {
  169. const idx = indexOfCurrentQuestion() as number;
  170. if (idx < store.currentTask.questionList.length - 1) {
  171. chooseQuestion(store.currentTask.questionList[idx + 1]);
  172. }
  173. keys = [];
  174. return;
  175. }
  176. // 处理回退删除分数
  177. if (event.key === "Backspace") {
  178. if (keys.length > 0) {
  179. keys.splice(keys.length - 1, 1);
  180. } else {
  181. return;
  182. }
  183. }
  184. if (event.key === "Escape") {
  185. keys = [];
  186. }
  187. // TODO: 确认数字按键的间隔
  188. // if (event.timeStamp - keyPressTimestamp > 1.5 * 1000) {
  189. // keys = [];
  190. // }
  191. // keyPressTimestamp = event.timeStamp;
  192. // 此时不再接受任何非数字键
  193. if (".0123456789".includes(event.key)) {
  194. keys.push(event.key);
  195. }
  196. if (isNaN(parseFloat(keys.join("")))) {
  197. keys = [];
  198. }
  199. // FIXME: for update. 得想个更好的办法来解决不能更新的问题
  200. store.currentQuestion.__updateScore = keys.join("");
  201. // console.log(
  202. // keys,
  203. // keys.join(""),
  204. // isCurrentQuestion(store.currentQuestion) && keys.length > 0
  205. // );
  206. }
  207. onMounted(() => {
  208. document.addEventListener("keydown", numberKeyListener);
  209. });
  210. onUnmounted(() => {
  211. document.removeEventListener("keydown", numberKeyListener);
  212. });
  213. const showScore = (question: Question) => {
  214. return isCurrentQuestion(question) && keys.length > 0
  215. ? keys.join("")
  216. : question.score;
  217. };
  218. function submit() {
  219. const errors: any = [];
  220. store.currentTask?.questionList.forEach((question, index) => {
  221. if (!isNumber(question.score)) {
  222. errors.push({ question, index, error: "没有赋分不能提交" });
  223. }
  224. });
  225. if (errors.length === 0) {
  226. emit("submit");
  227. } else {
  228. console.log(errors);
  229. message.error({
  230. content: errors
  231. .map((e: any) => `${e.index + 1}、${e.error}`)
  232. .join("\n"),
  233. duration: 10,
  234. });
  235. }
  236. }
  237. return {
  238. store,
  239. toggleKeyMouse,
  240. isCurrentQuestion,
  241. chooseQuestion,
  242. keys,
  243. showScore,
  244. questionScoreSteps,
  245. submit,
  246. };
  247. },
  248. });
  249. </script>
  250. <style scoped>
  251. .mark-board-track-container {
  252. max-width: 250px;
  253. min-width: 250px;
  254. border-left: 1px solid grey;
  255. padding-left: 6px;
  256. padding-right: 6px;
  257. max-height: calc(100vh - 41px);
  258. overflow: scroll;
  259. }
  260. .question {
  261. min-width: 80px;
  262. border: 1px solid grey;
  263. }
  264. .current-question {
  265. border: 1px solid yellowgreen;
  266. background-color: lightblue;
  267. }
  268. .single-score {
  269. width: 30px;
  270. height: 30px;
  271. display: grid;
  272. place-content: center;
  273. border: 1px solid black;
  274. border-radius: 5px;
  275. }
  276. .current-score {
  277. border: 1px solid yellowgreen;
  278. background-color: lightblue;
  279. }
  280. </style>