forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseProperty.js
More file actions
95 lines (83 loc) · 2.81 KB
/
Copy pathuseProperty.js
File metadata and controls
95 lines (83 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* 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 './useBlock'
const { SCHEMA_DATA_TYPE } = constants
// 遍历区块属性,查找已关联的组件属性
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 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 resetLink = (properties) => {
if (properties && Array.isArray(properties)) {
properties.forEach((group) => {
if (group?.content && Array.isArray(group.content)) {
group.content.forEach((property) => {
property.linked = null
})
}
})
}
}
export default ({ 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
}
}