MarkBoardKeyBoard.vue 11 KB

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