-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidget.ts
124 lines (109 loc) · 3 KB
/
widget.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { resolveComponent, type ConcreteComponent } from 'vue'
import type {
Schema,
PresetConfig,
WidgetPresetConfig,
WidgetChildrenItem
} from '../types'
import { SchemaKeys } from '../shared'
// 判断是配置对象
export function isWidgetPresetConfig(
config: any
): config is WidgetPresetConfig {
return typeof config === 'object' && 'widget' in config
}
// 获取组件预置配置
function getWidgetPresetConfig(
config: PresetConfig,
widgetType: string
): WidgetPresetConfig | undefined {
if (!widgetType) {
console.warn('widget is required')
return
}
const widgetConfig = config?.widgets?.[widgetType]
if (!widgetConfig) return
if (!isWidgetPresetConfig(widgetConfig))
return {
widget: widgetConfig
}
return widgetConfig
}
// 通过组件类型获取组件
export function getComponentByType(
config: PresetConfig,
widgetType: string
): {
component: ConcreteComponent | string
presetProps: object
} {
// 获取预设配置
const widgetPresetConfig = getWidgetPresetConfig(config, widgetType)
const component: ConcreteComponent | string =
widgetPresetConfig?.widget || widgetType
const presetProps = widgetPresetConfig?.props || {}
return typeof component === 'string'
? {
component: resolveComponent(component),
presetProps
}
: {
component,
presetProps
}
}
// 通过schema获取组件
export function getComponent(
config: PresetConfig,
schema: { [SchemaKeys.WidgetType]?: string },
defaultWidgetType?: string
): {
component: ConcreteComponent | string
presetProps: object
} {
if (schema[SchemaKeys.HtmlType])
return {
component: schema[SchemaKeys.HtmlType],
presetProps: {}
}
const widgetType = schema[SchemaKeys.WidgetType] || defaultWidgetType
if (!widgetType) {
console.warn('widget is required')
return {
component: 'div',
presetProps: {}
}
}
return getComponentByType(config, widgetType)
}
// 获取 prop 映射
export function getMappingProp(
config: PresetConfig,
widgetType: string | undefined,
prop: string,
defaultProp: string
) {
if (!widgetType) return defaultProp
const widgetPresetConfig = getWidgetPresetConfig(config, widgetType)
if (widgetPresetConfig) {
return widgetPresetConfig?.propMapping?.[prop] || defaultProp
}
return defaultProp
}
// options 映射成 children
export function getChildren(
config: PresetConfig,
schema: Schema
): WidgetChildrenItem[] {
if (schema[SchemaKeys.WidgetChildren])
return schema[SchemaKeys.WidgetChildren]
const widgetType = schema[SchemaKeys.WidgetType]
if (!widgetType) return []
const widgetPresetConfig = getWidgetPresetConfig(config, widgetType)
if (widgetPresetConfig && widgetPresetConfig?.generateChildren) {
return widgetPresetConfig.generateChildren(
schema?.[SchemaKeys.WidgetProps]?.options || []
)
}
return []
}