CardDesign.vue 16 KB

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