* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { computed } from 'vue'
import { extend } from '@opentiny/vue-renderless/common/object'
import { constants } from '@opentiny/tiny-engine-utils'
import { useBlock } from '@opentiny/tiny-engine-meta-register'
const { SCHEMA_DATA_TYPE } = constants
const addPropertyLinks = ({ linked, propertyName, componentProperties }) => {
for (let i = 0; i < componentProperties.length; i++) {
const propertyList = componentProperties[i].content
for (let j = 0; j < propertyList.length; j++) {
const property = propertyList[j]
if (property.property === propertyName) {
propertyList[j] = extend(true, {}, property, {
linked,
widget: {
props: {
modelValue: {
type: SCHEMA_DATA_TYPE.JSExpression,
value: `this.props.${linked.blockProperty}`
}
}
}
})
}
}
}
}
const findLinked = ({ componentProperties, componentId, blockProperties }) => {
for (let i = 0; i < blockProperties.length; i++) {
const property = blockProperties[i]
if (property.linked && componentId === property.linked.id) {
addPropertyLinks({
componentProperties,
linked: { ...property.linked, blockProperty: property.property },
defaultValue: property.defaultValue,
propertyName: property.linked.property
})
}
}
}
const resetLink = (properties) => {
if (properties && Array.isArray(properties)) {
properties.forEach((group) => {
if (group?.content && Array.isArray(group.content)) {
group.content.forEach((property) => {
property.linked = null
})
}
})
}
}
const getProperty = ({ pageState }) => {
const { getCurrentBlock, getBlockProperties } = useBlock()
const properties = computed(() => {
resetLink(pageState.properties)
if (pageState.isBlock && pageState.currentSchema?.id) {
findLinked({
componentProperties: pageState.properties,
componentId: pageState.currentSchema.id,
blockProperties: getBlockProperties(getCurrentBlock())
})
}
return pageState.properties
})
return {
properties
}
}
export default () => ({ getProperty })