|
| 1 | +<script setup lang="ts"> |
| 2 | +import FcDesigner, { Config } from "@form-create/designer"; |
| 3 | +import type { Rule, Options } from "@form-create/element-ui"; |
| 4 | +import { ref, useTemplateRef, type Ref } from "vue"; |
| 5 | +
|
| 6 | +defineOptions({ |
| 7 | + name: "Designer06" |
| 8 | +}); |
| 9 | +
|
| 10 | +const designer = useTemplateRef("designer") as Ref< |
| 11 | + InstanceType<typeof import("@form-create/designer").FcDesigner> |
| 12 | +>; |
| 13 | +const formCreate = FcDesigner.formCreate; |
| 14 | +const handleEmit = (type, data) => { |
| 15 | + console.log(type, data); |
| 16 | +}; |
| 17 | +const binding = { |
| 18 | + config: { |
| 19 | + autoActive: true, |
| 20 | + fieldReadonly: false, |
| 21 | + showSaveBtn: true |
| 22 | + } satisfies Config, |
| 23 | + ...[ |
| 24 | + "active", |
| 25 | + "create", |
| 26 | + "copy", |
| 27 | + "delete", |
| 28 | + "drag", |
| 29 | + "inputData", |
| 30 | + "save", |
| 31 | + "clear" |
| 32 | + ].reduce((prev, cur) => { |
| 33 | + const eventKey = "on" + cur[0].toUpperCase() + cur.slice(1); |
| 34 | + prev[eventKey] = (...args) => handleEmit(cur, args); |
| 35 | + return prev; |
| 36 | + }, {}) |
| 37 | +}; |
| 38 | +
|
| 39 | +// 弹窗相关 |
| 40 | +const modalVisible = ref(false); |
| 41 | +const editMode = ref(false); |
| 42 | +const mode = ref("rule"); |
| 43 | +const json = ref(""); |
| 44 | +const handelOption = () => { |
| 45 | + modalVisible.value = true; |
| 46 | + json.value = designer.value?.getOptionsJson(); |
| 47 | +}; |
| 48 | +const handleRule = () => { |
| 49 | + modalVisible.value = true; |
| 50 | + json.value = designer.value?.getJson(); |
| 51 | +}; |
| 52 | +const handleData = () => { |
| 53 | + modalVisible.value = true; |
| 54 | + json.value = JSON.stringify(designer.value?.getFormData()); |
| 55 | +}; |
| 56 | +// 处理弹窗保存事件 |
| 57 | +const handleSave = () => { |
| 58 | + if (mode.value === "rule") { |
| 59 | + designer.value?.setRule(formCreate.parseJson(json.value) as Rule[]); |
| 60 | + } else if (mode.value === "option") { |
| 61 | + designer.value?.setOptions(formCreate.parseJson(json.value) as Options); |
| 62 | + } else if (mode.value === "data") { |
| 63 | + designer.value?.setFormData(JSON.parse(json.value)); |
| 64 | + } |
| 65 | + modalVisible.value = false; |
| 66 | +}; |
| 67 | +const handleEditOption = () => { |
| 68 | + mode.value = "option"; |
| 69 | + editMode.value = true; |
| 70 | + modalVisible.value = true; |
| 71 | +}; |
| 72 | +const handleEditRule = () => { |
| 73 | + mode.value = "rule"; |
| 74 | + editMode.value = true; |
| 75 | + modalVisible.value = true; |
| 76 | +}; |
| 77 | +const handleEditData = () => { |
| 78 | + mode.value = "data"; |
| 79 | + editMode.value = true; |
| 80 | + modalVisible.value = true; |
| 81 | +}; |
| 82 | +
|
| 83 | +const handleEvent = (type: String, value: any) => { |
| 84 | + console.log(type, value); |
| 85 | +}; |
| 86 | +</script> |
| 87 | + |
| 88 | +<template> |
| 89 | + <div class="fd-container"> |
| 90 | + <div class="flex justify-center p-2"> |
| 91 | + <el-button type="primary" @click="handleEditOption"> |
| 92 | + 导入 Option |
| 93 | + </el-button> |
| 94 | + <el-button type="primary" @click="handleEditRule">导入 Rule</el-button> |
| 95 | + <el-button type="primary" @click="handleEditData">导入 Data</el-button> |
| 96 | + <el-button type="success" @click="handelOption">导出 Option</el-button> |
| 97 | + <el-button type="success" @click="handleRule">导出 Rule</el-button> |
| 98 | + <el-button type="success" @click="handleData">导出 Data</el-button> |
| 99 | + <el-button type="primary" plain @click="designer?.openPreview()"> |
| 100 | + 预览 |
| 101 | + </el-button> |
| 102 | + <el-button type="danger" plain @click="designer?.clearDragRule()"> |
| 103 | + 清空 Rule |
| 104 | + </el-button> |
| 105 | + </div> |
| 106 | + <FcDesigner ref="designer" v-bind="binding" /> |
| 107 | + <el-dialog |
| 108 | + v-model="modalVisible" |
| 109 | + title="导出JSON" |
| 110 | + width="40%" |
| 111 | + @close="() => (json = '')" |
| 112 | + > |
| 113 | + <el-input v-model="json" type="textarea" :rows="10" /> |
| 114 | + <template #footer> |
| 115 | + <div class="dialog-footer"> |
| 116 | + <el-button @click="handleSave">保存</el-button> |
| 117 | + </div> |
| 118 | + </template> |
| 119 | + </el-dialog> |
| 120 | + </div> |
| 121 | +</template> |
| 122 | + |
| 123 | +<style lang="scss"> |
| 124 | +.fd-container { |
| 125 | + display: flex; |
| 126 | + flex-direction: column; |
| 127 | + height: 100%; |
| 128 | + padding: 8px 24px; |
| 129 | + margin: 0 !important; |
| 130 | +} |
| 131 | +
|
| 132 | +._fd-event-title { |
| 133 | + line-height: 100%; |
| 134 | +} |
| 135 | +</style> |
0 commit comments