MarkBoardKeyBoard.vue 11 KB

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