-
-
Notifications
You must be signed in to change notification settings - Fork 476
/
checkbox-widget.ts
205 lines (178 loc) · 7.67 KB
/
checkbox-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
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
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { IMouseEvent, IPointerEvent, UniverRenderingContext2D } from '@univerjs/engine-render';
import { Checkbox, fixLineWidthByScale, Transform } from '@univerjs/engine-render';
import { HorizontalAlign, ICommandService, isFormulaString, ThemeService, VerticalAlign } from '@univerjs/core';
import type { ICellRenderContext, IDataValidationRule, IStyleData, Nullable } from '@univerjs/core';
import type { ISetRangeValuesCommandParams } from '@univerjs/sheets';
import { SetRangeValuesCommand } from '@univerjs/sheets';
import type { BaseDataValidator, IBaseDataValidationWidget, IFormulaResult } from '@univerjs/data-validation';
import { Inject } from '@wendellhu/redi';
import { CHECKBOX_FORMULA_1, CHECKBOX_FORMULA_2 } from '../validators/checkbox-validator';
import { DataValidationFormulaService } from '../services/dv-formula.service';
import { getFormulaResult } from '../utils/formula';
import { getCellValueOrigin } from '../utils/get-cell-data-origin';
const MARGIN_H = 6;
export class CheckboxRender implements IBaseDataValidationWidget {
private _calc(cellInfo: { startX: number; endX: number; startY: number;endY: number }, style: Nullable<IStyleData>) {
const { vt, ht } = style || {};
const width = cellInfo.endX - cellInfo.startX - (MARGIN_H * 2);
const height = cellInfo.endY - cellInfo.startY;
const size = (style?.fs ?? 10) * 1.6;
let widgetLeft = 0;
let widgetTop = 0;
switch (vt) {
case VerticalAlign.TOP:
widgetTop = 0;
break;
case VerticalAlign.BOTTOM:
widgetTop = 0 + (height - size);
break;
default:
widgetTop = 0 + (height - size) / 2;
break;
}
switch (ht) {
case HorizontalAlign.LEFT:
widgetLeft = MARGIN_H;
break;
case HorizontalAlign.RIGHT:
widgetLeft = MARGIN_H + (width - size);
break;
default:
widgetLeft = MARGIN_H + (width - size) / 2;
break;
}
return {
left: cellInfo.startX + widgetLeft,
top: cellInfo.startY + widgetTop,
width: (style?.fs ?? 10) * 1.6,
height: (style?.fs ?? 10) * 1.6,
};
}
constructor(
@ICommandService private readonly _commandService: ICommandService,
@Inject(DataValidationFormulaService) private readonly _formulaService: DataValidationFormulaService,
@Inject(ThemeService) private readonly _themeService: ThemeService
) {
// super
}
calcCellAutoHeight(info: ICellRenderContext): number | undefined {
const { style } = info;
return (style?.fs ?? 10) * 1.6;
}
private async _parseFormula(rule: IDataValidationRule, unitId: string, subUnitId: string): Promise<IFormulaResult> {
const { formula1 = CHECKBOX_FORMULA_1, formula2 = CHECKBOX_FORMULA_2 } = rule;
const results = await this._formulaService.getRuleFormulaResult(unitId, subUnitId, rule.uid);
return {
formula1: isFormulaString(formula1) ? getFormulaResult(results?.[0]?.result) : formula1,
formula2: isFormulaString(formula2) ? getFormulaResult(results?.[1]?.result) : formula2,
};
}
drawWith(ctx: UniverRenderingContext2D, info: ICellRenderContext): void {
const { style, data, primaryWithCoord, unitId, subUnitId } = info;
const cellBounding = primaryWithCoord.isMergedMainCell ? primaryWithCoord.mergeInfo : primaryWithCoord;
const value = getCellValueOrigin(data);
const rule = data.dataValidation?.rule;
const validator = data.dataValidation?.validator as BaseDataValidator;
if (!rule || !validator) {
return;
}
const colors = this._themeService.getCurrentTheme();
if (!validator.skipDefaultFontRender(rule, value, { unitId, subUnitId })) {
return;
}
const { formula1 = CHECKBOX_FORMULA_1 } = rule;
const layout = this._calc(cellBounding, style);
const { a: scaleX, d: scaleY } = ctx.getTransform();
const left = fixLineWidthByScale(layout.left, scaleX);
const top = fixLineWidthByScale(layout.top, scaleY);
const transform = Transform.create().composeMatrix({
left,
top,
scaleX: 1,
scaleY: 1,
angle: 0,
skewX: 0,
skewY: 0,
flipX: false,
flipY: false,
});
const cellWidth = cellBounding.endX - cellBounding.startX;
const cellHeight = cellBounding.endY - cellBounding.startY;
ctx.save();
ctx.beginPath();
ctx.rect(cellBounding.startX, cellBounding.startY, cellWidth, cellHeight);
ctx.clip();
const m = transform.getMatrix();
ctx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
const size = (style?.fs ?? 10) * 1.6;
const checked = String(value) === String(formula1);
const defaultColor = colors.hyacinth500;
Checkbox.drawWith(ctx, {
checked,
width: size,
height: size,
fill: style?.cl?.rgb ?? defaultColor,
});
ctx.restore();
}
isHit(evt: { x: number;y: number }, info: ICellRenderContext): boolean {
const cellBounding = info.primaryWithCoord.isMergedMainCell ? info.primaryWithCoord.mergeInfo : info.primaryWithCoord;
const layout = this._calc(cellBounding, info.style);
const startY = layout.top;
const endY = layout.top + layout.height;
const startX = layout.left;
const endX = layout.left + layout.width;
const { x: offsetX, y: offsetY } = evt;
if (offsetX <= endX && offsetX >= startX && offsetY <= endY && offsetY >= startY) {
return true;
}
return false;
}
async onPointerDown(info: ICellRenderContext, evt: IPointerEvent | IMouseEvent) {
if (evt.button === 2) {
return;
}
const { primaryWithCoord, unitId, subUnitId, data } = info;
const value = getCellValueOrigin(data);
const rule = data.dataValidation?.rule;
const validator = data.dataValidation?.validator as BaseDataValidator;
if (!rule || !validator) {
return;
}
if (!validator.skipDefaultFontRender(rule, value, { unitId, subUnitId })) {
return;
}
const { formula1, formula2 } = await this._parseFormula(rule, unitId!, subUnitId);
const params: ISetRangeValuesCommandParams = {
range: {
startColumn: primaryWithCoord.actualColumn,
endColumn: primaryWithCoord.actualColumn,
startRow: primaryWithCoord.actualRow,
endRow: primaryWithCoord.actualRow,
},
value: {
v: String(value) === String(formula1) ? formula2 : formula1,
p: null,
},
};
this._commandService.executeCommand(
SetRangeValuesCommand.id,
params
);
};
}