autoChooseFirstQuestion.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Question } from "@/types";
  2. import { store } from "@/store/store";
  3. import { watch } from "vue";
  4. const scrollToQuestionOfBoard = async (question: Question) => {
  5. const node = document.querySelector(
  6. `#bq-${question.mainNumber}-${question.subNumber}`
  7. );
  8. const questionNode = document.querySelector(
  9. `#q-${question.mainNumber}-${question.subNumber}`
  10. );
  11. if (!questionNode) {
  12. // 非多媒体阅卷
  13. node && node.scrollIntoView({ block: "center", behavior: "smooth" });
  14. return;
  15. }
  16. // console.log(node);
  17. // node && node.scrollIntoView({ behavior: "smooth" });
  18. // if (node) node.scrollBy({ top: -50 });
  19. // setTimeout(() => {
  20. // if (node) node.parentElement?.scrollTo({ top: 50, left: 0 });
  21. // // node && node.scrollTop = 50//node.scrollIntoView({ behavior: "auto", block: "center" });
  22. // if(node.)
  23. // }, 1500);
  24. async function checkIfEleMoving(ele: Element) {
  25. const { top: oldTop } = ele.getBoundingClientRect();
  26. await new Promise((res) => setTimeout(res, 200));
  27. // console.log(ele.getBoundingClientRect().top, oldTop);
  28. return ele.getBoundingClientRect().top - oldTop !== 0;
  29. }
  30. if (questionNode) {
  31. let isMoving = await checkIfEleMoving(questionNode);
  32. while (isMoving) {
  33. isMoving = await checkIfEleMoving(questionNode);
  34. }
  35. node && node.scrollIntoView({ block: "center", behavior: "smooth" });
  36. }
  37. };
  38. export function chooseQuestion(question: Question) {
  39. store.currentQuestion = question;
  40. // FIXME: maybe should be an async function, temp fix for eslint
  41. void scrollToQuestionOfBoard(question);
  42. }
  43. /** chooseQuestion 当currentTask改变是,自动选择第一题 */
  44. export function autoChooseFirstQuestion() {
  45. watch(
  46. () => store.currentTask,
  47. () => {
  48. // FIXed ME: 此时取到的还是score:null,但是 chooseQuestion之后就变成了score:0
  49. const firstQuestion = store.currentTask?.questionList[0];
  50. if (firstQuestion) {
  51. chooseQuestion(firstQuestion);
  52. }
  53. }
  54. );
  55. return { chooseQuestion };
  56. }