2023-02-11 00:44:00 +08:00
|
|
|
|
<template>
|
|
|
|
|
<ContentWrap>
|
|
|
|
|
<el-row>
|
|
|
|
|
<el-col>
|
|
|
|
|
<div class="mb-2 float-right">
|
2023-03-25 16:22:43 +08:00
|
|
|
|
<el-button size="small" type="primary" @click="showJson">生成 JSON</el-button>
|
2023-04-15 22:46:45 +08:00
|
|
|
|
<el-button size="small" type="success" @click="showOption">生成 Options</el-button>
|
2023-02-11 00:44:00 +08:00
|
|
|
|
<el-button size="small" type="danger" @click="showTemplate">生成组件</el-button>
|
|
|
|
|
</div>
|
|
|
|
|
</el-col>
|
2023-03-25 16:22:43 +08:00
|
|
|
|
<!-- 表单设计器 -->
|
2023-02-11 00:44:00 +08:00
|
|
|
|
<el-col>
|
2023-04-15 22:46:45 +08:00
|
|
|
|
<FcDesigner ref="designer" height="780px" />
|
2023-02-11 00:44:00 +08:00
|
|
|
|
</el-col>
|
|
|
|
|
</el-row>
|
|
|
|
|
</ContentWrap>
|
2023-03-25 16:22:43 +08:00
|
|
|
|
|
|
|
|
|
<!-- 弹窗:表单预览 -->
|
|
|
|
|
<Dialog :title="dialogTitle" v-model="dialogVisible" max-height="600">
|
|
|
|
|
<div ref="editor" v-if="dialogVisible">
|
|
|
|
|
<el-button style="float: right" @click="copy(formData)">
|
|
|
|
|
{{ t('common.copy') }}
|
|
|
|
|
</el-button>
|
|
|
|
|
<el-scrollbar height="580">
|
2023-04-15 22:46:45 +08:00
|
|
|
|
<div>
|
|
|
|
|
<pre><code class="hljs" v-html="highlightedCode(formData)"></code></pre>
|
2023-03-25 16:22:43 +08:00
|
|
|
|
</div>
|
|
|
|
|
</el-scrollbar>
|
|
|
|
|
</div>
|
|
|
|
|
</Dialog>
|
2023-02-11 00:44:00 +08:00
|
|
|
|
</template>
|
2023-06-21 19:14:34 +08:00
|
|
|
|
<script setup lang="ts">
|
2023-04-15 22:46:45 +08:00
|
|
|
|
import FcDesigner from '@form-create/designer'
|
2023-02-11 00:44:00 +08:00
|
|
|
|
import { useClipboard } from '@vueuse/core'
|
2023-04-15 22:46:45 +08:00
|
|
|
|
import { isString } from '@/utils/is'
|
2023-04-23 19:35:14 +08:00
|
|
|
|
|
|
|
|
|
import hljs from 'highlight.js' // 导入代码高亮文件
|
|
|
|
|
import 'highlight.js/styles/github.css' // 导入代码高亮样式
|
|
|
|
|
import xml from 'highlight.js/lib/languages/java'
|
|
|
|
|
import json from 'highlight.js/lib/languages/json'
|
|
|
|
|
|
2023-06-21 19:14:34 +08:00
|
|
|
|
defineOptions({ name: 'InfraBuild' })
|
|
|
|
|
|
2023-03-25 16:22:43 +08:00
|
|
|
|
const { t } = useI18n() // 国际化
|
|
|
|
|
const message = useMessage() // 消息
|
2023-02-11 00:44:00 +08:00
|
|
|
|
|
2023-03-25 16:22:43 +08:00
|
|
|
|
const designer = ref() // 表单设计器
|
|
|
|
|
const dialogVisible = ref(false) // 弹窗的是否展示
|
|
|
|
|
const dialogTitle = ref('') // 弹窗的标题
|
|
|
|
|
const formType = ref(-1) // 表单的类型:0 - 生成 JSON;1 - 生成 Options;2 - 生成组件
|
|
|
|
|
const formData = ref('') // 表单数据
|
2023-02-11 00:44:00 +08:00
|
|
|
|
|
2023-03-25 16:22:43 +08:00
|
|
|
|
/** 打开弹窗 */
|
2023-02-11 00:44:00 +08:00
|
|
|
|
const openModel = (title: string) => {
|
|
|
|
|
dialogVisible.value = true
|
|
|
|
|
dialogTitle.value = title
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-25 16:22:43 +08:00
|
|
|
|
/** 生成 JSON */
|
2023-02-11 00:44:00 +08:00
|
|
|
|
const showJson = () => {
|
2023-03-25 16:22:43 +08:00
|
|
|
|
openModel('生成 JSON')
|
|
|
|
|
formType.value = 0
|
|
|
|
|
formData.value = designer.value.getRule()
|
2023-02-11 00:44:00 +08:00
|
|
|
|
}
|
2023-03-25 16:22:43 +08:00
|
|
|
|
|
|
|
|
|
/** 生成 Options */
|
2023-02-11 00:44:00 +08:00
|
|
|
|
const showOption = () => {
|
2023-03-25 16:22:43 +08:00
|
|
|
|
openModel('生成 Options')
|
|
|
|
|
formType.value = 1
|
|
|
|
|
formData.value = designer.value.getOption()
|
2023-02-11 00:44:00 +08:00
|
|
|
|
}
|
2023-03-25 16:22:43 +08:00
|
|
|
|
|
|
|
|
|
/** 生成组件 */
|
2023-02-11 00:44:00 +08:00
|
|
|
|
const showTemplate = () => {
|
|
|
|
|
openModel('生成组件')
|
2023-03-25 16:22:43 +08:00
|
|
|
|
formType.value = 2
|
|
|
|
|
formData.value = makeTemplate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const makeTemplate = () => {
|
|
|
|
|
const rule = designer.value.getRule()
|
|
|
|
|
const opt = designer.value.getOption()
|
|
|
|
|
return `<template>
|
|
|
|
|
<form-create
|
|
|
|
|
v-model="fapi"
|
|
|
|
|
:rule="rule"
|
|
|
|
|
:option="option"
|
|
|
|
|
@submit="onSubmit"
|
|
|
|
|
></form-create>
|
|
|
|
|
</template>
|
|
|
|
|
<script setup lang=ts>
|
|
|
|
|
import formCreate from "@form-create/element-ui";
|
|
|
|
|
const faps = ref(null)
|
|
|
|
|
const rule = ref('')
|
|
|
|
|
const option = ref('')
|
|
|
|
|
const init = () => {
|
|
|
|
|
rule.value = formCreate.parseJson('${formCreate.toJson(rule).replaceAll('\\', '\\\\')}')
|
|
|
|
|
option.value = formCreate.parseJson('${JSON.stringify(opt)}')
|
|
|
|
|
}
|
|
|
|
|
const onSubmit = (formData) => {
|
|
|
|
|
//todo 提交表单
|
|
|
|
|
}
|
|
|
|
|
init()
|
|
|
|
|
<\/script>`
|
2023-02-11 00:44:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 复制 **/
|
|
|
|
|
const copy = async (text: string) => {
|
|
|
|
|
const { copy, copied, isSupported } = useClipboard({ source: text })
|
|
|
|
|
if (!isSupported) {
|
|
|
|
|
message.error(t('common.copyError'))
|
|
|
|
|
} else {
|
|
|
|
|
await copy()
|
|
|
|
|
if (unref(copied)) {
|
|
|
|
|
message.success(t('common.copySuccess'))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-15 22:46:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 代码高亮
|
|
|
|
|
*/
|
|
|
|
|
const highlightedCode = (code) => {
|
|
|
|
|
// 处理语言和代码
|
|
|
|
|
let language = 'json'
|
|
|
|
|
if (formType.value === 2) {
|
|
|
|
|
language = 'xml'
|
|
|
|
|
}
|
|
|
|
|
if (!isString(code)) {
|
|
|
|
|
code = JSON.stringify(code)
|
|
|
|
|
}
|
|
|
|
|
// 高亮
|
|
|
|
|
const result = hljs.highlight(language, code, true)
|
|
|
|
|
return result.value || ' '
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 初始化 **/
|
|
|
|
|
onMounted(async () => {
|
|
|
|
|
// 注册代码高亮的各种语言
|
|
|
|
|
hljs.registerLanguage('xml', xml)
|
|
|
|
|
hljs.registerLanguage('json', json)
|
|
|
|
|
})
|
2023-02-11 00:44:00 +08:00
|
|
|
|
</script>
|