CardDesign.vue 16 KB

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