index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <ContentWrap>
  3. <el-row>
  4. <el-col>
  5. <div class="float-right mb-2">
  6. <el-button size="small" type="primary" @click="showJson">生成 JSON</el-button>
  7. <el-button size="small" type="success" @click="showOption">生成 Options</el-button>
  8. <el-button size="small" type="danger" @click="showTemplate">生成组件</el-button>
  9. </div>
  10. </el-col>
  11. </el-row>
  12. <!-- 表单设计器 -->
  13. <FcDesigner ref="designer" height="780px" />
  14. </ContentWrap>
  15. <!-- 弹窗:表单预览 -->
  16. <Dialog v-model="dialogVisible" :title="dialogTitle" max-height="600">
  17. <div v-if="dialogVisible" ref="editor">
  18. <el-button style="float: right" @click="copy(formData)">
  19. {{ t('common.copy') }}
  20. </el-button>
  21. <el-scrollbar height="580">
  22. <div>
  23. <pre><code v-dompurify-html="highlightedCode(formData)" class="hljs"></code></pre>
  24. </div>
  25. </el-scrollbar>
  26. </div>
  27. </Dialog>
  28. </template>
  29. <script lang="ts" setup>
  30. import { useFormCreateDesigner } from '@/components/FormCreate'
  31. import { useClipboard } from '@vueuse/core'
  32. import { isString } from '@/utils/is'
  33. import hljs from 'highlight.js' // 导入代码高亮文件
  34. import 'highlight.js/styles/github.css' // 导入代码高亮样式
  35. import xml from 'highlight.js/lib/languages/java'
  36. import json from 'highlight.js/lib/languages/json'
  37. import formCreate from '@form-create/element-ui'
  38. defineOptions({ name: 'InfraBuild' })
  39. const { t } = useI18n() // 国际化
  40. const message = useMessage() // 消息
  41. const designer = ref() // 表单设计器
  42. const dialogVisible = ref(false) // 弹窗的是否展示
  43. const dialogTitle = ref('') // 弹窗的标题
  44. const formType = ref(-1) // 表单的类型:0 - 生成 JSON;1 - 生成 Options;2 - 生成组件
  45. const formData = ref('') // 表单数据
  46. useFormCreateDesigner(designer) // 表单设计器增强
  47. /** 打开弹窗 */
  48. const openModel = (title: string) => {
  49. dialogVisible.value = true
  50. dialogTitle.value = title
  51. }
  52. /** 生成 JSON */
  53. const showJson = () => {
  54. openModel('生成 JSON')
  55. formType.value = 0
  56. formData.value = designer.value.getRule()
  57. }
  58. /** 生成 Options */
  59. const showOption = () => {
  60. openModel('生成 Options')
  61. formType.value = 1
  62. formData.value = designer.value.getOption()
  63. }
  64. /** 生成组件 */
  65. const showTemplate = () => {
  66. openModel('生成组件')
  67. formType.value = 2
  68. formData.value = makeTemplate()
  69. }
  70. const makeTemplate = () => {
  71. const rule = designer.value.getRule()
  72. const opt = designer.value.getOption()
  73. return `<template>
  74. <form-create
  75. v-model:api="fApi"
  76. :rule="rule"
  77. :option="option"
  78. @submit="onSubmit"
  79. ></form-create>
  80. </template>
  81. <script setup lang=ts>
  82. const faps = ref(null)
  83. const rule = ref('')
  84. const option = ref('')
  85. const init = () => {
  86. rule.value = formCreate.parseJson('${formCreate.toJson(rule).replaceAll('\\', '\\\\')}')
  87. option.value = formCreate.parseJson('${JSON.stringify(opt)}')
  88. }
  89. const onSubmit = (formData) => {
  90. //todo 提交表单
  91. }
  92. init()
  93. <\/script>`
  94. }
  95. /** 复制 **/
  96. const copy = async (text: string) => {
  97. const { copy, copied, isSupported } = useClipboard({ source: text })
  98. if (!isSupported) {
  99. message.error(t('common.copyError'))
  100. } else {
  101. await copy()
  102. if (unref(copied)) {
  103. message.success(t('common.copySuccess'))
  104. }
  105. }
  106. }
  107. /**
  108. * 代码高亮
  109. */
  110. const highlightedCode = (code) => {
  111. // 处理语言和代码
  112. let language = 'json'
  113. if (formType.value === 2) {
  114. language = 'xml'
  115. }
  116. if (!isString(code)) {
  117. code = JSON.stringify(code)
  118. }
  119. // 高亮
  120. const result = hljs.highlight(language, code, true)
  121. return result.value || '&nbsp;'
  122. }
  123. /** 初始化 **/
  124. onMounted(async () => {
  125. // 注册代码高亮的各种语言
  126. hljs.registerLanguage('xml', xml)
  127. hljs.registerLanguage('json', json)
  128. })
  129. </script>