Files
zkxw/web-admin/src/components/oa-approval/OaFormDesignerEmbed.vue
T
2026-07-15 16:20:48 +08:00

172 lines
4.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!-- OA 表单设计:iframe 隔离 FormCreate,避免主应用 Tailwind 打乱三栏 -->
<template>
<div class="oa-form-designer-embed">
<p class="oa-form-designer-embed__hint">
拖拽左侧控件到中间画布;顶部可切换 PC / 手机预览。选中控件后在右侧配置属性。
</p>
<iframe
ref="frameRef"
class="oa-form-designer-embed__frame"
title="表单设计器"
:src="hostSrc"
@load="onFrameLoad"
/>
</div>
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref, toRaw, watch } from 'vue'
defineOptions({ name: 'OaFormDesignerEmbed' })
const props = defineProps<{
/** { rule, option } */
modelValue?: { rule?: unknown; option?: unknown } | null
}>()
const frameRef = ref<HTMLIFrameElement | null>(null)
const frameReady = ref(false)
let reqSeq = 0
const pending = new Map<
number,
{ resolve: (v: { rule: unknown; option: unknown }) => void; reject: (e: Error) => void }
>()
const MSG = {
READY: 'oa-fc:ready',
SET: 'oa-fc:set-schema',
GET: 'oa-fc:get-schema',
SCHEMA: 'oa-fc:schema'
} as const
const hostSrc = computed(() => {
const base = import.meta.env.BASE_URL || '/'
const normalized = base.endsWith('/') ? base : `${base}/`
return `${normalized}oa-fc-host.html`
})
/** postMessage 无法可靠克隆 Vue Proxy,必须先变成纯 JSON */
function toPlainSchema(schema: unknown): { rule: unknown; option: unknown } {
const raw = toRaw(schema as any) || {}
try {
const cloned = JSON.parse(JSON.stringify(raw))
return {
rule: Array.isArray(cloned?.rule) ? cloned.rule : Array.isArray(cloned) ? cloned : [],
option: cloned?.option && typeof cloned.option === 'object' ? cloned.option : {}
}
} catch {
return { rule: [], option: {} }
}
}
function postToFrame(payload: Record<string, unknown>) {
try {
frameRef.value?.contentWindow?.postMessage(payload, '*')
} catch (e) {
console.warn('[OaFormDesignerEmbed] postMessage failed', e)
}
}
function pushSchemaToFrame() {
if (!frameReady.value) return
postToFrame({
type: MSG.SET,
schema: toPlainSchema(props.modelValue)
})
}
function onFrameLoad() {
if (frameReady.value) pushSchemaToFrame()
}
function onMessage(ev: MessageEvent) {
const data = ev.data
if (!data || typeof data !== 'object') return
if (data.type === MSG.READY) {
frameReady.value = true
pushSchemaToFrame()
// 宿主刚就绪时再补推一次,避免 setRule 早于内部 dragForm 初始化
window.setTimeout(() => pushSchemaToFrame(), 50)
window.setTimeout(() => pushSchemaToFrame(), 200)
return
}
if (data.type === MSG.SCHEMA && typeof data.requestId === 'number') {
const wait = pending.get(data.requestId)
if (!wait) return
pending.delete(data.requestId)
const schema = toPlainSchema(data.schema)
wait.resolve(schema)
}
}
/** 供父页保存草稿 / 发布前拉取最新 schema */
function fetchSchema(timeoutMs = 3000): Promise<{ rule: unknown; option: unknown }> {
if (!frameReady.value) {
return Promise.resolve(toPlainSchema(props.modelValue))
}
const requestId = ++reqSeq
return new Promise((resolve, reject) => {
const timer = window.setTimeout(() => {
pending.delete(requestId)
reject(new Error('表单设计器响应超时'))
}, timeoutMs)
pending.set(requestId, {
resolve: (v) => {
window.clearTimeout(timer)
resolve(v)
},
reject
})
postToFrame({ type: MSG.GET, requestId })
})
}
watch(
() => props.modelValue,
() => {
pushSchemaToFrame()
},
{ deep: true }
)
onMounted(() => {
window.addEventListener('message', onMessage)
})
onBeforeUnmount(() => {
window.removeEventListener('message', onMessage)
pending.forEach((p) => p.reject(new Error('表单设计器已卸载')))
pending.clear()
})
defineExpose({ fetchSchema })
</script>
<style scoped lang="scss">
.oa-form-designer-embed {
display: flex;
flex-direction: column;
width: 100%;
height: calc(100vh - 200px);
min-height: 520px;
}
.oa-form-designer-embed__hint {
flex-shrink: 0;
margin: 0 0 10px;
color: var(--mint-text-muted, #94a3b8);
font-size: 13px;
}
.oa-form-designer-embed__frame {
display: block;
flex: 1 1 auto;
width: 100%;
min-height: 0;
border: 1px solid var(--mint-border, #cbd5e1);
border-radius: 10px;
background: #fff;
box-shadow: 0 2px 8px rgb(15 23 42 / 8%);
}
</style>