index.vue 3.9 KB

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