FillQuestion.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <QuestionBase>
  3. <template #content>
  4. <div class="stem">
  5. <el-input
  6. v-if="data.property.stem_type === stemTypeList[0].value"
  7. v-model="data.stem"
  8. rows="3"
  9. resize="none"
  10. type="textarea"
  11. placeholder="输入题干"
  12. />
  13. <RichText v-if="data.property.stem_type === stemTypeList[1].value" v-model="data.stem" placeholder="输入题干" />
  14. <UploadAudio
  15. v-show="isEnable(data.property.is_enable_listening)"
  16. :file-id="data.file_id_list?.[0]"
  17. @upload="upload"
  18. @deleteFile="deleteFile"
  19. />
  20. <el-input
  21. v-show="isEnable(data.property.is_enable_description)"
  22. v-model="data.description"
  23. rows="3"
  24. resize="none"
  25. type="textarea"
  26. placeholder="输入描述"
  27. />
  28. </div>
  29. <div class="content">
  30. <RichText
  31. ref="modelEssay"
  32. v-model="data.article"
  33. :is-fill="true"
  34. :toolbar="false"
  35. :wordlimit-num="false"
  36. placeholder="输入范文"
  37. @showContentmenu="showContentmenu"
  38. @hideContentmenu="hideContentmenu"
  39. />
  40. <div v-show="isShow" ref="contentmenu" :style="contentmenu" class="contentmenu">
  41. <SvgIcon icon-class="slice" size="16" @click="setFill" />
  42. <span class="button" @click="setFill">设为填空</span>
  43. <span class="line"></span>
  44. <SvgIcon icon-class="close-circle" size="16" @click="deleteFill" />
  45. <span class="button" @click="deleteFill">删除填空</span>
  46. </div>
  47. <el-button @click="identifyText">识别</el-button>
  48. <div v-if="data.answer.answer_list.length > 0" class="correct-answer">
  49. <div class="subtitle">正确答案</div>
  50. <el-input
  51. v-for="(item, i) in data.answer.answer_list.filter(({ type }) => type === 'any_one')"
  52. :key="item.mark"
  53. v-model="item.value"
  54. >
  55. <span slot="prefix">{{ i + 1 }}.</span>
  56. </el-input>
  57. </div>
  58. </div>
  59. </template>
  60. <template #property>
  61. <el-form :model="data.property">
  62. <el-form-item label="题干">
  63. <el-radio
  64. v-for="{ value, label } in stemTypeList"
  65. :key="value"
  66. v-model="data.property.stem_type"
  67. :label="value"
  68. >
  69. {{ label }}
  70. </el-radio>
  71. </el-form-item>
  72. <el-form-item label="题号">
  73. <el-input v-model="data.property.question_number" />
  74. </el-form-item>
  75. <el-form-item label-width="45px">
  76. <el-radio
  77. v-for="{ value, label } in questionNumberTypeList"
  78. :key="value"
  79. v-model="data.other.question_number_type"
  80. :label="value"
  81. >
  82. {{ label }}
  83. </el-radio>
  84. </el-form-item>
  85. <el-form-item label="描述">
  86. <el-radio
  87. v-for="{ value, label } in switchOption"
  88. :key="value"
  89. v-model="data.property.is_enable_description"
  90. :label="value"
  91. >
  92. {{ label }}
  93. </el-radio>
  94. </el-form-item>
  95. <el-form-item label="听力">
  96. <el-radio
  97. v-for="{ value, label } in switchOption"
  98. :key="value"
  99. v-model="data.property.is_enable_listening"
  100. :label="value"
  101. >
  102. {{ label }}
  103. </el-radio>
  104. </el-form-item>
  105. <el-form-item label="分值">
  106. <el-radio
  107. v-for="{ value, label } in scoreTypeList"
  108. :key="value"
  109. v-model="data.property.score_type"
  110. :label="value"
  111. >
  112. {{ label }}
  113. </el-radio>
  114. </el-form-item>
  115. <el-form-item label-width="45px">
  116. <el-input v-model="data.property.score" type="number" />
  117. </el-form-item>
  118. </el-form>
  119. </template>
  120. </QuestionBase>
  121. </template>
  122. <script>
  123. import UploadAudio from '../common/UploadAudio.vue';
  124. import QuestionMixin from '../common/QuestionMixin.js';
  125. import { getRandomNumber } from '@/utils';
  126. import { fillData } from '@/views/exercise_questions/data/fill';
  127. export default {
  128. name: 'FillQuestion',
  129. components: {
  130. UploadAudio,
  131. },
  132. mixins: [QuestionMixin],
  133. data() {
  134. return {
  135. isShow: false,
  136. contentmenu: {
  137. top: 0,
  138. left: 0,
  139. },
  140. data: JSON.parse(JSON.stringify(fillData)),
  141. };
  142. },
  143. created() {
  144. window.addEventListener('click', this.hideContentmenu);
  145. },
  146. beforeDestroy() {
  147. window.removeEventListener('click', this.hideContentmenu);
  148. },
  149. methods: {
  150. // 识别文本
  151. identifyText() {
  152. this.data.model_essay = [];
  153. this.data.answer.answer_list = [];
  154. this.data.article
  155. .split(/<p>(.*?)<\/p>/gi)
  156. .filter((item) => item)
  157. .forEach((item) => {
  158. if (item.charCodeAt() === 10) return;
  159. // 匹配 span 标签和三个以上的_,并将它们组成数组
  160. let str = item.replace(/<span.*?>(.*?)<\/span>|([_]{3,})/gi, '###$1$2###');
  161. this.data.model_essay.push(this.splitRichText(str));
  162. });
  163. },
  164. // 分割富文本
  165. splitRichText(str) {
  166. let _str = str;
  167. let start = 0;
  168. let index = 0;
  169. let arr = [];
  170. let matchNum = 0;
  171. while (index !== -1) {
  172. index = _str.indexOf('###', start);
  173. if (index === -1) break;
  174. matchNum += 1;
  175. arr.push({ content: _str.slice(start, index), type: 'text' });
  176. if (matchNum % 2 === 0 && arr.length > 0) {
  177. arr[arr.length - 1].type = 'input';
  178. let mark = getRandomNumber();
  179. arr[arr.length - 1].mark = mark;
  180. let content = arr[arr.length - 1].content;
  181. // 设置答案数组
  182. let isUnderline = /^_{3,}$/.test(content);
  183. this.data.answer.answer_list.push({
  184. value: isUnderline ? '' : content,
  185. mark,
  186. type: isUnderline ? 'any_one' : 'only_one',
  187. });
  188. // 将 content 设置为空,为预览准备
  189. arr[arr.length - 1].content = '';
  190. }
  191. start = index + 3;
  192. }
  193. let last = _str.slice(start);
  194. if (last) {
  195. arr.push({ content: last, type: 'text' });
  196. }
  197. return arr;
  198. },
  199. // 设置填空
  200. setFill() {
  201. this.$refs.modelEssay.setContent();
  202. this.hideContentmenu();
  203. },
  204. // 删除填空
  205. deleteFill() {
  206. this.$refs.modelEssay.deleteContent();
  207. this.hideContentmenu();
  208. },
  209. hideContentmenu() {
  210. this.isShow = false;
  211. },
  212. showContentmenu({ pixelsFromLeft, pixelsFromTop }) {
  213. this.isShow = true;
  214. this.contentmenu = {
  215. left: `${pixelsFromLeft + 14}px`,
  216. top: `${pixelsFromTop - 18}px`,
  217. };
  218. },
  219. },
  220. };
  221. </script>
  222. <style lang="scss" scoped>
  223. .content {
  224. position: relative;
  225. .el-button {
  226. margin-top: 8px;
  227. }
  228. .correct-answer {
  229. .subtitle {
  230. margin: 8px 0;
  231. font-size: 14px;
  232. color: #4e5969;
  233. }
  234. .el-input {
  235. width: 180px;
  236. :deep &__prefix {
  237. display: flex;
  238. align-items: center;
  239. color: $text-color;
  240. }
  241. + .el-input {
  242. margin-left: 8px;
  243. }
  244. }
  245. }
  246. .contentmenu {
  247. position: absolute;
  248. z-index: 999;
  249. display: flex;
  250. column-gap: 4px;
  251. align-items: center;
  252. padding: 4px 8px;
  253. font-size: 14px;
  254. background-color: #fff;
  255. border-radius: 2px;
  256. box-shadow: 0 1px 10px 0 rgba(0, 0, 0, 10%);
  257. .svg-icon,
  258. .button {
  259. cursor: pointer;
  260. }
  261. .line {
  262. min-height: 16px;
  263. margin: 0 4px;
  264. }
  265. }
  266. }
  267. </style>