MarkBoardKeyBoard.vue 12 KB

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