CardFreeDesign.vue 11 KB

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