index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 class="components-draggable" :list="inputComponents"
  15. :group="{ name: 'componentsGroup', pull: 'clone', put: false }" :clone="cloneComponent"
  16. draggable=".components-item" :sort="false" @end="onEnd" item-key="label">
  17. <template #item="{ element, index }">
  18. <div :key="index" class="components-item" @click="addComponent(element)">
  19. <div class="components-body">
  20. <svg-icon :icon-class="element.tagIcon" />
  21. {{ element.label }}
  22. </div>
  23. </div>
  24. </template>
  25. </draggable>
  26. <div class="components-title">
  27. <svg-icon icon-class="component" />选择型组件
  28. </div>
  29. <draggable class="components-draggable" :list="selectComponents"
  30. :group="{ name: 'componentsGroup', pull: 'clone', put: false }" :clone="cloneComponent"
  31. draggable=".components-item" :sort="false" @end="onEnd" item-key="label">
  32. <template #item="{ element, index }">
  33. <div :key="index" class="components-item" @click="addComponent(element)">
  34. <div class="components-body">
  35. <svg-icon :icon-class="element.tagIcon" />
  36. {{ element.label }}
  37. </div>
  38. </div>
  39. </template>
  40. </draggable>
  41. <div class="components-title">
  42. <svg-icon icon-class="component" /> 布局型组件
  43. </div>
  44. <draggable class="components-draggable" :list="layoutComponents"
  45. :group="{ name: 'componentsGroup', pull: 'clone', put: false }" :clone="cloneComponent"
  46. draggable=".components-item" :sort="false" @end="onEnd" item-key="label">
  47. <template #item="{ element, index }">
  48. <div :key="index" class="components-item" @click="addComponent(element)">
  49. <div class="components-body">
  50. <svg-icon :icon-class="element.tagIcon" />
  51. {{ element.label }}
  52. </div>
  53. </div>
  54. </template>
  55. </draggable>
  56. </div>
  57. </el-scrollbar>
  58. </div>
  59. <div class="center-board">
  60. <div class="action-bar">
  61. <el-button icon="Download" type="primary" text @click="download">
  62. 导出vue文件
  63. </el-button>
  64. <el-button class="copy-btn-main" icon="DocumentCopy" type="primary" text @click="copy">
  65. 复制代码
  66. </el-button>
  67. <el-button class="delete-btn" icon="Delete" text @click="empty" type="danger">
  68. 清空
  69. </el-button>
  70. </div>
  71. <el-scrollbar class="center-scrollbar">
  72. <el-row class="center-board-row" :gutter="formConf.gutter">
  73. <el-form :size="formConf.size" :label-position="formConf.labelPosition" :disabled="formConf.disabled"
  74. :label-width="formConf.labelWidth + 'px'">
  75. <draggable class="drawing-board" :list="drawingList" :animation="340" group="componentsGroup"
  76. item-key="label">
  77. <template #item="{ element, index }">
  78. <draggable-item :key="element.renderKey" :drawing-list="drawingList" :element="element" :index="index"
  79. :active-id="activeId" :form-conf="formConf" @activeItem="activeFormItem" @copyItem="drawingItemCopy"
  80. @deleteItem="drawingItemDelete" />
  81. </template>
  82. </draggable>
  83. <div v-show="!drawingList.length" class="empty-info">
  84. 从左侧拖入或点选组件进行表单设计
  85. </div>
  86. </el-form>
  87. </el-row>
  88. </el-scrollbar>
  89. </div>
  90. <right-panel :active-data="activeData" :form-conf="formConf" :show-field="!!drawingList.length"
  91. @tag-change="tagChange" />
  92. <code-type-dialog v-model="dialogVisible" title="选择生成类型" :showFileName="showFileName" @confirm="generate" />
  93. <input id="copyNode" type="hidden">
  94. </div>
  95. </template>
  96. <script setup>
  97. import draggable from "vuedraggable/dist/vuedraggable.common"
  98. import ClipboardJS from 'clipboard'
  99. import beautifier from 'js-beautify'
  100. import logo from '@/assets/logo/logo.png'
  101. import { inputComponents, selectComponents, layoutComponents, formConf as formConfData } from '@/utils/generator/config'
  102. import { beautifierConf } from '@/utils/index'
  103. import drawingDefalut from '@/utils/generator/drawingDefalut'
  104. import { makeUpHtml, vueTemplate, vueScript, cssStyle } from '@/utils/generator/html'
  105. import { makeUpJs } from '@/utils/generator/js'
  106. import { makeUpCss } from '@/utils/generator/css'
  107. import Download from '@/plugins/download'
  108. import { ElNotification } from 'element-plus'
  109. import DraggableItem from './DraggableItem'
  110. import RightPanel from './RightPanel'
  111. import CodeTypeDialog from './CodeTypeDialog'
  112. import { onMounted, watch } from 'vue'
  113. const drawingList = ref(drawingDefalut)
  114. const { proxy } = getCurrentInstance()
  115. const dialogVisible = ref(false)
  116. const showFileName = ref(false)
  117. const operationType = ref('')
  118. const idGlobal = ref(100)
  119. const activeData = ref(drawingDefalut[0])
  120. const activeId = ref(drawingDefalut[0].formId)
  121. const generateConf = ref(null)
  122. const formData = ref({})
  123. const formConf = ref(formConfData)
  124. let oldActiveId
  125. let tempActiveData
  126. function activeFormItem(element) {
  127. activeData.value = element
  128. activeId.value = element.formId
  129. }
  130. function copy() {
  131. dialogVisible.value = true
  132. showFileName.value = false
  133. operationType.value = 'copy'
  134. }
  135. function download() {
  136. dialogVisible.value = true
  137. showFileName.value = true
  138. operationType.value = 'download'
  139. }
  140. function empty() {
  141. proxy.$modal.confirm('确定要清空所有组件吗?', '提示', { type: 'warning' }).then(() => {
  142. idGlobal.value = 100
  143. drawingList.value = []
  144. }
  145. )
  146. }
  147. function onEnd(obj, a) {
  148. if (obj.from !== obj.to) {
  149. activeData.value = tempActiveData
  150. activeId.value = idGlobal.value
  151. }
  152. }
  153. function addComponent(item) {
  154. const clone = cloneComponent(item)
  155. drawingList.value.push(clone)
  156. activeFormItem(clone)
  157. }
  158. function cloneComponent(origin) {
  159. const clone = JSON.parse(JSON.stringify(origin))
  160. clone.formId = ++idGlobal.value
  161. clone.span = formConf.value.span
  162. clone.renderKey = +new Date() // 改变renderKey后可以实现强制更新组件
  163. if (!clone.layout) clone.layout = 'colFormItem'
  164. if (clone.layout === 'colFormItem') {
  165. clone.vModel = `field${idGlobal.value}`
  166. clone.placeholder !== undefined && (clone.placeholder += clone.label)
  167. tempActiveData = clone
  168. } else if (clone.layout === 'rowFormItem') {
  169. delete clone.label
  170. clone.componentName = `row${idGlobal.value}`
  171. clone.gutter = formConf.value.gutter
  172. tempActiveData = clone
  173. }
  174. return tempActiveData
  175. }
  176. function drawingItemCopy(item, parent) {
  177. let clone = JSON.parse(JSON.stringify(item))
  178. clone = createIdAndKey(clone)
  179. parent.push(clone)
  180. activeFormItem(clone)
  181. }
  182. function createIdAndKey(item) {
  183. item.formId = ++idGlobal.value
  184. item.renderKey = +new Date()
  185. if (item.layout === 'colFormItem') {
  186. item.vModel = `field${idGlobal.value}`
  187. } else if (item.layout === 'rowFormItem') {
  188. item.componentName = `row${idGlobal.value}`
  189. }
  190. if (Array.isArray(item.children)) {
  191. item.children = item.children.map(childItem => createIdAndKey(childItem))
  192. }
  193. return item
  194. }
  195. function drawingItemDelete(index, parent) {
  196. parent.splice(index, 1)
  197. nextTick(() => {
  198. const len = drawingList.value.length
  199. if (len) {
  200. activeFormItem(drawingList.value[len - 1])
  201. }
  202. })
  203. }
  204. function tagChange(newTag) {
  205. newTag = cloneComponent(newTag)
  206. newTag.vModel = activeData.value.vModel
  207. newTag.formId = activeId.value
  208. newTag.span = activeData.value.span
  209. delete activeData.value.tag
  210. delete activeData.value.tagIcon
  211. delete activeData.value.document
  212. Object.keys(newTag).forEach(key => {
  213. if (activeData.value[key] !== undefined
  214. && typeof activeData.value[key] === typeof newTag[key]) {
  215. newTag[key] = activeData.value[key]
  216. }
  217. })
  218. activeData.value = newTag
  219. updateDrawingList(newTag, drawingList.value)
  220. }
  221. function updateDrawingList(newTag, list) {
  222. const index = list.findIndex(item => item.formId === activeId.value)
  223. if (index > -1) {
  224. list.splice(index, 1, newTag)
  225. } else {
  226. list.forEach(item => {
  227. if (Array.isArray(item.children)) updateDrawingList(newTag, item.children)
  228. })
  229. }
  230. }
  231. function generate(data) {
  232. generateConf.value = data
  233. nextTick(() => {
  234. switch (operationType.value) {
  235. case 'copy':
  236. execCopy(data)
  237. break
  238. case 'download':
  239. execDownload(data)
  240. break
  241. default:
  242. break
  243. }
  244. })
  245. }
  246. function execDownload(data) {
  247. const codeStr = generateCode()
  248. const blob = new Blob([codeStr], { type: 'text/plain;charset=utf-8' })
  249. Download.saveAs(blob, data.fileName)
  250. }
  251. function execCopy(data) {
  252. document.getElementById('copyNode').click()
  253. }
  254. function AssembleFormData() {
  255. formData.value = { fields: JSON.parse(JSON.stringify(drawingList.value)), ...formConf.value }
  256. }
  257. function generateCode() {
  258. const { type } = generateConf.value
  259. AssembleFormData()
  260. const script = vueScript(makeUpJs(formData.value, type))
  261. const html = vueTemplate(makeUpHtml(formData.value, type))
  262. const css = cssStyle(makeUpCss(formData.value))
  263. return beautifier.html(html + script + css, beautifierConf.html)
  264. }
  265. watch(() => activeData.value.label, (val, oldVal) => {
  266. if (
  267. activeData.value.placeholder === undefined
  268. || !activeData.value.tag
  269. || oldActiveId !== activeId.value
  270. ) {
  271. return
  272. }
  273. activeData.value.placeholder = activeData.value.placeholder.replace(oldVal, '') + val
  274. })
  275. watch(activeId, (val) => {
  276. oldActiveId = val
  277. }, { immediate: true })
  278. onMounted(() => {
  279. const clipboard = new ClipboardJS('#copyNode', {
  280. text: trigger => {
  281. const codeStr = generateCode()
  282. ElNotification({ title: '成功', message: '代码已复制到剪切板,可粘贴。', type: 'success' })
  283. return codeStr
  284. }
  285. })
  286. clipboard.on('error', e => {
  287. proxy.$modal.msgError('代码复制失败')
  288. })
  289. })
  290. </script>
  291. <style lang='scss'>
  292. $lighterBlue: #409EFF;
  293. .container {
  294. position: relative;
  295. width: 100%;
  296. background-color: var(--el-bg-color-overlay);
  297. height: calc(100vh - 50px - 40px);
  298. overflow: hidden;
  299. .left-board {
  300. width: 260px;
  301. position: absolute;
  302. left: 0;
  303. top: 0;
  304. height: calc(100vh - 50px - 40px);
  305. .logo-wrapper {
  306. position: relative;
  307. height: 42px;
  308. border-bottom: 1px solid var(--el-border-color-extra-light);
  309. box-sizing: border-box;
  310. .logo {
  311. position: absolute;
  312. left: 12px;
  313. top: 6px;
  314. line-height: 30px;
  315. color: #00afff;
  316. font-weight: 600;
  317. font-size: 17px;
  318. white-space: nowrap;
  319. >img {
  320. width: 30px;
  321. height: 30px;
  322. vertical-align: top;
  323. }
  324. .github {
  325. display: inline-block;
  326. vertical-align: sub;
  327. margin-left: 15px;
  328. >img {
  329. height: 22px;
  330. }
  331. }
  332. }
  333. }
  334. .left-scrollbar {
  335. .el-scrollbar__wrap {
  336. box-sizing: border-box;
  337. overflow-x: hidden !important;
  338. margin-bottom: 0 !important;
  339. .components-list {
  340. padding: 8px;
  341. box-sizing: border-box;
  342. height: 100%;
  343. .components-title {
  344. font-size: 14px;
  345. // color: #222;
  346. margin: 6px 2px;
  347. .svg-icon {
  348. // color: #666;
  349. font-size: 18px;
  350. margin-right: 5px;
  351. }
  352. }
  353. .components-draggable {
  354. padding-bottom: 20px;
  355. .components-item {
  356. display: inline-block;
  357. width: 48%;
  358. margin: 1%;
  359. transition: transform 0ms !important;
  360. .components-body {
  361. padding: 8px 10px;
  362. background: var(--el-border-color-extra-light);
  363. font-size: 12px;
  364. cursor: move;
  365. border: 1px dashed var(--el-border-color-extra-light);
  366. border-radius: 3px;
  367. .svg-icon {
  368. // color: #777;
  369. font-size: 15px;
  370. margin-right: 5px;
  371. }
  372. &:hover {
  373. border: 1px dashed #787be8;
  374. color: #787be8;
  375. .svg-icon {
  376. color: #787be8;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. .center-board {
  387. height: calc(100vh - 50px - 40px);
  388. width: auto;
  389. margin: 0 350px 0 260px;
  390. box-sizing: border-box;
  391. .action-bar {
  392. position: relative;
  393. height: 42px;
  394. padding: 0 15px;
  395. box-sizing: border-box;
  396. ;
  397. border: 1px solid var(--el-border-color-extra-light);
  398. border-top: none;
  399. border-left: none;
  400. display: flex;
  401. align-items: center;
  402. justify-content: flex-end;
  403. u .delete-btn {
  404. color: #F56C6C;
  405. }
  406. }
  407. .center-scrollbar {
  408. height: calc(100vh - 50px - 40px - 42px);
  409. overflow: hidden;
  410. border-left: 1px solid var(--el-border-color-extra-light);
  411. border-right: 1px solid var(--el-border-color-extra-light);
  412. box-sizing: border-box;
  413. .el-scrollbar__view {
  414. overflow-x: hidden;
  415. }
  416. .center-board-row {
  417. padding: 12px 12px 15px 12px;
  418. box-sizing: border-box;
  419. &>.el-form {
  420. // 69 = 12+15+42
  421. height: calc(100vh - 50px - 40px - 69px);
  422. flex: 1;
  423. .drawing-board {
  424. height: 100%;
  425. position: relative;
  426. .components-body {
  427. padding: 0;
  428. margin: 0;
  429. font-size: 0;
  430. }
  431. .sortable-ghost {
  432. position: relative;
  433. display: block;
  434. overflow: hidden;
  435. &::before {
  436. content: " ";
  437. position: absolute;
  438. left: 0;
  439. right: 0;
  440. top: 0;
  441. height: 3px;
  442. background: rgb(89, 89, 223);
  443. z-index: 2;
  444. }
  445. }
  446. .components-item.sortable-ghost {
  447. width: 100%;
  448. height: 60px;
  449. background: var(--el-border-color-extra-light);
  450. }
  451. .active-from-item {
  452. &>.el-form-item {
  453. background: var(--el-border-color-extra-light);
  454. border-radius: 6px;
  455. }
  456. &>.drawing-item-copy,
  457. &>.drawing-item-delete {
  458. display: initial;
  459. }
  460. &>.component-name {
  461. color: $lighterBlue;
  462. }
  463. .el-input__wrapper {
  464. box-shadow: 0 0 0 1px var(--el-input-hover-border-color) inset;
  465. }
  466. }
  467. .el-form-item {
  468. margin-bottom: 15px;
  469. }
  470. }
  471. .drawing-item {
  472. position: relative;
  473. cursor: move;
  474. &.unfocus-bordered:not(.activeFromItem)>div:first-child {
  475. border: 1px dashed #ccc;
  476. }
  477. .el-form-item {
  478. padding: 12px 10px;
  479. }
  480. }
  481. .drawing-row-item {
  482. position: relative;
  483. cursor: move;
  484. box-sizing: border-box;
  485. border: 1px dashed #ccc;
  486. border-radius: 3px;
  487. padding: 0 2px;
  488. margin-bottom: 15px;
  489. .drawing-row-item {
  490. margin-bottom: 2px;
  491. }
  492. .el-col {
  493. margin-top: 22px;
  494. }
  495. .el-form-item {
  496. margin-bottom: 0;
  497. }
  498. .drag-wrapper {
  499. min-height: 80px;
  500. flex: 1;
  501. display: flex;
  502. flex-wrap: wrap;
  503. }
  504. &.active-from-item {
  505. border: 1px dashed $lighterBlue;
  506. }
  507. .component-name {
  508. position: absolute;
  509. top: 0;
  510. left: 0;
  511. font-size: 12px;
  512. color: #bbb;
  513. display: inline-block;
  514. padding: 0 6px;
  515. }
  516. }
  517. .drawing-item,
  518. .drawing-row-item {
  519. &:hover {
  520. &>.el-form-item {
  521. background: var(--el-border-color-extra-light);
  522. border-radius: 6px;
  523. }
  524. &>.drawing-item-copy,
  525. &>.drawing-item-delete {
  526. display: initial;
  527. }
  528. }
  529. &>.drawing-item-copy,
  530. &>.drawing-item-delete {
  531. display: none;
  532. position: absolute;
  533. top: -10px;
  534. width: 22px;
  535. height: 22px;
  536. line-height: 22px;
  537. text-align: center;
  538. border-radius: 50%;
  539. font-size: 12px;
  540. border: 1px solid;
  541. cursor: pointer;
  542. z-index: 1;
  543. }
  544. &>.drawing-item-copy {
  545. right: 56px;
  546. border-color: $lighterBlue;
  547. color: $lighterBlue;
  548. background: #fff;
  549. &:hover {
  550. background: $lighterBlue;
  551. color: #fff;
  552. }
  553. }
  554. &>.drawing-item-delete {
  555. right: 24px;
  556. border-color: #F56C6C;
  557. color: #F56C6C;
  558. background: #fff;
  559. &:hover {
  560. background: #F56C6C;
  561. color: #fff;
  562. }
  563. }
  564. }
  565. .empty-info {
  566. position: absolute;
  567. top: 46%;
  568. left: 0;
  569. right: 0;
  570. text-align: center;
  571. font-size: 18px;
  572. color: #ccb1ea;
  573. letter-spacing: 4px;
  574. }
  575. }
  576. }
  577. }
  578. }
  579. }
  580. </style>