index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. () => {
  143. idGlobal.value = 100
  144. drawingList.value = []
  145. }
  146. )
  147. }
  148. function onEnd(obj, a) {
  149. if (obj.from !== obj.to) {
  150. activeData.value = tempActiveData
  151. activeId.value = idGlobal.value
  152. }
  153. }
  154. function addComponent(item) {
  155. const clone = cloneComponent(item)
  156. drawingList.value.push(clone)
  157. activeFormItem(clone)
  158. }
  159. function cloneComponent(origin) {
  160. const clone = JSON.parse(JSON.stringify(origin))
  161. clone.formId = ++idGlobal.value
  162. clone.span = formConf.value.span
  163. clone.renderKey = +new Date() // 改变renderKey后可以实现强制更新组件
  164. if (!clone.layout) clone.layout = 'colFormItem'
  165. if (clone.layout === 'colFormItem') {
  166. clone.vModel = `field${idGlobal.value}`
  167. clone.placeholder !== undefined && (clone.placeholder += clone.label)
  168. tempActiveData = clone
  169. } else if (clone.layout === 'rowFormItem') {
  170. delete clone.label
  171. clone.componentName = `row${idGlobal.value}`
  172. clone.gutter = formConf.value.gutter
  173. tempActiveData = clone
  174. }
  175. return tempActiveData
  176. }
  177. function drawingItemCopy(item, parent) {
  178. let clone = JSON.parse(JSON.stringify(item))
  179. clone = createIdAndKey(clone)
  180. parent.push(clone)
  181. activeFormItem(clone)
  182. }
  183. function createIdAndKey(item) {
  184. item.formId = ++idGlobal.value
  185. item.renderKey = +new Date()
  186. if (item.layout === 'colFormItem') {
  187. item.vModel = `field${idGlobal.value}`
  188. } else if (item.layout === 'rowFormItem') {
  189. item.componentName = `row${idGlobal.value}`
  190. }
  191. if (Array.isArray(item.children)) {
  192. item.children = item.children.map(childItem => createIdAndKey(childItem))
  193. }
  194. return item
  195. }
  196. function drawingItemDelete(index, parent) {
  197. parent.splice(index, 1)
  198. nextTick(() => {
  199. const len = drawingList.value.length
  200. if (len) {
  201. activeFormItem(drawingList.value[len - 1])
  202. }
  203. })
  204. }
  205. function tagChange(newTag) {
  206. newTag = cloneComponent(newTag)
  207. newTag.vModel = activeData.value.vModel
  208. newTag.formId = activeId.value
  209. newTag.span = activeData.value.span
  210. delete activeData.value.tag
  211. delete activeData.value.tagIcon
  212. delete activeData.value.document
  213. Object.keys(newTag).forEach(key => {
  214. if (activeData.value[key] !== undefined
  215. && typeof activeData.value[key] === typeof newTag[key]) {
  216. newTag[key] = activeData.value[key]
  217. }
  218. })
  219. activeData.value = newTag
  220. updateDrawingList(newTag, drawingList.value)
  221. }
  222. function updateDrawingList(newTag, list) {
  223. const index = list.findIndex(item => item.formId === activeId.value)
  224. if (index > -1) {
  225. list.splice(index, 1, newTag)
  226. } else {
  227. list.forEach(item => {
  228. if (Array.isArray(item.children)) updateDrawingList(newTag, item.children)
  229. })
  230. }
  231. }
  232. function generate(data) {
  233. generateConf.value = data
  234. nextTick(() => {
  235. switch (operationType.value) {
  236. case 'copy':
  237. execCopy(data)
  238. break;
  239. case 'download':
  240. execDownload(data)
  241. break;
  242. default:
  243. break;
  244. }
  245. })
  246. }
  247. function execDownload(data) {
  248. const codeStr = generateCode()
  249. const blob = new Blob([codeStr], { type: 'text/plain;charset=utf-8' })
  250. Download.saveAs(blob, data.fileName)
  251. }
  252. function execCopy(data) {
  253. document.getElementById('copyNode').click()
  254. }
  255. function AssembleFormData() {
  256. formData.value = {
  257. fields: JSON.parse(JSON.stringify(drawingList.value)),
  258. ...formConf.value
  259. }
  260. }
  261. function generateCode() {
  262. const { type } = generateConf.value
  263. AssembleFormData()
  264. const script = vueScript(makeUpJs(formData.value, type))
  265. const html = vueTemplate(makeUpHtml(formData.value, type))
  266. const css = cssStyle(makeUpCss(formData.value))
  267. return beautifier.html(html + script + css, beautifierConf.html)
  268. }
  269. watch(() => activeData.value.label, (val, oldVal) => {
  270. if (
  271. activeData.value.placeholder === undefined
  272. || !activeData.value.tag
  273. || oldActiveId !== activeId.value
  274. ) {
  275. return
  276. }
  277. activeData.value.placeholder = activeData.value.placeholder.replace(oldVal, '') + val
  278. })
  279. watch(activeId, (val) => {
  280. oldActiveId = val
  281. }, {
  282. immediate: true
  283. })
  284. onMounted(() => {
  285. const clipboard = new ClipboardJS('#copyNode', {
  286. text: trigger => {
  287. const codeStr = generateCode()
  288. ElNotification({
  289. title: '成功',
  290. message: '代码已复制到剪切板,可粘贴。',
  291. type: 'success'
  292. })
  293. return codeStr
  294. }
  295. })
  296. clipboard.on('error', e => {
  297. ElMessage.error('代码复制失败')
  298. })
  299. })
  300. </script>
  301. <style lang='scss'>
  302. // body,
  303. // html {
  304. // margin: 0;
  305. // padding: 0;
  306. // background: #fff;
  307. // -moz-osx-font-smoothing: grayscale;
  308. // -webkit-font-smoothing: antialiased;
  309. // text-rendering: optimizeLegibility;
  310. // font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
  311. // }
  312. // input,
  313. // textarea {
  314. // font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
  315. // }
  316. $lighterBlue: #409EFF;
  317. .container {
  318. position: relative;
  319. width: 100%;
  320. background-color: var(--el-bg-color-overlay);
  321. height: calc(100vh - 50px - 40px);
  322. overflow: hidden;
  323. .left-board {
  324. width: 260px;
  325. position: absolute;
  326. left: 0;
  327. top: 0;
  328. height: calc(100vh - 50px - 40px);
  329. .logo-wrapper {
  330. position: relative;
  331. height: 42px;
  332. border-bottom: 1px solid var(--el-border-color-extra-light);
  333. box-sizing: border-box;
  334. .logo {
  335. position: absolute;
  336. left: 12px;
  337. top: 6px;
  338. line-height: 30px;
  339. color: #00afff;
  340. font-weight: 600;
  341. font-size: 17px;
  342. white-space: nowrap;
  343. >img {
  344. width: 30px;
  345. height: 30px;
  346. vertical-align: top;
  347. }
  348. .github {
  349. display: inline-block;
  350. vertical-align: sub;
  351. margin-left: 15px;
  352. >img {
  353. height: 22px;
  354. }
  355. }
  356. }
  357. }
  358. .left-scrollbar {
  359. .el-scrollbar__wrap {
  360. box-sizing: border-box;
  361. overflow-x: hidden !important;
  362. margin-bottom: 0 !important;
  363. .components-list {
  364. padding: 8px;
  365. box-sizing: border-box;
  366. height: 100%;
  367. .components-title {
  368. font-size: 14px;
  369. // color: #222;
  370. margin: 6px 2px;
  371. .svg-icon {
  372. // color: #666;
  373. font-size: 18px;
  374. margin-right: 5px;
  375. }
  376. }
  377. .components-draggable {
  378. padding-bottom: 20px;
  379. .components-item {
  380. display: inline-block;
  381. width: 48%;
  382. margin: 1%;
  383. transition: transform 0ms !important;
  384. .components-body {
  385. padding: 8px 10px;
  386. background: var(--el-border-color-extra-light);
  387. font-size: 12px;
  388. cursor: move;
  389. border: 1px dashed var(--el-border-color-extra-light);
  390. border-radius: 3px;
  391. .svg-icon {
  392. // color: #777;
  393. font-size: 15px;
  394. margin-right: 5px;
  395. }
  396. &:hover {
  397. border: 1px dashed #787be8;
  398. color: #787be8;
  399. .svg-icon {
  400. color: #787be8;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. }
  409. }
  410. .center-board {
  411. height: calc(100vh - 50px - 40px);
  412. width: auto;
  413. margin: 0 350px 0 260px;
  414. box-sizing: border-box;
  415. .action-bar {
  416. position: relative;
  417. height: 42px;
  418. padding: 0 15px;
  419. box-sizing: border-box;
  420. ;
  421. border: 1px solid var(--el-border-color-extra-light);
  422. border-top: none;
  423. border-left: none;
  424. display: flex;
  425. align-items: center;
  426. justify-content: flex-end;
  427. u .delete-btn {
  428. color: #F56C6C;
  429. }
  430. }
  431. .center-scrollbar {
  432. height: calc(100vh - 50px - 40px - 42px);
  433. overflow: hidden;
  434. border-left: 1px solid var(--el-border-color-extra-light);
  435. border-right: 1px solid var(--el-border-color-extra-light);
  436. box-sizing: border-box;
  437. .el-scrollbar__view {
  438. overflow-x: hidden;
  439. }
  440. .center-board-row {
  441. padding: 12px 12px 15px 12px;
  442. box-sizing: border-box;
  443. &>.el-form {
  444. // 69 = 12+15+42
  445. height: calc(100vh - 50px - 40px - 69px);
  446. flex: 1;
  447. .drawing-board {
  448. height: 100%;
  449. position: relative;
  450. .components-body {
  451. padding: 0;
  452. margin: 0;
  453. font-size: 0;
  454. }
  455. .sortable-ghost {
  456. position: relative;
  457. display: block;
  458. overflow: hidden;
  459. &::before {
  460. content: " ";
  461. position: absolute;
  462. left: 0;
  463. right: 0;
  464. top: 0;
  465. height: 3px;
  466. background: rgb(89, 89, 223);
  467. z-index: 2;
  468. }
  469. }
  470. .components-item.sortable-ghost {
  471. width: 100%;
  472. height: 60px;
  473. background: var(--el-border-color-extra-light);
  474. }
  475. .active-from-item {
  476. &>.el-form-item {
  477. background: var(--el-border-color-extra-light);
  478. border-radius: 6px;
  479. }
  480. &>.drawing-item-copy,
  481. &>.drawing-item-delete {
  482. display: initial;
  483. }
  484. &>.component-name {
  485. color: $lighterBlue;
  486. }
  487. .el-input__wrapper {
  488. box-shadow: 0 0 0 1px var(--el-input-hover-border-color) inset;
  489. }
  490. }
  491. .el-form-item {
  492. margin-bottom: 15px;
  493. }
  494. }
  495. .drawing-item {
  496. position: relative;
  497. cursor: move;
  498. &.unfocus-bordered:not(.activeFromItem)>div:first-child {
  499. border: 1px dashed #ccc;
  500. }
  501. .el-form-item {
  502. padding: 12px 10px;
  503. }
  504. }
  505. .drawing-row-item {
  506. position: relative;
  507. cursor: move;
  508. box-sizing: border-box;
  509. border: 1px dashed #ccc;
  510. border-radius: 3px;
  511. padding: 0 2px;
  512. margin-bottom: 15px;
  513. .drawing-row-item {
  514. margin-bottom: 2px;
  515. }
  516. .el-col {
  517. margin-top: 22px;
  518. }
  519. .el-form-item {
  520. margin-bottom: 0;
  521. }
  522. .drag-wrapper {
  523. min-height: 80px;
  524. flex: 1;
  525. display: flex;
  526. flex-wrap: wrap;
  527. }
  528. &.active-from-item {
  529. border: 1px dashed $lighterBlue;
  530. }
  531. .component-name {
  532. position: absolute;
  533. top: 0;
  534. left: 0;
  535. font-size: 12px;
  536. color: #bbb;
  537. display: inline-block;
  538. padding: 0 6px;
  539. }
  540. }
  541. .drawing-item,
  542. .drawing-row-item {
  543. &:hover {
  544. &>.el-form-item {
  545. background: var(--el-border-color-extra-light);
  546. border-radius: 6px;
  547. }
  548. &>.drawing-item-copy,
  549. &>.drawing-item-delete {
  550. display: initial;
  551. }
  552. }
  553. &>.drawing-item-copy,
  554. &>.drawing-item-delete {
  555. display: none;
  556. position: absolute;
  557. top: -10px;
  558. width: 22px;
  559. height: 22px;
  560. line-height: 22px;
  561. text-align: center;
  562. border-radius: 50%;
  563. font-size: 12px;
  564. border: 1px solid;
  565. cursor: pointer;
  566. z-index: 1;
  567. }
  568. &>.drawing-item-copy {
  569. right: 56px;
  570. border-color: $lighterBlue;
  571. color: $lighterBlue;
  572. background: #fff;
  573. &:hover {
  574. background: $lighterBlue;
  575. color: #fff;
  576. }
  577. }
  578. &>.drawing-item-delete {
  579. right: 24px;
  580. border-color: #F56C6C;
  581. color: #F56C6C;
  582. background: #fff;
  583. &:hover {
  584. background: #F56C6C;
  585. color: #fff;
  586. }
  587. }
  588. }
  589. .empty-info {
  590. position: absolute;
  591. top: 46%;
  592. left: 0;
  593. right: 0;
  594. text-align: center;
  595. font-size: 18px;
  596. color: #ccb1ea;
  597. letter-spacing: 4px;
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. </style>