index.vue 10 KB

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