useAutoChooseFirstQuestion.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. watch(
  10. () => markStore.currentTask,
  11. () => {
  12. // 重置当前选择的quesiton和score
  13. markStore.currentQuestion = undefined;
  14. markStore.currentScore = undefined;
  15. // FIXed ME: 此时取到的还是score:null,但是 chooseQuestion之后就变成了score:0
  16. const firstQuestion = markStore.currentTask?.questionList[0];
  17. if (firstQuestion) {
  18. chooseQuestion(firstQuestion);
  19. }
  20. }
  21. );
  22. const scrollToQuestionOfBoard = async (question: Question) => {
  23. const node = document.querySelector(
  24. `#bq-${question.mainNumber}-${question.subNumber}`
  25. );
  26. const questionNode = document.querySelector(
  27. `#q-${question.mainNumber}-${question.subNumber}`
  28. );
  29. if (!questionNode) {
  30. // 非多媒体阅卷
  31. node && node.scrollIntoView({ block: "center", behavior: "smooth" });
  32. return;
  33. }
  34. // console.log(node);
  35. // node && node.scrollIntoView({ behavior: "smooth" });
  36. // if (node) node.scrollBy({ top: -50 });
  37. // setTimeout(() => {
  38. // if (node) node.parentElement?.scrollTo({ top: 50, left: 0 });
  39. // // node && node.scrollTop = 50//node.scrollIntoView({ behavior: "auto", block: "center" });
  40. // if(node.)
  41. // }, 1500);
  42. async function checkIfEleMoving(ele: Element) {
  43. const { top: oldTop } = ele.getBoundingClientRect();
  44. await new Promise((res) => setTimeout(res, 200));
  45. // console.log(ele.getBoundingClientRect().top, oldTop);
  46. return ele.getBoundingClientRect().top - oldTop !== 0;
  47. }
  48. if (questionNode) {
  49. let isMoving = await checkIfEleMoving(questionNode);
  50. while (isMoving) {
  51. isMoving = await checkIfEleMoving(questionNode);
  52. }
  53. node && node.scrollIntoView({ block: "center", behavior: "smooth" });
  54. }
  55. };
  56. function chooseQuestion(question: Question) {
  57. if (!question.selfMark) return;
  58. markStore.currentQuestion = question;
  59. // FIXME: maybe should be an async function, temp fix for eslint
  60. void scrollToQuestionOfBoard(question);
  61. void nextTick(() => {
  62. if (markStore.currentQuestion) {
  63. showRejectedReason(markStore.currentQuestion);
  64. }
  65. });
  66. }
  67. return { chooseQuestion };
  68. }