create.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <main class="create">
  3. <div class="create-operate">
  4. <div class="left-operate">
  5. <el-button type="primary" @click="$emit('createNewQuestion')">创建新题</el-button>
  6. <a
  7. :class="['pre', { disabled: curIndex === 0 }]"
  8. :disabled="curIndex === 0"
  9. @click="selectExerciseItem(curIndex - 1)"
  10. >
  11. <i class="el-icon-back"></i><span>上一题</span>
  12. </a>
  13. <a
  14. :class="['next', { disabled: curIndex === indexList.length - 1 || indexList.length === 0 }]"
  15. @click="selectExerciseItem(curIndex + 1)"
  16. >
  17. <span>下一题</span><i class="el-icon-right"></i>
  18. </a>
  19. </div>
  20. <div class="save-tip">{{ curSaveDate }} 已自动保存</div>
  21. <div class="right-operate">
  22. <a class="setting" @click="setUp"><SvgIcon icon-class="setting" /><span>设置</span></a>
  23. <span class="line"></span>
  24. <a class="preview" @click="setPreview"><SvgIcon icon-class="eye" /><span>预览</span></a>
  25. <a class="delete" @click="deleteQuestion"><SvgIcon icon-class="delete" /><span>删除</span></a>
  26. </div>
  27. </div>
  28. <div class="create-content">
  29. <SelectQuestionType v-if="indexList.length > 0" :type="exerciseTypeList[indexList[curIndex].type]" />
  30. <template v-for="({ id }, i) in indexList">
  31. <KeepAlive :key="id">
  32. <component :is="curExercisePage" v-if="i === curIndex" ref="exercise" :question-id="id" />
  33. </KeepAlive>
  34. </template>
  35. </div>
  36. </main>
  37. </template>
  38. <script>
  39. import { SaveQuestion } from '@/api/exercise';
  40. import { timeFormatConversion } from '@/utils/transform';
  41. import { exerciseTypeList } from '@/views/exercise_questions/data/common';
  42. import SelectQuestionType from './common/SelectQuestionType.vue';
  43. import SelectQuestion from './exercises/SelectQuestion.vue';
  44. import JudgeQuestion from './exercises/JudgeQuestion.vue';
  45. import MatchingQuestion from './exercises/MatchingQuestion.vue';
  46. import FillQuestion from './exercises/FillQuestion.vue';
  47. import ReadAloudQuestion from './exercises/ReadAloudQuestion.vue';
  48. import ChineseQuestion from './exercises/ChineseQuestion.vue';
  49. import WriteQuestion from './exercises/WriteQuestion.vue';
  50. import DialogueQuestion from './exercises/DialogueQuestion.vue';
  51. import TalkPictureQuestion from './exercises/TalkPictureQuestion.vue';
  52. import ChooseToneQuestion from './exercises/ChooseToneQuestion.vue';
  53. import RepeatQuestion from './exercises/RepeatQuestion.vue';
  54. import ReadQuestion from './exercises/ReadQuestion.vue';
  55. import SortQuestion from './exercises/SortQuestion.vue';
  56. import ListenSelectQuestion from './exercises/ListenSelectQuestion.vue';
  57. import ListenJudgeQuestion from './exercises/ListenJudgeQuestion.vue';
  58. import ListenFillQuestion from './exercises/ListenFillQuestion.vue';
  59. import WordCardQuestion from './exercises/WordCardQuestion.vue';
  60. import AnswerQuestion from './exercises/AnswerQuestion.vue';
  61. import WritePictureQuestion from './exercises/WritePictureQuestion.vue';
  62. import ReplaceAnswerQuestion from './exercises/ReplaceAnswerQuestion.vue';
  63. import EssayQuestion from './exercises/EssayQuestion.vue';
  64. export default {
  65. name: 'CreateMain',
  66. components: {
  67. SelectQuestion,
  68. JudgeQuestion,
  69. SelectQuestionType,
  70. MatchingQuestion,
  71. FillQuestion,
  72. ReadAloudQuestion,
  73. ChineseQuestion,
  74. WriteQuestion,
  75. DialogueQuestion,
  76. TalkPictureQuestion,
  77. ChooseToneQuestion,
  78. RepeatQuestion,
  79. ReadQuestion,
  80. SortQuestion,
  81. ListenSelectQuestion,
  82. ListenJudgeQuestion,
  83. ListenFillQuestion,
  84. WordCardQuestion,
  85. AnswerQuestion,
  86. WritePictureQuestion,
  87. ReplaceAnswerQuestion,
  88. EssayQuestion,
  89. },
  90. provide() {
  91. return {
  92. curSaveDate: () => this.curSaveDate,
  93. recognition: this.recognition,
  94. };
  95. },
  96. props: {
  97. curIndex: {
  98. type: Number,
  99. required: true,
  100. },
  101. indexList: {
  102. type: Array,
  103. required: true,
  104. },
  105. },
  106. data() {
  107. return {
  108. timer: null,
  109. curSaveDate: '',
  110. exerciseTypeList,
  111. exerciseComponents: {
  112. select: SelectQuestion,
  113. judge: JudgeQuestion,
  114. matching: MatchingQuestion,
  115. fill: FillQuestion,
  116. read_aloud: ReadAloudQuestion,
  117. chinese: ChineseQuestion,
  118. write: WriteQuestion,
  119. dialogue: DialogueQuestion,
  120. talk_picture: TalkPictureQuestion,
  121. choose_tone: ChooseToneQuestion,
  122. repeat: RepeatQuestion,
  123. read: ReadQuestion,
  124. sort: SortQuestion,
  125. listen_select: ListenSelectQuestion,
  126. listen_judge: ListenJudgeQuestion,
  127. listen_fill: ListenFillQuestion,
  128. word_card: WordCardQuestion,
  129. answer_question: AnswerQuestion,
  130. write_picture: WritePictureQuestion,
  131. replace_answer: ReplaceAnswerQuestion,
  132. essay_question: EssayQuestion,
  133. },
  134. };
  135. },
  136. computed: {
  137. curExercisePage() {
  138. if (this.indexList.length === 0) return '';
  139. return this.exerciseComponents[this.indexList[this.curIndex].type];
  140. },
  141. },
  142. mounted() {
  143. this.resetSaveDate();
  144. },
  145. beforeDestroy() {
  146. this.saveQuestion();
  147. clearInterval(this.timer);
  148. },
  149. methods: {
  150. // 重置保存时间
  151. resetSaveDate() {
  152. if (this.timer) {
  153. clearInterval(this.timer);
  154. }
  155. this.timer = setInterval(() => {
  156. this.saveQuestion();
  157. }, 30000);
  158. },
  159. // 清除保存时间
  160. clearSaveDate() {
  161. clearInterval(this.timer);
  162. },
  163. /**
  164. * 选择练习
  165. * @param {Number} i 练习索引
  166. */
  167. selectExerciseItem(i) {
  168. if (this.indexList.length <= 0) return;
  169. this.$emit('selectExerciseItem', i, this.dataConversion());
  170. },
  171. /**
  172. * 设置
  173. */
  174. setUp() {
  175. this.$emit('setUp');
  176. },
  177. /**
  178. * 预览
  179. */
  180. setPreview() {
  181. this.$emit('setPreview');
  182. },
  183. /**
  184. * 删除
  185. */
  186. deleteQuestion() {
  187. this.$confirm('是否删除当前题目', '提示', {
  188. confirmButtonText: '确定',
  189. cancelButtonText: '取消',
  190. type: 'warning',
  191. })
  192. .then(() => {
  193. this.$emit('deleteQuestion');
  194. })
  195. .catch(() => {});
  196. },
  197. /**
  198. * 数据格式转换
  199. */
  200. dataConversion() {
  201. const data = this.$refs.exercise[0].data;
  202. const { select_type } = data.property;
  203. return {
  204. question_id: this.indexList[this.curIndex].id,
  205. type: data.type,
  206. additional_type: select_type || '',
  207. content: JSON.stringify(data),
  208. };
  209. },
  210. /**
  211. * 保存题目
  212. */
  213. async saveQuestion() {
  214. if (this.indexList.length <= 0) return;
  215. try {
  216. this.$bus.$emit('saveQuestion');
  217. await SaveQuestion(this.dataConversion());
  218. const curDate = timeFormatConversion(new Date());
  219. if (this.curSaveDate !== curDate) {
  220. this.curSaveDate = curDate;
  221. }
  222. } catch (err) {
  223. console.log(err);
  224. }
  225. },
  226. /**
  227. * 智能识别
  228. * @param {String} text
  229. */
  230. recognition(text) {
  231. this.$refs.exercise[0]?.recognition(text);
  232. },
  233. },
  234. };
  235. </script>
  236. <style lang="scss" scoped>
  237. .create {
  238. padding: 16px 24px;
  239. overflow: hidden;
  240. background-color: $main-background-color;
  241. &-operate {
  242. display: flex;
  243. align-items: center;
  244. justify-content: space-between;
  245. .left-operate {
  246. .pre,
  247. .next {
  248. display: inline-flex;
  249. column-gap: 8px;
  250. align-items: center;
  251. height: 32px;
  252. padding: 5px 8px;
  253. margin-left: 8px;
  254. font-size: 14px;
  255. color: #2f3742;
  256. cursor: pointer;
  257. &:hover {
  258. color: $main-color;
  259. background-color: #f4f8ff;
  260. }
  261. &.disabled {
  262. color: #999;
  263. cursor: not-allowed;
  264. &:hover {
  265. color: #999;
  266. background-color: $main-background-color;
  267. }
  268. }
  269. }
  270. }
  271. .save-tip {
  272. font-size: 12px;
  273. color: #858585;
  274. }
  275. .right-operate {
  276. display: flex;
  277. column-gap: 8px;
  278. align-items: center;
  279. %setting,
  280. .setting {
  281. display: flex;
  282. column-gap: 8px;
  283. align-items: center;
  284. padding: 4px 8px;
  285. color: #2f3742;
  286. cursor: pointer;
  287. border-radius: 2px;
  288. &:hover {
  289. background-color: $border-color;
  290. }
  291. }
  292. .preview {
  293. @extend %setting;
  294. color: #175dff;
  295. }
  296. .delete {
  297. @extend %setting;
  298. color: $danger-color;
  299. &:hover {
  300. background-color: $main-background-color;
  301. }
  302. }
  303. > span {
  304. color: #999;
  305. cursor: pointer;
  306. }
  307. }
  308. }
  309. &-content {
  310. width: 100%;
  311. height: calc(100% - 32px);
  312. margin-top: 16px;
  313. overflow: auto;
  314. }
  315. }
  316. </style>