FillBlankQuestion.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="fill-blank-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. label-width="100px"
  8. >
  9. <el-form-item prop="quesBody" label="题干">
  10. <v-editor
  11. v-model="modalForm.quesBody"
  12. enable-answer-point
  13. @answer-point-changed="answerPointsChange"
  14. @change="quesBodyChange"
  15. ></v-editor>
  16. </el-form-item>
  17. <el-form-item label="答案"></el-form-item>
  18. <el-form-item
  19. v-for="(answer, oindex) in modalForm.quesAnswer"
  20. :key="oindex"
  21. :rules="answerRule"
  22. >
  23. <!-- :prop="`quesAnswer.${oindex}.body`" -->
  24. <div class="question-edit-option">
  25. <div class="option-check">({{ oindex + 1 }})</div>
  26. <div class="option-body">
  27. <v-editor
  28. v-model="answer.body"
  29. :enable-audio="false"
  30. @change="() => answerBodyChange(oindex)"
  31. ></v-editor>
  32. </div>
  33. </div>
  34. </el-form-item>
  35. <el-form-item label="答案解析">
  36. <v-editor
  37. v-model="modalForm.answerAnalysis"
  38. :enable-audio="false"
  39. ></v-editor>
  40. </el-form-item>
  41. </el-form>
  42. <question-info-edit
  43. v-if="canEditQuestionInfo"
  44. ref="QuestionInfoEdit"
  45. :question="modalForm"
  46. @change="questionInfoChange"
  47. ></question-info-edit>
  48. </div>
  49. </template>
  50. <script>
  51. import { isAnEmptyRichText } from "@/utils/utils";
  52. import { getInitQuestionModel } from "./questionModel";
  53. import QuestionInfoEdit from "../QuestionInfoEdit.vue";
  54. export default {
  55. name: "FillBlankQuestion",
  56. components: { QuestionInfoEdit },
  57. props: {
  58. question: {
  59. type: Object,
  60. default() {
  61. return {};
  62. },
  63. },
  64. canEditQuestionInfo: {
  65. type: Boolean,
  66. default: true,
  67. },
  68. },
  69. data() {
  70. return {
  71. modalForm: {},
  72. rules: {
  73. quesBody: [
  74. {
  75. validator: (rule, value, callback) => {
  76. if (!value || isAnEmptyRichText(value)) {
  77. return callback(new Error(`请输入题干`));
  78. }
  79. if (!this.modalForm.quesAnswer.length) {
  80. return callback(new Error(`请插入答题点`));
  81. }
  82. callback();
  83. },
  84. trigger: "change",
  85. },
  86. ],
  87. },
  88. answerRule: {
  89. validator: (rule, value, callback) => {
  90. if (!value || isAnEmptyRichText(value)) {
  91. return callback(new Error(`请输入答案内容`));
  92. }
  93. callback();
  94. },
  95. trigger: "change",
  96. },
  97. };
  98. },
  99. created() {
  100. this.initData();
  101. },
  102. methods: {
  103. initData() {
  104. this.modalForm = this.$objAssign(
  105. getInitQuestionModel("FILL_BLANK_QUESTION"),
  106. this.question
  107. );
  108. this.modalForm.quesAnswer = this.question.quesAnswer
  109. ? JSON.parse(this.question.quesAnswer)
  110. : [];
  111. this.modalForm.quesAnswer = this.modalForm.quesAnswer.map((item) => {
  112. return {
  113. body: item,
  114. };
  115. });
  116. },
  117. answerPointsChange(answerPointsChanged) {
  118. // console.log(answerPointsChanged);
  119. let quesAnswer = [];
  120. answerPointsChanged.forEach((item) => {
  121. const order = Number(item);
  122. if (order === 0) {
  123. quesAnswer.push({
  124. body: null,
  125. });
  126. } else {
  127. quesAnswer.push(this.modalForm.quesAnswer[order - 1]);
  128. }
  129. });
  130. this.modalForm.quesAnswer = quesAnswer;
  131. },
  132. quesBodyChange() {
  133. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  134. },
  135. answerBodyChange(oindex) {
  136. this.$refs.modalFormComp.validateField(
  137. `quesAnswer.${oindex}.body`,
  138. () => {}
  139. );
  140. },
  141. questionInfoChange(questionInfo) {
  142. this.modalForm = Object.assign({}, this.modalForm, questionInfo);
  143. },
  144. validate() {
  145. return this.$refs.modalFormComp.validate();
  146. },
  147. getData() {
  148. let data = { ...this.modalForm };
  149. data.quesAnswer = this.modalForm.quesAnswer.map((item, index) => {
  150. return {
  151. ...item.body,
  152. index,
  153. };
  154. });
  155. data.quesAnswer = JSON.stringify(data.quesAnswer);
  156. return data;
  157. },
  158. },
  159. };
  160. </script>