MarkBoardKeyBoard.vue 12 KB

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