-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathcorner-cell.ts
368 lines (324 loc) · 9.18 KB
/
corner-cell.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import type { Point } from '@antv/g-canvas';
import {
cond,
constant,
isEmpty,
isEqual,
last,
matches,
max,
stubTrue,
} from 'lodash';
import {
CellTypes,
ELLIPSIS_SYMBOL,
EXTRA_FIELD,
KEY_GROUP_CORNER_RESIZE_AREA,
ResizeAreaEffect,
ResizeDirectionType,
S2Event,
} from '../common/constant';
import { CellBorderPosition } from '../common/interface';
import type { FormatResult, TextTheme } from '../common/interface';
import { CornerNodeType } from '../common/interface/node';
import type { CornerHeaderConfig } from '../facet/header/corner';
import {
getBorderPositionAndStyle,
getTextPosition,
getVerticalPosition,
} from '../utils/cell/cell';
import { formattedFieldValue } from '../utils/cell/header-cell';
import { renderLine, renderText, renderTreeIcon } from '../utils/g-renders';
import {
getOrCreateResizeAreaGroupById,
getResizeAreaAttrs,
} from '../utils/interaction/resize';
import { isIPhoneX } from '../utils/is-mobile';
import { getEllipsisText, getEmptyPlaceholder } from '../utils/text';
import { shouldAddResizeArea } from './../utils/interaction/resize';
import { HeaderCell } from './header-cell';
export class CornerCell extends HeaderCell {
protected declare headerConfig: CornerHeaderConfig;
protected isBolderText() {
const { cornerType } = this.meta;
return cornerType === CornerNodeType.Col;
}
/* 角头 label 类型 */
public cornerType: CornerNodeType;
public get cellType() {
return CellTypes.CORNER_CELL;
}
protected initCell() {
super.initCell();
this.resetTextAndConditionIconShapes();
this.drawBackgroundShape();
this.drawTreeIcon();
this.drawCellText();
this.drawConditionIconShapes();
this.drawActionIcons();
this.drawBorderShape();
this.drawResizeArea();
this.update();
}
/**
* @deprecated 已废弃, 请使用 drawTextShape
*/
protected drawCellText() {
this.drawTextShape();
}
protected drawTextShape() {
const { x } = this.getContentArea();
const { y, height } = this.getCellArea();
const textStyle = this.getTextStyle();
const cornerText = this.getCornerText();
// 当为树状结构下需要计算文本前收起展开的icon占的位置
const maxWidth = this.getMaxTextWidth();
const emptyPlaceholder = getEmptyPlaceholder(
this.meta,
this.spreadsheet.options.placeholder,
);
const { measureTextWidth } = this.spreadsheet;
const text = getEllipsisText({
measureTextWidth,
text: cornerText,
maxWidth,
fontParam: textStyle,
placeholder: emptyPlaceholder,
});
this.actualText = text;
const ellipseIndex = text.indexOf(ELLIPSIS_SYMBOL);
let firstLine = text;
let secondLine = '';
// 存在文字的省略号 & 展示为tree结构
if (ellipseIndex !== -1 && this.spreadsheet.isHierarchyTreeType()) {
// 剪裁到 ... 最有点的后1个像素位置
const lastIndex = ellipseIndex + (isIPhoneX() ? 1 : 0);
firstLine = cornerText.substr(0, lastIndex);
secondLine = cornerText.slice(lastIndex);
// 第二行重新计算...逻辑
secondLine = getEllipsisText({
measureTextWidth,
text: secondLine,
maxWidth,
fontParam: textStyle,
});
}
const { x: textX } = getTextPosition(
{
x: x + this.getTreeIconWidth(),
y,
width: maxWidth,
height,
},
textStyle,
);
const textY = y + (isEmpty(secondLine) ? height / 2 : height / 4);
// first line
this.addTextShape(
renderText(
this,
[this.textShapes[0]],
textX,
textY,
firstLine,
textStyle,
),
);
// second line
if (!isEmpty(secondLine)) {
this.addTextShape(
renderText(
this,
[this.textShapes[1]],
textX,
y + height * 0.75,
secondLine,
textStyle,
),
);
}
this.actualTextWidth = max([
measureTextWidth(firstLine, textStyle),
measureTextWidth(secondLine, textStyle),
]);
}
/**
* 绘制折叠展开的icon
*/
protected drawTreeIcon() {
if (!this.showTreeIcon() || this.meta.cornerType === CornerNodeType.Col) {
return;
}
const { hierarchyCollapse } = this.headerConfig;
const { size } = this.getStyle().icon;
const { textBaseline, fill } = this.getTextStyle();
const area = this.getContentArea();
this.treeIcon = renderTreeIcon(
this,
{
x: area.x,
y: getVerticalPosition(area, textBaseline, size),
width: size,
height: size,
},
fill,
hierarchyCollapse,
() => {
this.headerConfig.spreadsheet.store.set('scrollY', 0);
this.headerConfig.spreadsheet.emit(
S2Event.LAYOUT_TREE_ROWS_COLLAPSE_ALL,
hierarchyCollapse,
);
},
);
}
/**
* Render cell horizontalBorder border
* @protected
*/
protected drawBorderShape() {
[CellBorderPosition.TOP, CellBorderPosition.LEFT].forEach((type) => {
const { position, style } = getBorderPositionAndStyle(
type,
this.getCellArea(),
this.getStyle().cell,
);
renderLine(this, position, style);
});
}
protected isLastRowCornerCell() {
const { cornerType, field } = this.meta;
const { rows } = this.headerConfig;
return (
cornerType === CornerNodeType.Row &&
(this.spreadsheet.isHierarchyTreeType() || last(rows) === field)
);
}
protected getResizeAreaEffect() {
const { cornerType } = this.meta;
if (cornerType === CornerNodeType.Series) {
return ResizeAreaEffect.Series;
}
return this.isLastRowCornerCell() && this.spreadsheet.isHierarchyTreeType()
? ResizeAreaEffect.Tree
: ResizeAreaEffect.Field;
}
protected drawResizeArea() {
if (!this.shouldDrawResizeAreaByType('cornerCellHorizontal', this)) {
return;
}
const resizeStyle = this.getResizeAreaStyle();
const resizeArea = getOrCreateResizeAreaGroupById(
this.spreadsheet,
KEY_GROUP_CORNER_RESIZE_AREA,
);
const {
position,
scrollX,
scrollY,
width: headerWidth,
height: headerHeight,
} = this.headerConfig;
const { x, y, width, height, field, cornerType } = this.meta;
const resizeAreaBBox = {
x: x + width - resizeStyle.size / 2,
y,
width: resizeStyle.size,
height,
};
const resizeClipAreaBBox = {
x: 0,
y: 0,
width: headerWidth,
height: headerHeight,
};
if (
cornerType === CornerNodeType.Col ||
!shouldAddResizeArea(resizeAreaBBox, resizeClipAreaBBox, {
scrollX,
scrollY,
})
) {
return;
}
// 将相对坐标映射到全局坐标系中
// 最后一个维度需要撑满角头高度
const offsetX = position.x + x - scrollX;
const offsetY = position.y + (this.isLastRowCornerCell() ? 0 : y);
resizeArea.addShape('rect', {
attrs: {
...getResizeAreaAttrs({
theme: resizeStyle,
id: field,
type: ResizeDirectionType.Horizontal,
effect: this.getResizeAreaEffect(),
offsetX,
offsetY,
width,
height,
meta: this.meta,
}),
x: offsetX + width - resizeStyle.size / 2,
y: offsetY,
height: this.isLastRowCornerCell() ? headerHeight : height,
},
});
}
protected showTreeIcon() {
// 批量折叠或者展开的icon,只存在树状结构的第一个cell前
return this.spreadsheet.isHierarchyTreeType() && this.meta?.x === 0;
}
protected getIconPosition(): Point {
const textCfg = this.textShapes?.[0]?.cfg.attrs;
const { textBaseline, textAlign } = this.getTextStyle();
const { size, margin } = this.getStyle().icon;
const iconX =
textCfg?.x +
cond([
[matches('center'), constant(this.actualTextWidth / 2)],
[matches('right'), constant(0)],
[stubTrue, constant(this.actualTextWidth)],
])(textAlign) +
margin.left;
const iconY = getVerticalPosition(
this.getContentArea(),
textBaseline,
size,
);
return { x: iconX, y: iconY };
}
protected getTreeIconWidth() {
const { size, margin } = this.getStyle().icon;
return this.showTreeIcon() ? size + margin.right : 0;
}
protected getTextStyle(): TextTheme {
const { text, bolderText } = this.getStyle();
const cornerTextStyle = this.isBolderText() ? text : bolderText;
const fill = this.getTextConditionFill(cornerTextStyle);
return {
...cornerTextStyle,
fill,
};
}
protected getMaxTextWidth(): number {
const { width } = this.getContentArea();
return width - this.getTreeIconWidth() - this.getActionIconsWidth();
}
protected getTextPosition(): Point {
return {
x: 0,
y: 0,
};
}
// corner cell 不需要使用formatter进行格式化
protected getFormattedFieldValue(): FormatResult {
return formattedFieldValue(
this.meta,
this.spreadsheet.dataSet.getFieldName(this.meta.label),
);
}
protected getCornerText(): string {
const { formattedValue } = this.getFormattedFieldValue();
return formattedValue;
}
}