index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <ContentWrap>
  3. <el-row>
  4. <el-col>
  5. <div class="mb-2 float-right">
  6. <el-button size="small" type="primary" @click="showJson">生成 JSON</el-button>
  7. <el-button size="small" type="success" @click="showOption">生成O ptions</el-button>
  8. <el-button size="small" type="danger" @click="showTemplate">生成组件</el-button>
  9. </div>
  10. </el-col>
  11. <!-- 表单设计器 -->
  12. <el-col>
  13. <fc-designer ref="designer" height="780px" />
  14. </el-col>
  15. </el-row>
  16. </ContentWrap>
  17. <!-- 弹窗:表单预览 -->
  18. <Dialog :title="dialogTitle" v-model="dialogVisible" max-height="600">
  19. <div ref="editor" v-if="dialogVisible">
  20. <el-button style="float: right" @click="copy(formData)">
  21. {{ t('common.copy') }}
  22. </el-button>
  23. <el-scrollbar height="580">
  24. <div v-highlight>
  25. <code class="hljs">
  26. {{ formData }}
  27. </code>
  28. </div>
  29. </el-scrollbar>
  30. </div>
  31. </Dialog>
  32. </template>
  33. <script setup lang="ts" name="InfraBuild">
  34. import formCreate from '@form-create/element-ui'
  35. import { useClipboard } from '@vueuse/core'
  36. const { t } = useI18n() // 国际化
  37. const message = useMessage() // 消息
  38. const designer = ref() // 表单设计器
  39. const dialogVisible = ref(false) // 弹窗的是否展示
  40. const dialogTitle = ref('') // 弹窗的标题
  41. const formType = ref(-1) // 表单的类型:0 - 生成 JSON;1 - 生成 Options;2 - 生成组件
  42. const formData = ref('') // 表单数据
  43. /** 打开弹窗 */
  44. const openModel = (title: string) => {
  45. dialogVisible.value = true
  46. dialogTitle.value = title
  47. }
  48. /** 生成 JSON */
  49. const showJson = () => {
  50. openModel('生成 JSON')
  51. formType.value = 0
  52. formData.value = designer.value.getRule()
  53. }
  54. /** 生成 Options */
  55. const showOption = () => {
  56. openModel('生成 Options')
  57. formType.value = 1
  58. formData.value = designer.value.getOption()
  59. }
  60. /** 生成组件 */
  61. const showTemplate = () => {
  62. openModel('生成组件')
  63. formType.value = 2
  64. formData.value = makeTemplate()
  65. }
  66. const makeTemplate = () => {
  67. const rule = designer.value.getRule()
  68. const opt = designer.value.getOption()
  69. return `<template>
  70. <form-create
  71. v-model="fapi"
  72. :rule="rule"
  73. :option="option"
  74. @submit="onSubmit"
  75. ></form-create>
  76. </template>
  77. <script setup lang=ts>
  78. import formCreate from "@form-create/element-ui";
  79. const faps = ref(null)
  80. const rule = ref('')
  81. const option = ref('')
  82. const init = () => {
  83. rule.value = formCreate.parseJson('${formCreate.toJson(rule).replaceAll('\\', '\\\\')}')
  84. option.value = formCreate.parseJson('${JSON.stringify(opt)}')
  85. }
  86. const onSubmit = (formData) => {
  87. //todo 提交表单
  88. }
  89. init()
  90. <\/script>`
  91. }
  92. /** 复制 **/
  93. const copy = async (text: string) => {
  94. const { copy, copied, isSupported } = useClipboard({ source: text })
  95. if (!isSupported) {
  96. message.error(t('common.copyError'))
  97. } else {
  98. await copy()
  99. if (unref(copied)) {
  100. message.success(t('common.copySuccess'))
  101. }
  102. }
  103. }
  104. </script>