index.vue
4.72 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
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
125
126
127
128
129
130
<template>
<div class="go-chart-configurations-data" v-if="targetData">
<n-space vertical>
<n-tag type="success"> 分组返回的数据源 </n-tag>
<n-card size="small">
<n-code word-wrap :code="toString(targetData.option.dataset)" language="json"></n-code>
</n-card>
<n-tag type="info"> 目前支持实时多属性 </n-tag>
<n-divider />
<div v-for="(item, index) in groupList" :key="item.key + index">
<n-space justify="space-between">
<n-ellipsis> 组件id </n-ellipsis>
<n-input size="small" v-model:value="item.id" :disabled="true"></n-input>
</n-space>
<n-space justify="space-between">
<n-ellipsis> 组件名称 </n-ellipsis>
<n-input size="small" v-model:value="item.chartConfig.title" :disabled="true"></n-input>
</n-space>
<n-space vertical justify="space-between">
<n-ellipsis> 选择键值 </n-ellipsis>
<n-select size="small" :placeholder="String(!item.option.selectKey?'':item.option.selectKey)"
:options="dataCacheKeys" @update:value="(key:string,options:SelectOption[]) => handleSelectDataKey(key,options,item.id,groupList!)">
</n-select>
</n-space>
<n-space vertical justify="space-between">
<n-ellipsis>数据内容 </n-ellipsis>
<n-input
type="textarea"
size="small"
:placeholder="String(item.option.dataset)"
></n-input>
</n-space>
<n-divider />
</div>
</n-space>
</div>
</template>
<script setup lang="ts">
import { toRefs, ref, watch,computed } from 'vue'
import { useTargetData } from '../../hooks/useTargetData.hook'
import { toString } from '@/utils'
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
import { datasetType, saveHistoryInputValueListType, historyInputValue } from './index.d'
import { uniqBy } from 'lodash'
import { SelectOption } from 'naive-ui'
const { targetData } = useTargetData()
const { groupList, option } = toRefs(targetData.value as unknown as Recordable)
const saveHistoryInputValueList = ref<saveHistoryInputValueListType>([])
const handleSelectDataKey = (key:string, _:SelectOption[], currentComponentId:string, groupList:CreateComponentType[]) => {
saveHistoryInputValueList.value.unshift({
id:currentComponentId,
inputValue:key
})
handleGroupListById(groupList, currentComponentId, key, option.value.dataset)
}
const executeFn = (inputValue: string, dataset: any) => {
try {
return Function('res', `return ${dataset[inputValue]}`)(dataset)
} catch (e) {
return inputValue
}
}
const setTargetDataset = (inputValue: string, dataset: datasetType) => {
//不能用!,会排除0,0也是需要显示的
return executeFn(inputValue, dataset) === undefined ? '' : executeFn(inputValue, dataset)
}
const handleGroupListById = (
groupList: CreateComponentType[],
id: string,
inputValue: string,
dataset: datasetType
) => {
//原生for循环优化性能
for (let index = 0, total = groupList.length; index < total; index++) {
const targetItem = groupList[index]
if (id === targetItem.id) {
targetItem.option.selectKey = inputValue // 回显选择的下拉框
targetItem.option.dataset = setTargetDataset(inputValue, dataset)
}
}
}
//键值
const dataCacheKeys = computed(()=>{
const datasetCache = targetData.value.option.dataset
if(!datasetCache) return []
if(datasetCache.constructor === Object){
//暂且不兼容嵌套对象
if(Reflect.ownKeys(datasetCache).length !== 0){
return Object.keys(datasetCache).map((dataItem:string)=>({label:dataItem,value:dataItem})) as SelectOption[]
}
}
return []
})
watch(
() => targetData.value,
(newValue: CreateComponentType | CreateComponentGroupType) => {
try {
const uniqHistoryInputValueList = uniqBy(saveHistoryInputValueList.value, 'id')
if (uniqHistoryInputValueList) {
newValue.saveHistoryInput = JSON.stringify(uniqHistoryInputValueList)
window.localStorage.setItem('CACHE_HISTORY_INPUT_VALUE', JSON.stringify(uniqHistoryInputValueList))
}
newValue?.groupList?.forEach((item: CreateComponentType) => {
uniqHistoryInputValueList.forEach((uniqueItem: historyInputValue) => {
if (uniqueItem.id === item.id) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
handleGroupListById(newValue.groupList!, uniqueItem.id, uniqueItem.inputValue, newValue.option.dataset)
}
})
})
} catch (e) {
console.log(e)
}
},
{
deep: true
}
)
</script>