index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <template>
  2. <div class="exercise">
  3. <div class="list">
  4. <el-button icon="el-icon-back" @click="back">返回练习管理</el-button>
  5. <div class="list-title">
  6. <el-input v-if="isEditExercise" v-model="exercise.name" @keyup.enter.native="UpdateExerciseName" />
  7. <span v-else class="nowrap-ellipsis">{{ exercise.name }}</span>
  8. <SvgIcon icon-class="edit" class-name="edit" @click="editExercise" />
  9. </div>
  10. <div class="exercise-list">
  11. <ul>
  12. <li v-for="(item, i) in index_list" :key="i" :class="['exercise-item', { active: i === curIndex }]">
  13. <SvgIcon icon-class="child" :size="20" />
  14. <span class="item-name nowrap-ellipsis" @click="selectExerciseItem(i)">
  15. {{ i + 1 }}. {{ exerciseNames[item.type] }}
  16. </span>
  17. <SvgIcon icon-class="copy" class="pointer" :size="10" @click="copy(i)" />
  18. </li>
  19. </ul>
  20. </div>
  21. <div class="list-operate">
  22. <!-- <el-button type="primary">批量导入</el-button> -->
  23. <el-button type="primary" @click="addQuestionToExercise('select', 'single')">新建</el-button>
  24. </div>
  25. </div>
  26. <CreateMain
  27. ref="createMain"
  28. :cur-index="curIndex"
  29. :index-list="index_list"
  30. @selectExerciseItem="selectExerciseItem"
  31. @setUp="setUp"
  32. @setPreview="setPreview"
  33. @deleteQuestion="deleteQuestion"
  34. @createNewQuestion="createNewQuestion"
  35. />
  36. <div class="preview" :style="{ minHeight: preview ? '300px' : 'auto' }">
  37. <div class="preview-header">
  38. <span class="quick-preview">快捷预览:</span>
  39. <div class="preview-right">
  40. <template v-if="preview">
  41. <span class="preview-button plain" @click="refreshPreviewData">
  42. <SvgIcon icon-class="loop" size="14" /><span>刷新</span>
  43. </span>
  44. <span class="preview-button"><SvgIcon icon-class="eye" /><span>完整预览</span></span>
  45. <span class="preview-button plain" @click="setPreview"><SvgIcon icon-class="close" />关闭预览</span>
  46. </template>
  47. <template v-else>
  48. <span class="preview-button" @click="setPreview"><SvgIcon icon-class="eye" />预览</span>
  49. </template>
  50. </div>
  51. </div>
  52. <div class="preview-content">
  53. <component :is="curPreviewPage" v-if="preview" :data="previewData" :child-preview-data="childPreviewData" />
  54. </div>
  55. </div>
  56. </div>
  57. </template>
  58. <script>
  59. import { exerciseNames } from '../data/common';
  60. import {
  61. AddQuestionToExercise,
  62. DeleteQuestion,
  63. GetExerciseQuestionIndexList,
  64. GetQuestionInfo,
  65. GetExerciseInfo,
  66. UpdateExercise,
  67. } from '@/api/exercise';
  68. import CreateMain from './components/create.vue';
  69. import SelectPreview from '@/views/exercise_questions/preview/SelectPreview.vue';
  70. import JudgePreview from '@/views/exercise_questions/preview/JudgePreview.vue';
  71. import MatchingPreview from '@/views/exercise_questions/preview/MatchingPreview.vue';
  72. import ChinesePreview from '@/views/exercise_questions/preview/ChinesePreview.vue';
  73. import WritePreview from '../preview/WritePreview.vue';
  74. import FillPreview from '../preview/FillPreview.vue';
  75. import ReadAloudPreview from '../preview/ReadAloudPreview.vue';
  76. import DialoguePreview from '../preview/DialoguePreview.vue';
  77. import TalkPictruePreview from '../preview/TalkPictruePreview.vue';
  78. import ChooseTonePreview from '../preview/ChooseTonePreview.vue';
  79. import RepeatPreview from '../preview/RepeatPreview.vue';
  80. import ReadPreview from '../preview/ReadPreview.vue';
  81. import SortPreview from '../preview/SortPreview.vue';
  82. import ListenSelectPreview from '../preview/ListenSelectPreview.vue';
  83. import ListenFillPreview from '../preview/ListenFillPreview.vue';
  84. import ListenJudgePreview from '../preview/ListenJudgePreview.vue';
  85. import WordCardPreview from '../preview/WordCardPreview.vue';
  86. export default {
  87. name: 'CreateExercise',
  88. components: {
  89. CreateMain,
  90. SelectPreview,
  91. JudgePreview,
  92. MatchingPreview,
  93. ChinesePreview,
  94. WritePreview,
  95. FillPreview,
  96. ReadAloudPreview,
  97. DialoguePreview,
  98. TalkPictruePreview,
  99. ChooseTonePreview,
  100. RepeatPreview,
  101. ReadPreview,
  102. SortPreview,
  103. ListenSelectPreview,
  104. ListenFillPreview,
  105. ListenJudgePreview,
  106. WordCardPreview,
  107. },
  108. provide() {
  109. return {
  110. isSetUp: () => this.isSetUp,
  111. updateCurQuestionType: this.updateCurQuestionType,
  112. exercise_id: this.id,
  113. refreshPreviewData: this.refreshPreviewData,
  114. };
  115. },
  116. data() {
  117. const { id } = this.$route.query;
  118. return {
  119. id, // 练习id
  120. exercise: { name: '' }, // 练习信息
  121. isEditExercise: false, // 是否编辑练习
  122. curIndex: 0, // 当前练习索引
  123. index_list: [], // 练习列表
  124. exerciseNames, // 练习名称
  125. isSetUp: false, // 设置
  126. preview: false, // 预览显示
  127. previewData: {}, // 预览数据
  128. previewComponents: {
  129. select: SelectPreview,
  130. judge: JudgePreview,
  131. matching: MatchingPreview,
  132. chinese: ChinesePreview,
  133. write: WritePreview,
  134. fill: FillPreview,
  135. read_aloud: ReadAloudPreview,
  136. dialogue: DialoguePreview,
  137. talk_picture: TalkPictruePreview,
  138. choose_tone: ChooseTonePreview,
  139. repeat: RepeatPreview,
  140. read: ReadPreview,
  141. sort: SortPreview,
  142. listen_select: ListenSelectPreview,
  143. listen_fill: ListenFillPreview,
  144. listen_judge: ListenJudgePreview,
  145. word_card: WordCardPreview,
  146. },
  147. };
  148. },
  149. computed: {
  150. curPreviewPage() {
  151. if (this.index_list.length === 0) return '';
  152. return this.previewComponents[this.index_list[this.curIndex].type];
  153. },
  154. },
  155. watch: {
  156. curIndex() {
  157. if (this.index_list.length === 0) return;
  158. this.getQuestionInfo();
  159. },
  160. },
  161. created() {
  162. this.getExerciseQuestionIndexList(true);
  163. this.getExerciseInfo();
  164. },
  165. methods: {
  166. // 返回练习管理
  167. back() {
  168. this.$router.push('/personal_question');
  169. },
  170. /**
  171. * 添加题目到练习
  172. * @param {string} type 题目类型
  173. * @param {string} additional_type 附加类型
  174. * @param {string} content 题目内容
  175. */
  176. addQuestionToExercise(type, additional_type, content = '') {
  177. AddQuestionToExercise({
  178. exercise_id: this.id,
  179. type,
  180. additional_type,
  181. content,
  182. })
  183. .then(() => {
  184. this.getExerciseQuestionIndexList();
  185. })
  186. .catch(() => {
  187. this.$message.error('添加失败');
  188. });
  189. },
  190. // 创建新题
  191. createNewQuestion() {
  192. if (this.index_list.length === 0) {
  193. this.addQuestionToExercise('select', 'single');
  194. return;
  195. }
  196. let { type, additional_type } = this.index_list[this.curIndex];
  197. this.addQuestionToExercise(type, additional_type);
  198. },
  199. /**
  200. * 获取练习题目索引列表
  201. */
  202. getExerciseQuestionIndexList(init) {
  203. GetExerciseQuestionIndexList({ exercise_id: this.id })
  204. .then(({ index_list }) => {
  205. this.index_list = index_list;
  206. if (!init) return;
  207. this.getQuestionInfo();
  208. })
  209. .catch((err) => {
  210. console.log(err);
  211. });
  212. },
  213. getExerciseInfo() {
  214. GetExerciseInfo({ exercise_id: this.id }).then(({ exercise }) => {
  215. this.exercise = exercise;
  216. });
  217. },
  218. editExercise() {
  219. if (this.isEditExercise) {
  220. return this.UpdateExerciseName();
  221. }
  222. this.isEditExercise = !this.isEditExercise;
  223. },
  224. UpdateExerciseName() {
  225. UpdateExercise(this.exercise)
  226. .then(() => {
  227. this.isEditExercise = false;
  228. })
  229. .catch(() => {
  230. this.$message.error('修改失败');
  231. });
  232. },
  233. /**
  234. * 获取题目信息
  235. */
  236. getQuestionInfo() {
  237. if (this.index_list.length === 0) return;
  238. GetQuestionInfo({ question_id: this.index_list[this.curIndex].id })
  239. .then(({ question }) => {
  240. if (!question.content) return;
  241. this.$nextTick(() => {
  242. this.$refs.createMain.$refs.exercise?.[0].setQuestion(question);
  243. this.refreshPreviewData();
  244. });
  245. })
  246. .catch(() => {});
  247. },
  248. /**
  249. * 复制练习
  250. * @param {Number} index 练习索引
  251. */
  252. copy(index) {
  253. this.$confirm('是否复制当前题目', '提示', {
  254. confirmButtonText: '确定',
  255. cancelButtonText: '取消',
  256. type: 'warning',
  257. })
  258. .then(() => {
  259. GetQuestionInfo({ question_id: this.index_list[index].id }).then(
  260. ({ question: { type, additional_type, content } }) => {
  261. this.addQuestionToExercise(type, additional_type, content);
  262. },
  263. );
  264. })
  265. .catch(() => {});
  266. },
  267. /**
  268. * 选择练习
  269. * @param {Number} index 练习索引
  270. * @param {object} data 练习数据
  271. */
  272. selectExerciseItem(index, data) {
  273. if (index < 0 || index > this.index_list.length - 1) return;
  274. this.curIndex = index;
  275. this.$refs.createMain.resetSaveDate();
  276. this.$refs.createMain.saveQuestion(data);
  277. },
  278. // 设置
  279. setUp() {
  280. this.isSetUp = !this.isSetUp;
  281. },
  282. // 刷新预览数据
  283. refreshPreviewData() {
  284. this.previewData = this.$refs.createMain.$refs.exercise?.[0].data || {};
  285. this.childPreviewData = this.$refs.createMain.$refs.exercise?.[0].childPreviewData || [];
  286. },
  287. // 预览
  288. setPreview() {
  289. this.preview = !this.preview;
  290. this.refreshPreviewData();
  291. },
  292. // 删除练习
  293. deleteQuestion() {
  294. DeleteQuestion({ question_id: this.index_list[this.curIndex].id })
  295. .then(() => {
  296. this.curIndex = Math.max(0, this.curIndex - 1);
  297. this.getExerciseQuestionIndexList();
  298. this.$message.success('删除成功');
  299. })
  300. .catch(() => {
  301. this.$message.error('删除失败');
  302. });
  303. },
  304. /**
  305. * 修改当前题目类型
  306. * @param {Array} arr
  307. */
  308. updateCurQuestionType(arr) {
  309. let type = arr[arr.length - 1];
  310. this.index_list[this.curIndex].type = type;
  311. },
  312. },
  313. };
  314. </script>
  315. <style lang="scss" scoped>
  316. .exercise {
  317. display: grid;
  318. grid-template: 1fr auto / 224px 1fr;
  319. height: 100%;
  320. .list {
  321. display: flex;
  322. flex-direction: column;
  323. grid-area: 1 / 1 / -1;
  324. row-gap: 16px;
  325. padding: 16px 8px;
  326. background-color: #fff;
  327. border-right: 1px solid $border-color;
  328. &-title {
  329. display: flex;
  330. column-gap: 8px;
  331. align-items: center;
  332. font-weight: bold;
  333. :first-child {
  334. flex: 1;
  335. }
  336. .edit {
  337. cursor: pointer;
  338. }
  339. }
  340. .exercise-list {
  341. max-height: calc(100% - 130px);
  342. overflow-y: auto;
  343. .exercise-item {
  344. display: flex;
  345. column-gap: 8px;
  346. align-items: center;
  347. padding: 4px 8px;
  348. color: #333;
  349. cursor: pointer;
  350. border-radius: 2px;
  351. &.active {
  352. color: $main-color;
  353. background-color: #f4f8ff;
  354. }
  355. .item-name {
  356. flex: 1;
  357. }
  358. }
  359. }
  360. &-operate {
  361. display: flex;
  362. > .el-button {
  363. flex: 1;
  364. }
  365. }
  366. }
  367. .preview {
  368. position: relative;
  369. max-height: 450px;
  370. padding: 16px 24px;
  371. overflow: auto;
  372. background-color: #eff1f5;
  373. border-top: 1px solid $border-color;
  374. .preview-header {
  375. display: flex;
  376. justify-content: space-between;
  377. width: 100%;
  378. .quick-preview {
  379. font-weight: bold;
  380. }
  381. .preview-right {
  382. display: flex;
  383. column-gap: 18px;
  384. align-items: center;
  385. .preview-button {
  386. display: flex;
  387. column-gap: 8px;
  388. align-items: center;
  389. color: #175dff;
  390. cursor: pointer;
  391. &.plain {
  392. color: #2f3742;
  393. }
  394. }
  395. }
  396. }
  397. &-content {
  398. width: 100%;
  399. height: calc(100% - 24px);
  400. overflow: auto;
  401. }
  402. }
  403. }
  404. </style>
  405. <style>
  406. .el-cascader-menu__wrap {
  407. height: 235px;
  408. }
  409. </style>