123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <template>
- <ContentWrap>
- <el-row>
- <el-col>
- <div class="mb-2 float-right">
- <el-button size="small" @click="setJson"> 导入JSON</el-button>
- <el-button size="small" @click="setOption"> 导入Options</el-button>
- <el-button size="small" type="primary" @click="showJson">生成 JSON</el-button>
- <el-button size="small" type="success" @click="showOption">生成 Options</el-button>
- <el-button size="small" type="danger" @click="showTemplate">生成组件</el-button>
- </div>
- </el-col>
- <!-- 表单设计器 -->
- <el-col>
- <FcDesigner ref="designer" height="780px" />
- </el-col>
- </el-row>
- </ContentWrap>
- <!-- 弹窗:表单预览 -->
- <Dialog :title="dialogTitle" v-model="dialogVisible" max-height="600">
- <div ref="editor" v-if="dialogVisible">
- <XTextButton style="float: right" :title="t('common.copy')" @click="copy(formData)" />
- <el-scrollbar height="580">
- <div>
- <pre><code class="hljs" v-html="highlightedCode(formData)"></code></pre>
- </div>
- </el-scrollbar>
- </div>
- </Dialog>
- </template>
- <script setup lang="ts" name="InfraBuild">
- import FcDesigner from '@form-create/designer'
- // import { useClipboard } from '@vueuse/core'
- import { isString } from '@/utils/is'
- import formCreate from '@form-create/element-ui'
- 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'
- const { t } = useI18n() // 国际化
- const message = useMessage() // 消息
- const designer = ref() // 表单设计器
- const dialogVisible = ref(false) // 弹窗的是否展示
- const dialogTitle = ref('') // 弹窗的标题
- const formType = ref(-1) // 表单的类型:0 - 生成 JSON;1 - 生成 Options;2 - 生成组件
- const formData = ref('') // 表单数据
- /** 打开弹窗 */
- const openModel = (title: string) => {
- dialogVisible.value = true
- dialogTitle.value = title
- }
- const setJson = () => {
- openModel('导入JSON--未实现')
- }
- const setOption = () => {
- openModel('导入Options--未实现')
- }
- const showJson = () => {
- openModel('生成 JSON')
- formType.value = 0
- formData.value = designer.value.getRule()
- }
- /** 生成 Options */
- const showOption = () => {
- openModel('生成 Options')
- formType.value = 1
- formData.value = designer.value.getOption()
- }
- /** 生成组件 */
- const showTemplate = () => {
- openModel('生成组件')
- 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>`
- }
- /** 复制 **/
- const copy = async (text: string) => {
- // const { copy, copied, isSupported } = useClipboard({ source: JSON.stringify(text) })
- // if (!isSupported.value) {
- // message.error(t('common.copyError'))
- // } else {
- // await copy()
- // if (unref(copied.value)) {
- // message.success(t('common.copySuccess'))
- // }
- // }
- let url = JSON.stringify(text)
- let oInput = document.createElement('textarea')
- oInput.value = url
- document.body.appendChild(oInput)
- oInput.select() // 选择对象;
- // console.log(oInput.value)
- document.execCommand('Copy') // 执行浏览器复制命令
- message.success(t('common.copySuccess'))
- oInput.remove()
- }
- /**
- * 代码高亮
- */
- 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)
- })
- </script>
|