index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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.png'
  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. // eslint-disable-next-line func-names
  188. 'activeData.label': function (val, oldVal) {
  189. if (
  190. this.activeData.placeholder === undefined
  191. || !this.activeData.tag
  192. || oldActiveId !== this.activeId
  193. ) {
  194. return
  195. }
  196. this.activeData.placeholder = this.activeData.placeholder.replace(oldVal, '') + val
  197. },
  198. activeId: {
  199. handler(val) {
  200. oldActiveId = val
  201. },
  202. immediate: true
  203. }
  204. },
  205. mounted() {
  206. const clipboard = new ClipboardJS('#copyNode', {
  207. text: trigger => {
  208. const codeStr = this.generateCode()
  209. this.$notify({
  210. title: '成功',
  211. message: '代码已复制到剪切板,可粘贴。',
  212. type: 'success'
  213. })
  214. return codeStr
  215. }
  216. })
  217. clipboard.on('error', e => {
  218. this.$message.error('代码复制失败')
  219. })
  220. },
  221. methods: {
  222. activeFormItem(element) {
  223. this.activeData = element
  224. this.activeId = element.formId
  225. },
  226. onEnd(obj, a) {
  227. if (obj.from !== obj.to) {
  228. this.activeData = tempActiveData
  229. this.activeId = this.idGlobal
  230. }
  231. },
  232. addComponent(item) {
  233. const clone = this.cloneComponent(item)
  234. this.drawingList.push(clone)
  235. this.activeFormItem(clone)
  236. },
  237. cloneComponent(origin) {
  238. const clone = JSON.parse(JSON.stringify(origin))
  239. clone.formId = ++this.idGlobal
  240. clone.span = formConf.span
  241. clone.renderKey = +new Date() // 改变renderKey后可以实现强制更新组件
  242. if (!clone.layout) clone.layout = 'colFormItem'
  243. if (clone.layout === 'colFormItem') {
  244. clone.vModel = `field${this.idGlobal}`
  245. clone.placeholder !== undefined && (clone.placeholder += clone.label)
  246. tempActiveData = clone
  247. } else if (clone.layout === 'rowFormItem') {
  248. delete clone.label
  249. clone.componentName = `row${this.idGlobal}`
  250. clone.gutter = this.formConf.gutter
  251. tempActiveData = clone
  252. }
  253. return tempActiveData
  254. },
  255. AssembleFormData() {
  256. this.formData = {
  257. fields: JSON.parse(JSON.stringify(this.drawingList)),
  258. ...this.formConf
  259. }
  260. },
  261. generate(data) {
  262. const func = this[`exec${titleCase(this.operationType)}`]
  263. this.generateConf = data
  264. func && func(data)
  265. },
  266. execRun(data) {
  267. this.AssembleFormData()
  268. this.drawerVisible = true
  269. },
  270. execDownload(data) {
  271. const codeStr = this.generateCode()
  272. const blob = new Blob([codeStr], { type: 'text/plain;charset=utf-8' })
  273. this.$download.saveAs(blob, data.fileName)
  274. },
  275. execCopy(data) {
  276. document.getElementById('copyNode').click()
  277. },
  278. empty() {
  279. this.$confirm('确定要清空所有组件吗?', '提示', { type: 'warning' }).then(
  280. () => {
  281. this.drawingList = []
  282. }
  283. )
  284. },
  285. drawingItemCopy(item, parent) {
  286. let clone = JSON.parse(JSON.stringify(item))
  287. clone = this.createIdAndKey(clone)
  288. parent.push(clone)
  289. this.activeFormItem(clone)
  290. },
  291. createIdAndKey(item) {
  292. item.formId = ++this.idGlobal
  293. item.renderKey = +new Date()
  294. if (item.layout === 'colFormItem') {
  295. item.vModel = `field${this.idGlobal}`
  296. } else if (item.layout === 'rowFormItem') {
  297. item.componentName = `row${this.idGlobal}`
  298. }
  299. if (Array.isArray(item.children)) {
  300. item.children = item.children.map(childItem => this.createIdAndKey(childItem))
  301. }
  302. return item
  303. },
  304. drawingItemDelete(index, parent) {
  305. parent.splice(index, 1)
  306. this.$nextTick(() => {
  307. const len = this.drawingList.length
  308. if (len) {
  309. this.activeFormItem(this.drawingList[len - 1])
  310. }
  311. })
  312. },
  313. generateCode() {
  314. const { type } = this.generateConf
  315. this.AssembleFormData()
  316. const script = vueScript(makeUpJs(this.formData, type))
  317. const html = vueTemplate(makeUpHtml(this.formData, type))
  318. const css = cssStyle(makeUpCss(this.formData))
  319. return beautifier.html(html + script + css, beautifierConf.html)
  320. },
  321. download() {
  322. this.dialogVisible = true
  323. this.showFileName = true
  324. this.operationType = 'download'
  325. },
  326. run() {
  327. this.dialogVisible = true
  328. this.showFileName = false
  329. this.operationType = 'run'
  330. },
  331. copy() {
  332. this.dialogVisible = true
  333. this.showFileName = false
  334. this.operationType = 'copy'
  335. },
  336. tagChange(newTag) {
  337. newTag = this.cloneComponent(newTag)
  338. newTag.vModel = this.activeData.vModel
  339. newTag.formId = this.activeId
  340. newTag.span = this.activeData.span
  341. delete this.activeData.tag
  342. delete this.activeData.tagIcon
  343. delete this.activeData.document
  344. Object.keys(newTag).forEach(key => {
  345. if (this.activeData[key] !== undefined
  346. && typeof this.activeData[key] === typeof newTag[key]) {
  347. newTag[key] = this.activeData[key]
  348. }
  349. })
  350. this.activeData = newTag
  351. this.updateDrawingList(newTag, this.drawingList)
  352. },
  353. updateDrawingList(newTag, list) {
  354. const index = list.findIndex(item => item.formId === this.activeId)
  355. if (index > -1) {
  356. list.splice(index, 1, newTag)
  357. } else {
  358. list.forEach(item => {
  359. if (Array.isArray(item.children)) this.updateDrawingList(newTag, item.children)
  360. })
  361. }
  362. }
  363. }
  364. }
  365. </script>
  366. <style lang='scss'>
  367. .editor-tabs{
  368. background: #121315;
  369. .el-tabs__header{
  370. margin: 0;
  371. border-bottom-color: #121315;
  372. .el-tabs__nav{
  373. border-color: #121315;
  374. }
  375. }
  376. .el-tabs__item{
  377. height: 32px;
  378. line-height: 32px;
  379. color: #888a8e;
  380. border-left: 1px solid #121315 !important;
  381. background: #363636;
  382. margin-right: 5px;
  383. user-select: none;
  384. }
  385. .el-tabs__item.is-active{
  386. background: #1e1e1e;
  387. border-bottom-color: #1e1e1e!important;
  388. color: #fff;
  389. }
  390. .el-icon-edit{
  391. color: #f1fa8c;
  392. }
  393. .el-icon-document{
  394. color: #a95812;
  395. }
  396. }
  397. // home
  398. .right-scrollbar {
  399. .el-scrollbar__view {
  400. padding: 12px 18px 15px 15px;
  401. }
  402. }
  403. .left-scrollbar .el-scrollbar__wrap {
  404. box-sizing: border-box;
  405. overflow-x: hidden !important;
  406. margin-bottom: 0 !important;
  407. }
  408. .center-tabs{
  409. .el-tabs__header{
  410. margin-bottom: 0!important;
  411. }
  412. .el-tabs__item{
  413. width: 50%;
  414. text-align: center;
  415. }
  416. .el-tabs__nav{
  417. width: 100%;
  418. }
  419. }
  420. .reg-item{
  421. padding: 12px 6px;
  422. background: #f8f8f8;
  423. position: relative;
  424. border-radius: 4px;
  425. .close-btn{
  426. position: absolute;
  427. right: -6px;
  428. top: -6px;
  429. display: block;
  430. width: 16px;
  431. height: 16px;
  432. line-height: 16px;
  433. background: rgba(0, 0, 0, 0.2);
  434. border-radius: 50%;
  435. color: #fff;
  436. text-align: center;
  437. z-index: 1;
  438. cursor: pointer;
  439. font-size: 12px;
  440. &:hover{
  441. background: rgba(210, 23, 23, 0.5)
  442. }
  443. }
  444. & + .reg-item{
  445. margin-top: 18px;
  446. }
  447. }
  448. .action-bar{
  449. & .el-button+.el-button {
  450. margin-left: 15px;
  451. }
  452. & i {
  453. font-size: 20px;
  454. vertical-align: middle;
  455. position: relative;
  456. top: -1px;
  457. }
  458. }
  459. .custom-tree-node{
  460. width: 100%;
  461. font-size: 14px;
  462. .node-operation{
  463. float: right;
  464. }
  465. i[class*="el-icon"] + i[class*="el-icon"]{
  466. margin-left: 6px;
  467. }
  468. .el-icon-plus{
  469. color: #409EFF;
  470. }
  471. .el-icon-delete{
  472. color: #157a0c;
  473. }
  474. }
  475. .left-scrollbar .el-scrollbar__view{
  476. overflow-x: hidden;
  477. }
  478. .el-rate{
  479. display: inline-block;
  480. vertical-align: text-top;
  481. }
  482. .el-upload__tip{
  483. line-height: 1.2;
  484. }
  485. $selectedColor: #f6f7ff;
  486. $lighterBlue: #409EFF;
  487. .container {
  488. position: relative;
  489. width: 100%;
  490. height: 100%;
  491. }
  492. .components-list {
  493. padding: 8px;
  494. box-sizing: border-box;
  495. height: 100%;
  496. .components-item {
  497. display: inline-block;
  498. width: 48%;
  499. margin: 1%;
  500. transition: transform 0ms !important;
  501. }
  502. }
  503. .components-draggable{
  504. padding-bottom: 20px;
  505. }
  506. .components-title{
  507. font-size: 14px;
  508. color: #222;
  509. margin: 6px 2px;
  510. .svg-icon{
  511. color: #666;
  512. font-size: 18px;
  513. }
  514. }
  515. .components-body {
  516. padding: 8px 10px;
  517. background: $selectedColor;
  518. font-size: 12px;
  519. cursor: move;
  520. border: 1px dashed $selectedColor;
  521. border-radius: 3px;
  522. .svg-icon{
  523. color: #777;
  524. font-size: 15px;
  525. }
  526. &:hover {
  527. border: 1px dashed #787be8;
  528. color: #787be8;
  529. .svg-icon {
  530. color: #787be8;
  531. }
  532. }
  533. }
  534. .left-board {
  535. width: 260px;
  536. position: absolute;
  537. left: 0;
  538. top: 0;
  539. height: 100vh;
  540. }
  541. .left-scrollbar{
  542. height: calc(100vh - 42px);
  543. overflow: hidden;
  544. }
  545. .center-scrollbar {
  546. height: calc(100vh - 42px);
  547. overflow: hidden;
  548. border-left: 1px solid #f1e8e8;
  549. border-right: 1px solid #f1e8e8;
  550. box-sizing: border-box;
  551. }
  552. .center-board {
  553. height: 100vh;
  554. width: auto;
  555. margin: 0 350px 0 260px;
  556. box-sizing: border-box;
  557. }
  558. .empty-info{
  559. position: absolute;
  560. top: 46%;
  561. left: 0;
  562. right: 0;
  563. text-align: center;
  564. font-size: 18px;
  565. color: #ccb1ea;
  566. letter-spacing: 4px;
  567. }
  568. .action-bar{
  569. position: relative;
  570. height: 42px;
  571. text-align: right;
  572. padding: 0 15px;
  573. box-sizing: border-box;;
  574. border: 1px solid #f1e8e8;
  575. border-top: none;
  576. border-left: none;
  577. .delete-btn{
  578. color: #F56C6C;
  579. }
  580. }
  581. .logo-wrapper{
  582. position: relative;
  583. height: 42px;
  584. background: #fff;
  585. border-bottom: 1px solid #f1e8e8;
  586. box-sizing: border-box;
  587. }
  588. .logo{
  589. position: absolute;
  590. left: 12px;
  591. top: 6px;
  592. line-height: 30px;
  593. color: #00afff;
  594. font-weight: 600;
  595. font-size: 17px;
  596. white-space: nowrap;
  597. > img{
  598. width: 30px;
  599. height: 30px;
  600. vertical-align: top;
  601. }
  602. .github{
  603. display: inline-block;
  604. vertical-align: sub;
  605. margin-left: 15px;
  606. > img{
  607. height: 22px;
  608. }
  609. }
  610. }
  611. .center-board-row {
  612. padding: 12px 12px 15px 12px;
  613. box-sizing: border-box;
  614. & > .el-form {
  615. // 69 = 12+15+42
  616. height: calc(100vh - 69px);
  617. }
  618. }
  619. .drawing-board {
  620. height: 100%;
  621. position: relative;
  622. .components-body {
  623. padding: 0;
  624. margin: 0;
  625. font-size: 0;
  626. }
  627. .sortable-ghost {
  628. position: relative;
  629. display: block;
  630. overflow: hidden;
  631. &::before {
  632. content: " ";
  633. position: absolute;
  634. left: 0;
  635. right: 0;
  636. top: 0;
  637. height: 3px;
  638. background: rgb(89, 89, 223);
  639. z-index: 2;
  640. }
  641. }
  642. .components-item.sortable-ghost {
  643. width: 100%;
  644. height: 60px;
  645. background-color: $selectedColor;
  646. }
  647. .active-from-item {
  648. & > .el-form-item{
  649. background: $selectedColor;
  650. border-radius: 6px;
  651. }
  652. & > .drawing-item-copy, & > .drawing-item-delete{
  653. display: initial;
  654. }
  655. & > .component-name{
  656. color: $lighterBlue;
  657. }
  658. }
  659. .el-form-item{
  660. margin-bottom: 15px;
  661. }
  662. }
  663. .drawing-item{
  664. position: relative;
  665. cursor: move;
  666. &.unfocus-bordered:not(.activeFromItem) > div:first-child {
  667. border: 1px dashed #ccc;
  668. }
  669. .el-form-item{
  670. padding: 12px 10px;
  671. }
  672. }
  673. .drawing-row-item{
  674. position: relative;
  675. cursor: move;
  676. box-sizing: border-box;
  677. border: 1px dashed #ccc;
  678. border-radius: 3px;
  679. padding: 0 2px;
  680. margin-bottom: 15px;
  681. .drawing-row-item {
  682. margin-bottom: 2px;
  683. }
  684. .el-col{
  685. margin-top: 22px;
  686. }
  687. .el-form-item{
  688. margin-bottom: 0;
  689. }
  690. .drag-wrapper{
  691. min-height: 80px;
  692. }
  693. &.active-from-item{
  694. border: 1px dashed $lighterBlue;
  695. }
  696. .component-name{
  697. position: absolute;
  698. top: 0;
  699. left: 0;
  700. font-size: 12px;
  701. color: #bbb;
  702. display: inline-block;
  703. padding: 0 6px;
  704. }
  705. }
  706. .drawing-item, .drawing-row-item{
  707. &:hover {
  708. & > .el-form-item{
  709. background: $selectedColor;
  710. border-radius: 6px;
  711. }
  712. & > .drawing-item-copy, & > .drawing-item-delete{
  713. display: initial;
  714. }
  715. }
  716. & > .drawing-item-copy, & > .drawing-item-delete{
  717. display: none;
  718. position: absolute;
  719. top: -10px;
  720. width: 22px;
  721. height: 22px;
  722. line-height: 22px;
  723. text-align: center;
  724. border-radius: 50%;
  725. font-size: 12px;
  726. border: 1px solid;
  727. cursor: pointer;
  728. z-index: 1;
  729. }
  730. & > .drawing-item-copy{
  731. right: 56px;
  732. border-color: $lighterBlue;
  733. color: $lighterBlue;
  734. background: #fff;
  735. &:hover{
  736. background: $lighterBlue;
  737. color: #fff;
  738. }
  739. }
  740. & > .drawing-item-delete{
  741. right: 24px;
  742. border-color: #F56C6C;
  743. color: #F56C6C;
  744. background: #fff;
  745. &:hover{
  746. background: #F56C6C;
  747. color: #fff;
  748. }
  749. }
  750. }
  751. </style>