CardDesign.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 class="mt-2"></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. <elem-undertaking
  193. v-if="cardConfig.undertakingEnable && pageNo % 2 === 1"
  194. :content="cardConfig.undertakingBody"
  195. ></elem-undertaking>
  196. </div>
  197. </div>
  198. </div>
  199. </div>
  200. <!-- all topics -->
  201. <div class="topic-list">
  202. <div :class="['page-box', `page-box-${cardConfig.pageSize}`]">
  203. <div class="page-main-inner">
  204. <div
  205. :class="['page-main', `page-main-${cardConfig.columnNumber}`]"
  206. :style="{ margin: `0 -${cardConfig.columnGap / 2}px` }"
  207. >
  208. <div
  209. class="page-column"
  210. :style="{ padding: `0 ${cardConfig.columnGap / 2}px` }"
  211. >
  212. <div class="page-column-main" id="topic-column">
  213. <div class="page-column-body">
  214. <!-- card-head-sample -->
  215. <card-head-sample
  216. :data="cardHeadSampleData"
  217. id="simple-card-head"
  218. v-if="topics.length && cardHeadSampleData"
  219. ></card-head-sample>
  220. <!-- topic element -->
  221. <topic-element-preview
  222. class="page-column-element"
  223. v-for="element in topics"
  224. :key="element.id"
  225. :data="element"
  226. ></topic-element-preview>
  227. </div>
  228. </div>
  229. </div>
  230. </div>
  231. </div>
  232. </div>
  233. </div>
  234. <!-- element-prop-edit -->
  235. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  236. <!-- right-click-menu -->
  237. <right-click-menu @inset-topic="insetNewTopic"></right-click-menu>
  238. <!-- paper-params -->
  239. <paper-params
  240. :pages="pages"
  241. :paper-params="paperParams"
  242. @confirm="paperParamsModified"
  243. ref="PaperParams"
  244. ></paper-params>
  245. <!-- topic select dialog -->
  246. <topic-select-dialog
  247. ref="TopicSelectDialog"
  248. :topics="topicList"
  249. @confirm="addNewTopic"
  250. ></topic-select-dialog>
  251. </div>
  252. </template>
  253. <script>
  254. import { mapState, mapMutations, mapActions } from "vuex";
  255. import {
  256. getElementModel,
  257. getCardHeadModel,
  258. ELEMENT_LIST,
  259. TOPIC_LIST,
  260. NOT_TOPIC_LIST,
  261. OTHER_ELEMENT,
  262. } from "../elementModel";
  263. import { CARD_VERSION } from "../enumerate";
  264. import CardConfigPropEdit from "../components/CardConfigPropEdit";
  265. import TopicElementEdit from "../components/TopicElementEdit";
  266. import TopicElementPreview from "../components/TopicElementPreview";
  267. import PagePropEdit from "../components/PagePropEdit";
  268. import ElementPropEdit from "../components/ElementPropEdit";
  269. import RightClickMenu from "../components/RightClickMenu";
  270. import PageNumber from "../components/PageNumber";
  271. import PaperParams from "../components/PaperParams";
  272. import CardHeadSample from "../elements/card-head/CardHead";
  273. import TopicSelectDialog from "../components/TopicSelectDialog";
  274. import ElemUndertaking from "../elements/undertaking/ElemUndertaking.vue";
  275. import timeMixin from "../mixins/timeMixin";
  276. export default {
  277. name: "card-design",
  278. props: {
  279. content: {
  280. type: Object,
  281. default() {
  282. return {
  283. pages: [],
  284. cardConfig: {},
  285. paperParams: {},
  286. };
  287. },
  288. },
  289. showSaveBtn: {
  290. type: Boolean,
  291. default: true,
  292. },
  293. },
  294. mixins: [timeMixin],
  295. components: {
  296. CardConfigPropEdit,
  297. TopicElementEdit,
  298. TopicElementPreview,
  299. PagePropEdit,
  300. ElementPropEdit,
  301. RightClickMenu,
  302. CardHeadSample,
  303. PageNumber,
  304. PaperParams,
  305. TopicSelectDialog,
  306. ElemUndertaking,
  307. },
  308. data() {
  309. return {
  310. ELEMENT_LIST,
  311. TOPIC_LIST,
  312. NOT_TOPIC_LIST,
  313. topicList: [],
  314. steps: ["添加标题", "基本设置", "试题配置", "预览生成"],
  315. columnWidth: 0,
  316. isSubmit: false,
  317. canSave: false,
  318. };
  319. },
  320. computed: {
  321. ...mapState("card", [
  322. "cardConfig",
  323. "topics",
  324. "pages",
  325. "paperParams",
  326. "curElement",
  327. "curPage",
  328. "curPageNo",
  329. ]),
  330. cardHeadSampleData() {
  331. if (!this.cardConfig["pageSize"]) return;
  332. const data = getCardHeadModel(this.cardConfig);
  333. data.isSimple = true;
  334. return data;
  335. },
  336. },
  337. mounted() {
  338. this.initCard();
  339. // this.openAutoSave();
  340. },
  341. beforeDestroy() {
  342. this.initState();
  343. this.clearSetTs();
  344. },
  345. methods: {
  346. ...mapMutations("card", [
  347. "addPage",
  348. "setCurPage",
  349. "setCurElement",
  350. "setCardConfig",
  351. "setOpenElementEditDialog",
  352. "setCurDragElement",
  353. "setPages",
  354. "setPaperParams",
  355. "setInsetTarget",
  356. "initState",
  357. ]),
  358. ...mapActions("card", [
  359. "resetTopicSeries",
  360. "removePage",
  361. "addElement",
  362. "modifyCardHead",
  363. "modifyElement",
  364. "rebuildPages",
  365. "initTopicsFromPages",
  366. "scrollToElementPage",
  367. ]),
  368. openAutoSave() {
  369. this.clearSetTs();
  370. this.addSetTime(async () => {
  371. await this.toSave();
  372. this.openAutoSave();
  373. }, 2 * 60 * 1000);
  374. },
  375. async initCard() {
  376. const { cardConfig, pages, paperParams } = this.content;
  377. this.setCardConfig(cardConfig);
  378. this.setPaperParams(paperParams);
  379. if (pages && pages.length) {
  380. this.setPages(pages);
  381. this.initTopicsFromPages();
  382. this.resetTopicSeries();
  383. this.setCurPage(0);
  384. } else {
  385. this.initPageData();
  386. }
  387. this.addWatch();
  388. this.$nextTick(() => {
  389. this.registSrollEvent();
  390. });
  391. },
  392. initPageData() {
  393. this.modifyCardHead({
  394. ...getCardHeadModel(this.cardConfig),
  395. });
  396. this.$nextTick(() => {
  397. this.rebuildPages();
  398. this.setCurPage(0);
  399. });
  400. },
  401. addNewTopic(item) {
  402. let element = getElementModel(item.type);
  403. element.w = document.getElementById("topic-column").offsetWidth;
  404. if (item.type === "FORBID_AREA") {
  405. const lastTopicElement = this.topics.findLast(
  406. (item) => !OTHER_ELEMENT.includes(item.type)
  407. );
  408. element.sign = lastTopicElement ? lastTopicElement.sign : "objective";
  409. this.addElement(element);
  410. this.setCurElement(element);
  411. this.$nextTick(() => {
  412. this.rebuildPages();
  413. this.$nextTick(() => {
  414. this.scrollToElementPage(element);
  415. });
  416. });
  417. } else {
  418. this.setCurElement(element);
  419. this.$refs.ElementPropEdit.open();
  420. // to elementPropEdit/ElementPropEdit open topic edit dialog
  421. }
  422. },
  423. insetNewTopic({ id, type }) {
  424. console.log(id, type);
  425. this.setInsetTarget({ id, type });
  426. if (type === "FILL_QUESTION") {
  427. this.topicList = this.TOPIC_LIST;
  428. } else {
  429. this.topicList = this.TOPIC_LIST.filter(
  430. (item) => item.type !== "FILL_QUESTION"
  431. );
  432. }
  433. this.$refs.TopicSelectDialog.open();
  434. },
  435. // 元件编辑
  436. dragstart(element) {
  437. this.setCurDragElement(getElementModel(element.type));
  438. },
  439. addWatch() {
  440. this.$watch("cardConfig", (val) => {
  441. const element = getCardHeadModel(val);
  442. this.modifyCardHead(element);
  443. this.$nextTick(() => {
  444. this.rebuildPages();
  445. });
  446. });
  447. },
  448. // 页面切换
  449. swithPage(pindex) {
  450. if (this.curPageNo === pindex) return;
  451. let pageTops = this.pages.map((page, pageNo) => {
  452. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  453. });
  454. pageTops = pageTops.map((item) => item - pageTops[0]);
  455. document.getElementById("design-main").scrollTop = pageTops[pindex];
  456. this.setCurPage(pindex);
  457. this.setCurElement({});
  458. },
  459. registSrollEvent() {
  460. document.getElementById("design-main").addEventListener("scroll", (e) => {
  461. e.preventDefault();
  462. e.stopPropagation();
  463. let pageTops = this.pages.map((page, pageNo) => {
  464. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  465. });
  466. const pageRangeHeight =
  467. document.getElementById(`edit-page-box-0`).offsetHeight * 0.2;
  468. pageTops = pageTops.map((item) => item - pageTops[0] - pageRangeHeight);
  469. const scrollTop = e.target.scrollTop;
  470. // console.log(pageTops, scrollTop);
  471. const targePageTop = pageTops.find((item) => scrollTop < item);
  472. const pageNo = targePageTop
  473. ? pageTops.indexOf(targePageTop) - 1
  474. : pageTops.length - 1;
  475. if (this.curPageNo === pageNo) return;
  476. this.setCurPage(pageNo);
  477. });
  478. },
  479. // paper-params
  480. modifyParams() {
  481. this.$refs.PaperParams.open();
  482. },
  483. paperParamsModified(paperParams) {
  484. this.setPaperParams(paperParams);
  485. },
  486. // save
  487. getCardData(htmlContent = "", model = "") {
  488. const data = {
  489. title: this.cardConfig.cardTitle,
  490. content: model,
  491. htmlContent,
  492. };
  493. return data;
  494. },
  495. checkElementCovered() {
  496. let elements = [];
  497. this.pages.forEach((page) => {
  498. page.columns.forEach((column) => {
  499. column.elements.forEach((element) => {
  500. if (element.isCovered) {
  501. elements.push(element.id);
  502. }
  503. });
  504. });
  505. });
  506. return elements.length;
  507. },
  508. checkCardValid() {
  509. if (!this.cardConfig.cardTitle) {
  510. this.$message.error("题卡标题不能为空!");
  511. this.setCurPageNo(0);
  512. setTimeout(() => {
  513. document.getElementById("cardTitleInput").focus();
  514. });
  515. return false;
  516. }
  517. // if (!this.cardConfig.cardDesc) {
  518. // this.$message.error("题卡描述信息不能为空!");
  519. // this.setCurPage(0);
  520. // setTimeout(() => {
  521. // document.getElementById("cardDescInput").focus();
  522. // });
  523. // return false;
  524. // }
  525. if (this.checkElementCovered()) {
  526. this.$message.error("题卡中存在被遮挡的元件,请注意调整!");
  527. return false;
  528. }
  529. return true;
  530. },
  531. getCardJson() {
  532. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  533. return new Promise((resolve) => {
  534. setTimeout(() => {
  535. const data = JSON.stringify(
  536. {
  537. version: CARD_VERSION,
  538. cardType: "STANDARD",
  539. cardConfig: this.cardConfig,
  540. paperParams: this.paperParams,
  541. pages: this.pages,
  542. },
  543. (k, v) => (k.startsWith("_") ? undefined : v)
  544. );
  545. resolve(data);
  546. }, 100);
  547. });
  548. },
  549. async toPreview() {
  550. this.$emit("on-preview", {
  551. cardConfig: this.cardConfig,
  552. pages: this.pages,
  553. paperParams: this.paperParams,
  554. });
  555. },
  556. async toSave() {
  557. const model = await this.getCardJson();
  558. const datas = this.getCardData("", model);
  559. this.$emit("on-save", datas);
  560. },
  561. toSubmit() {
  562. if (this.isSubmit) return;
  563. if (!this.checkCardValid()) return;
  564. this.$emit("on-submit", {
  565. cardConfig: this.cardConfig,
  566. pages: this.pages,
  567. paperParams: this.paperParams,
  568. });
  569. },
  570. toExit() {
  571. this.$emit("on-exit");
  572. },
  573. loading() {
  574. this.isSubmit = true;
  575. },
  576. unloading() {
  577. this.isSubmit = false;
  578. },
  579. },
  580. };
  581. </script>