FillBlankQuestionView.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div v-if="isSyncState" class="question-view">
  3. <question-body
  4. :question-body="questionBody"
  5. :exam-question="examQuestion"
  6. ></question-body>
  7. <div class="ops">
  8. <div class="score">({{ examQuestion.questionScore }}分)</div>
  9. </div>
  10. <div
  11. v-for="(option, index) in studentAnswer.split('##')"
  12. :key="examQuestion.questionId + index"
  13. class="option"
  14. >
  15. <span class="question-options">{{ index + 1 }}. </span>
  16. <input
  17. type="text"
  18. maxlength="5000"
  19. name="question"
  20. class="input-answer"
  21. :value="option"
  22. @input="inputAnswer"
  23. />
  24. </div>
  25. <div class="reset">
  26. <!-- <i-button type="warning" size="large" @click="resetAnswer">
  27. 重置答案
  28. </i-button> -->
  29. <span v-if="examShouldShowAnswer()">
  30. &nbsp;&nbsp;&nbsp;<i-button
  31. type="info"
  32. size="large"
  33. @click="showAnswer"
  34. >
  35. 显示答案
  36. </i-button>
  37. </span>
  38. <div v-if="examShouldShowAnswer() && isShowAnswer">
  39. 正确答案:
  40. <div class="right-answer-section" v-html="rightAnswerTransform"></div>
  41. </div>
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. import QuestionBody from "./QuestionBody";
  47. import { createNamespacedHelpers } from "vuex";
  48. const { mapMutations, mapGetters } = createNamespacedHelpers(
  49. "examingHomeModule"
  50. );
  51. /**
  52. * 1. 进入页面,this.studentAnswer从examQuestion.studentAnswer获得数据
  53. * 2. 输入答案,this.studentAnswer更新,同时questionbody更新,但不提交答案
  54. * 3. 输入框失去焦点,提交答案
  55. * 4. 切换页面,this.studentAnswer从examQuestion获得数据
  56. *
  57. * */
  58. export default {
  59. name: "FillBlankQuestionView",
  60. components: {
  61. QuestionBody,
  62. },
  63. props: {
  64. question: {
  65. type: Object,
  66. default() {
  67. return {};
  68. },
  69. },
  70. examQuestion: {
  71. type: Object,
  72. default() {
  73. return {};
  74. },
  75. },
  76. },
  77. data() {
  78. return {
  79. studentAnswer: "",
  80. questionBody: "",
  81. isShowAnswer: false,
  82. };
  83. },
  84. computed: {
  85. isSyncState() {
  86. return this.examQuestion.order == this.$route.params.order;
  87. },
  88. rightAnswerTransform() {
  89. // if (
  90. // this.question.rightAnswer &&
  91. // !this.question.rightAnswer.join("").includes("##")
  92. // ) {
  93. // return this.question.rightAnswer;
  94. // }
  95. return (
  96. this.question.rightAnswer &&
  97. this.question.rightAnswer
  98. .join("")
  99. .split("##")
  100. .map((v, i) => `${i + 1}、${v}<br>`)
  101. .join("")
  102. );
  103. },
  104. },
  105. watch: {
  106. // examQuestion: function() {
  107. // this.prepareData();
  108. // },
  109. question(question) {
  110. this.questionBody = question.body;
  111. this.prepareData();
  112. },
  113. studentAnswer() {
  114. let realAnswer = null;
  115. if (this.studentAnswer && this.studentAnswer.replace(/##/g, "").trim()) {
  116. // 如果有实际内容
  117. realAnswer = this.studentAnswer
  118. .replace(/</gi, "&lt;")
  119. .replace(/>/gi, "&gt;");
  120. // console.log("answers to store:", realAnswer);
  121. }
  122. if (realAnswer !== this.examQuestion.studentAnswer) {
  123. this.updateExamQuestion({
  124. order: this.examQuestion.order,
  125. studentAnswer: realAnswer,
  126. });
  127. }
  128. },
  129. },
  130. created() {
  131. this.prepareData();
  132. },
  133. methods: {
  134. ...mapMutations(["updateExamQuestion"]),
  135. ...mapGetters(["examShouldShowAnswer"]),
  136. prepareData() {
  137. const questionNumber = this.question.body.split(/_{5,}/).length - 1;
  138. this.studentAnswer =
  139. this.examQuestion.studentAnswer || "##".repeat(questionNumber - 1);
  140. const answers = this.studentAnswer
  141. .replace(/</gi, "&lt;")
  142. .replace(/>/gi, "&gt;")
  143. .split("##");
  144. this.questionBody = this.question.body.replace(
  145. /_{5,}/g,
  146. () =>
  147. "<span style='display: inline-block; min-width: 80px; border-bottom: 1px solid black; text-align: center'>" +
  148. (answers.shift() || questionNumber - answers.length) +
  149. "</span>"
  150. );
  151. },
  152. inputAnswer: function() {
  153. const questionNumber = this.question.body.split(/_{5,}/).length - 1;
  154. let ans = "";
  155. document
  156. .querySelectorAll(".option input")
  157. .forEach(e => (ans += e.value + "##"));
  158. this.studentAnswer = ans.slice(0, -2);
  159. const answers = this.studentAnswer
  160. .replace(/</gi, "&lt;")
  161. .replace(/>/gi, "&gt;")
  162. .split("##");
  163. // console.log("answers: ", answers);
  164. this.questionBody = this.question.body.replace(
  165. /_{5,}/g,
  166. () =>
  167. "<span style='display: inline-block; min-width: 80px; border-bottom: 1px solid black; text-align: center'>" +
  168. (answers.shift() || questionNumber - answers.length) +
  169. "</span>"
  170. );
  171. },
  172. resetAnswer() {
  173. this.updateExamQuestion({
  174. order: this.examQuestion.order,
  175. studentAnswer: null,
  176. });
  177. this.examQuestion.studentAnswer = null;
  178. this.prepareData();
  179. },
  180. showAnswer() {
  181. this.isShowAnswer = !this.isShowAnswer;
  182. },
  183. },
  184. };
  185. </script>
  186. <style scoped>
  187. .question-view {
  188. display: grid;
  189. grid-row-gap: 10px;
  190. }
  191. .question-body {
  192. font-size: 18px;
  193. /* margin-bottom: 10px; */
  194. }
  195. .ops {
  196. display: flex;
  197. align-items: flex-end;
  198. }
  199. .stu-answer {
  200. width: 100px;
  201. border-bottom: 1px solid black;
  202. text-align: center;
  203. }
  204. .option {
  205. display: flex;
  206. line-height: 24px;
  207. }
  208. .input-answer {
  209. width: 400px;
  210. }
  211. .question-options {
  212. padding-right: 10px;
  213. }
  214. </style>
  215. <style>
  216. div.right-answer-section > p {
  217. display: inline;
  218. }
  219. </style>