index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <ContentWrap>
  3. <el-row>
  4. <el-col>
  5. <div class="mb-2 float-right">
  6. <el-button size="small" @click="setJson"> 导入JSON</el-button>
  7. <el-button size="small" @click="setOption"> 导入Options</el-button>
  8. <el-button size="small" type="primary" @click="showJson">生成 JSON</el-button>
  9. <el-button size="small" type="success" @click="showOption">生成 Options</el-button>
  10. <el-button size="small" type="danger" @click="showTemplate">生成组件</el-button>
  11. </div>
  12. </el-col>
  13. <!-- 表单设计器 -->
  14. <el-col>
  15. <FcDesigner ref="designer" height="780px" />
  16. </el-col>
  17. </el-row>
  18. </ContentWrap>
  19. <!-- 弹窗:表单预览 -->
  20. <Dialog :title="dialogTitle" v-model="dialogVisible" max-height="600">
  21. <div ref="editor" v-if="dialogVisible">
  22. <XTextButton style="float: right" :title="t('common.copy')" @click="copy(formData)" />
  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 setup lang="ts" name="InfraBuild">
  32. import FcDesigner from '@form-create/designer'
  33. // import { useClipboard } from '@vueuse/core'
  34. import { isString } from '@/utils/is'
  35. import formCreate from '@form-create/element-ui'
  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. const { t } = useI18n() // 国际化
  41. const message = useMessage() // 消息
  42. const designer = ref() // 表单设计器
  43. const dialogVisible = ref(false) // 弹窗的是否展示
  44. const dialogTitle = ref('') // 弹窗的标题
  45. const formType = ref(-1) // 表单的类型:0 - 生成 JSON;1 - 生成 Options;2 - 生成组件
  46. const formData = ref('') // 表单数据
  47. /** 打开弹窗 */
  48. const openModel = (title: string) => {
  49. dialogVisible.value = true
  50. dialogTitle.value = title
  51. }
  52. const setJson = () => {
  53. openModel('导入JSON--未实现')
  54. }
  55. const setOption = () => {
  56. openModel('导入Options--未实现')
  57. }
  58. const showJson = () => {
  59. openModel('生成 JSON')
  60. formType.value = 0
  61. formData.value = designer.value.getRule()
  62. }
  63. /** 生成 Options */
  64. const showOption = () => {
  65. openModel('生成 Options')
  66. formType.value = 1
  67. formData.value = designer.value.getOption()
  68. }
  69. /** 生成组件 */
  70. const showTemplate = () => {
  71. openModel('生成组件')
  72. formType.value = 2
  73. formData.value = makeTemplate()
  74. }
  75. const makeTemplate = () => {
  76. const rule = designer.value.getRule()
  77. const opt = designer.value.getOption()
  78. return `<template>
  79. <form-create
  80. v-model="fapi"
  81. :rule="rule"
  82. :option="option"
  83. @submit="onSubmit"
  84. ></form-create>
  85. </template>
  86. <script setup lang=ts>
  87. import formCreate from "@form-create/element-ui";
  88. const faps = ref(null)
  89. const rule = ref('')
  90. const option = ref('')
  91. const init = () => {
  92. rule.value = formCreate.parseJson('${formCreate.toJson(rule).replaceAll('\\', '\\\\')}')
  93. option.value = formCreate.parseJson('${JSON.stringify(opt)}')
  94. }
  95. const onSubmit = (formData) => {
  96. //todo 提交表单
  97. }
  98. init()
  99. <\/script>`
  100. }
  101. /** 复制 **/
  102. const copy = async (text: string) => {
  103. // const { copy, copied, isSupported } = useClipboard({ source: JSON.stringify(text) })
  104. // if (!isSupported.value) {
  105. // message.error(t('common.copyError'))
  106. // } else {
  107. // await copy()
  108. // if (unref(copied.value)) {
  109. // message.success(t('common.copySuccess'))
  110. // }
  111. // }
  112. let url = JSON.stringify(text)
  113. let oInput = document.createElement('textarea')
  114. oInput.value = url
  115. document.body.appendChild(oInput)
  116. oInput.select() // 选择对象;
  117. // console.log(oInput.value)
  118. document.execCommand('Copy') // 执行浏览器复制命令
  119. message.success(t('common.copySuccess'))
  120. oInput.remove()
  121. }
  122. /**
  123. * 代码高亮
  124. */
  125. const highlightedCode = (code) => {
  126. // 处理语言和代码
  127. let language = 'json'
  128. if (formType.value === 2) {
  129. language = 'xml'
  130. }
  131. if (!isString(code)) {
  132. code = JSON.stringify(code)
  133. }
  134. // 高亮
  135. const result = hljs.highlight(language, code, true)
  136. return result.value || '&nbsp;'
  137. }
  138. /** 初始化 **/
  139. onMounted(async () => {
  140. // 注册代码高亮的各种语言
  141. hljs.registerLanguage('xml', xml)
  142. hljs.registerLanguage('json', json)
  143. })
  144. </script>