CardDesign.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <template>
  2. <div class="card-design">
  3. <div class="design-header">
  4. <div class="design-steps">
  5. <div class="step-item" v-for="(step, index) in steps" :key="index">
  6. <i>{{ index + 1 }}</i>
  7. <span>{{ step }}</span>
  8. </div>
  9. </div>
  10. </div>
  11. <!-- actions -->
  12. <div class="design-action">
  13. <div class="design-logo">
  14. <h1>
  15. <i class="el-icon-d-arrow-left" @click="toExit" title="退出"></i>
  16. 答题卡制作
  17. </h1>
  18. </div>
  19. <div class="action-part">
  20. <div class="action-part-title"><h2>基本设置</h2></div>
  21. <div class="action-part-body">
  22. <page-prop-edit @init-page="initPageData"></page-prop-edit>
  23. </div>
  24. </div>
  25. <div class="action-part">
  26. <div class="action-part-title"><h2>试题配置</h2></div>
  27. <div class="action-part-body">
  28. <div class="type-list">
  29. <div class="type-item" v-for="item in TOPIC_LIST" :key="item.type">
  30. <el-button @click="addNewTopic(item)"
  31. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  32. >
  33. </div>
  34. <div
  35. class="type-item"
  36. v-for="item in NOT_TOPIC_LIST"
  37. :key="item.type"
  38. >
  39. <el-button @click="addNewTopic(item)"
  40. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  41. >
  42. </div>
  43. </div>
  44. <p class="tips-info">提示:点击创建试题</p>
  45. </div>
  46. </div>
  47. <div class="action-part">
  48. <div class="action-part-title"><h2>插入元素</h2></div>
  49. <div class="action-part-body">
  50. <div class="type-list">
  51. <div
  52. class="type-item"
  53. v-for="(item, index) in ELEMENT_LIST"
  54. :key="index"
  55. draggable="true"
  56. @dragstart="dragstart(item)"
  57. >
  58. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  59. </div>
  60. <p class="tips-info">提示:拖动插入元素</p>
  61. </div>
  62. <!-- Develop btns -->
  63. <!-- <card-config-prop-edit></card-config-prop-edit> -->
  64. </div>
  65. <!-- <br /><br /> -->
  66. <!-- <el-button @click="initCard">新建页面</el-button> -->
  67. </div>
  68. <!-- <div class="action-part">
  69. <div class="action-part-title"><h2>阅卷参数</h2></div>
  70. <div class="action-part-body">
  71. <el-button type="primary" @click="modifyParams"
  72. >上传阅卷参数<span class="color-danger"
  73. >({{ paperParams["pageSumScore"] || 0 }}分)</span
  74. ></el-button
  75. >
  76. </div>
  77. </div> -->
  78. </div>
  79. <div id="design-main" class="design-main">
  80. <!-- menus -->
  81. <div class="design-control">
  82. <div class="control-left tab-btns">
  83. <el-button
  84. v-for="(page, pageNo) in pages"
  85. :key="pageNo"
  86. :type="curPageNo === pageNo ? 'primary' : 'default'"
  87. @click="swithPage(pageNo)"
  88. >第{{ pageNo + 1 }}页</el-button
  89. >
  90. </div>
  91. <div class="control-right">
  92. <el-button
  93. type="success"
  94. :loading="isSubmit"
  95. :disabled="!pages.length"
  96. @click="toPreview"
  97. >预览</el-button
  98. >
  99. <!-- <el-button
  100. v-if="showSaveBtn"
  101. type="primary"
  102. :loading="isSubmit"
  103. :disabled="canSave || !pages.length"
  104. @click="toSave"
  105. >暂存</el-button
  106. > -->
  107. <el-button type="primary" :loading="isSubmit" @click="toSubmit"
  108. >提交</el-button
  109. >
  110. </div>
  111. </div>
  112. <!-- edit body -->
  113. <div class="design-body">
  114. <!-- 注意:后台要替换内容,改类名时,要注意 -->
  115. <div
  116. v-for="(page, pageNo) in pages"
  117. :key="pageNo"
  118. :id="`edit-page-box-${pageNo}`"
  119. :class="[
  120. 'page-box',
  121. `page-box-${cardConfig.pageSize}`,
  122. `page-box-${pageNo % 2}`,
  123. { 'page-box-less': pages.length <= 2 },
  124. ]"
  125. >
  126. <!-- locator -->
  127. <div class="page-locator page-locator-top">
  128. <div
  129. v-for="elem in page.locators.top"
  130. :key="elem.id"
  131. :id="elem.id"
  132. class="page-locator-item"
  133. ></div>
  134. </div>
  135. <div class="page-locator page-locator-bottom">
  136. <div
  137. v-for="elem in page.locators.bottom"
  138. :key="elem.id"
  139. :id="elem.id"
  140. class="page-locator-item"
  141. ></div>
  142. </div>
  143. <!-- inner edit area -->
  144. <div class="page-main-inner">
  145. <div
  146. :class="['page-main', `page-main-${page.columns.length}`]"
  147. :style="{ margin: `0 -${page.columnGap / 2}px` }"
  148. >
  149. <div
  150. class="page-column"
  151. v-for="(column, columnNo) in page.columns"
  152. :key="columnNo"
  153. :style="{ padding: `0 ${page.columnGap / 2}px` }"
  154. >
  155. <div
  156. class="page-column-main"
  157. :id="[`column-${pageNo}-${columnNo}`]"
  158. >
  159. <div class="page-column-body" v-if="column.elements.length">
  160. <topic-element-edit
  161. class="page-column-element"
  162. :data-h="element.h"
  163. v-for="element in column.elements"
  164. :key="element.id"
  165. :data="element"
  166. ></topic-element-edit>
  167. </div>
  168. <div class="page-column-body" v-else>
  169. <div
  170. class="page-column-forbid-area"
  171. v-if="cardConfig.showForbidArea"
  172. >
  173. <p>该区域严禁作答</p>
  174. </div>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. </div>
  180. <!-- outer edit area -->
  181. <div class="page-main-outer">
  182. <page-number
  183. type="rect"
  184. :total="pages.length"
  185. :current="pageNo + 1"
  186. ></page-number>
  187. <page-number
  188. type="text"
  189. :total="pages.length"
  190. :current="pageNo + 1"
  191. ></page-number>
  192. </div>
  193. </div>
  194. </div>
  195. </div>
  196. <!-- all topics -->
  197. <div class="topic-list">
  198. <div :class="['page-box', `page-box-${cardConfig.pageSize}`]">
  199. <div class="page-main-inner">
  200. <div
  201. :class="['page-main', `page-main-${cardConfig.columnNumber}`]"
  202. :style="{ margin: `0 -${cardConfig.columnGap / 2}px` }"
  203. >
  204. <div
  205. class="page-column"
  206. :style="{ padding: `0 ${cardConfig.columnGap / 2}px` }"
  207. >
  208. <div class="page-column-main" id="topic-column">
  209. <div class="page-column-body">
  210. <!-- card-head-sample -->
  211. <card-head-sample
  212. :data="cardHeadSampleData"
  213. id="simple-card-head"
  214. v-if="topics.length && cardHeadSampleData"
  215. ></card-head-sample>
  216. <!-- topic element -->
  217. <topic-element-preview
  218. class="page-column-element"
  219. v-for="element in topics"
  220. :key="element.id"
  221. :data="element"
  222. ></topic-element-preview>
  223. </div>
  224. </div>
  225. </div>
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. <!-- element-prop-edit -->
  231. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  232. <!-- right-click-menu -->
  233. <right-click-menu @inset-topic="insetNewTopic"></right-click-menu>
  234. <!-- paper-params -->
  235. <paper-params
  236. :pages="pages"
  237. :paper-params="paperParams"
  238. @confirm="paperParamsModified"
  239. ref="PaperParams"
  240. ></paper-params>
  241. <!-- topic select dialog -->
  242. <topic-select-dialog
  243. ref="TopicSelectDialog"
  244. :topics="topicList"
  245. @confirm="addNewTopic"
  246. ></topic-select-dialog>
  247. </div>
  248. </template>
  249. <script>
  250. import { mapState, mapMutations, mapActions } from "vuex";
  251. import {
  252. getElementModel,
  253. getCardHeadModel,
  254. ELEMENT_LIST,
  255. TOPIC_LIST,
  256. NOT_TOPIC_LIST,
  257. OTHER_ELEMENT,
  258. } from "../elementModel";
  259. import { CARD_VERSION } from "../enumerate";
  260. // import CardConfigPropEdit from "../components/CardConfigPropEdit";
  261. import TopicElementEdit from "../components/TopicElementEdit";
  262. import TopicElementPreview from "../components/TopicElementPreview";
  263. import PagePropEdit from "../components/PagePropEdit";
  264. import ElementPropEdit from "../components/ElementPropEdit";
  265. import RightClickMenu from "../components/RightClickMenu";
  266. import PageNumber from "../components/PageNumber";
  267. import PaperParams from "../components/PaperParams";
  268. import CardHeadSample from "../elements/card-head/CardHead";
  269. import TopicSelectDialog from "../components/TopicSelectDialog";
  270. import timeMixin from "../mixins/timeMixin";
  271. export default {
  272. name: "card-design",
  273. props: {
  274. content: {
  275. type: Object,
  276. default() {
  277. return {
  278. pages: [],
  279. cardConfig: {},
  280. paperParams: {},
  281. };
  282. },
  283. },
  284. showSaveBtn: {
  285. type: Boolean,
  286. default: true,
  287. },
  288. },
  289. mixins: [timeMixin],
  290. components: {
  291. // CardConfigPropEdit,
  292. TopicElementEdit,
  293. TopicElementPreview,
  294. PagePropEdit,
  295. ElementPropEdit,
  296. RightClickMenu,
  297. CardHeadSample,
  298. PageNumber,
  299. PaperParams,
  300. TopicSelectDialog,
  301. },
  302. data() {
  303. return {
  304. ELEMENT_LIST,
  305. TOPIC_LIST,
  306. NOT_TOPIC_LIST,
  307. topicList: [],
  308. steps: ["添加标题", "基本设置", "试题配置", "预览生成"],
  309. columnWidth: 0,
  310. isSubmit: false,
  311. canSave: false,
  312. };
  313. },
  314. computed: {
  315. ...mapState("card", [
  316. "cardConfig",
  317. "topics",
  318. "pages",
  319. "paperParams",
  320. "curElement",
  321. "curPage",
  322. "curPageNo",
  323. ]),
  324. cardHeadSampleData() {
  325. if (!this.cardConfig["pageSize"]) return;
  326. const data = getCardHeadModel(this.cardConfig);
  327. data.isSimple = true;
  328. return data;
  329. },
  330. },
  331. mounted() {
  332. this.initCard();
  333. this.openAutoSave();
  334. },
  335. beforeDestroy() {
  336. this.initState();
  337. this.clearSetTs();
  338. },
  339. methods: {
  340. ...mapMutations("card", [
  341. "addPage",
  342. "setCurPage",
  343. "setCurElement",
  344. "setCardConfig",
  345. "setOpenElementEditDialog",
  346. "setCurDragElement",
  347. "setPages",
  348. "setPaperParams",
  349. "setInsetTarget",
  350. "initState",
  351. ]),
  352. ...mapActions("card", [
  353. "resetTopicSeries",
  354. "removePage",
  355. "addElement",
  356. "modifyCardHead",
  357. "modifyElement",
  358. "rebuildPages",
  359. "initTopicsFromPages",
  360. "scrollToElementPage",
  361. ]),
  362. openAutoSave() {
  363. this.clearSetTs();
  364. this.addSetTime(async () => {
  365. await this.toSave();
  366. this.openAutoSave();
  367. }, 2 * 60 * 1000);
  368. },
  369. async initCard() {
  370. const { cardConfig, pages, paperParams } = this.content;
  371. this.setCardConfig(cardConfig);
  372. this.setPaperParams(paperParams);
  373. if (pages && pages.length) {
  374. this.setPages(pages);
  375. this.initTopicsFromPages();
  376. this.resetTopicSeries();
  377. this.setCurPage(0);
  378. } else {
  379. this.initPageData();
  380. }
  381. this.addWatch();
  382. this.$nextTick(() => {
  383. this.registSrollEvent();
  384. });
  385. },
  386. initPageData() {
  387. this.modifyCardHead({
  388. ...getCardHeadModel(this.cardConfig),
  389. });
  390. this.$nextTick(() => {
  391. this.rebuildPages();
  392. this.setCurPage(0);
  393. });
  394. },
  395. addNewTopic(item) {
  396. let element = getElementModel(item.type);
  397. element.w = document.getElementById("topic-column").offsetWidth;
  398. if (item.type === "FORBID_AREA") {
  399. const lastTopicElement = this.topics.findLast(
  400. (item) => !OTHER_ELEMENT.includes(item.type)
  401. );
  402. element.sign = lastTopicElement ? lastTopicElement.sign : "objective";
  403. this.addElement(element);
  404. this.setCurElement(element);
  405. this.$nextTick(() => {
  406. this.rebuildPages();
  407. this.$nextTick(() => {
  408. this.scrollToElementPage(element);
  409. });
  410. });
  411. } else {
  412. this.setCurElement(element);
  413. this.$refs.ElementPropEdit.open();
  414. // to elementPropEdit/ElementPropEdit open topic edit dialog
  415. }
  416. },
  417. insetNewTopic({ id, type }) {
  418. console.log(id, type);
  419. this.setInsetTarget({ id, type });
  420. if (type === "FILL_QUESTION") {
  421. this.topicList = this.TOPIC_LIST;
  422. } else {
  423. this.topicList = this.TOPIC_LIST.filter(
  424. (item) => item.type !== "FILL_QUESTION"
  425. );
  426. }
  427. this.$refs.TopicSelectDialog.open();
  428. },
  429. // 元件编辑
  430. dragstart(element) {
  431. this.setCurDragElement(getElementModel(element.type));
  432. },
  433. addWatch() {
  434. this.$watch("cardConfig", (val) => {
  435. const element = getCardHeadModel(val);
  436. this.modifyCardHead(element);
  437. this.$nextTick(() => {
  438. this.rebuildPages();
  439. });
  440. });
  441. },
  442. // 页面切换
  443. swithPage(pindex) {
  444. if (this.curPageNo === pindex) return;
  445. let pageTops = this.pages.map((page, pageNo) => {
  446. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  447. });
  448. pageTops = pageTops.map((item) => item - pageTops[0]);
  449. document.getElementById("design-main").scrollTop = pageTops[pindex];
  450. this.setCurPage(pindex);
  451. this.setCurElement({});
  452. },
  453. registSrollEvent() {
  454. document.getElementById("design-main").addEventListener("scroll", (e) => {
  455. e.preventDefault();
  456. e.stopPropagation();
  457. let pageTops = this.pages.map((page, pageNo) => {
  458. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  459. });
  460. const pageRangeHeight =
  461. document.getElementById(`edit-page-box-0`).offsetHeight * 0.2;
  462. pageTops = pageTops.map((item) => item - pageTops[0] - pageRangeHeight);
  463. const scrollTop = e.target.scrollTop;
  464. // console.log(pageTops, scrollTop);
  465. const targePageTop = pageTops.find((item) => scrollTop < item);
  466. const pageNo = targePageTop
  467. ? pageTops.indexOf(targePageTop) - 1
  468. : pageTops.length - 1;
  469. if (this.curPageNo === pageNo) return;
  470. this.setCurPage(pageNo);
  471. });
  472. },
  473. // paper-params
  474. modifyParams() {
  475. this.$refs.PaperParams.open();
  476. },
  477. paperParamsModified(paperParams) {
  478. this.setPaperParams(paperParams);
  479. },
  480. // save
  481. getCardData(htmlContent = "", model = "") {
  482. const data = {
  483. title: this.cardConfig.cardTitle,
  484. content: model,
  485. htmlContent,
  486. };
  487. return data;
  488. },
  489. checkElementCovered() {
  490. let elements = [];
  491. this.pages.forEach((page) => {
  492. page.columns.forEach((column) => {
  493. column.elements.forEach((element) => {
  494. if (element.isCovered) {
  495. elements.push(element.id);
  496. }
  497. });
  498. });
  499. });
  500. return elements.length;
  501. },
  502. checkCardValid() {
  503. if (!this.cardConfig.cardTitle) {
  504. this.$message.error("题卡标题不能为空!");
  505. this.setCurPageNo(0);
  506. setTimeout(() => {
  507. document.getElementById("cardTitleInput").focus();
  508. });
  509. return false;
  510. }
  511. // if (!this.cardConfig.cardDesc) {
  512. // this.$message.error("题卡描述信息不能为空!");
  513. // this.setCurPage(0);
  514. // setTimeout(() => {
  515. // document.getElementById("cardDescInput").focus();
  516. // });
  517. // return false;
  518. // }
  519. if (this.checkElementCovered()) {
  520. this.$message.error("题卡中存在被遮挡的元件,请注意调整!");
  521. return false;
  522. }
  523. return true;
  524. },
  525. getCardJson() {
  526. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  527. return new Promise((resolve) => {
  528. setTimeout(() => {
  529. const data = JSON.stringify(
  530. {
  531. version: CARD_VERSION,
  532. cardType: "STANDARD",
  533. cardConfig: this.cardConfig,
  534. paperParams: this.paperParams,
  535. pages: this.pages,
  536. },
  537. (k, v) => (k.startsWith("_") ? undefined : v)
  538. );
  539. resolve(data);
  540. }, 100);
  541. });
  542. },
  543. async toPreview() {
  544. this.$emit("on-preview", {
  545. cardConfig: this.cardConfig,
  546. pages: this.pages,
  547. paperParams: this.paperParams,
  548. });
  549. },
  550. async toSave() {
  551. const model = await this.getCardJson();
  552. const datas = this.getCardData("", model);
  553. this.$emit("on-save", datas);
  554. },
  555. toSubmit() {
  556. if (this.isSubmit) return;
  557. if (!this.checkCardValid()) return;
  558. this.$emit("on-submit", {
  559. cardConfig: this.cardConfig,
  560. pages: this.pages,
  561. paperParams: this.paperParams,
  562. });
  563. },
  564. toExit() {
  565. this.$emit("on-exit");
  566. },
  567. loading() {
  568. this.isSubmit = true;
  569. },
  570. unloading() {
  571. this.isSubmit = false;
  572. },
  573. },
  574. };
  575. </script>