-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy pathTable.ts
More file actions
224 lines (205 loc) · 5.93 KB
/
Table.ts
File metadata and controls
224 lines (205 loc) · 5.93 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { BaseChart, type ChartAxis, type ChartData } from '@/views/chat/component/BaseChart.ts'
import {
copyToClipboard,
type S2DataConfig,
S2Event,
type S2MountContainer,
type S2Options,
type SortMethod,
TableSheet,
} from '@antv/s2'
import { debounce, filter } from 'lodash-es'
import { i18n } from '@/i18n'
import '@antv/s2/dist/s2.min.css'
const { t } = i18n.global
export class Table extends BaseChart {
table?: TableSheet = undefined
container: S2MountContainer | null = null
debounceRender: any
resizeObserver: ResizeObserver
constructor(id: string) {
super(id, 'table')
this.container = document.getElementById(id)
this.debounceRender = debounce(async (width?: number, height?: number) => {
if (this.table) {
this.table.changeSheetSize(width, height)
await this.table.render(false)
}
}, 200)
this.resizeObserver = new ResizeObserver(([entry] = []) => {
const [size] = entry.borderBoxSize || []
this.debounceRender(size.inlineSize, size.blockSize)
})
if (this.container?.parentElement) {
this.resizeObserver.observe(this.container.parentElement)
}
}
init(axis: Array<ChartAxis>, data: Array<ChartData>) {
super.init(
filter(axis, (a) => !a.hidden), //隐藏多指标的other-info列
data
)
const s2DataConfig: S2DataConfig = {
sortParams:
this.axis?.map((a) => {
return {
sortFieldId: a.value,
}
}) ?? [],
fields: {
columns: this.axis?.map((a) => a.value) ?? [],
},
meta:
this.axis?.map((a) => {
return {
field: a.value,
name: a.name,
}
}) ?? [],
data: this.data,
}
const sortState: Record<string, string> = {}
const handleSortClick = (params: any) => {
const { meta } = params
const s2 = meta.spreadsheet
if (s2 && meta.isLeaf) {
const fieldId = meta.field
const currentMethod = sortState[fieldId] || 'none'
const sortOrder = ['none', 'desc', 'asc']
const nextMethod = sortOrder[(sortOrder.indexOf(currentMethod) + 1) % sortOrder.length]
sortState[fieldId] = nextMethod
s2.groupSortByMethod(nextMethod === 'none' ? 'none' : (nextMethod as SortMethod), meta)
s2.render()
}
}
const s2Options: S2Options = {
width: 600,
height: 360,
showDefaultHeaderActionIcon: false,
headerActionIcons: [
{
icons: ['GlobalDesc'],
belongsCell: 'colCell',
displayCondition: (node: any) => node.isLeaf && sortState[node.field] === 'desc',
onClick: handleSortClick,
},
{
icons: ['GlobalAsc'],
belongsCell: 'colCell',
displayCondition: (node: any) => node.isLeaf && sortState[node.field] === 'asc',
onClick: handleSortClick,
},
{
icons: ['SortDown'],
belongsCell: 'colCell',
displayCondition: (node: any) =>
node.isLeaf && (!sortState[node.field] || sortState[node.field] === 'none'),
onClick: handleSortClick,
},
],
tooltip: {
operation: {
sort: true,
},
},
// 如果有省略号, 复制到的是完整文本
interaction: {
copy: {
enable: true,
withFormat: true,
withHeader: true,
},
brushSelection: {
dataCell: true,
rowCell: true,
colCell: true,
},
},
placeholder: {
cell: '-',
empty: {
icon: 'Empty',
description: 'No Data',
},
},
}
if (this.container) {
this.table = new TableSheet(this.container, s2DataConfig, s2Options)
// right click
this.table.on(S2Event.GLOBAL_COPIED, (data) => {
ElMessage.success(t('qa.copied'))
console.debug('copied: ', data)
})
this.table.getCanvasElement().addEventListener('contextmenu', (event) => {
event.preventDefault()
})
this.table.on(S2Event.GLOBAL_CONTEXT_MENU, (event) => copyData(event, this.table))
// this.table.on(S2Event.RANGE_SORT, (sortParams) => {
// console.log('sortParams:', sortParams)
// })
}
}
render() {
this.table?.render()
}
destroy() {
this.table?.destroy()
this.resizeObserver?.disconnect()
}
}
function copyData(event: any, s2?: TableSheet) {
event.preventDefault()
if (!s2) {
return
}
const cells = s2.interaction.getCells()
if (cells.length == 0) {
return
} else if (cells.length == 1) {
const c = cells[0]
const cellMeta = s2.facet.getCellMeta(c.rowIndex, c.colIndex)
if (cellMeta) {
let value = cellMeta.fieldValue
if (value === null || value === undefined) {
value = '-'
}
value = value + ''
copyToClipboard(value).finally(() => {
ElMessage.success(t('qa.copied'))
console.debug('copied:', cellMeta.fieldValue)
})
}
return
} else {
let currentRowIndex = -1
let currentRowData: Array<string> = []
const rowData: Array<string> = []
for (let i = 0; i < cells.length; i++) {
const c = cells[i]
const cellMeta = s2.facet.getCellMeta(c.rowIndex, c.colIndex)
if (!cellMeta) {
continue
}
if (currentRowIndex == -1) {
currentRowIndex = c.rowIndex
}
if (c.rowIndex !== currentRowIndex) {
rowData.push(currentRowData.join('\t'))
currentRowData = []
currentRowIndex = c.rowIndex
}
let value = cellMeta.fieldValue
if (value === null || value === undefined) {
value = '-'
}
value = value + ''
currentRowData.push(value)
}
rowData.push(currentRowData.join('\t'))
const finalValue = rowData.join('\n')
copyToClipboard(finalValue).finally(() => {
ElMessage.success(t('qa.copied'))
console.debug('copied:\n', finalValue)
})
}
}