TextAnswerQuestion.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div class="text-answer-question">
  3. <div
  4. :class="['ep-question-content', { 'is-active': isActive }]"
  5. @click="activeQuestion"
  6. >
  7. <div class="ep-question-title">
  8. <span class="ep-question-title-number">{{ question.number }}.</span>
  9. <rich-text :text-json="question.body"></rich-text>
  10. </div>
  11. <div v-if="question.exceptions.length" class="ep-question-exception">
  12. <p
  13. class="tips-info tips-error"
  14. v-for="(cont, index) in question.exceptions"
  15. :key="index"
  16. >
  17. {{ cont }}
  18. </p>
  19. </div>
  20. <question-info-view
  21. v-if="!isActive"
  22. :question="getData()"
  23. ></question-info-view>
  24. </div>
  25. <div v-if="isActive" class="ep-question-props">
  26. <el-form ref="modalFormComp" :model="modalForm" label-width="72px">
  27. <el-form-item label="答案">
  28. <v-editor
  29. v-model="quesAnswer"
  30. :enable-audio="false"
  31. @change="answerChange"
  32. ></v-editor>
  33. </el-form-item>
  34. <el-form-item label="答案解析">
  35. <v-editor
  36. v-model="answerAnalysis"
  37. :enable-audio="false"
  38. @change="answerAnalysisChange"
  39. ></v-editor>
  40. </el-form-item>
  41. </el-form>
  42. <question-info-edit
  43. ref="QuestionInfoEdit"
  44. :question="modalForm"
  45. label-width="72px"
  46. use-course-property-cache
  47. @change="questionInfoChange"
  48. ></question-info-edit>
  49. <div class="ep-question-props-close" @click="unactiveQuestion">
  50. <i class="el-icon-error"></i>
  51. </div>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. import { getInitQuestionModel } from "./model";
  57. import QuestionInfoEdit from "../QuestionInfoEdit.vue";
  58. import QuestionInfoView from "./QuestionInfoView.vue";
  59. import { mapState, mapMutations } from "vuex";
  60. import { objTypeOf } from "@/plugins/utils";
  61. import { isAnEmptyRichText } from "@/utils/utils";
  62. export default {
  63. name: "TextAnswerQuestion",
  64. components: { QuestionInfoEdit, QuestionInfoView },
  65. props: {
  66. question: {
  67. type: Object,
  68. default() {
  69. return {};
  70. },
  71. },
  72. },
  73. data() {
  74. return {
  75. modalForm: {},
  76. quesAnswer: null,
  77. answerAnalysis: null,
  78. };
  79. },
  80. computed: {
  81. ...mapState("import-edit", ["curQuestion"]),
  82. isActive() {
  83. return this.curQuestion.id === this.question.id;
  84. },
  85. },
  86. created() {
  87. this.initData();
  88. },
  89. methods: {
  90. ...mapMutations("import-edit", ["setCurQuestion"]),
  91. initData() {
  92. this.modalForm = this.$objAssign(getInitQuestionModel(), this.question);
  93. this.initAnswer(this.question.quesAnswer);
  94. this.initAnswerAnalysis(this.question.answerAnalysis);
  95. this.quesAnswer = this.modalForm.quesAnswer[0] || {
  96. sections: [],
  97. };
  98. this.answerAnalysis = this.modalForm.answerAnalysis[0] || {
  99. sections: [],
  100. };
  101. },
  102. initAnswer(answer) {
  103. let quesAnswer = [];
  104. if (answer) {
  105. try {
  106. quesAnswer = JSON.parse(answer);
  107. } catch (err) {
  108. console.log(`answer error:${answer}`);
  109. }
  110. }
  111. if (this.checkAnswerFormatValid(quesAnswer)) {
  112. this.modalForm.quesAnswer = quesAnswer.map((item) =>
  113. isAnEmptyRichText(item)
  114. ? {
  115. sections: [],
  116. }
  117. : item
  118. );
  119. } else {
  120. this.modalForm.quesAnswer = [];
  121. }
  122. },
  123. initAnswerAnalysis(answer) {
  124. let answerAnalysis = [];
  125. if (answer) {
  126. try {
  127. answerAnalysis = JSON.parse(answer);
  128. } catch (err) {
  129. console.log(`answer error:${answer}`);
  130. }
  131. }
  132. if (this.checkAnswerFormatValid(answerAnalysis)) {
  133. this.modalForm.answerAnalysis = answerAnalysis.map((item) =>
  134. isAnEmptyRichText(item)
  135. ? {
  136. sections: [],
  137. }
  138. : item
  139. );
  140. } else {
  141. this.modalForm.answerAnalysis = [];
  142. }
  143. },
  144. checkAnswerFormatValid(answer) {
  145. return objTypeOf(answer) === "array";
  146. },
  147. answerChange() {
  148. this.modalForm.quesAnswer = [
  149. {
  150. index: 1,
  151. ...this.quesAnswer,
  152. },
  153. ];
  154. },
  155. answerAnalysisChange() {
  156. this.modalForm.answerAnalysis = [
  157. {
  158. ...this.answerAnalysis,
  159. },
  160. ];
  161. },
  162. questionInfoChange(questionInfo) {
  163. this.modalForm = Object.assign({}, this.modalForm, questionInfo);
  164. },
  165. validate() {
  166. return Promise.resolve(true);
  167. },
  168. getData() {
  169. let data = Object.assign({}, this.question, this.modalForm);
  170. data.quesAnswer = JSON.stringify(data.quesAnswer);
  171. data.answerAnalysis =
  172. data.answerAnalysis && data.answerAnalysis?.length
  173. ? data.answerAnalysis[0]
  174. : { sections: [] };
  175. return data;
  176. },
  177. activeQuestion() {
  178. this.setCurQuestion(this.question);
  179. },
  180. unactiveQuestion() {
  181. this.setCurQuestion({});
  182. },
  183. },
  184. };
  185. </script>