CardFreeDesign.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <div class="card-design card-free-design">
  3. <div class="design-header">
  4. <div class="design-header-cont box-justify">
  5. <div></div>
  6. <el-button
  7. class="btn-help"
  8. icon="el-icon-question"
  9. type="text"
  10. @click="showHelp"
  11. ></el-button>
  12. </div>
  13. </div>
  14. <!-- actions -->
  15. <div class="design-action">
  16. <div class="design-logo">
  17. <h1>
  18. <i class="el-icon-d-arrow-left" @click="toExit" title="退出"></i>
  19. 答题卡制作
  20. </h1>
  21. </div>
  22. <div class="action-part">
  23. <div class="action-part-title"><h2>基本设置</h2></div>
  24. <div class="action-part-body">
  25. <page-prop-edit></page-prop-edit>
  26. </div>
  27. </div>
  28. <div class="action-part">
  29. <div class="action-part-title"><h2>当前页面设置</h2></div>
  30. <div class="action-part-body">
  31. <edit-page></edit-page>
  32. </div>
  33. </div>
  34. <div class="action-part">
  35. <div class="action-part-title"><h2>复杂元素</h2></div>
  36. <div class="action-part-body">
  37. <div class="type-list">
  38. <div
  39. class="type-item"
  40. v-for="(item, index) in TOPIC_LIST"
  41. :key="index"
  42. draggable="true"
  43. @dragstart="dragstart(item)"
  44. >
  45. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  46. </div>
  47. </div>
  48. <p class="tips-info">提示:拖动插入元素</p>
  49. </div>
  50. </div>
  51. <div class="action-part">
  52. <div class="action-part-title"><h2>简单元素</h2></div>
  53. <div class="action-part-body">
  54. <div class="type-list">
  55. <div
  56. class="type-item"
  57. v-for="(item, index) in ELEMENT_LIST"
  58. :key="index"
  59. draggable="true"
  60. @dragstart="dragstart(item)"
  61. >
  62. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  63. </div>
  64. <p class="tips-info">提示:拖动插入元素</p>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="action-part">
  69. <div class="action-part-title"><h2>元素面板</h2></div>
  70. <div class="action-part-body">
  71. <!-- element-tier-edit -->
  72. <element-tier-edit ref="ElementTierEdit"></element-tier-edit>
  73. </div>
  74. </div>
  75. </div>
  76. <div class="design-main">
  77. <!-- menus -->
  78. <div class="design-control">
  79. <div class="control-left tab-btns">
  80. <el-button
  81. v-for="(page, pageNo) in pages"
  82. :key="pageNo"
  83. :type="curPageNo === pageNo ? 'primary' : 'default'"
  84. @click="swithPage(pageNo)"
  85. >
  86. <span>第{{ pageNo + 1 }}页</span>
  87. <span class="page-delete" @click.stop="toDeletePage(page)"
  88. ><i class="el-icon-error"></i
  89. ></span>
  90. </el-button>
  91. <el-button icon="el-icon-plus" @click="toAddPage"></el-button>
  92. </div>
  93. <div class="control-right">
  94. <el-button
  95. type="success"
  96. :loading="isSubmit"
  97. :disabled="!pages.length"
  98. @click="toPreview"
  99. >预览</el-button
  100. >
  101. <el-button
  102. v-if="showSaveBtn"
  103. type="primary"
  104. :loading="isSubmit"
  105. :disabled="canSave || !pages.length"
  106. @click="toSave"
  107. >暂存</el-button
  108. >
  109. <el-button type="primary" :loading="isSubmit" @click="toSubmit"
  110. >提交</el-button
  111. >
  112. </div>
  113. </div>
  114. <!-- edit body -->
  115. <div class="design-body">
  116. <div
  117. :class="[
  118. 'page-box',
  119. `page-box-${curPage.pageSize}`,
  120. `page-box-${curPageNo % 2}`,
  121. { 'page-box-less': pages.length <= 2 }
  122. ]"
  123. v-if="curPage.id"
  124. >
  125. <div
  126. :class="[
  127. 'page-locators',
  128. `page-locators-${curPage.locators.length}`
  129. ]"
  130. >
  131. <ul
  132. class="page-locator-group"
  133. v-for="(locator, iind) in curPage.locators"
  134. :key="iind"
  135. >
  136. <li
  137. v-for="(elem, eindex) in locator"
  138. :key="eindex"
  139. :id="elem.id"
  140. ></li>
  141. </ul>
  142. </div>
  143. <!-- inner edit area -->
  144. <div class="page-main-inner">
  145. <div
  146. :class="['page-main', `page-main-${curPage.columns.length}`]"
  147. :style="{ margin: `0 -${curPage.columnGap / 2}px` }"
  148. >
  149. <div
  150. class="page-column"
  151. v-for="(column, columnNo) in curPage.columns"
  152. :key="columnNo"
  153. :style="{ padding: `0 ${curPage.columnGap / 2}px` }"
  154. >
  155. <topic-column-edit
  156. class="page-column-main"
  157. :data="column"
  158. ></topic-column-edit>
  159. </div>
  160. </div>
  161. </div>
  162. <!-- outer edit area -->
  163. <div class="page-main-outer">
  164. <page-number
  165. type="rect"
  166. :total="pages.length"
  167. :current="curPageNo + 1"
  168. ></page-number>
  169. <page-number
  170. type="text"
  171. :total="pages.length"
  172. :current="curPageNo + 1"
  173. ></page-number>
  174. </div>
  175. </div>
  176. </div>
  177. </div>
  178. <!-- element-prop-edit -->
  179. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  180. <!-- right-click-menu -->
  181. <right-click-menu></right-click-menu>
  182. <!-- shortcut-key -->
  183. <shortcut-key
  184. @sk-save="skSave"
  185. @sk-submit="skSubmit"
  186. @sk-preview="skPreview"
  187. ></shortcut-key>
  188. <!-- help-dialog -->
  189. <help-dialog ref="HelpDialog"></help-dialog>
  190. </div>
  191. </template>
  192. <script>
  193. import { mapState, mapMutations, mapActions } from "vuex";
  194. import { getElementModel, ELEMENT_LIST, TOPIC_LIST } from "../elements/model";
  195. import { getModel as getPageModel } from "../../../elements/page/model";
  196. import { CARD_VERSION } from "../../../enumerate";
  197. import TopicColumnEdit from "../components/TopicColumnEdit";
  198. import ElementPropEdit from "../components/ElementPropEdit";
  199. import ElementTierEdit from "../components/ElementTierEdit";
  200. import PagePropEdit from "../components/PagePropEdit";
  201. import RightClickMenu from "../components/RightClickMenu";
  202. import ShortcutKey from "../components/ShortcutKey";
  203. import HelpDialog from "../components/HelpDialog";
  204. import EditPage from "../../../elements/page/EditPage";
  205. import PageNumber from "../../../components/PageNumber";
  206. export default {
  207. name: "card-free-design",
  208. props: {
  209. content: {
  210. type: Object,
  211. default() {
  212. return {
  213. pages: [],
  214. cardConfig: {}
  215. };
  216. }
  217. },
  218. showSaveBtn: {
  219. type: Boolean,
  220. default: true
  221. }
  222. },
  223. components: {
  224. TopicColumnEdit,
  225. PagePropEdit,
  226. EditPage,
  227. ElementPropEdit,
  228. ElementTierEdit,
  229. RightClickMenu,
  230. ShortcutKey,
  231. HelpDialog,
  232. PageNumber
  233. },
  234. data() {
  235. return {
  236. ELEMENT_LIST,
  237. TOPIC_LIST,
  238. topicList: [],
  239. columnWidth: 0,
  240. isSubmit: false,
  241. canSave: false
  242. };
  243. },
  244. computed: {
  245. ...mapState("free", [
  246. "cardConfig",
  247. "pages",
  248. "curElement",
  249. "curPage",
  250. "curPageNo",
  251. "curDragElement",
  252. "curColumnId"
  253. ])
  254. },
  255. mounted() {
  256. this.initCard();
  257. },
  258. methods: {
  259. ...mapMutations("free", [
  260. "setCurPageNo",
  261. "setCurElement",
  262. "setCardConfig",
  263. "setOpenElementEditDialog",
  264. "setCurDragElement",
  265. "setPages",
  266. "initState"
  267. ]),
  268. ...mapActions("free", [
  269. "addPage",
  270. "removePage",
  271. "addElement",
  272. "modifyElement"
  273. ]),
  274. async initCard() {
  275. const { cardConfig, pages } = this.content;
  276. this.setCardConfig(cardConfig);
  277. if (pages && pages.length) {
  278. this.setPages(pages);
  279. this.setCurPageNo(0);
  280. } else {
  281. this.initPageData();
  282. }
  283. },
  284. initPageData() {
  285. const page = getPageModel(this.cardConfig);
  286. this.addPage(page);
  287. this.setCurPageNo(0);
  288. },
  289. toAddPage() {
  290. const page = getPageModel(this.cardConfig);
  291. this.addPage(page);
  292. },
  293. addNewTopic(item) {
  294. let element = getElementModel(item.type);
  295. this.setCurElement(element);
  296. this.$refs.ElementPropEdit.open();
  297. // to elementPropEdit/ElementPropEdit open topic edit dialog
  298. },
  299. // 元件编辑
  300. dragstart(element) {
  301. this.setCurDragElement(getElementModel(element.type));
  302. },
  303. // 操作
  304. swithPage(pindex) {
  305. if (this.curPageNo === pindex) return;
  306. this.setCurPageNo(pindex);
  307. this.setCurElement({});
  308. },
  309. toDeletePage(page) {
  310. if (this.pages.length === 1) {
  311. this.$message.error("只剩最后一页,不能再删除了");
  312. return;
  313. }
  314. this.removePage(page);
  315. },
  316. // save
  317. getCardData(htmlContent = "", model = "") {
  318. let data = {
  319. title: this.cardConfig.cardTitle,
  320. content: model,
  321. htmlContent
  322. };
  323. return data;
  324. },
  325. getCardJson() {
  326. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  327. return new Promise(resolve => {
  328. setTimeout(() => {
  329. const data = JSON.stringify(
  330. {
  331. version: CARD_VERSION,
  332. cardType: "FREE",
  333. cardConfig: this.cardConfig,
  334. paperParams: this.paperParams,
  335. pages: this.pages
  336. },
  337. (k, v) => (k.startsWith("_") ? undefined : v)
  338. );
  339. resolve(data);
  340. }, 100);
  341. });
  342. },
  343. skSave() {
  344. this.toSave();
  345. },
  346. skSubmit() {
  347. this.toSubmit();
  348. },
  349. skPreview() {
  350. this.toPreview();
  351. },
  352. async toPreview() {
  353. if (this.isSubmit) return;
  354. this.isSubmit = true;
  355. const model = await this.getCardJson();
  356. const datas = this.getCardData("", model);
  357. this.$emit("on-preview", datas);
  358. },
  359. async toSave() {
  360. if (this.isSubmit) return;
  361. this.isSubmit = true;
  362. const model = await this.getCardJson();
  363. const datas = this.getCardData("", model);
  364. this.$emit("on-save", datas);
  365. },
  366. toSubmit() {
  367. if (this.isSubmit) return;
  368. if (this.pages.length % 2) {
  369. this.$message.error("请确保题卡页数是偶数");
  370. return;
  371. }
  372. this.$emit("on-submit", {
  373. cardConfig: this.cardConfig,
  374. pages: this.pages,
  375. paperParams: this.paperParams
  376. });
  377. },
  378. toExit() {
  379. this.$emit("on-exit");
  380. },
  381. showHelp() {
  382. this.$refs.HelpDialog.open();
  383. },
  384. loading() {
  385. this.isSubmit = true;
  386. },
  387. unloading() {
  388. this.isSubmit = false;
  389. }
  390. },
  391. beforeDestroy() {
  392. this.initState();
  393. }
  394. };
  395. </script>