index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <template>
  2. <div class="container">
  3. <div class="left-board">
  4. <div class="logo-wrapper">
  5. <div class="logo">
  6. <img :src="logo" alt="logo"> Form Generator
  7. </div>
  8. </div>
  9. <el-scrollbar class="left-scrollbar">
  10. <div class="components-list">
  11. <div class="components-title">
  12. <svg-icon icon-class="component" />输入型组件
  13. </div>
  14. <draggable
  15. class="components-draggable"
  16. :list="inputComponents"
  17. :group="{ name: 'componentsGroup', pull: 'clone', put: false }"
  18. :clone="cloneComponent"
  19. draggable=".components-item"
  20. :sort="false"
  21. @end="onEnd"
  22. >
  23. <div
  24. v-for="(element, index) in inputComponents" :key="index" class="components-item"
  25. @click="addComponent(element)"
  26. >
  27. <div class="components-body">
  28. <svg-icon :icon-class="element.tagIcon" />
  29. {{ element.label }}
  30. </div>
  31. </div>
  32. </draggable>
  33. <div class="components-title">
  34. <svg-icon icon-class="component" />选择型组件
  35. </div>
  36. <draggable
  37. class="components-draggable"
  38. :list="selectComponents"
  39. :group="{ name: 'componentsGroup', pull: 'clone', put: false }"
  40. :clone="cloneComponent"
  41. draggable=".components-item"
  42. :sort="false"
  43. @end="onEnd"
  44. >
  45. <div
  46. v-for="(element, index) in selectComponents"
  47. :key="index"
  48. class="components-item"
  49. @click="addComponent(element)"
  50. >
  51. <div class="components-body">
  52. <svg-icon :icon-class="element.tagIcon" />
  53. {{ element.label }}
  54. </div>
  55. </div>
  56. </draggable>
  57. <div class="components-title">
  58. <svg-icon icon-class="component" /> 布局型组件
  59. </div>
  60. <draggable
  61. class="components-draggable" :list="layoutComponents"
  62. :group="{ name: 'componentsGroup', pull: 'clone', put: false }" :clone="cloneComponent"
  63. draggable=".components-item" :sort="false" @end="onEnd"
  64. >
  65. <div
  66. v-for="(element, index) in layoutComponents" :key="index" class="components-item"
  67. @click="addComponent(element)"
  68. >
  69. <div class="components-body">
  70. <svg-icon :icon-class="element.tagIcon" />
  71. {{ element.label }}
  72. </div>
  73. </div>
  74. </draggable>
  75. </div>
  76. </el-scrollbar>
  77. </div>
  78. <div class="center-board">
  79. <div class="action-bar">
  80. <el-button icon="el-icon-download" type="text" @click="download">
  81. 导出vue文件
  82. </el-button>
  83. <el-button class="copy-btn-main" icon="el-icon-document-copy" type="text" @click="copy">
  84. 复制代码
  85. </el-button>
  86. <el-button class="delete-btn" icon="el-icon-delete" type="text" @click="empty">
  87. 清空
  88. </el-button>
  89. </div>
  90. <el-scrollbar class="center-scrollbar">
  91. <el-row class="center-board-row" :gutter="formConf.gutter">
  92. <el-form
  93. :size="formConf.size"
  94. :label-position="formConf.labelPosition"
  95. :disabled="formConf.disabled"
  96. :label-width="formConf.labelWidth + 'px'"
  97. >
  98. <draggable class="drawing-board" :list="drawingList" :animation="340" group="componentsGroup">
  99. <draggable-item
  100. v-for="(element, index) in drawingList"
  101. :key="element.renderKey"
  102. :drawing-list="drawingList"
  103. :element="element"
  104. :index="index"
  105. :active-id="activeId"
  106. :form-conf="formConf"
  107. @activeItem="activeFormItem"
  108. @copyItem="drawingItemCopy"
  109. @deleteItem="drawingItemDelete"
  110. />
  111. </draggable>
  112. <div v-show="!drawingList.length" class="empty-info">
  113. 从左侧拖入或点选组件进行表单设计
  114. </div>
  115. </el-form>
  116. </el-row>
  117. </el-scrollbar>
  118. </div>
  119. <right-panel
  120. :active-data="activeData"
  121. :form-conf="formConf"
  122. :show-field="!!drawingList.length"
  123. @tag-change="tagChange"
  124. />
  125. <code-type-dialog
  126. :visible.sync="dialogVisible"
  127. title="选择生成类型"
  128. :show-file-name="showFileName"
  129. @confirm="generate"
  130. />
  131. <input id="copyNode" type="hidden">
  132. </div>
  133. </template>
  134. <script>
  135. import draggable from 'vuedraggable'
  136. import beautifier from 'js-beautify'
  137. import ClipboardJS from 'clipboard'
  138. import render from '@/utils/generator/render'
  139. import RightPanel from './RightPanel'
  140. import { inputComponents, selectComponents, layoutComponents, formConf } from '@/utils/generator/config'
  141. import { beautifierConf, titleCase } from '@/utils/index'
  142. import { makeUpHtml, vueTemplate, vueScript, cssStyle } from '@/utils/generator/html'
  143. import { makeUpJs } from '@/utils/generator/js'
  144. import { makeUpCss } from '@/utils/generator/css'
  145. import drawingDefault from '@/utils/generator/drawingDefault'
  146. import logo from '@/assets/logo/logo.jpg'
  147. import CodeTypeDialog from './CodeTypeDialog'
  148. import DraggableItem from './DraggableItem'
  149. let oldActiveId
  150. let tempActiveData
  151. export default {
  152. components: {
  153. draggable,
  154. render,
  155. RightPanel,
  156. CodeTypeDialog,
  157. DraggableItem
  158. },
  159. data() {
  160. return {
  161. logo,
  162. idGlobal: 100,
  163. formConf,
  164. inputComponents,
  165. selectComponents,
  166. layoutComponents,
  167. labelWidth: 100,
  168. drawingList: drawingDefault,
  169. drawingData: {},
  170. activeId: drawingDefault[0].formId,
  171. drawerVisible: false,
  172. formData: {},
  173. dialogVisible: false,
  174. generateConf: null,
  175. showFileName: false,
  176. activeData: drawingDefault[0]
  177. }
  178. },
  179. created() {
  180. // 防止 firefox 下 拖拽 会新打卡一个选项卡
  181. document.body.ondrop = event => {
  182. event.preventDefault()
  183. event.stopPropagation()
  184. }
  185. },
  186. watch: {
  187. 'activeData.label': function (val, oldVal) {
  188. if (
  189. this.activeData.placeholder === undefined
  190. || !this.activeData.tag
  191. || oldActiveId !== this.activeId
  192. ) {
  193. return
  194. }
  195. this.activeData.placeholder = this.activeData.placeholder.replace(oldVal, '') + val
  196. },
  197. activeId: {
  198. handler(val) {
  199. oldActiveId = val
  200. },
  201. immediate: true
  202. }
  203. },
  204. mounted() {
  205. const clipboard = new ClipboardJS('#copyNode', {
  206. text: trigger => {
  207. const codeStr = this.generateCode()
  208. this.$notify({
  209. title: '成功',
  210. message: '代码已复制到剪切板,可粘贴。',
  211. type: 'success'
  212. })
  213. return codeStr
  214. }
  215. })
  216. clipboard.on('error', e => {
  217. this.$message.error('代码复制失败')
  218. })
  219. },
  220. methods: {
  221. activeFormItem(element) {
  222. this.activeData = element
  223. this.activeId = element.formId
  224. },
  225. onEnd(obj, a) {
  226. if (obj.from !== obj.to) {
  227. this.activeData = tempActiveData
  228. this.activeId = this.idGlobal
  229. }
  230. },
  231. addComponent(item) {
  232. const clone = this.cloneComponent(item)
  233. this.drawingList.push(clone)
  234. this.activeFormItem(clone)
  235. },
  236. cloneComponent(origin) {
  237. const clone = JSON.parse(JSON.stringify(origin))
  238. clone.formId = ++this.idGlobal
  239. clone.span = formConf.span
  240. clone.renderKey = +new Date() // 改变renderKey后可以实现强制更新组件
  241. if (!clone.layout) clone.layout = 'colFormItem'
  242. if (clone.layout === 'colFormItem') {
  243. clone.vModel = `field${this.idGlobal}`
  244. clone.placeholder !== undefined && (clone.placeholder += clone.label)
  245. tempActiveData = clone
  246. } else if (clone.layout === 'rowFormItem') {
  247. delete clone.label
  248. clone.componentName = `row${this.idGlobal}`
  249. clone.gutter = this.formConf.gutter
  250. tempActiveData = clone
  251. }
  252. return tempActiveData
  253. },
  254. AssembleFormData() {
  255. this.formData = {
  256. fields: JSON.parse(JSON.stringify(this.drawingList)),
  257. ...this.formConf
  258. }
  259. },
  260. generate(data) {
  261. const func = this[`exec${titleCase(this.operationType)}`]
  262. this.generateConf = data
  263. func && func(data)
  264. },
  265. execRun(data) {
  266. this.AssembleFormData()
  267. this.drawerVisible = true
  268. },
  269. execDownload(data) {
  270. const codeStr = this.generateCode()
  271. const blob = new Blob([codeStr], { type: 'text/plain;charset=utf-8' })
  272. this.$download.saveAs(blob, data.fileName)
  273. },
  274. execCopy(data) {
  275. document.getElementById('copyNode').click()
  276. },
  277. empty() {
  278. this.$confirm('确定要清空所有组件吗?', '提示', { type: 'warning' }).then(
  279. () => {
  280. this.drawingList = []
  281. }
  282. )
  283. },
  284. drawingItemCopy(item, parent) {
  285. let clone = JSON.parse(JSON.stringify(item))
  286. clone = this.createIdAndKey(clone)
  287. parent.push(clone)
  288. this.activeFormItem(clone)
  289. },
  290. createIdAndKey(item) {
  291. item.formId = ++this.idGlobal
  292. item.renderKey = +new Date()
  293. if (item.layout === 'colFormItem') {
  294. item.vModel = `field${this.idGlobal}`
  295. } else if (item.layout === 'rowFormItem') {
  296. item.componentName = `row${this.idGlobal}`
  297. }
  298. if (Array.isArray(item.children)) {
  299. item.children = item.children.map(childItem => this.createIdAndKey(childItem))
  300. }
  301. return item
  302. },
  303. drawingItemDelete(index, parent) {
  304. parent.splice(index, 1)
  305. this.$nextTick(() => {
  306. const len = this.drawingList.length
  307. if (len) {
  308. this.activeFormItem(this.drawingList[len - 1])
  309. }
  310. })
  311. },
  312. generateCode() {
  313. const { type } = this.generateConf
  314. this.AssembleFormData()
  315. const script = vueScript(makeUpJs(this.formData, type))
  316. const html = vueTemplate(makeUpHtml(this.formData, type))
  317. const css = cssStyle(makeUpCss(this.formData))
  318. return beautifier.html(html + script + css, beautifierConf.html)
  319. },
  320. download() {
  321. this.dialogVisible = true
  322. this.showFileName = true
  323. this.operationType = 'download'
  324. },
  325. run() {
  326. this.dialogVisible = true
  327. this.showFileName = false
  328. this.operationType = 'run'
  329. },
  330. copy() {
  331. this.dialogVisible = true
  332. this.showFileName = false
  333. this.operationType = 'copy'
  334. },
  335. tagChange(newTag) {
  336. newTag = this.cloneComponent(newTag)
  337. newTag.vModel = this.activeData.vModel
  338. newTag.formId = this.activeId
  339. newTag.span = this.activeData.span
  340. delete this.activeData.tag
  341. delete this.activeData.tagIcon
  342. delete this.activeData.document
  343. Object.keys(newTag).forEach(key => {
  344. if (this.activeData[key] !== undefined
  345. && typeof this.activeData[key] === typeof newTag[key]) {
  346. newTag[key] = this.activeData[key]
  347. }
  348. })
  349. this.activeData = newTag
  350. this.updateDrawingList(newTag, this.drawingList)
  351. },
  352. updateDrawingList(newTag, list) {
  353. const index = list.findIndex(item => item.formId === this.activeId)
  354. if (index > -1) {
  355. list.splice(index, 1, newTag)
  356. } else {
  357. list.forEach(item => {
  358. if (Array.isArray(item.children)) this.updateDrawingList(newTag, item.children)
  359. })
  360. }
  361. }
  362. }
  363. }
  364. </script>
  365. <style lang='scss'>
  366. .editor-tabs{
  367. background: #121315;
  368. .el-tabs__header{
  369. margin: 0;
  370. border-bottom-color: #121315;
  371. .el-tabs__nav{
  372. border-color: #121315;
  373. }
  374. }
  375. .el-tabs__item{
  376. height: 32px;
  377. line-height: 32px;
  378. color: #888a8e;
  379. border-left: 1px solid #121315 !important;
  380. background: #363636;
  381. margin-right: 5px;
  382. user-select: none;
  383. }
  384. .el-tabs__item.is-active{
  385. background: #1e1e1e;
  386. border-bottom-color: #1e1e1e!important;
  387. color: #fff;
  388. }
  389. .el-icon-edit{
  390. color: #f1fa8c;
  391. }
  392. .el-icon-document{
  393. color: #a95812;
  394. }
  395. }
  396. // home
  397. .right-scrollbar {
  398. .el-scrollbar__view {
  399. padding: 12px 18px 15px 15px;
  400. }
  401. }
  402. .left-scrollbar .el-scrollbar__wrap {
  403. box-sizing: border-box;
  404. overflow-x: hidden !important;
  405. margin-bottom: 0 !important;
  406. }
  407. .center-tabs{
  408. .el-tabs__header{
  409. margin-bottom: 0!important;
  410. }
  411. .el-tabs__item{
  412. width: 50%;
  413. text-align: center;
  414. }
  415. .el-tabs__nav{
  416. width: 100%;
  417. }
  418. }
  419. .reg-item{
  420. padding: 12px 6px;
  421. background: #f8f8f8;
  422. position: relative;
  423. border-radius: 4px;
  424. .close-btn{
  425. position: absolute;
  426. right: -6px;
  427. top: -6px;
  428. display: block;
  429. width: 16px;
  430. height: 16px;
  431. line-height: 16px;
  432. background: rgba(0, 0, 0, 0.2);
  433. border-radius: 50%;
  434. color: #fff;
  435. text-align: center;
  436. z-index: 1;
  437. cursor: pointer;
  438. font-size: 12px;
  439. &:hover{
  440. background: rgba(210, 23, 23, 0.5)
  441. }
  442. }
  443. & + .reg-item{
  444. margin-top: 18px;
  445. }
  446. }
  447. .action-bar{
  448. & .el-button+.el-button {
  449. margin-left: 15px;
  450. }
  451. & i {
  452. font-size: 20px;
  453. vertical-align: middle;
  454. position: relative;
  455. top: -1px;
  456. }
  457. }
  458. .custom-tree-node{
  459. width: 100%;
  460. font-size: 14px;
  461. .node-operation{
  462. float: right;
  463. }
  464. i[class*="el-icon"] + i[class*="el-icon"]{
  465. margin-left: 6px;
  466. }
  467. .el-icon-plus{
  468. color: #409EFF;
  469. }
  470. .el-icon-delete{
  471. color: #157a0c;
  472. }
  473. }
  474. .left-scrollbar .el-scrollbar__view{
  475. overflow-x: hidden;
  476. }
  477. .el-rate{
  478. display: inline-block;
  479. vertical-align: text-top;
  480. }
  481. .el-upload__tip{
  482. line-height: 1.2;
  483. }
  484. $selectedColor: #f6f7ff;
  485. $lighterBlue: #409EFF;
  486. .container {
  487. position: relative;
  488. width: 100%;
  489. height: 100%;
  490. }
  491. .components-list {
  492. padding: 8px;
  493. box-sizing: border-box;
  494. height: 100%;
  495. .components-item {
  496. display: inline-block;
  497. width: 48%;
  498. margin: 1%;
  499. transition: transform 0ms !important;
  500. }
  501. }
  502. .components-draggable{
  503. padding-bottom: 20px;
  504. }
  505. .components-title{
  506. font-size: 14px;
  507. color: #222;
  508. margin: 6px 2px;
  509. .svg-icon{
  510. color: #666;
  511. font-size: 18px;
  512. }
  513. }
  514. .components-body {
  515. padding: 8px 10px;
  516. background: $selectedColor;
  517. font-size: 12px;
  518. cursor: move;
  519. border: 1px dashed $selectedColor;
  520. border-radius: 3px;
  521. .svg-icon{
  522. color: #777;
  523. font-size: 15px;
  524. }
  525. &:hover {
  526. border: 1px dashed #787be8;
  527. color: #787be8;
  528. .svg-icon {
  529. color: #787be8;
  530. }
  531. }
  532. }
  533. .left-board {
  534. width: 260px;
  535. position: absolute;
  536. left: 0;
  537. top: 0;
  538. height: 100vh;
  539. }
  540. .left-scrollbar{
  541. height: calc(100vh - 42px);
  542. overflow: hidden;
  543. }
  544. .center-scrollbar {
  545. height: calc(100vh - 42px);
  546. overflow: hidden;
  547. border-left: 1px solid #f1e8e8;
  548. border-right: 1px solid #f1e8e8;
  549. box-sizing: border-box;
  550. }
  551. .center-board {
  552. height: 100vh;
  553. width: auto;
  554. margin: 0 350px 0 260px;
  555. box-sizing: border-box;
  556. }
  557. .empty-info{
  558. position: absolute;
  559. top: 46%;
  560. left: 0;
  561. right: 0;
  562. text-align: center;
  563. font-size: 18px;
  564. color: #ccb1ea;
  565. letter-spacing: 4px;
  566. }
  567. .action-bar{
  568. position: relative;
  569. height: 42px;
  570. text-align: right;
  571. padding: 0 15px;
  572. box-sizing: border-box;;
  573. border: 1px solid #f1e8e8;
  574. border-top: none;
  575. border-left: none;
  576. .delete-btn{
  577. color: #F56C6C;
  578. }
  579. }
  580. .logo-wrapper{
  581. position: relative;
  582. height: 42px;
  583. background: #fff;
  584. border-bottom: 1px solid #f1e8e8;
  585. box-sizing: border-box;
  586. }
  587. .logo{
  588. position: absolute;
  589. left: 12px;
  590. top: 6px;
  591. line-height: 30px;
  592. color: #00afff;
  593. font-weight: 600;
  594. font-size: 17px;
  595. white-space: nowrap;
  596. > img{
  597. width: 30px;
  598. height: 30px;
  599. vertical-align: top;
  600. }
  601. .github{
  602. display: inline-block;
  603. vertical-align: sub;
  604. margin-left: 15px;
  605. > img{
  606. height: 22px;
  607. }
  608. }
  609. }
  610. .center-board-row {
  611. padding: 12px 12px 15px 12px;
  612. box-sizing: border-box;
  613. & > .el-form {
  614. // 69 = 12+15+42
  615. height: calc(100vh - 69px);
  616. }
  617. }
  618. .drawing-board {
  619. height: 100%;
  620. position: relative;
  621. .components-body {
  622. padding: 0;
  623. margin: 0;
  624. font-size: 0;
  625. }
  626. .sortable-ghost {
  627. position: relative;
  628. display: block;
  629. overflow: hidden;
  630. &::before {
  631. content: " ";
  632. position: absolute;
  633. left: 0;
  634. right: 0;
  635. top: 0;
  636. height: 3px;
  637. background: rgb(89, 89, 223);
  638. z-index: 2;
  639. }
  640. }
  641. .components-item.sortable-ghost {
  642. width: 100%;
  643. height: 60px;
  644. background-color: $selectedColor;
  645. }
  646. .active-from-item {
  647. & > .el-form-item{
  648. background: $selectedColor;
  649. border-radius: 6px;
  650. }
  651. & > .drawing-item-copy, & > .drawing-item-delete{
  652. display: initial;
  653. }
  654. & > .component-name{
  655. color: $lighterBlue;
  656. }
  657. }
  658. .el-form-item{
  659. margin-bottom: 15px;
  660. }
  661. }
  662. .drawing-item{
  663. position: relative;
  664. cursor: move;
  665. &.unfocus-bordered:not(.activeFromItem) > div:first-child {
  666. border: 1px dashed #ccc;
  667. }
  668. .el-form-item{
  669. padding: 12px 10px;
  670. }
  671. }
  672. .drawing-row-item{
  673. position: relative;
  674. cursor: move;
  675. box-sizing: border-box;
  676. border: 1px dashed #ccc;
  677. border-radius: 3px;
  678. padding: 0 2px;
  679. margin-bottom: 15px;
  680. .drawing-row-item {
  681. margin-bottom: 2px;
  682. }
  683. .el-col{
  684. margin-top: 22px;
  685. }
  686. .el-form-item{
  687. margin-bottom: 0;
  688. }
  689. .drag-wrapper{
  690. min-height: 80px;
  691. }
  692. &.active-from-item{
  693. border: 1px dashed $lighterBlue;
  694. }
  695. .component-name{
  696. position: absolute;
  697. top: 0;
  698. left: 0;
  699. font-size: 12px;
  700. color: #bbb;
  701. display: inline-block;
  702. padding: 0 6px;
  703. }
  704. }
  705. .drawing-item, .drawing-row-item{
  706. &:hover {
  707. & > .el-form-item{
  708. background: $selectedColor;
  709. border-radius: 6px;
  710. }
  711. & > .drawing-item-copy, & > .drawing-item-delete{
  712. display: initial;
  713. }
  714. }
  715. & > .drawing-item-copy, & > .drawing-item-delete{
  716. display: none;
  717. position: absolute;
  718. top: -10px;
  719. width: 22px;
  720. height: 22px;
  721. line-height: 22px;
  722. text-align: center;
  723. border-radius: 50%;
  724. font-size: 12px;
  725. border: 1px solid;
  726. cursor: pointer;
  727. z-index: 1;
  728. }
  729. & > .drawing-item-copy{
  730. right: 56px;
  731. border-color: $lighterBlue;
  732. color: $lighterBlue;
  733. background: #fff;
  734. &:hover{
  735. background: $lighterBlue;
  736. color: #fff;
  737. }
  738. }
  739. & > .drawing-item-delete{
  740. right: 24px;
  741. border-color: #F56C6C;
  742. color: #F56C6C;
  743. background: #fff;
  744. &:hover{
  745. background: #F56C6C;
  746. color: #fff;
  747. }
  748. }
  749. }
  750. </style>