index.vue 9.1 KB

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