index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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" :style="{ minHeight: preview ? '300px' : 'auto' }">
  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 FillPreview from '../preview/FillPreview.vue';
  64. import ReadAloudPreview from '../preview/ReadAloudPreview.vue';
  65. import DialoguePreview from '../preview/DialoguePreview.vue';
  66. import TalkPictruePreview from '../preview/TalkPictruePreview.vue';
  67. import ChooseTonePreview from '../preview/ChooseTonePreview.vue';
  68. import RepeatPreview from '../preview/RepeatPreview.vue';
  69. import ReadPreview from '../preview/ReadPreview.vue';
  70. export default {
  71. name: 'CreateExercise',
  72. components: {
  73. CreateMain,
  74. SelectPreview,
  75. JudgePreview,
  76. MatchingPreview,
  77. ChinesePreview,
  78. WritePreview,
  79. FillPreview,
  80. ReadAloudPreview,
  81. DialoguePreview,
  82. TalkPictruePreview,
  83. ChooseTonePreview,
  84. RepeatPreview,
  85. ReadPreview,
  86. },
  87. provide() {
  88. return {
  89. isSetUp: () => this.isSetUp,
  90. updateCurQuestionType: this.updateCurQuestionType,
  91. };
  92. },
  93. data() {
  94. const { id } = this.$route.query;
  95. return {
  96. id, // 练习id
  97. curIndex: 0, // 当前练习索引
  98. index_list: [], // 练习列表
  99. exerciseNames, // 练习名称
  100. isSetUp: false, // 设置
  101. preview: false, // 预览显示
  102. previewData: {}, // 预览数据
  103. previewComponents: {
  104. select: SelectPreview,
  105. judge: JudgePreview,
  106. matching: MatchingPreview,
  107. chinese: ChinesePreview,
  108. write: WritePreview,
  109. fill: FillPreview,
  110. read_aloud: ReadAloudPreview,
  111. dialogue: DialoguePreview,
  112. talk_picture: TalkPictruePreview,
  113. choose_tone: ChooseTonePreview,
  114. repeat: RepeatPreview,
  115. read: ReadPreview,
  116. },
  117. };
  118. },
  119. computed: {
  120. curPreviewPage() {
  121. if (this.index_list.length === 0) return '';
  122. return this.previewComponents[this.index_list[this.curIndex].type];
  123. },
  124. },
  125. watch: {
  126. curIndex() {
  127. if (this.index_list.length === 0) return;
  128. this.getQuestionInfo();
  129. },
  130. },
  131. created() {
  132. this.getExerciseQuestionIndexList(true);
  133. },
  134. methods: {
  135. // 返回练习管理
  136. back() {
  137. this.$router.push('/personal_question');
  138. },
  139. /**
  140. * 添加题目到练习
  141. * @param {string} type 题目类型
  142. * @param {string} additional_type 附加类型
  143. * @param {string} content 题目内容
  144. */
  145. addQuestionToExercise(type, additional_type, content = '') {
  146. AddQuestionToExercise({
  147. exercise_id: this.id,
  148. type,
  149. additional_type,
  150. content,
  151. })
  152. .then(() => {
  153. this.getExerciseQuestionIndexList();
  154. })
  155. .catch(() => {
  156. this.$message.error('添加失败');
  157. });
  158. },
  159. // 创建新题
  160. createNewQuestion() {
  161. if (this.index_list.length === 0) {
  162. this.addQuestionToExercise('select', 'single');
  163. return;
  164. }
  165. let { type, additional_type } = this.index_list[this.curIndex];
  166. this.addQuestionToExercise(type, additional_type);
  167. },
  168. /**
  169. * 获取练习题目索引列表
  170. */
  171. getExerciseQuestionIndexList(init) {
  172. GetExerciseQuestionIndexList({ exercise_id: this.id })
  173. .then(({ index_list }) => {
  174. this.index_list = index_list;
  175. if (!init) return;
  176. this.getQuestionInfo();
  177. })
  178. .catch((err) => {
  179. console.log(err);
  180. });
  181. },
  182. /**
  183. * 获取题目信息
  184. */
  185. getQuestionInfo() {
  186. if (this.index_list.length === 0) return;
  187. GetQuestionInfo({ question_id: this.index_list[this.curIndex].id })
  188. .then(({ question }) => {
  189. if (question.content) {
  190. this.$nextTick(() => {
  191. this.$refs.createMain.$refs.exercise?.[0].setQuestion(question);
  192. this.refreshPreviewData();
  193. });
  194. }
  195. })
  196. .catch(() => {});
  197. },
  198. /**
  199. * 复制练习
  200. * @param {Number} index 练习索引
  201. */
  202. copy(index) {
  203. this.$confirm('是否复制当前题目', '提示', {
  204. confirmButtonText: '确定',
  205. cancelButtonText: '取消',
  206. type: 'warning',
  207. })
  208. .then(() => {
  209. GetQuestionInfo({ question_id: this.index_list[index].id }).then(
  210. ({ question: { type, additional_type, content } }) => {
  211. this.addQuestionToExercise(type, additional_type, content);
  212. },
  213. );
  214. })
  215. .catch(() => {});
  216. },
  217. /**
  218. * 选择练习
  219. * @param {Number} index 练习索引
  220. * @param {object} data 练习数据
  221. */
  222. selectExerciseItem(index, data) {
  223. if (index < 0 || index > this.index_list.length - 1) return;
  224. this.curIndex = index;
  225. this.$refs.createMain.resetSaveDate();
  226. this.$refs.createMain.saveQuestion(data);
  227. },
  228. // 设置
  229. setUp() {
  230. this.isSetUp = !this.isSetUp;
  231. },
  232. // 刷新预览数据
  233. refreshPreviewData() {
  234. this.previewData = this.$refs.createMain.$refs.exercise?.[0].data || {};
  235. },
  236. // 预览
  237. setPreview() {
  238. this.preview = !this.preview;
  239. this.refreshPreviewData();
  240. },
  241. // 删除练习
  242. deleteQuestion() {
  243. DeleteQuestion({ question_id: this.index_list[this.curIndex].id })
  244. .then(() => {
  245. this.curIndex = Math.max(0, this.curIndex - 1);
  246. this.getExerciseQuestionIndexList();
  247. this.$message.success('删除成功');
  248. })
  249. .catch(() => {
  250. this.$message.error('删除失败');
  251. });
  252. },
  253. /**
  254. * 修改当前题目类型
  255. * @param {Array} arr
  256. */
  257. updateCurQuestionType(arr) {
  258. let type = arr[arr.length - 1];
  259. this.index_list[this.curIndex].type = type;
  260. },
  261. },
  262. };
  263. </script>
  264. <style lang="scss" scoped>
  265. .exercise {
  266. display: grid;
  267. grid-template: 1fr auto / 224px 1fr;
  268. height: 100%;
  269. .list {
  270. display: flex;
  271. flex-direction: column;
  272. grid-area: 1 / 1 / -1;
  273. row-gap: 16px;
  274. padding: 16px 8px;
  275. background-color: #fff;
  276. border-right: 1px solid $border-color;
  277. &-title {
  278. font-weight: bold;
  279. }
  280. .exercise-list {
  281. max-height: calc(100% - 130px);
  282. overflow-y: auto;
  283. .exercise-item {
  284. display: flex;
  285. column-gap: 8px;
  286. align-items: center;
  287. padding: 4px 8px;
  288. color: #333;
  289. cursor: pointer;
  290. border-radius: 2px;
  291. &.active {
  292. color: $main-color;
  293. background-color: #f4f8ff;
  294. }
  295. .item-name {
  296. flex: 1;
  297. }
  298. }
  299. }
  300. &-operate {
  301. display: flex;
  302. > .el-button {
  303. width: 50%;
  304. }
  305. }
  306. }
  307. .preview {
  308. position: relative;
  309. max-height: 450px;
  310. padding: 16px 24px;
  311. overflow: auto;
  312. background-color: #eff1f5;
  313. border-top: 1px solid $border-color;
  314. .preview-header {
  315. display: flex;
  316. justify-content: space-between;
  317. width: 100%;
  318. .quick-preview {
  319. font-weight: bold;
  320. }
  321. .preview-right {
  322. display: flex;
  323. column-gap: 18px;
  324. align-items: center;
  325. .preview-button {
  326. display: flex;
  327. column-gap: 8px;
  328. align-items: center;
  329. color: #175dff;
  330. cursor: pointer;
  331. &.plain {
  332. color: #2f3742;
  333. }
  334. }
  335. }
  336. }
  337. &-content {
  338. width: 100%;
  339. height: calc(100% - 24px);
  340. overflow: auto;
  341. }
  342. }
  343. }
  344. </style>
  345. <style>
  346. .el-cascader-menu__wrap {
  347. height: 235px;
  348. }
  349. </style>