MultiMediaMarkBody.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div>
  3. <div ref="rendered" class="rich-text-question-container"></div>
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import { getJSON } from "@/api/jsonMark";
  8. import { store } from "./store";
  9. import { ref, watch } from "vue";
  10. import { renderRichText } from "@/utils/renderJSON";
  11. import type { RichTextJSON } from "@/types";
  12. const rendered = ref(null as unknown as HTMLDivElement);
  13. async function updateStudentAnswerJSON() {
  14. return getJSON(store.currentTask?.jsonUrl as string);
  15. }
  16. watch(
  17. () => store.currentTask,
  18. async () => {
  19. const res = await updateStudentAnswerJSON();
  20. const questions = res.data; // TODO: add type
  21. for (const q of questions) {
  22. if (q.answer && !Array.isArray(q.answer)) {
  23. q.answer = [q.answer];
  24. }
  25. }
  26. // TODO: 最好变成有结构的v-for渲染
  27. for (const taskQuestion of store.currentTask?.questionList || []) {
  28. const q = questions.find(
  29. (v: any) =>
  30. v.mainNumber === taskQuestion.mainNumber &&
  31. v.subNumber == taskQuestion.subNumber
  32. ) || {
  33. mainNumber: taskQuestion.mainNumber,
  34. subNumber: taskQuestion.subNumber,
  35. answer: [null],
  36. };
  37. const questionBody = store.setting.subject.questions.find(
  38. (ques) => ques.unionOrder === `${q.mainNumber}-${q.subNumber}`
  39. );
  40. // const jsonContent = q.answer;
  41. function appendRichText(rt: RichTextJSON) {
  42. const richText = renderRichText(rt);
  43. richText && rendered.value.appendChild(richText);
  44. return richText;
  45. }
  46. const questionOrderNode = document.createElement("div");
  47. questionOrderNode.innerHTML = `<span id="q-${questionBody?.unionOrder}">题号:${questionBody?.unionOrder}</span>`;
  48. rendered.value.appendChild(questionOrderNode);
  49. const questionBodyNode = document.createElement("div");
  50. questionBodyNode.innerHTML = `题干:`;
  51. rendered.value.appendChild(questionBodyNode);
  52. questionBody?.parentBody && appendRichText(questionBody?.parentBody);
  53. questionBody?.body && appendRichText(questionBody?.body);
  54. const studentAnswerNode = document.createElement("div");
  55. let tempNode = document.createElement("div");
  56. for (const an of q.answer) {
  57. // allAnswer += renderRichText(an);
  58. tempNode.appendChild(renderRichText(an));
  59. }
  60. studentAnswerNode.innerHTML = `考生答案:(字数统计:${
  61. tempNode?.innerText?.length ?? 0
  62. })${tempNode?.innerHTML}`;
  63. rendered.value.appendChild(studentAnswerNode);
  64. q.answer && appendRichText(q.answer);
  65. const standardAnswerNode = document.createElement("div");
  66. standardAnswerNode.innerHTML = `标准答案:`;
  67. rendered.value.appendChild(standardAnswerNode);
  68. // questionBody?.answer && appendRichText(questionBody.answer);
  69. // console.log(questionBody.answer, renderRichText(questionBody.answer));
  70. if (questionBody?.answer) {
  71. // let allAnswer = "";
  72. for (const an of questionBody?.answer) {
  73. // allAnswer += renderRichText(an);
  74. rendered.value.appendChild(renderRichText(an));
  75. }
  76. }
  77. const questionSeperatorNode = document.createElement("hr");
  78. questionSeperatorNode.style.margin = "20px";
  79. rendered.value.appendChild(questionSeperatorNode);
  80. // q.answer && appendRichText(q.answer);
  81. }
  82. },
  83. { immediate: true }
  84. );
  85. </script>
  86. <style scoped>
  87. .rich-text-question-container {
  88. background-color: white;
  89. }
  90. hr {
  91. margin: 20px;
  92. }
  93. </style>
  94. <style>
  95. .rich-text-question-container img.inline {
  96. display: inline;
  97. }
  98. </style>