<template>
<component :is="tag" v-bind="$attrs" :key="updateKey">
<slot>
<canvas-placeholder></canvas-placeholder>
</slot>
</component>
</template>
<script lang="ts">
import { ref, watch, computed } from 'vue'
import CanvasPlaceholder from './CanvasPlaceholder.vue'
import { getController } from '../render'
import { getHandler } from './CanvasCollection.ts'
export const fetchDataSourceDetail = (dataSourceId) =>
getController().request.get(`/app-center/api/sources/detail/${dataSourceId}`)
export default {
components: {
CanvasPlaceholder
},
props: {
tag: {
type: String,
default: 'div'
},
schema: {
type: Object,
default: () => ({})
},
dataSource: [String, Array, Number]
},
setup(props) {
const source = ref(null)
const updateKey = ref(0)
if (props.dataSource) {
fetchDataSourceDetail(props.dataSource).then((res) => {
source.value = res
})
}
let handler
watch(
() => props.dataSource,
async (value) => {
if (value) {
source.value = await fetchDataSourceDetail(value)
const { getSchema, getNode } = window.host.schemaUtils
const node = getNode(props.schema?.children?.[0]?.id)
if (node) {
handler = getHandler({
sourceRef: source,
node,
schemaId: props.schema.id,
pageSchema: getSchema(),
updateKey
})
}
handler?.updateNode()
updateKey.value++
}
}
)
const isEmpty = computed(() => {
const { children } = props.schema || {}
return Array.isArray(children) ? !children.length : !children
})
watch(
() => isEmpty.value,
(value) => {
const { getSchema, getNode } = window.host.schemaUtils
const pageSchema = getSchema()
if (value) {
if (handler) {
handler.clearBindVar()
} else {
const schemaId = props.schema?.id
Object.keys(pageSchema.methods || {})?.some((item) => {
if (item.includes(schemaId)) {
delete pageSchema.methods[item]
return true
}
return false
})
}
} else {
const node = getNode(props.schema?.children?.[0]?.id)
if (node) {
handler = getHandler({
sourceRef: source,
node,
schemaId: props.schema.id,
pageSchema,
updateKey
})
handler.updateNode()
}
}
const { publish } = getController().useMessage()
publish({ topic: 'schemaChange', data: {} })
updateKey.value++
}
)
return { source, updateKey }
}
}
</script>