MarkBoardKeyBoard.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <template>
  2. <div
  3. v-if="store.currentTask"
  4. class="mark-board-track-container"
  5. :class="[store.setting.uiSetting['score.board.collapse'] ? 'hide' : 'show']"
  6. >
  7. <div class="tw-my-2 tw-flex">
  8. <a-dropdown class="tw-self-end">
  9. <template #overlay>
  10. <a-menu>
  11. <a-menu-item key="1" @click="toggleKeyMouse">
  12. 鼠标给分
  13. </a-menu-item>
  14. </a-menu>
  15. </template>
  16. <a-button>
  17. 键盘给分
  18. <DownOutlined style="display: inline-flex" />
  19. </a-button>
  20. </a-dropdown>
  21. </div>
  22. <div
  23. class="
  24. tw-flex tw-rounded tw-justify-between tw-p-2 tw-pl-5
  25. top-container
  26. tw-mb-4
  27. "
  28. >
  29. <div class="tw-flex tw-flex-col">
  30. <div>总分</div>
  31. <div class="total-score">
  32. {{ store.currentMarkResult?.markerScore }}
  33. </div>
  34. </div>
  35. <div
  36. class="
  37. tw-flex tw-flex-col tw-place-content-center tw-items-center tw-gap-1
  38. "
  39. >
  40. <a-popconfirm
  41. v-if="store.setting.enableAllZero"
  42. title="确定给全零分?"
  43. ok-text="确定"
  44. cancel-text="取消"
  45. @confirm="$emit('allZeroSubmit')"
  46. >
  47. <a-button type="primary" shape="round" size="middle">
  48. 全零分
  49. </a-button>
  50. </a-popconfirm>
  51. <qm-button type="primary" shape="round" size="middle" @click="submit">
  52. 提交
  53. </qm-button>
  54. </div>
  55. </div>
  56. <div v-if="store.currentTask && store.currentTask.questionList">
  57. <template
  58. v-for="(question, index) in store.currentTask?.questionList"
  59. :key="index"
  60. >
  61. <div
  62. @click="
  63. () => {
  64. chooseQuestion(question);
  65. scrollToQuestion(question);
  66. }
  67. "
  68. class="
  69. question
  70. tw-rounded tw-p-1 tw-mb-2 tw-cursor-pointer tw-relative
  71. "
  72. :class="isCurrentQuestion(question) && 'current-question'"
  73. :id="'bq-' + question.mainNumber + '-' + question.subNumber"
  74. >
  75. <div
  76. v-if="isCurrentQuestion(question)"
  77. class="current-score-indicator"
  78. ></div>
  79. <div class="tw-flex tw-justify-between">
  80. <div>
  81. <div>
  82. {{ question.title }} {{ question.mainNumber }}-{{
  83. question.subNumber
  84. }}
  85. </div>
  86. <div
  87. class="tw-text-center tw-text-3xl"
  88. :class="isCurrentQuestion(question) && 'current-score'"
  89. >
  90. {{ isCurrentQuestion(question) ? scoreStr : question.score }}
  91. </div>
  92. </div>
  93. <div>
  94. <div class="tw-text-center">
  95. 间隔{{ question.intervalScore }}分
  96. </div>
  97. <div class="tw-flex tw-text-3xl" style="width: 80px">
  98. <span class="tw-flex-1">{{ question.minScore }}</span>
  99. <span class="tw-flex-1">~</span>
  100. <span class="tw-flex-1 tw-text-center">{{
  101. question.maxScore
  102. }}</span>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </template>
  108. </div>
  109. </div>
  110. </template>
  111. <script setup lang="ts">
  112. import type { Question } from "@/types";
  113. import { isNumber } from "lodash";
  114. import { computed, onMounted, onUnmounted, ref, watch } from "vue";
  115. import { store } from "./store";
  116. import { keyMouse } from "./use/keyboardAndMouse";
  117. import { autoChooseFirstQuestion } from "./use/autoChooseFirstQuestion";
  118. import { message } from "ant-design-vue";
  119. import { DownOutlined } from "@ant-design/icons-vue";
  120. const emit = defineEmits(["submit", "allZeroSubmit"]);
  121. const { toggleKeyMouse } = keyMouse();
  122. const { chooseQuestion } = autoChooseFirstQuestion();
  123. /**
  124. * 当前题的输入串,初次是question.score,然后接收输入字符,回车时判断是否合法,合法则赋值给question.score
  125. * 切换到下一题,则重新开始
  126. * */
  127. let scoreStr = ref("");
  128. watch(
  129. () => [store.currentQuestion, store.setting.uiSetting["normal.mode"]],
  130. () => {
  131. if (isNumber(store.currentQuestion?.score)) {
  132. scoreStr.value = "" + store.currentQuestion?.score;
  133. } else {
  134. scoreStr.value = "";
  135. }
  136. },
  137. { immediate: true }
  138. );
  139. const questionScoreSteps = computed(() => {
  140. const question = store.currentQuestion;
  141. if (!question) return [];
  142. const remainScore = Math.round(question.maxScore * 100) / 100;
  143. const steps = [];
  144. for (
  145. let i = 0;
  146. i <= remainScore;
  147. i = Math.round(i * 100 + question.intervalScore * 100) / 100
  148. ) {
  149. steps.push(i);
  150. }
  151. if (
  152. Math.round(remainScore * 100) % Math.round(question.intervalScore * 100) !==
  153. 0
  154. ) {
  155. steps.push(remainScore);
  156. }
  157. return steps;
  158. });
  159. function isCurrentQuestion(question: Question) {
  160. return (
  161. store.currentQuestion?.mainNumber === question.mainNumber &&
  162. store.currentQuestion?.subNumber === question.subNumber
  163. );
  164. }
  165. function numberKeyListener(event: KeyboardEvent) {
  166. // console.log(event);
  167. if (!store.currentQuestion || !store.currentTask) return;
  168. if (store.globalMask) return;
  169. function indexOfCurrentQuestion() {
  170. return store.currentTask?.questionList.findIndex(
  171. (q) =>
  172. q.mainNumber === store.currentQuestion?.mainNumber &&
  173. q.subNumber === store.currentQuestion.subNumber
  174. );
  175. }
  176. // 处理Enter跳下一题或submit
  177. if (event.key === "Enter") {
  178. const allScoreMarked = store.currentTask?.questionList.every((q) =>
  179. isNumber(q.score)
  180. );
  181. // 如果所有题已赋分,并且当前题赋分和输入串和当前题分数一致,则可以在任意题提交
  182. if (allScoreMarked && scoreStr.value === "" + store.currentQuestion.score) {
  183. submit();
  184. return;
  185. }
  186. if (scoreStr.value.length === 0) {
  187. message.error({ content: "请输入分数", duration: 5 });
  188. console.log("请输入分数");
  189. return;
  190. }
  191. // 0 分
  192. // 整数分 (开始不为0)
  193. // 小数分 (开始和结束不为0,结束必须为1-9
  194. if (
  195. !(
  196. scoreStr.value === "0" ||
  197. /^0\.[1-9]\d*$/.test(scoreStr.value) ||
  198. /^[1-9]\d*$/.test(scoreStr.value) ||
  199. /^[1-9]\d*\.\d*[1-9]$/.test(scoreStr.value)
  200. )
  201. ) {
  202. message.error({ content: "分数格式不正确", duration: 5 });
  203. console.log("分数格式不正确");
  204. return;
  205. }
  206. const score = parseFloat(scoreStr.value);
  207. // console.log(score);
  208. if (!isNumber(score)) {
  209. message.error({ content: "非数字输入", duration: 5 });
  210. console.log("非数字输入");
  211. return;
  212. }
  213. if (!questionScoreSteps.value.includes(score)) {
  214. message.error({ content: "输入的分数不在有效间隔内", duration: 5 });
  215. return;
  216. }
  217. store.currentQuestion.score = score;
  218. //
  219. // scoreStr.value = "";
  220. // console.log("give score", score);
  221. const idx = indexOfCurrentQuestion() as number;
  222. if (idx + 1 < store.currentTask?.questionList.length) {
  223. chooseQuestion(store.currentTask.questionList[idx + 1]);
  224. }
  225. return;
  226. }
  227. if (event.key === "ArrowLeft") {
  228. const idx = indexOfCurrentQuestion() as number;
  229. if (idx > 0) {
  230. chooseQuestion(store.currentTask.questionList[idx - 1]);
  231. }
  232. event.preventDefault();
  233. return;
  234. }
  235. if (event.key === "ArrowRight") {
  236. const idx = indexOfCurrentQuestion() as number;
  237. if (idx < store.currentTask.questionList.length - 1) {
  238. chooseQuestion(store.currentTask.questionList[idx + 1]);
  239. }
  240. event.preventDefault();
  241. return;
  242. }
  243. // 处理回退删除分数
  244. if (event.key === "Backspace") {
  245. if (scoreStr.value.length > 0) {
  246. scoreStr.value = scoreStr.value.slice(0, -1);
  247. } else {
  248. return;
  249. }
  250. }
  251. if (event.key === "Escape") {
  252. scoreStr.value = "";
  253. }
  254. // 此时不再接受任何非数字键
  255. if (".0123456789".includes(event.key)) {
  256. scoreStr.value += event.key;
  257. }
  258. }
  259. onMounted(() => {
  260. document.addEventListener("keydown", numberKeyListener);
  261. });
  262. onUnmounted(() => {
  263. document.removeEventListener("keydown", numberKeyListener);
  264. });
  265. const handleMouseOverWithBoard = () => {
  266. if (store.setting.uiSetting["score.board.collapse"]) {
  267. const container = document.querySelector(".mark-board-track-container");
  268. if (container) {
  269. container.classList.add("show-board");
  270. }
  271. }
  272. };
  273. const handleMouseOutWithBoard = () => {
  274. if (store.setting.uiSetting["score.board.collapse"]) {
  275. const container = document.querySelector(".mark-board-track-container");
  276. if (container) {
  277. container.classList.remove("show-board");
  278. }
  279. }
  280. };
  281. const scrollToQuestion = (question: Question) => {
  282. const node = document.querySelector(
  283. `#q-${question.mainNumber}-${question.subNumber}`
  284. );
  285. node && node.scrollIntoView({ behavior: "smooth" });
  286. };
  287. watch(
  288. () => store.currentQuestion,
  289. () => {
  290. store.currentQuestion && scrollToQuestion(store.currentQuestion);
  291. }
  292. );
  293. function submit() {
  294. const errors: any = [];
  295. store.currentTask?.questionList.forEach((question, index) => {
  296. if (!isNumber(question.score)) {
  297. errors.push({
  298. question,
  299. index,
  300. error: `${question.mainNumber}-${question.subNumber} 没有赋分不能提交。`,
  301. });
  302. }
  303. });
  304. if (errors.length === 0) {
  305. emit("submit");
  306. } else {
  307. console.log(errors);
  308. message.error({
  309. content: errors.map((e: any) => `${e.error}`).join("\n"),
  310. duration: 10,
  311. });
  312. }
  313. }
  314. </script>
  315. <style scoped>
  316. .mark-board-track-container {
  317. max-width: 290px;
  318. min-width: 290px;
  319. padding: 20px;
  320. max-height: calc(100vh - 56px - 0px);
  321. overflow: auto;
  322. z-index: 1001;
  323. transition: margin-right 0.5s;
  324. --big-score-color: #283e76;
  325. --high-light-score-color: #5d65ff;
  326. --the-color: #7584ac;
  327. color: var(--the-color);
  328. }
  329. .mark-board-track-container.show {
  330. margin-right: 0;
  331. }
  332. .mark-board-track-container.hide {
  333. margin-right: -290px;
  334. }
  335. .top-container {
  336. background-color: white;
  337. }
  338. .total-score {
  339. color: var(--big-score-color);
  340. font-size: 32px;
  341. }
  342. .question {
  343. min-width: 80px;
  344. padding: 10px;
  345. background-color: white;
  346. }
  347. .current-question .score {
  348. color: var(--high-light-score-color);
  349. }
  350. .current-score {
  351. color: #5d65ff;
  352. }
  353. .current-score-indicator {
  354. position: absolute;
  355. top: 0;
  356. left: 0;
  357. width: 5px;
  358. height: 5px;
  359. background-color: #5c69f6;
  360. }
  361. </style>