create.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. * 选择练习
  161. * @param {Number} i 练习索引
  162. */
  163. selectExerciseItem(i) {
  164. if (this.indexList.length <= 0) return;
  165. this.$emit('selectExerciseItem', i, this.dataConversion());
  166. },
  167. /**
  168. * 设置
  169. */
  170. setUp() {
  171. this.$emit('setUp');
  172. },
  173. /**
  174. * 预览
  175. */
  176. setPreview() {
  177. this.$emit('setPreview');
  178. },
  179. /**
  180. * 删除
  181. */
  182. deleteQuestion() {
  183. this.$confirm('是否删除当前题目', '提示', {
  184. confirmButtonText: '确定',
  185. cancelButtonText: '取消',
  186. type: 'warning',
  187. })
  188. .then(() => {
  189. this.$emit('deleteQuestion');
  190. })
  191. .catch(() => {});
  192. },
  193. /**
  194. * 数据格式转换
  195. */
  196. dataConversion() {
  197. const data = this.$refs.exercise[0].data;
  198. const { select_type } = data.property;
  199. return {
  200. question_id: this.indexList[this.curIndex].id,
  201. type: data.type,
  202. additional_type: select_type || '',
  203. content: JSON.stringify(data),
  204. };
  205. },
  206. /**
  207. * 保存题目
  208. */
  209. async saveQuestion() {
  210. if (this.indexList.length <= 0) return;
  211. try {
  212. this.$bus.$emit('saveQuestion');
  213. await SaveQuestion(this.dataConversion());
  214. const curDate = timeFormatConversion(new Date());
  215. if (this.curSaveDate !== curDate) {
  216. this.curSaveDate = curDate;
  217. }
  218. } catch (err) {
  219. console.log(err);
  220. }
  221. },
  222. /**
  223. * 智能识别
  224. * @param {String} text
  225. */
  226. recognition(text) {
  227. this.$refs.exercise[0]?.recognition(text);
  228. },
  229. },
  230. };
  231. </script>
  232. <style lang="scss" scoped>
  233. .create {
  234. padding: 16px 24px;
  235. overflow: hidden;
  236. background-color: $main-background-color;
  237. &-operate {
  238. display: flex;
  239. align-items: center;
  240. justify-content: space-between;
  241. .left-operate {
  242. .pre,
  243. .next {
  244. display: inline-flex;
  245. column-gap: 8px;
  246. align-items: center;
  247. height: 32px;
  248. padding: 5px 8px;
  249. margin-left: 8px;
  250. font-size: 14px;
  251. color: #2f3742;
  252. cursor: pointer;
  253. &:hover {
  254. color: $main-color;
  255. background-color: #f4f8ff;
  256. }
  257. &.disabled {
  258. color: #999;
  259. cursor: not-allowed;
  260. &:hover {
  261. color: #999;
  262. background-color: $main-background-color;
  263. }
  264. }
  265. }
  266. }
  267. .save-tip {
  268. font-size: 12px;
  269. color: #858585;
  270. }
  271. .right-operate {
  272. display: flex;
  273. column-gap: 8px;
  274. align-items: center;
  275. %setting,
  276. .setting {
  277. display: flex;
  278. column-gap: 8px;
  279. align-items: center;
  280. padding: 4px 8px;
  281. color: #2f3742;
  282. cursor: pointer;
  283. border-radius: 2px;
  284. &:hover {
  285. background-color: $border-color;
  286. }
  287. }
  288. .preview {
  289. @extend %setting;
  290. color: #175dff;
  291. }
  292. .delete {
  293. @extend %setting;
  294. color: $danger-color;
  295. &:hover {
  296. background-color: $main-background-color;
  297. }
  298. }
  299. > span {
  300. color: #999;
  301. cursor: pointer;
  302. }
  303. }
  304. }
  305. &-content {
  306. width: 100%;
  307. height: calc(100% - 32px);
  308. margin-top: 16px;
  309. overflow: auto;
  310. }
  311. }
  312. </style>