CardFreeDesign.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. <!-- locator -->
  126. <div class="page-locator page-locator-top">
  127. <div
  128. v-for="elem in curPage.locators.top"
  129. :key="elem.id"
  130. :id="elem.id"
  131. class="page-locator-item"
  132. ></div>
  133. </div>
  134. <div class="page-locator page-locator-bottom">
  135. <div
  136. v-for="elem in curPage.locators.bottom"
  137. :key="elem.id"
  138. :id="elem.id"
  139. class="page-locator-item"
  140. ></div>
  141. </div>
  142. <!-- inner edit area -->
  143. <div class="page-main-inner">
  144. <div
  145. :class="['page-main', `page-main-${curPage.columns.length}`]"
  146. :style="{ margin: `0 -${curPage.columnGap / 2}px` }"
  147. >
  148. <div
  149. class="page-column"
  150. v-for="(column, columnNo) in curPage.columns"
  151. :key="columnNo"
  152. :style="{ padding: `0 ${curPage.columnGap / 2}px` }"
  153. >
  154. <topic-column-edit
  155. class="page-column-main"
  156. :data="column"
  157. ></topic-column-edit>
  158. </div>
  159. </div>
  160. </div>
  161. <!-- outer edit area -->
  162. <div class="page-main-outer">
  163. <page-number
  164. type="rect"
  165. :total="pages.length"
  166. :current="curPageNo + 1"
  167. ></page-number>
  168. <page-number
  169. type="text"
  170. :total="pages.length"
  171. :current="curPageNo + 1"
  172. ></page-number>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. <!-- element-prop-edit -->
  178. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  179. <!-- right-click-menu -->
  180. <right-click-menu></right-click-menu>
  181. <!-- shortcut-key -->
  182. <shortcut-key
  183. ref="ShortcutKey"
  184. @sk-save="skSave"
  185. @sk-submit="skSubmit"
  186. @sk-preview="skPreview"
  187. ></shortcut-key>
  188. <!-- help-dialog -->
  189. <help-dialog
  190. ref="HelpDialog"
  191. @on-enable="shortcutEnableChange"
  192. ></help-dialog>
  193. </div>
  194. </template>
  195. <script>
  196. import { mapState, mapMutations, mapActions } from "vuex";
  197. import { getElementModel, ELEMENT_LIST, TOPIC_LIST } from "../elements/model";
  198. import { getModel as getPageModel } from "../../../elements/page/model";
  199. import { CARD_VERSION } from "../../../enumerate";
  200. import TopicColumnEdit from "../components/TopicColumnEdit";
  201. import ElementPropEdit from "../components/ElementPropEdit";
  202. import ElementTierEdit from "../components/ElementTierEdit";
  203. import PagePropEdit from "../components/PagePropEdit";
  204. import RightClickMenu from "../components/RightClickMenu";
  205. import ShortcutKey from "../components/ShortcutKey";
  206. import HelpDialog from "../components/HelpDialog";
  207. import EditPage from "../../../elements/page/EditPage";
  208. import PageNumber from "../../../components/PageNumber";
  209. export default {
  210. name: "card-free-design",
  211. props: {
  212. content: {
  213. type: Object,
  214. default() {
  215. return {
  216. pages: [],
  217. cardConfig: {},
  218. };
  219. },
  220. },
  221. showSaveBtn: {
  222. type: Boolean,
  223. default: true,
  224. },
  225. },
  226. components: {
  227. TopicColumnEdit,
  228. PagePropEdit,
  229. EditPage,
  230. ElementPropEdit,
  231. ElementTierEdit,
  232. RightClickMenu,
  233. ShortcutKey,
  234. HelpDialog,
  235. PageNumber,
  236. },
  237. data() {
  238. return {
  239. ELEMENT_LIST,
  240. TOPIC_LIST,
  241. topicList: [],
  242. columnWidth: 0,
  243. isSubmit: false,
  244. canSave: false,
  245. };
  246. },
  247. computed: {
  248. ...mapState("free", [
  249. "cardConfig",
  250. "pages",
  251. "curElement",
  252. "curPage",
  253. "curPageNo",
  254. "curDragElement",
  255. "curColumnId",
  256. ]),
  257. },
  258. mounted() {
  259. this.initCard();
  260. },
  261. methods: {
  262. ...mapMutations("free", [
  263. "setCurPageNo",
  264. "setCurElement",
  265. "setCardConfig",
  266. "setOpenElementEditDialog",
  267. "setCurDragElement",
  268. "setPages",
  269. "initState",
  270. ]),
  271. ...mapActions("free", [
  272. "addPage",
  273. "removePage",
  274. "addElement",
  275. "modifyElement",
  276. ]),
  277. async initCard() {
  278. const { cardConfig, pages } = this.content;
  279. this.setCardConfig(cardConfig);
  280. if (pages && pages.length) {
  281. this.setPages(pages);
  282. this.setCurPageNo(0);
  283. } else {
  284. this.initPageData();
  285. }
  286. },
  287. initPageData() {
  288. const page = getPageModel(this.cardConfig);
  289. this.addPage(page);
  290. this.setCurPageNo(0);
  291. },
  292. toAddPage() {
  293. const page = getPageModel(this.cardConfig);
  294. this.addPage(page);
  295. },
  296. addNewTopic(item) {
  297. let element = getElementModel(item.type);
  298. this.setCurElement(element);
  299. this.$refs.ElementPropEdit.open();
  300. // to elementPropEdit/ElementPropEdit open topic edit dialog
  301. },
  302. // 元件编辑
  303. dragstart(element) {
  304. this.setCurDragElement(getElementModel(element.type));
  305. },
  306. // 操作
  307. swithPage(pindex) {
  308. if (this.curPageNo === pindex) return;
  309. this.setCurPageNo(pindex);
  310. this.setCurElement({});
  311. },
  312. toDeletePage(page) {
  313. if (this.pages.length === 1) {
  314. this.$message.error("只剩最后一页,不能再删除了");
  315. return;
  316. }
  317. this.removePage(page);
  318. },
  319. // save
  320. getCardData(htmlContent = "", model = "") {
  321. let data = {
  322. title: this.cardConfig.cardTitle,
  323. content: model,
  324. htmlContent,
  325. };
  326. return data;
  327. },
  328. getCardJson() {
  329. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  330. return new Promise((resolve) => {
  331. setTimeout(() => {
  332. const data = JSON.stringify(
  333. {
  334. version: CARD_VERSION,
  335. cardType: "FREE",
  336. cardConfig: this.cardConfig,
  337. paperParams: this.paperParams,
  338. pages: this.pages,
  339. },
  340. (k, v) => (k.startsWith("_") ? undefined : v)
  341. );
  342. resolve(data);
  343. }, 100);
  344. });
  345. },
  346. skSave() {
  347. this.toSave();
  348. },
  349. skSubmit() {
  350. this.toSubmit();
  351. },
  352. skPreview() {
  353. this.toPreview();
  354. },
  355. shortcutEnableChange(enable) {
  356. if (enable) {
  357. this.$refs.ShortcutKey.registShortcutKey();
  358. } else {
  359. this.$refs.ShortcutKey.removeShortcutKey();
  360. }
  361. },
  362. toPreview() {
  363. this.$emit("on-preview", {
  364. cardConfig: this.cardConfig,
  365. pages: this.pages,
  366. paperParams: this.paperParams,
  367. });
  368. },
  369. async toSave() {
  370. if (this.isSubmit) return;
  371. this.isSubmit = true;
  372. const model = await this.getCardJson();
  373. const datas = this.getCardData("", model);
  374. this.$emit("on-save", datas);
  375. },
  376. toSubmit() {
  377. if (this.isSubmit) return;
  378. if (this.pages.length % 2) {
  379. this.$message.error("请确保题卡页数是偶数");
  380. return;
  381. }
  382. this.$emit("on-submit", {
  383. cardConfig: this.cardConfig,
  384. pages: this.pages,
  385. paperParams: this.paperParams,
  386. });
  387. },
  388. toExit() {
  389. this.$emit("on-exit");
  390. },
  391. showHelp() {
  392. this.$refs.HelpDialog.open();
  393. },
  394. loading() {
  395. this.isSubmit = true;
  396. },
  397. unloading() {
  398. this.isSubmit = false;
  399. },
  400. },
  401. beforeDestroy() {
  402. this.initState();
  403. },
  404. };
  405. </script>