MarkBoardKeyBoard.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <div
  3. v-if="store.currentTask"
  4. class="mark-board-track is-key-board"
  5. :class="[store.isScoreBoardCollapsed ? 'hide' : 'show']"
  6. >
  7. <div class="board-mode">
  8. <a-dropdown>
  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 class="board-header">
  23. <div class="board-header-left">
  24. <div class="board-header-info">
  25. <img src="@/assets/icons/icon-star.svg" />
  26. <span>总分</span>
  27. </div>
  28. <div class="board-header-score">
  29. <transition-group name="score-number-animation" tag="span">
  30. <span :key="store.currentTask.markResult?.markerScore || 0">{{
  31. store.currentTask.markResult?.markerScore
  32. }}</span>
  33. </transition-group>
  34. </div>
  35. </div>
  36. <qm-button
  37. class="board-header-submit"
  38. size="medium"
  39. type="primary"
  40. @click="submit"
  41. >
  42. 提交
  43. </qm-button>
  44. </div>
  45. <div
  46. v-if="store.currentTaskEnsured.questionList"
  47. class="board-questions"
  48. :style="{
  49. flexGrow: 2,
  50. }"
  51. >
  52. <template
  53. v-for="(question, index) in store.currentTaskEnsured.questionList"
  54. :key="index"
  55. >
  56. <div class="board-question-full-box">
  57. <div
  58. :id="'bq-' + question.mainNumber + '-' + question.subNumber"
  59. :class="[
  60. 'board-question',
  61. { 'is-current': isCurrentQuestion(question) },
  62. ]"
  63. @click="
  64. () => {
  65. chooseQuestion(question);
  66. scrollToQuestion(question);
  67. }
  68. "
  69. >
  70. <div class="question-info">
  71. <div class="question-title" :title="question.title">
  72. {{ question.title }}
  73. </div>
  74. <div class="question-no">
  75. 间隔{{ question.intervalScore }}分,给分区间{{
  76. question.minScore
  77. }}~{{ question.maxScore }}
  78. </div>
  79. <div class="question-no">
  80. {{ question.mainNumber }}-{{ question.subNumber }}
  81. </div>
  82. </div>
  83. <div class="question-score">
  84. <transition-group name="score-number-animation" tag="span">
  85. <span
  86. :key="store.currentTask?.markResult?.scoreList[index] || 0"
  87. >
  88. <!-- 特殊的空格符号 -->
  89. <!-- eslint-disable-next-line no-irregular-whitespace -->
  90. {{ store.currentTask?.markResult?.scoreList[index] ?? " " }}
  91. </span>
  92. </transition-group>
  93. </div>
  94. </div>
  95. </div>
  96. </template>
  97. </div>
  98. </div>
  99. </template>
  100. <script setup lang="ts">
  101. import type { Question } from "@/types";
  102. import { isNumber } from "lodash-es";
  103. import { onMounted, onUnmounted, watch } from "vue";
  104. import { store } from "@/store/store";
  105. import { keyMouse } from "./use/keyboardAndMouse";
  106. import { autoChooseFirstQuestion } from "./use/autoChooseFirstQuestion";
  107. import { message } from "ant-design-vue";
  108. import { DownOutlined } from "@ant-design/icons-vue";
  109. const emit = defineEmits(["submit", "allZeroSubmit", "unselectiveSubmit"]);
  110. const { toggleKeyMouse } = keyMouse();
  111. const { chooseQuestion } = autoChooseFirstQuestion();
  112. /**
  113. * 当前题的输入串,初次是markResult.scoreList中score,然后接收输入字符,回车时判断是否合法,合法则赋值给markResult.scoreList
  114. * 切换到下一题,则重新开始
  115. * */
  116. let scoreStr = $ref("");
  117. watch(
  118. () => [store.currentQuestion, store.setting.uiSetting["normal.mode"]],
  119. () => {
  120. if (isNumber(store.currentQuestion?.score)) {
  121. scoreStr = "" + store.currentQuestion?.score;
  122. } else {
  123. scoreStr = "";
  124. }
  125. },
  126. { immediate: true }
  127. );
  128. const questionScoreSteps = $computed(() => {
  129. const question = store.currentQuestion;
  130. if (!question) return [];
  131. const remainScore = Math.round(question.maxScore * 100) / 100;
  132. const steps = [];
  133. for (
  134. let i = 0;
  135. i <= remainScore;
  136. i = Math.round(i * 100 + question.intervalScore * 100) / 100
  137. ) {
  138. steps.push(i);
  139. }
  140. if (
  141. Math.round(remainScore * 100) % Math.round(question.intervalScore * 100) !==
  142. 0
  143. ) {
  144. steps.push(remainScore);
  145. }
  146. return steps;
  147. });
  148. function isCurrentQuestion(question: Question) {
  149. return (
  150. store.currentQuestion?.mainNumber === question.mainNumber &&
  151. store.currentQuestion?.subNumber === question.subNumber
  152. );
  153. }
  154. const questionScore = $computed(
  155. () =>
  156. store.currentTask &&
  157. store.currentQuestion &&
  158. store.currentTask.markResult.scoreList[store.currentQuestion.__index]
  159. );
  160. function numberKeyListener(event: KeyboardEvent) {
  161. // console.log(event);
  162. if (!store.currentQuestion || !store.currentTask) return;
  163. if (store.globalMask) return;
  164. function indexOfCurrentQuestion() {
  165. return store.currentTaskEnsured.questionList.findIndex(
  166. (q) =>
  167. q.mainNumber === store.currentQuestion?.mainNumber &&
  168. q.subNumber === store.currentQuestion.subNumber
  169. );
  170. }
  171. // 处理Enter跳下一题或submit
  172. if (event.key === "Enter") {
  173. const allScoreMarked = store.currentTask.markResult.scoreList.every((s) =>
  174. isNumber(s)
  175. );
  176. // 如果所有题已赋分,并且当前题赋分和输入串和当前题分数一致,则可以在任意题提交
  177. if (allScoreMarked && scoreStr === "" + questionScore) {
  178. submit();
  179. return;
  180. }
  181. if (scoreStr.length === 0) {
  182. void message.error({ content: "请输入分数", duration: 5 });
  183. console.log("请输入分数");
  184. return;
  185. }
  186. // 0 分
  187. // 整数分 (开始不为0)
  188. // 小数分 (开始和结束不为0,结束必须为1-9
  189. if (
  190. !(
  191. scoreStr === "0" ||
  192. /^0\.[1-9]\d*$/.test(scoreStr) ||
  193. /^[1-9]\d*$/.test(scoreStr) ||
  194. /^[1-9]\d*\.\d*[1-9]$/.test(scoreStr)
  195. )
  196. ) {
  197. void message.error({ content: "分数格式不正确", duration: 5 });
  198. console.log("分数格式不正确");
  199. return;
  200. }
  201. const score = parseFloat(scoreStr);
  202. // console.log(score);
  203. if (!isNumber(score)) {
  204. void message.error({ content: "非数字输入", duration: 5 });
  205. console.log("非数字输入");
  206. return;
  207. }
  208. if (!questionScoreSteps.includes(score)) {
  209. void message.error({ content: "输入的分数不在有效间隔内", duration: 5 });
  210. return;
  211. }
  212. const { __index } = store.currentQuestion;
  213. store.currentTask.markResult.scoreList[__index] = score;
  214. //
  215. // scoreStr = "";
  216. // console.log("give score", score);
  217. const idx = indexOfCurrentQuestion();
  218. if (idx + 1 < store.currentTask.questionList.length) {
  219. chooseQuestion(store.currentTask.questionList[idx + 1]);
  220. }
  221. return;
  222. }
  223. if (event.key === "ArrowUp") {
  224. const idx = indexOfCurrentQuestion();
  225. if (idx > 0) {
  226. chooseQuestion(store.currentTask.questionList[idx - 1]);
  227. }
  228. event.preventDefault();
  229. return;
  230. }
  231. if (event.key === "ArrowDown") {
  232. const idx = indexOfCurrentQuestion();
  233. if (idx < store.currentTask.questionList.length - 1) {
  234. chooseQuestion(store.currentTask.questionList[idx + 1]);
  235. }
  236. event.preventDefault();
  237. return;
  238. }
  239. // 处理回退删除分数
  240. if (event.key === "Backspace") {
  241. if (scoreStr.length > 0) {
  242. scoreStr = scoreStr.slice(0, -1);
  243. } else {
  244. return;
  245. }
  246. }
  247. if (event.key === "Escape") {
  248. scoreStr = "";
  249. }
  250. // 此时不再接受任何非数字键
  251. if (".0123456789".includes(event.key)) {
  252. scoreStr += event.key;
  253. }
  254. }
  255. onMounted(() => {
  256. document.addEventListener("keydown", numberKeyListener);
  257. });
  258. onUnmounted(() => {
  259. document.removeEventListener("keydown", numberKeyListener);
  260. });
  261. const scrollToQuestion = (question: Question) => {
  262. const node = document.querySelector(
  263. `#q-${question.mainNumber}-${question.subNumber}`
  264. );
  265. node && node.scrollIntoView({ behavior: "smooth" });
  266. };
  267. watch(
  268. () => store.currentQuestion,
  269. () => {
  270. store.currentQuestion && scrollToQuestion(store.currentQuestion);
  271. }
  272. );
  273. function submit() {
  274. emit("submit");
  275. }
  276. const buttonHeightForSelective = $computed(() =>
  277. store.setting.selective && store.setting.enableAllZero ? "36px" : "76px"
  278. );
  279. </script>
  280. <style scoped>
  281. .mark-board-track-container {
  282. max-width: 290px;
  283. min-width: 290px;
  284. padding: 20px;
  285. max-height: calc(100vh - 56px - 0px);
  286. overflow: auto;
  287. z-index: 1001;
  288. transition: margin-right 0.5s;
  289. color: var(--app-small-header-text-color);
  290. }
  291. .mark-board-track-container.show {
  292. margin-right: 0;
  293. }
  294. .mark-board-track-container.hide {
  295. margin-right: -290px;
  296. }
  297. .top-container {
  298. background-color: var(--app-container-bg-color);
  299. }
  300. .total-score {
  301. color: var(--app-main-text-color);
  302. font-size: 32px;
  303. }
  304. .question {
  305. min-width: 80px;
  306. padding: 10px;
  307. background-color: var(--app-container-bg-color);
  308. }
  309. .current-question {
  310. color: white;
  311. background-color: var(--app-score-color);
  312. }
  313. .current-score {
  314. background-color: var(--app-score-color);
  315. color: white;
  316. }
  317. .all-zero-unselective-button {
  318. height: v-bind(buttonHeightForSelective);
  319. border-radius: 10px;
  320. padding: 7px;
  321. background-color: #4db9ff;
  322. border: none;
  323. }
  324. </style>