215 lines
6.0 KiB
Vue
215 lines
6.0 KiB
Vue
<!-- 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
|
||
/** 已被流程条件引用的 field 列表(用于拦截取消勾选) */
|
||
flowConditionFields?: string[]
|
||
}>()
|
||
|
||
const frameRef = ref<HTMLIFrameElement | null>(null)
|
||
const frameReady = ref(false)
|
||
let reqSeq = 0
|
||
/** 最近一次推入 iframe 的 schema JSON,用于避免保存回写后重复 setRule 清空画布 */
|
||
let lastPushedJson = ''
|
||
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',
|
||
FLOW_REFS: 'oa-fc:set-flow-refs'
|
||
} 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 schemaJson(schema: unknown) {
|
||
return JSON.stringify(toPlainSchema(schema))
|
||
}
|
||
|
||
function postToFrame(payload: Record<string, unknown>) {
|
||
try {
|
||
frameRef.value?.contentWindow?.postMessage(payload, '*')
|
||
} catch (e) {
|
||
console.warn('[OaFormDesignerEmbed] postMessage failed', e)
|
||
}
|
||
}
|
||
|
||
/** force=true 时即使内容相同也重新 setRule(用于切回表单 Tab 强制回显) */
|
||
function pushSchemaToFrame(force = false) {
|
||
if (!frameReady.value) return
|
||
const schema = toPlainSchema(props.modelValue)
|
||
const json = JSON.stringify(schema)
|
||
if (!force && json === lastPushedJson) return
|
||
lastPushedJson = json
|
||
postToFrame({
|
||
type: MSG.SET,
|
||
schema
|
||
})
|
||
}
|
||
|
||
function pushFlowRefs() {
|
||
if (!frameReady.value) return
|
||
postToFrame({
|
||
type: MSG.FLOW_REFS,
|
||
fields: Array.isArray(props.flowConditionFields) ? props.flowConditionFields : []
|
||
})
|
||
}
|
||
|
||
function onFrameLoad() {
|
||
if (frameReady.value) {
|
||
pushSchemaToFrame(true)
|
||
pushFlowRefs()
|
||
}
|
||
}
|
||
|
||
function onMessage(ev: MessageEvent) {
|
||
const data = ev.data
|
||
if (!data || typeof data !== 'object') return
|
||
if (data.type === MSG.READY) {
|
||
frameReady.value = true
|
||
lastPushedJson = ''
|
||
pushSchemaToFrame(true)
|
||
pushFlowRefs()
|
||
// 宿主刚就绪时再补推,避免 setRule 早于内部 dragForm / 菜单初始化
|
||
window.setTimeout(() => pushSchemaToFrame(true), 50)
|
||
window.setTimeout(() => pushSchemaToFrame(true), 200)
|
||
window.setTimeout(() => pushSchemaToFrame(true), 500)
|
||
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)
|
||
// 与父级即将回写的内容对齐,避免 watch 再次 setRule
|
||
lastPushedJson = JSON.stringify(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 })
|
||
})
|
||
}
|
||
|
||
/** 切回「表单设计」Tab 时强制把父级 schema 灌回画布 */
|
||
function reapplySchema() {
|
||
pushSchemaToFrame(true)
|
||
pushFlowRefs()
|
||
}
|
||
|
||
watch(
|
||
() => schemaJson(props.modelValue),
|
||
() => {
|
||
pushSchemaToFrame(false)
|
||
}
|
||
)
|
||
|
||
watch(
|
||
() => props.flowConditionFields,
|
||
() => {
|
||
pushFlowRefs()
|
||
},
|
||
{ deep: true }
|
||
)
|
||
|
||
onMounted(() => {
|
||
window.addEventListener('message', onMessage)
|
||
})
|
||
|
||
onBeforeUnmount(() => {
|
||
window.removeEventListener('message', onMessage)
|
||
pending.forEach((p) => p.reject(new Error('表单设计器已卸载')))
|
||
pending.clear()
|
||
})
|
||
|
||
defineExpose({ fetchSchema, reapplySchema })
|
||
</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>
|