CardDesign.vue 16 KB

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