CardDesign.vue 17 KB

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