useAutoChooseFirstQuestion.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Question } from "@/types";
  2. import { useMarkStore } from "@/store";
  3. import { watch, nextTick } from "vue";
  4. import useTaskRejection from "./useTaskRejection";
  5. /** chooseQuestion 当currentTask改变是,自动选择第一题 */
  6. export default function useAutoChooseFirstQuestion() {
  7. const markStore = useMarkStore();
  8. const { showRejectedReason } = useTaskRejection();
  9. // 监听currentTask的变化,自动选择第一题
  10. function watchCurrentTaskChooseQuestion() {
  11. watch(
  12. () => markStore.currentTask,
  13. () => {
  14. // 重置当前选择的quesiton和score
  15. markStore.currentQuestion = undefined;
  16. markStore.currentScore = undefined;
  17. // FIXed ME: 此时取到的还是score:null,但是 chooseQuestion之后就变成了score:0
  18. const firstQuestion = markStore.currentTask?.questionList.find(
  19. (question) => !isDisabledQuestion(question)
  20. );
  21. if (firstQuestion) {
  22. chooseQuestion(firstQuestion);
  23. }
  24. },
  25. {
  26. immediate: true,
  27. }
  28. );
  29. }
  30. const scrollToQuestionOfBoard = async (question: Question) => {
  31. const node = document.querySelector(
  32. `#bq-${question.mainNumber}-${question.subNumber}`
  33. );
  34. const questionNode = document.querySelector(
  35. `#q-${question.mainNumber}-${question.subNumber}`
  36. );
  37. if (!questionNode) {
  38. // 非多媒体阅卷
  39. node && node.scrollIntoView({ block: "center", behavior: "smooth" });
  40. return;
  41. }
  42. // console.log(node);
  43. // node && node.scrollIntoView({ behavior: "smooth" });
  44. // if (node) node.scrollBy({ top: -50 });
  45. // setTimeout(() => {
  46. // if (node) node.parentElement?.scrollTo({ top: 50, left: 0 });
  47. // // node && node.scrollTop = 50//node.scrollIntoView({ behavior: "auto", block: "center" });
  48. // if(node.)
  49. // }, 1500);
  50. async function checkIfEleMoving(ele: Element) {
  51. const { top: oldTop } = ele.getBoundingClientRect();
  52. await new Promise((res) => setTimeout(res, 200));
  53. // console.log(ele.getBoundingClientRect().top, oldTop);
  54. return ele.getBoundingClientRect().top - oldTop !== 0;
  55. }
  56. if (questionNode) {
  57. let isMoving = await checkIfEleMoving(questionNode);
  58. while (isMoving) {
  59. isMoving = await checkIfEleMoving(questionNode);
  60. }
  61. node && node.scrollIntoView({ block: "center", behavior: "smooth" });
  62. }
  63. };
  64. // 是否处于仲裁阶段
  65. function isArbitrated(question: Question) {
  66. return ["WAIT_ARBITRATE", "ARBITRATED"].includes(question.status);
  67. }
  68. function getArbitratedStatusName(question: Question) {
  69. if (question.status === "WAIT_ARBITRATE") {
  70. return "待仲裁";
  71. } else if (question.status === "ARBITRATED") {
  72. return "已仲裁";
  73. }
  74. return "";
  75. }
  76. function isDisabledQuestion(question: Question) {
  77. return (
  78. (!markStore.historyOpen && !question.selfMark) ||
  79. (markStore.historyOpen && question.problem) ||
  80. isArbitrated(question)
  81. );
  82. }
  83. function chooseQuestion(question: Question) {
  84. if (isDisabledQuestion(question)) return;
  85. markStore.currentQuestion = question;
  86. // FIXME: maybe should be an async function, temp fix for eslint
  87. void scrollToQuestionOfBoard(question);
  88. void nextTick(() => {
  89. if (markStore.currentQuestion) {
  90. showRejectedReason(markStore.currentQuestion);
  91. }
  92. });
  93. }
  94. return {
  95. watchCurrentTaskChooseQuestion,
  96. chooseQuestion,
  97. isDisabledQuestion,
  98. isArbitrated,
  99. getArbitratedStatusName,
  100. };
  101. }