JudgePreview.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="judge-preview" :style="getAreaStyle()">
  4. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  5. <div class="main">
  6. <ul class="option-list">
  7. <li
  8. v-for="({ content, mark, paragraph_list }, i) in data.option_list"
  9. :key="mark"
  10. :style="{ cursor: disabled ? 'not-allowed' : 'pointer' }"
  11. :class="['option-item', { active: isAnswer(mark) }]"
  12. >
  13. <div
  14. :style="{ borderColor: data.unified_attrib.topic_color }"
  15. :class="['option-content', computedIsJudgeRight(mark)]"
  16. >
  17. <span class="serial-number">{{ convertNumberToLetter(i) }}.</span>
  18. <PinyinText
  19. v-if="isEnable(data.property.view_pinyin)"
  20. class="content"
  21. :paragraph-list="paragraph_list"
  22. :pinyin-position="data.property.pinyin_position"
  23. :is-preview="true"
  24. />
  25. <div
  26. v-else
  27. class="rich-text"
  28. :style="{ fontSize: type === typeList[0] ? '12pt' : '' }"
  29. v-html="sanitizeHTML(content)"
  30. ></div>
  31. </div>
  32. <div class="option-type">
  33. <div
  34. v-for="option_type in incertitudeList"
  35. :key="option_type"
  36. :style="{ cursor: disabled ? 'not-allowed' : 'pointer' }"
  37. :class="[
  38. 'option-type-item',
  39. {
  40. active: isAnswer(mark, option_type),
  41. },
  42. computedIsShowRightAnswer(mark, option_type),
  43. ]"
  44. @click="selectAnswer(mark, option_type)"
  45. >
  46. <SvgIcon
  47. v-if="option_type === option_type_list[0].value"
  48. icon-class="check-mark"
  49. width="17"
  50. height="12"
  51. />
  52. <SvgIcon v-if="option_type === option_type_list[1].value" icon-class="cross" size="12" />
  53. <SvgIcon v-if="option_type === option_type_list[2].value" icon-class="circle" size="16" />
  54. </div>
  55. </div>
  56. </li>
  57. </ul>
  58. </div>
  59. </div>
  60. </template>
  61. <script>
  62. import { getJudgeData, option_type_list, isEnable } from '@/views/book/courseware/data/judge';
  63. import PreviewMixin from '../common/PreviewMixin';
  64. export default {
  65. name: 'JudgePreview',
  66. mixins: [PreviewMixin],
  67. data() {
  68. return {
  69. data: getJudgeData(),
  70. option_type_list,
  71. isEnable,
  72. };
  73. },
  74. computed: {
  75. incertitudeList() {
  76. let _option_type_list = this.data.property.option_type_list;
  77. if (isEnable(this.data.property.is_view_incertitude)) {
  78. return _option_type_list;
  79. }
  80. // 返回不包含第三个元素的新数组
  81. return [..._option_type_list.slice(0, 2), ..._option_type_list.slice(3)];
  82. },
  83. },
  84. watch: {
  85. 'answer.answer_list': {
  86. handler(val) {
  87. if (val.length === 0) {
  88. this.answer.is_right = false;
  89. return;
  90. }
  91. if (val.length !== this.data.answer.answer_list.length) {
  92. this.answer.is_right = false;
  93. return;
  94. }
  95. this.answer.is_right = val.every((item) =>
  96. this.data.answer.answer_list.some(
  97. (answerItem) => answerItem.mark === item.mark && answerItem.option_type === item.option_type,
  98. ),
  99. );
  100. },
  101. deep: true,
  102. },
  103. },
  104. methods: {
  105. // 将数字转换为小写字母
  106. convertNumberToLetter(number) {
  107. return String.fromCharCode(97 + number);
  108. },
  109. isAnswer(mark, option_type) {
  110. return this.answer.answer_list.some((li) => li.mark === mark && li.option_type === option_type);
  111. },
  112. // 选择答案
  113. selectAnswer(mark, option_type) {
  114. if (this.disabled) return;
  115. const index = this.answer.answer_list.findIndex((li) => li.mark === mark);
  116. if (index === -1) {
  117. this.answer.answer_list.push({ mark, option_type });
  118. } else {
  119. this.answer.answer_list[index].option_type = option_type;
  120. }
  121. },
  122. // 计算判断题小题题目样式
  123. computedIsJudgeRight(mark) {
  124. if (!this.isJudgingRightWrong) return '';
  125. let selectOption = this.answer.answer_list.find((item) => item.mark === mark); // 查找是否已选中的选项
  126. if (!selectOption) return 'wrong';
  127. return this.data.answer.answer_list.find((item) => item.mark === mark)?.option_type === selectOption.option_type
  128. ? 'right'
  129. : 'wrong';
  130. },
  131. // 计算是否显示正确答案的样式
  132. computedIsShowRightAnswer(mark, option_type) {
  133. if (!this.isShowRightAnswer) return '';
  134. let selectOption = this.answer.answer_list.find((item) => item.mark === mark); // 查找是否已选中的选项
  135. // 是否是正确的选项类型
  136. let isCorrectType = this.data.answer.answer_list.find((item) => item.mark === mark)?.option_type === option_type;
  137. if (!selectOption) {
  138. return isCorrectType ? 'answer-right' : '';
  139. }
  140. return isCorrectType && !(selectOption.option_type === option_type) ? 'answer-right' : '';
  141. },
  142. },
  143. };
  144. </script>
  145. <style lang="scss" scoped>
  146. @use '@/styles/mixin.scss' as *;
  147. .judge-preview {
  148. @include preview-base;
  149. .option-list {
  150. display: flex;
  151. flex-direction: column;
  152. row-gap: 16px;
  153. .option-item {
  154. display: flex;
  155. column-gap: 16px;
  156. .option-content {
  157. display: flex;
  158. flex: 1;
  159. column-gap: 8px;
  160. align-items: center;
  161. padding: 12px 24px;
  162. background-color: $content-color;
  163. border-style: solid;
  164. border-width: 1px;
  165. border-radius: 40px;
  166. &.right {
  167. background-color: $right-bc-color;
  168. }
  169. &.wrong {
  170. box-shadow: 0 0 0 1px $error-color;
  171. }
  172. .serial-number {
  173. font-size: 16pt;
  174. color: #000;
  175. }
  176. }
  177. .option-type {
  178. display: flex;
  179. column-gap: 8px;
  180. align-items: center;
  181. &-item {
  182. display: flex;
  183. align-items: center;
  184. justify-content: center;
  185. width: 48px;
  186. height: 48px;
  187. color: #000;
  188. cursor: pointer;
  189. background-color: $content-color;
  190. border-radius: 50%;
  191. &.active {
  192. color: #fff;
  193. background-color: $light-main-color;
  194. }
  195. &.answer-right {
  196. color: $right-color;
  197. border: 1px solid $right-color;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. </style>