CardDesign.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 % 2 === 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. @confirm="addNewTopic"
  235. ></topic-select-dialog>
  236. </div>
  237. </template>
  238. <script>
  239. import { mapState, mapMutations, mapActions } from "vuex";
  240. import {
  241. getElementModel,
  242. getCardHeadModel,
  243. ELEMENT_LIST,
  244. TOPIC_LIST,
  245. NOT_TOPIC_LIST,
  246. OTHER_ELEMENT,
  247. } from "../elementModel";
  248. import { CARD_VERSION } from "../enumerate";
  249. import CardConfigPropEdit from "../components/CardConfigPropEdit";
  250. import TopicElementEdit from "../components/TopicElementEdit";
  251. import TopicElementPreview from "../components/TopicElementPreview";
  252. import PagePropEdit from "../components/PagePropEdit";
  253. import ElementPropEdit from "../components/ElementPropEdit";
  254. import RightClickMenu from "../components/RightClickMenu";
  255. import PageNumber from "../components/PageNumber";
  256. import CardHeadSample from "../elements/card-head/CardHead";
  257. import TopicSelectDialog from "../components/TopicSelectDialog";
  258. import ElemUndertaking from "../elements/undertaking/ElemUndertaking.vue";
  259. export default {
  260. name: "card-design",
  261. props: {
  262. content: {
  263. type: Object,
  264. default() {
  265. return {
  266. pages: [],
  267. cardConfig: {},
  268. };
  269. },
  270. },
  271. showSaveBtn: {
  272. type: Boolean,
  273. default: true,
  274. },
  275. },
  276. components: {
  277. CardConfigPropEdit,
  278. TopicElementEdit,
  279. TopicElementPreview,
  280. PagePropEdit,
  281. ElementPropEdit,
  282. RightClickMenu,
  283. CardHeadSample,
  284. PageNumber,
  285. TopicSelectDialog,
  286. ElemUndertaking,
  287. },
  288. data() {
  289. return {
  290. ELEMENT_LIST,
  291. TOPIC_LIST,
  292. NOT_TOPIC_LIST,
  293. topicList: [],
  294. columnWidth: 0,
  295. isSubmit: false,
  296. canSave: false,
  297. hasUnsubmitCont: false,
  298. };
  299. },
  300. computed: {
  301. ...mapState("card", [
  302. "cardConfig",
  303. "topics",
  304. "pages",
  305. "curElement",
  306. "curPage",
  307. "curPageNo",
  308. ]),
  309. cardHeadSampleData() {
  310. if (!this.cardConfig["pageSize"]) return;
  311. const data = getCardHeadModel(this.cardConfig);
  312. data.isSimple = true;
  313. return data;
  314. },
  315. isFromCardManage() {
  316. return this.$route.name === "CardManage";
  317. },
  318. },
  319. mounted() {
  320. this.initCard();
  321. },
  322. beforeDestroy() {
  323. this.initState();
  324. },
  325. methods: {
  326. ...mapMutations("card", [
  327. "addPage",
  328. "setCurPage",
  329. "setCurElement",
  330. "setCardConfig",
  331. "setOpenElementEditDialog",
  332. "setCurDragElement",
  333. "setPages",
  334. "setInsetTarget",
  335. "initState",
  336. ]),
  337. ...mapActions("card", [
  338. "resetTopicSeries",
  339. "removePage",
  340. "addElement",
  341. "modifyCardHead",
  342. "modifyElement",
  343. "rebuildPages",
  344. "initTopicsFromPages",
  345. "scrollToElementPage",
  346. ]),
  347. async initCard() {
  348. const { cardConfig, pages } = this.content;
  349. this.setCardConfig(cardConfig);
  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. if (item.type === "FORBID_AREA") {
  376. const lastTopicElement = this.topics.findLast(
  377. (item) => !OTHER_ELEMENT.includes(item.type)
  378. );
  379. element.sign = lastTopicElement ? lastTopicElement.sign : "objective";
  380. this.addElement(element);
  381. this.setCurElement(element);
  382. this.$nextTick(() => {
  383. this.rebuildPages();
  384. this.$nextTick(() => {
  385. this.scrollToElementPage(element);
  386. });
  387. });
  388. } else {
  389. this.setCurElement(element);
  390. this.$refs.ElementPropEdit.open();
  391. // to elementPropEdit/ElementPropEdit open topic edit dialog
  392. }
  393. },
  394. insetNewTopic({ id, type }) {
  395. console.log(id, type);
  396. this.setInsetTarget({ id, type });
  397. if (type === "FILL_QUESTION") {
  398. this.topicList = this.TOPIC_LIST;
  399. } else {
  400. this.topicList = this.TOPIC_LIST.filter(
  401. (item) => item.type !== "FILL_QUESTION"
  402. );
  403. }
  404. this.$refs.TopicSelectDialog.open();
  405. },
  406. // 元件编辑
  407. dragstart(element) {
  408. this.setCurDragElement(getElementModel(element.type));
  409. },
  410. addWatch() {
  411. this.$watch("cardConfig", (val) => {
  412. const element = getCardHeadModel(val);
  413. this.modifyCardHead(element);
  414. this.$nextTick(() => {
  415. this.rebuildPages();
  416. });
  417. });
  418. },
  419. // 页面切换
  420. swithPage(pindex) {
  421. if (this.curPageNo === pindex) return;
  422. let pageTops = this.pages.map((page, pageNo) => {
  423. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  424. });
  425. pageTops = pageTops.map((item) => item - pageTops[0]);
  426. document.getElementById("design-main").scrollTop = pageTops[pindex];
  427. this.setCurPage(pindex);
  428. this.setCurElement({});
  429. },
  430. registSrollEvent() {
  431. document.getElementById("design-main").addEventListener("scroll", (e) => {
  432. e.preventDefault();
  433. e.stopPropagation();
  434. let pageTops = this.pages.map((page, pageNo) => {
  435. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  436. });
  437. const pageRangeHeight =
  438. document.getElementById(`edit-page-box-0`).offsetHeight * 0.2;
  439. pageTops = pageTops.map((item) => item - pageTops[0] - pageRangeHeight);
  440. const scrollTop = e.target.scrollTop;
  441. // console.log(pageTops, scrollTop);
  442. const targePageTop = pageTops.find((item) => scrollTop < item);
  443. const pageNo = targePageTop
  444. ? pageTops.indexOf(targePageTop) - 1
  445. : pageTops.length - 1;
  446. if (this.curPageNo === pageNo) return;
  447. this.setCurPage(pageNo);
  448. });
  449. },
  450. // save
  451. getCardData(htmlContent = "", model = "") {
  452. const data = {
  453. title: this.cardConfig.cardTitle,
  454. content: model,
  455. htmlContent,
  456. };
  457. return data;
  458. },
  459. checkElementCovered() {
  460. let elements = [];
  461. this.pages.forEach((page) => {
  462. page.columns.forEach((column) => {
  463. column.elements.forEach((element) => {
  464. if (element.isCovered) {
  465. elements.push(element.id);
  466. }
  467. });
  468. });
  469. });
  470. return elements.length;
  471. },
  472. checkCardValid() {
  473. if (!this.cardConfig.cardTitle) {
  474. this.$message.error("题卡标题不能为空!");
  475. this.setCurPageNo(0);
  476. setTimeout(() => {
  477. document.getElementById("cardTitleInput").focus();
  478. });
  479. return false;
  480. }
  481. // if (!this.cardConfig.cardDesc) {
  482. // this.$message.error("题卡描述信息不能为空!");
  483. // this.setCurPage(0);
  484. // setTimeout(() => {
  485. // document.getElementById("cardDescInput").focus();
  486. // });
  487. // return false;
  488. // }
  489. if (this.checkElementCovered()) {
  490. this.$message.error("题卡中存在被遮挡的元件,请注意调整!");
  491. return false;
  492. }
  493. return true;
  494. },
  495. getCardJson() {
  496. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  497. return new Promise((resolve) => {
  498. setTimeout(() => {
  499. const data = JSON.stringify(
  500. {
  501. version: CARD_VERSION,
  502. cardType: "STANDARD",
  503. cardConfig: this.cardConfig,
  504. pages: this.pages,
  505. },
  506. (k, v) => (k.startsWith("_") ? undefined : v)
  507. );
  508. resolve(data);
  509. }, 100);
  510. });
  511. },
  512. async toPreview() {
  513. this.$emit("on-preview", {
  514. cardConfig: this.cardConfig,
  515. pages: this.pages,
  516. });
  517. },
  518. async toSave() {
  519. const model = await this.getCardJson();
  520. const datas = this.getCardData("", model);
  521. this.$emit("on-save", datas);
  522. },
  523. toSubmit() {
  524. if (this.isSubmit) return;
  525. if (!this.checkCardValid()) return;
  526. this.$emit("on-submit", {
  527. cardConfig: this.cardConfig,
  528. pages: this.pages,
  529. });
  530. },
  531. toExit() {
  532. this.$emit("on-exit");
  533. },
  534. loading() {
  535. this.isSubmit = true;
  536. },
  537. unloading() {
  538. this.isSubmit = false;
  539. },
  540. updateUnsubmitStatus(status) {
  541. this.hasUnsubmitCont = status;
  542. },
  543. },
  544. };
  545. </script>