importTemplate.ts
3.89 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// 获取实例
const eTemplateString = `
console.log(e)
`
// 获取全局 echarts 实例
const echartsTemplateString = `
console.log(echarts)
`
// 获取当前组件图表集合
const componentsTemplateString = `
console.log(components)
`
// 获取 nodeModules 实例
const nodeModulesTemplateString = `
console.log(node_modules)
`
// 添加点击事件
const addClickTemplateString = `
// 在渲染之后才能获取 dom 实例
e.el.addEventListener('click', () => {
alert('我触发拉~');
}, false)
`
// 异步引入
const importTemplateString = `
await import('https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/lodash.js/4.17.21/lodash.js')
// lodash 默认赋值给 "_"
console.log('isEqual', _.isEqual(['1'], ['1']))
`
// 修改图表 tooltip
const tooltipTemplateString =
`
// 获取echart实例
const chart = this.refs.vChartRef.chart
// 图表设置tooltip
chart.setOption({
tooltip: {
trigger: 'axis', //item
enterable: true,
formatter (params) {
return` +
'`' +
`
<div>
<img src="https://portrait.gitee.com/uploads/avatars/user/1654/4964818_MTrun_1653229420.png!avatar30">
<b><a href="https://gitee.com/dromara/go-view">《这是一个自定义的tooltip》</a></b>
<div>
<div style='border-radius:35px;color:#666'>
` +
'$' +
`{Object.entries(params[0].value).map(kv => ` +
'`' +
`<div>` +
'$' +
`{kv[0]}:` +
'$' +
`{kv[1]}</div>` +
'`' +
`).join('')}
</div>
` +
'`;' +
`
},
}
})
`
// 添加【轮播列表】样式
const addStyleString =
`
// 组件样式作用域标识
const scoped = this.subTree.scopeId
function loadStyleString(css){
let style = document.createElement('style')
style.type = 'text/css'
style.appendChild(document.createTextNode(css))
let head = document.getElementsByTagName('head')[0]
head.appendChild(style)
}
loadStyleString(` +
'`' +
`
.dv-scroll-board[` +
'$' +
`{scoped}] {
position: relative;
overflow: hidden;
}
.dv-scroll-board[` +
'$' +
`{scoped}]::before {
content: '';
display: block;
position: absolute;
top: -20%;
left: -100%;
width: 550px;
height: 60px;
transform: rotate(-45deg);
background-image: linear-gradient(rgba(0, 0, 0, 0), rgba(255, 255, 255, 0.3), rgba(0, 0, 0, 0));
animation: cross 2s infinite;
}
@keyframes cross{
to{
top: 80%;
left: 100%;
transform: rotate(-45deg);
}
}
` +
'`' +
`)
`
// 修改地图原点大小
const editMapPointString = `
const chart = this.refs.vChartRef.chart
// 定义地图原点大小 同理可自定义标签等等内容
this.props.chartConfig.option.series[0].symbolSize = (val) => {
return Math.sqrt(val[2]) / 3;
}
this.setupState.vEchartsSetOption();
let i = 0; // 当前轮播索引
const len = 3; // 轮播部分提示
(function showTips() {
const action = (type, dataIndex) => {
chart.dispatchAction({
type,
dataIndex,
seriesIndex: 0,
});
}
setInterval(() => {
action("downplay", i);
action("hideTip", i);
if (i === len) i = 0;
i++;
action("highlight", i);
action("showTip", i);
}, 2000);
})()
`
export const templateList = [
{
description: '获取当前组件实例',
code: eTemplateString
},
{
description: '获取全局 echarts 实例',
code: echartsTemplateString
},
{
description: '获取组件图表集合',
code: componentsTemplateString
},
{
description: '获取 nodeModules 实例',
code: nodeModulesTemplateString
},
{
description: '获取远程 CDN 库',
code: importTemplateString
},
{
description: '设置文字组件点击事件',
code: addClickTemplateString
},
{
description: '修改图表 tooltip',
code: tooltipTemplateString
},
{
description: '添加【轮播列表】样式',
code: addStyleString
},
{
description: '修改【地图】圆点,新增提示自动轮播',
code: editMapPointString
}
]