MarkBoardKeyBoard.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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.currentMarkResult?.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. {{ isCurrentQuestion(question) ? scoreStr : question.score }}
  122. </div>
  123. </div>
  124. <div>
  125. <div class="tw-text-center">
  126. 间隔{{ question.intervalScore }}分
  127. </div>
  128. <div class="tw-flex tw-text-3xl" style="width: 80px">
  129. <span class="tw-flex-1">{{ question.minScore }}</span>
  130. <span class="tw-flex-1">~</span>
  131. <span class="tw-flex-1 tw-text-center">{{
  132. question.maxScore
  133. }}</span>
  134. </div>
  135. </div>
  136. </div>
  137. </div>
  138. </template>
  139. </div>
  140. </div>
  141. </template>
  142. <script setup lang="ts">
  143. import type { Question } from "@/types";
  144. import { isNumber } from "lodash";
  145. import { computed, onMounted, onUnmounted, watch } from "vue";
  146. import { store } from "./store";
  147. import { keyMouse } from "./use/keyboardAndMouse";
  148. import { autoChooseFirstQuestion } from "./use/autoChooseFirstQuestion";
  149. import { message } from "ant-design-vue";
  150. import { DownOutlined } from "@ant-design/icons-vue";
  151. const emit = defineEmits(["submit", "allZeroSubmit", "unselectiveSubmit"]);
  152. const { toggleKeyMouse } = keyMouse();
  153. const { chooseQuestion } = autoChooseFirstQuestion();
  154. /**
  155. * 当前题的输入串,初次是question.score,然后接收输入字符,回车时判断是否合法,合法则赋值给question.score
  156. * 切换到下一题,则重新开始
  157. * */
  158. let scoreStr = $ref("");
  159. watch(
  160. () => [store.currentQuestion, store.setting.uiSetting["normal.mode"]],
  161. () => {
  162. if (isNumber(store.currentQuestion?.score)) {
  163. scoreStr = "" + store.currentQuestion?.score;
  164. } else {
  165. scoreStr = "";
  166. }
  167. },
  168. { immediate: true }
  169. );
  170. let questionScoreSteps = $computed(() => {
  171. const question = store.currentQuestion;
  172. if (!question) return [];
  173. const remainScore = Math.round(question.maxScore * 100) / 100;
  174. const steps = [];
  175. for (
  176. let i = 0;
  177. i <= remainScore;
  178. i = Math.round(i * 100 + question.intervalScore * 100) / 100
  179. ) {
  180. steps.push(i);
  181. }
  182. if (
  183. Math.round(remainScore * 100) % Math.round(question.intervalScore * 100) !==
  184. 0
  185. ) {
  186. steps.push(remainScore);
  187. }
  188. return steps;
  189. });
  190. function isCurrentQuestion(question: Question) {
  191. return (
  192. store.currentQuestion?.mainNumber === question.mainNumber &&
  193. store.currentQuestion?.subNumber === question.subNumber
  194. );
  195. }
  196. function numberKeyListener(event: KeyboardEvent) {
  197. // console.log(event);
  198. if (!store.currentQuestion || !store.currentTask) return;
  199. if (store.globalMask) return;
  200. function indexOfCurrentQuestion() {
  201. return store.currentTask?.questionList.findIndex(
  202. (q) =>
  203. q.mainNumber === store.currentQuestion?.mainNumber &&
  204. q.subNumber === store.currentQuestion.subNumber
  205. );
  206. }
  207. // 处理Enter跳下一题或submit
  208. if (event.key === "Enter") {
  209. const allScoreMarked = store.currentTask?.questionList.every((q) =>
  210. isNumber(q.score)
  211. );
  212. // 如果所有题已赋分,并且当前题赋分和输入串和当前题分数一致,则可以在任意题提交
  213. if (allScoreMarked && scoreStr === "" + store.currentQuestion.score) {
  214. submit();
  215. return;
  216. }
  217. if (scoreStr.length === 0) {
  218. message.error({ content: "请输入分数", duration: 5 });
  219. console.log("请输入分数");
  220. return;
  221. }
  222. // 0 分
  223. // 整数分 (开始不为0)
  224. // 小数分 (开始和结束不为0,结束必须为1-9
  225. if (
  226. !(
  227. scoreStr === "0" ||
  228. /^0\.[1-9]\d*$/.test(scoreStr) ||
  229. /^[1-9]\d*$/.test(scoreStr) ||
  230. /^[1-9]\d*\.\d*[1-9]$/.test(scoreStr)
  231. )
  232. ) {
  233. message.error({ content: "分数格式不正确", duration: 5 });
  234. console.log("分数格式不正确");
  235. return;
  236. }
  237. const score = parseFloat(scoreStr);
  238. // console.log(score);
  239. if (!isNumber(score)) {
  240. message.error({ content: "非数字输入", duration: 5 });
  241. console.log("非数字输入");
  242. return;
  243. }
  244. if (!questionScoreSteps.includes(score)) {
  245. message.error({ content: "输入的分数不在有效间隔内", duration: 5 });
  246. return;
  247. }
  248. store.currentQuestion.score = score;
  249. //
  250. // scoreStr = "";
  251. // console.log("give score", score);
  252. const idx = indexOfCurrentQuestion() as number;
  253. if (idx + 1 < store.currentTask?.questionList.length) {
  254. chooseQuestion(store.currentTask.questionList[idx + 1]);
  255. }
  256. return;
  257. }
  258. if (event.key === "ArrowLeft") {
  259. const idx = indexOfCurrentQuestion() as number;
  260. if (idx > 0) {
  261. chooseQuestion(store.currentTask.questionList[idx - 1]);
  262. }
  263. event.preventDefault();
  264. return;
  265. }
  266. if (event.key === "ArrowRight") {
  267. const idx = indexOfCurrentQuestion() as number;
  268. if (idx < store.currentTask.questionList.length - 1) {
  269. chooseQuestion(store.currentTask.questionList[idx + 1]);
  270. }
  271. event.preventDefault();
  272. return;
  273. }
  274. // 处理回退删除分数
  275. if (event.key === "Backspace") {
  276. if (scoreStr.length > 0) {
  277. scoreStr = scoreStr.slice(0, -1);
  278. } else {
  279. return;
  280. }
  281. }
  282. if (event.key === "Escape") {
  283. scoreStr = "";
  284. }
  285. // 此时不再接受任何非数字键
  286. if (".0123456789".includes(event.key)) {
  287. scoreStr += event.key;
  288. }
  289. }
  290. onMounted(() => {
  291. document.addEventListener("keydown", numberKeyListener);
  292. });
  293. onUnmounted(() => {
  294. document.removeEventListener("keydown", numberKeyListener);
  295. });
  296. const handleMouseOverWithBoard = () => {
  297. if (store.setting.uiSetting["score.board.collapse"]) {
  298. const container = document.querySelector(".mark-board-track-container");
  299. if (container) {
  300. container.classList.add("show-board");
  301. }
  302. }
  303. };
  304. const handleMouseOutWithBoard = () => {
  305. if (store.setting.uiSetting["score.board.collapse"]) {
  306. const container = document.querySelector(".mark-board-track-container");
  307. if (container) {
  308. container.classList.remove("show-board");
  309. }
  310. }
  311. };
  312. const scrollToQuestion = (question: Question) => {
  313. const node = document.querySelector(
  314. `#q-${question.mainNumber}-${question.subNumber}`
  315. );
  316. node && node.scrollIntoView({ behavior: "smooth" });
  317. };
  318. watch(
  319. () => store.currentQuestion,
  320. () => {
  321. store.currentQuestion && scrollToQuestion(store.currentQuestion);
  322. }
  323. );
  324. function submit() {
  325. const errors: any = [];
  326. store.currentTask?.questionList.forEach((question, index) => {
  327. if (!isNumber(question.score)) {
  328. errors.push({
  329. question,
  330. index,
  331. error: `${question.mainNumber}-${question.subNumber} 没有赋分不能提交。`,
  332. });
  333. }
  334. });
  335. if (errors.length === 0) {
  336. emit("submit");
  337. } else {
  338. console.log(errors);
  339. message.error({
  340. content: errors.map((e: any) => `${e.error}`).join("\n"),
  341. duration: 10,
  342. });
  343. }
  344. }
  345. const buttonHeightForSelective = computed(() =>
  346. store.setting.selective && store.setting.enableAllZero ? "36px" : "76px"
  347. );
  348. </script>
  349. <style scoped>
  350. .mark-board-track-container {
  351. max-width: 290px;
  352. min-width: 290px;
  353. padding: 20px;
  354. max-height: calc(100vh - 56px - 0px);
  355. overflow: auto;
  356. z-index: 1001;
  357. transition: margin-right 0.5s;
  358. color: var(--app-small-header-text-color);
  359. }
  360. .mark-board-track-container.show {
  361. margin-right: 0;
  362. }
  363. .mark-board-track-container.hide {
  364. margin-right: -290px;
  365. }
  366. .top-container {
  367. background-color: var(--app-container-bg-color);
  368. }
  369. .total-score {
  370. color: var(--app-main-text-color);
  371. font-size: 32px;
  372. }
  373. .question {
  374. min-width: 80px;
  375. padding: 10px;
  376. background-color: var(--app-container-bg-color);
  377. }
  378. .current-question .score {
  379. color: var(--high-light-score-color);
  380. }
  381. .current-score {
  382. color: var(--app-score-color);
  383. }
  384. .current-score-indicator {
  385. position: absolute;
  386. top: 0;
  387. left: 0;
  388. width: 5px;
  389. height: 5px;
  390. background-color: #5c69f6;
  391. }
  392. .all-zero-unselective-button {
  393. height: v-bind(buttonHeightForSelective);
  394. border-radius: 10px;
  395. padding: 7px;
  396. background-color: #4db9ff;
  397. border: none;
  398. }
  399. </style>