SelectPreview.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="select-preview" :style="[getAreaStyle(), getComponentStyle()]">
  4. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  5. <div class="main">
  6. <ul
  7. class="option-list"
  8. :style="{ flexDirection: data.property.arrange_type === arrangeTypeList[0].value ? 'row' : 'column' }"
  9. >
  10. <li
  11. v-for="({ content, mark, multilingual, paragraph_list, rich_text_list }, i) in data.option_list"
  12. :key="mark"
  13. :style="{ cursor: disabled ? 'not-allowed' : 'pointer', borderColor: data.unified_attrib?.topic_color }"
  14. :class="['option-item', { active: isAnswer(mark) }, ...computedAnswerClass(mark)]"
  15. @click="selectAnswer(mark)"
  16. >
  17. <span :class="[isSingle ? 'radio' : 'checkbox']">
  18. <SvgIcon icon-class="check-mark" width="10" height="7" />
  19. </span>
  20. <span class="serial-number"> {{ computedOptionNumber(i) }}. </span>
  21. <PinyinText
  22. v-if="isEnable(data.property.view_pinyin)"
  23. class="content"
  24. :paragraph-list="paragraph_list"
  25. :rich-text-list="rich_text_list"
  26. :pinyin-position="data.property.pinyin_position"
  27. :is-preview="true"
  28. />
  29. <span
  30. v-else
  31. class="content rich-text"
  32. :style="{ fontSize: type === typeList[0] ? '12pt' : '' }"
  33. v-html="convertText(sanitizeHTML(content))"
  34. ></span>
  35. <div v-if="showLang" class="lang">
  36. {{ multilingual.find((item) => item.type === getLang())?.translation }}
  37. </div>
  38. </li>
  39. </ul>
  40. <PreviewOperation @showAnswerAnalysis="showAnswerAnalysis" @retry="retry" />
  41. </div>
  42. <AnswerCorrect
  43. :answer-correct="data?.answer_correct"
  44. :visible.sync="visibleAnswerCorrect"
  45. @closeAnswerCorrect="closeAnswerCorrect"
  46. />
  47. <AnswerAnalysis
  48. :visible.sync="visibleAnswerAnalysis"
  49. :answer-list="data.answer_list"
  50. :analysis-list="data.analysis_list"
  51. @closeAnswerAnalysis="closeAnswerAnalysis"
  52. >
  53. <ul
  54. slot="right-answer"
  55. class="option-list"
  56. :style="{ flexDirection: data.property.arrange_type === arrangeTypeList[0].value ? 'row' : 'column' }"
  57. >
  58. <li
  59. v-for="({ content, mark, multilingual, paragraph_list, rich_text_list }, i) in data.option_list"
  60. :key="mark"
  61. :style="{ cursor: disabled ? 'not-allowed' : 'pointer', borderColor: data.unified_attrib?.topic_color }"
  62. :class="['option-item', ...computedAnswerClass(mark, 'answer-analysis')]"
  63. @click="selectAnswer(mark)"
  64. >
  65. <span :class="[isSingle ? 'radio' : 'checkbox']">
  66. <SvgIcon icon-class="check-mark" width="10" height="7" />
  67. </span>
  68. <span class="serial-number"> {{ computedOptionNumber(i) }}. </span>
  69. <PinyinText
  70. v-if="isEnable(data.property.view_pinyin)"
  71. class="content"
  72. :paragraph-list="paragraph_list"
  73. :rich-text-list="rich_text_list"
  74. :pinyin-position="data.property.pinyin_position"
  75. :is-preview="true"
  76. />
  77. <span
  78. v-else
  79. class="content rich-text"
  80. :style="{ fontSize: type === typeList[0] ? '12pt' : '' }"
  81. v-html="convertText(sanitizeHTML(content))"
  82. ></span>
  83. <div v-if="showLang" class="lang">
  84. {{ multilingual.find((item) => item.type === getLang())?.translation }}
  85. </div>
  86. </li>
  87. </ul>
  88. </AnswerAnalysis>
  89. </div>
  90. </template>
  91. <script>
  92. import PreviewMixin from '../common/PreviewMixin';
  93. import { getSelectData, arrangeTypeList, selectTypeList } from '@/views/book/courseware/data/select';
  94. import { serialNumberTypeList, computeOptionMethods } from '@/views/book/courseware/data/common';
  95. export default {
  96. name: 'SelectPreview',
  97. mixins: [PreviewMixin],
  98. data() {
  99. return {
  100. data: getSelectData(),
  101. arrangeTypeList,
  102. };
  103. },
  104. computed: {
  105. isSingle() {
  106. return this.data.property?.select_type === selectTypeList[0].value;
  107. },
  108. },
  109. watch: {
  110. 'answer.answer_list': {
  111. handler(val) {
  112. if (val.length === 0) {
  113. this.answer.is_right = false;
  114. return;
  115. }
  116. if (val.length !== this.data.answer.answer_list.length) {
  117. this.answer.is_right = false;
  118. return;
  119. }
  120. this.answer.is_right = val.every((item) => this.data.answer.answer_list.includes(item));
  121. },
  122. deep: true,
  123. },
  124. },
  125. methods: {
  126. computedOptionNumber(number) {
  127. let type = serialNumberTypeList.find((item) => item.value === this.data.property.option_serial_type)?.value;
  128. if (!type) return number + 1;
  129. return computeOptionMethods[type](number);
  130. },
  131. /**
  132. * 判断选项是否被选中
  133. * @param {string} mark - 选项的标识
  134. */
  135. isAnswer(mark) {
  136. return this.answer.answer_list.includes(mark);
  137. },
  138. selectAnswer(mark) {
  139. if (this.disabled) return;
  140. const list = this.answer.answer_list || [];
  141. const isSelected = list.includes(mark);
  142. if (this.isSingle) {
  143. this.answer.answer_list = isSelected ? [] : [mark];
  144. return;
  145. }
  146. this.answer.answer_list = isSelected ? list.filter((item) => item !== mark) : list.concat(mark);
  147. },
  148. /**
  149. * 计算选项的类名
  150. * @param {string} mark - 选项的标识
  151. * @param {string} type - 计算类名的类型,默认为 'default',可选值为 'answer-analysis'
  152. * @returns {Array} - 选项的类名数组
  153. */
  154. computedAnswerClass(mark, type = 'default') {
  155. if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
  156. return [];
  157. }
  158. let isHas = this.answer.answer_list.includes(mark); // 用户是否已选中
  159. let isRight = this.data.answer.answer_list.includes(mark); // 是否是正确答案
  160. let answerClass = [];
  161. if (this.isShowRightAnswer && type === 'answer-analysis') {
  162. answerClass = isRight ? ['answer-right'] : [];
  163. }
  164. // 判断是否是正确答案
  165. if (isHas && this.isJudgingRightWrong && type !== 'answer-analysis') {
  166. answerClass = isRight ? ['answer-right'] : ['wrong'];
  167. }
  168. return answerClass;
  169. },
  170. retry() {
  171. this.answer.answer_list = [];
  172. },
  173. },
  174. };
  175. </script>
  176. <style lang="scss" scoped>
  177. @use '@/styles/mixin.scss' as *;
  178. .select-preview {
  179. @include preview-base;
  180. .option-list {
  181. display: flex;
  182. flex-flow: column wrap;
  183. gap: 16px;
  184. .option-item {
  185. display: flex;
  186. flex: 1;
  187. flex-wrap: wrap;
  188. column-gap: 8px;
  189. align-items: center;
  190. padding: 12px 24px;
  191. color: #706f78;
  192. cursor: pointer;
  193. background-color: $content-color;
  194. border: 1px solid $content-color;
  195. border-radius: 4px;
  196. .lang {
  197. // width: 100%;
  198. padding-left: 52px;
  199. }
  200. .radio,
  201. .checkbox {
  202. display: flex;
  203. align-items: center;
  204. justify-content: center;
  205. width: 14px;
  206. height: 14px;
  207. margin-right: 8px;
  208. cursor: pointer;
  209. background-color: #fff;
  210. border: 1px solid #dcdfe6;
  211. border-radius: 2px;
  212. .svg-icon {
  213. display: none;
  214. }
  215. }
  216. .radio {
  217. border-radius: 50%;
  218. }
  219. .serial-number {
  220. font-size: 16pt;
  221. }
  222. .content {
  223. flex: 1;
  224. }
  225. &.active {
  226. color: #34343a;
  227. background-color: $main-active-color;
  228. .checkbox {
  229. color: #fff;
  230. background-color: $light-main-color;
  231. .svg-icon {
  232. display: block;
  233. }
  234. }
  235. .radio {
  236. background-color: #fff;
  237. box-shadow: inset 0 0 0 4px $light-main-color;
  238. }
  239. }
  240. &.answer-right {
  241. background-color: #e8f7f2;
  242. &::after {
  243. font-size: 14px;
  244. color: $right-color;
  245. white-space: nowrap;
  246. content: '正确答案';
  247. }
  248. }
  249. &.wrong {
  250. margin: 1px 0;
  251. background-color: $content-color;
  252. box-shadow: 0 0 0 1px $error-color;
  253. &::after {
  254. font-size: 14px;
  255. color: #a09fa6;
  256. white-space: nowrap;
  257. content: '已选';
  258. }
  259. }
  260. }
  261. }
  262. }
  263. </style>