FillBlankQuestion.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 v-model="modalForm.comment"></v-editor>
  37. </el-form-item>
  38. </el-form>
  39. <question-info-edit
  40. ref="QuestionInfoEdit"
  41. :question="modalForm"
  42. @change="questionInfoChange"
  43. ></question-info-edit>
  44. </div>
  45. </template>
  46. <script>
  47. import { isAnEmptyRichText } from "@/utils/utils";
  48. import { getInitQuestionModel } from "../model/questionModel";
  49. import QuestionInfoEdit from "../QuestionInfoEdit.vue";
  50. export default {
  51. name: "FillBlankQuestion",
  52. components: { QuestionInfoEdit },
  53. props: {
  54. question: {
  55. type: Object,
  56. default() {
  57. return {};
  58. },
  59. },
  60. },
  61. data() {
  62. return {
  63. modalForm: {},
  64. rules: {
  65. quesBody: [
  66. {
  67. validator: (rule, value, callback) => {
  68. if (!value || isAnEmptyRichText(value)) {
  69. return callback(new Error(`请输入题干`));
  70. }
  71. if (!this.modalForm.quesAnswer.length) {
  72. return callback(new Error(`请插入答题点`));
  73. }
  74. callback();
  75. },
  76. trigger: "change",
  77. },
  78. ],
  79. },
  80. answerRule: {
  81. validator: (rule, value, callback) => {
  82. if (!value || isAnEmptyRichText(value)) {
  83. return callback(new Error(`请输入答案内容`));
  84. }
  85. callback();
  86. },
  87. trigger: "change",
  88. },
  89. };
  90. },
  91. created() {
  92. this.initData();
  93. },
  94. methods: {
  95. initData() {
  96. this.modalForm = this.$objAssign(
  97. getInitQuestionModel("FILL_BLANK_QUESTION"),
  98. this.question
  99. );
  100. this.modalForm.quesAnswer = this.question.quesAnswer
  101. ? JSON.parse(this.question.quesAnswer)
  102. : [];
  103. this.modalForm.quesAnswer = this.modalForm.quesAnswer.map((item) => {
  104. return {
  105. body: item,
  106. };
  107. });
  108. },
  109. answerPointsChange(answerPointsChanged) {
  110. // console.log(answerPointsChanged);
  111. let quesAnswer = [];
  112. answerPointsChanged.forEach((item) => {
  113. const order = Number(item);
  114. if (order === 0) {
  115. quesAnswer.push({
  116. body: null,
  117. });
  118. } else {
  119. quesAnswer.push(this.modalForm.quesAnswer[order - 1]);
  120. }
  121. });
  122. this.modalForm.quesAnswer = quesAnswer;
  123. },
  124. quesBodyChange() {
  125. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  126. },
  127. answerBodyChange(oindex) {
  128. this.$refs.modalFormComp.validateField(
  129. `quesAnswer.${oindex}.body`,
  130. () => {}
  131. );
  132. },
  133. questionInfoChange(questionInfo) {
  134. this.modalForm = Object.assign({}, this.modalForm, questionInfo);
  135. },
  136. validate() {
  137. return this.$refs.modalFormComp.validate();
  138. },
  139. getData() {
  140. let data = { ...this.modalForm };
  141. data.quesAnswer = this.modalForm.quesAnswer.map((item, index) => {
  142. return {
  143. ...item.body,
  144. index,
  145. };
  146. });
  147. data.quesAnswer = JSON.stringify(data.quesAnswer);
  148. return data;
  149. },
  150. },
  151. };
  152. </script>