index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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">生成 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 class="hljs" v-html="highlightedCode(formData)"></code></pre>
  26. </div>
  27. </el-scrollbar>
  28. </div>
  29. </Dialog>
  30. </template>
  31. <script lang="ts" setup>
  32. defineOptions({ name: 'InfraBuild' })
  33. import FcDesigner from '@form-create/designer'
  34. import { useClipboard } from '@vueuse/core'
  35. import { isString } from '@/utils/is'
  36. import hljs from 'highlight.js' // 导入代码高亮文件
  37. import 'highlight.js/styles/github.css' // 导入代码高亮样式
  38. import xml from 'highlight.js/lib/languages/java'
  39. import json from 'highlight.js/lib/languages/json'
  40. import formCreate from '@form-create/element-ui'
  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. /** 打开弹窗 */
  49. const openModel = (title: string) => {
  50. dialogVisible.value = true
  51. dialogTitle.value = title
  52. }
  53. /** 生成 JSON */
  54. const showJson = () => {
  55. openModel('生成 JSON')
  56. formType.value = 0
  57. formData.value = designer.value.getRule()
  58. }
  59. /** 生成 Options */
  60. const showOption = () => {
  61. openModel('生成 Options')
  62. formType.value = 1
  63. formData.value = designer.value.getOption()
  64. }
  65. /** 生成组件 */
  66. const showTemplate = () => {
  67. openModel('生成组件')
  68. formType.value = 2
  69. formData.value = makeTemplate()
  70. }
  71. const makeTemplate = () => {
  72. const rule = designer.value.getRule()
  73. const opt = designer.value.getOption()
  74. return `<template>
  75. <form-create
  76. v-model="fapi"
  77. :rule="rule"
  78. :option="option"
  79. @submit="onSubmit"
  80. ></form-create>
  81. </template>
  82. <script setup lang=ts>
  83. import formCreate from "@form-create/element-ui";
  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>