index.vue 18 KB

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