CardFreeDesign.vue 11 KB

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