CardDesign.vue 16 KB

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