stk-table-vue 0.6.12 → 0.6.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +213 -201
- package/lib/src/StkTable/StkTable.vue.d.ts +31 -11
- package/lib/src/StkTable/types/index.d.ts +1 -1
- package/lib/stk-table-vue.js +56 -53
- package/package.json +75 -74
- package/src/StkTable/StkTable.vue +1550 -1546
- package/src/StkTable/components/DragHandle.vue +9 -9
- package/src/StkTable/components/SortIcon.vue +6 -6
- package/src/StkTable/const.ts +37 -37
- package/src/StkTable/index.ts +4 -4
- package/src/StkTable/style.less +553 -553
- package/src/StkTable/types/highlightDimOptions.ts +26 -26
- package/src/StkTable/types/index.ts +239 -239
- package/src/StkTable/useAutoResize.ts +91 -91
- package/src/StkTable/useColResize.ts +216 -216
- package/src/StkTable/useFixedCol.ts +148 -148
- package/src/StkTable/useFixedStyle.ts +75 -75
- package/src/StkTable/useGetFixedColPosition.ts +65 -65
- package/src/StkTable/useHighlight.ts +318 -318
- package/src/StkTable/useKeyboardArrowScroll.ts +112 -112
- package/src/StkTable/useThDrag.ts +102 -102
- package/src/StkTable/useTrDrag.ts +118 -118
- package/src/StkTable/useVirtualScroll.ts +445 -447
- package/src/StkTable/utils/constRefUtils.ts +29 -29
- package/src/StkTable/utils/index.ts +212 -212
- package/src/StkTable/utils/useTriggerRef.ts +33 -33
- package/src/VirtualTree.vue +622 -622
- package/src/VirtualTreeSelect.vue +367 -367
- package/src/vite-env.d.ts +10 -10
|
@@ -1,318 +1,318 @@
|
|
|
1
|
-
import { interpolateRgb } from 'd3-interpolate';
|
|
2
|
-
import { Ref, computed } from 'vue';
|
|
3
|
-
import { HIGHLIGHT_CELL_CLASS, HIGHLIGHT_COLOR, HIGHLIGHT_DURATION, HIGHLIGHT_FREQ, HIGHLIGHT_ROW_CLASS } from './const';
|
|
4
|
-
import { HighlightConfig, UniqKey } from './types';
|
|
5
|
-
import { HighlightDimCellOption, HighlightDimRowOption } from './types/highlightDimOptions';
|
|
6
|
-
|
|
7
|
-
type Params = {
|
|
8
|
-
props: any;
|
|
9
|
-
stkTableId: string;
|
|
10
|
-
tableContainerRef: Ref<HTMLDivElement | undefined>;
|
|
11
|
-
};
|
|
12
|
-
/** 存放高亮行信息 */
|
|
13
|
-
type HighlightDimRowStore = {
|
|
14
|
-
/** 动画开始时间戳 */
|
|
15
|
-
readonly ts: number;
|
|
16
|
-
/** 行是否可见 */
|
|
17
|
-
visible: boolean;
|
|
18
|
-
/** 动画关键帧 */
|
|
19
|
-
keyframe: Parameters<Animatable['animate']>['0'];
|
|
20
|
-
/** 动画初始持续时间 */
|
|
21
|
-
readonly duration: number;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 高亮单元格,行
|
|
26
|
-
*/
|
|
27
|
-
export function useHighlight({ props, stkTableId, tableContainerRef }: Params) {
|
|
28
|
-
const config: HighlightConfig = props.highlightConfig;
|
|
29
|
-
|
|
30
|
-
/** 高亮颜色 */
|
|
31
|
-
const highlightColor = {
|
|
32
|
-
light: HIGHLIGHT_COLOR.light,
|
|
33
|
-
dark: HIGHLIGHT_COLOR.dark,
|
|
34
|
-
};
|
|
35
|
-
/** 持续时间 */
|
|
36
|
-
const highlightDuration = config.duration ? config.duration * 1000 : HIGHLIGHT_DURATION;
|
|
37
|
-
/** 高亮频率*/
|
|
38
|
-
const highlightFrequency = config.fps && config.fps > 0 ? 1000 / config.fps : null;
|
|
39
|
-
/** 高亮帧数(非帧率),用于 timing-function: steps() */
|
|
40
|
-
const highlightSteps = highlightFrequency ? Math.round(highlightDuration / highlightFrequency) : null;
|
|
41
|
-
/** 高亮开始 */
|
|
42
|
-
const highlightFrom = computed(() => highlightColor[props.theme as 'light' | 'dark'].from);
|
|
43
|
-
/** 高亮结束 */
|
|
44
|
-
const highlightTo = computed(() => highlightColor[props.theme as 'light' | 'dark'].to);
|
|
45
|
-
const highlightInter = computed(() => interpolateRgb(highlightFrom.value, highlightTo.value));
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 存放高亮行的状态-使用js计算颜色
|
|
49
|
-
* @key 行唯一键
|
|
50
|
-
* @value 记录高亮开始时间
|
|
51
|
-
*/
|
|
52
|
-
const highlightDimRowsJs = new Map<UniqKey, number>();
|
|
53
|
-
/** 是否正在计算高亮行的循环-使用js计算颜色 */
|
|
54
|
-
let calcHighlightDimLoopJs = false;
|
|
55
|
-
/**
|
|
56
|
-
* 存放高亮行的状态-使用animation api实现
|
|
57
|
-
* @key 行唯一键
|
|
58
|
-
* @value 记录高亮配置
|
|
59
|
-
*/
|
|
60
|
-
const highlightDimRowsAnimation = new Map<UniqKey, HighlightDimRowStore>();
|
|
61
|
-
/** 是否正在计算高亮行的循环-使用animation api实现 */
|
|
62
|
-
let calcHighlightDimLoopAnimation = false;
|
|
63
|
-
|
|
64
|
-
/** 高亮后渐暗的行定时器 */
|
|
65
|
-
const highlightDimRowsTimeout = new Map();
|
|
66
|
-
/** 高亮后渐暗的单元格定时器 */
|
|
67
|
-
const highlightDimCellsTimeout = new Map();
|
|
68
|
-
|
|
69
|
-
/** 高亮函数的默认参数 */
|
|
70
|
-
const defaultHighlightDimOption = (() => {
|
|
71
|
-
const keyframe: PropertyIndexedKeyframes = { backgroundColor: [highlightFrom.value, ''] };
|
|
72
|
-
if (highlightSteps) {
|
|
73
|
-
keyframe.easing = `steps(${highlightSteps})`;
|
|
74
|
-
}
|
|
75
|
-
return { duration: highlightDuration, keyframe };
|
|
76
|
-
})();
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 计算高亮渐暗颜色的循环
|
|
80
|
-
*/
|
|
81
|
-
function calcRowHighlightLoop() {
|
|
82
|
-
if (calcHighlightDimLoopAnimation) return;
|
|
83
|
-
calcHighlightDimLoopAnimation = true;
|
|
84
|
-
const recursion = () => {
|
|
85
|
-
window.requestAnimationFrame(
|
|
86
|
-
() => {
|
|
87
|
-
const nowTs = Date.now();
|
|
88
|
-
highlightDimRowsAnimation.forEach((store, rowKeyValue) => {
|
|
89
|
-
const { ts, duration } = store;
|
|
90
|
-
const timeOffset = nowTs - ts;
|
|
91
|
-
if (nowTs - ts < duration) {
|
|
92
|
-
updateRowAnimation(rowKeyValue, store, timeOffset);
|
|
93
|
-
} else {
|
|
94
|
-
highlightDimRowsAnimation.delete(rowKeyValue);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
if (highlightDimRowsAnimation.size > 0) {
|
|
99
|
-
// 还有高亮的行,则下一次循环
|
|
100
|
-
recursion();
|
|
101
|
-
} else {
|
|
102
|
-
// 没有则停止循环
|
|
103
|
-
calcHighlightDimLoopAnimation = false;
|
|
104
|
-
highlightDimRowsAnimation.clear();
|
|
105
|
-
}
|
|
106
|
-
} /* , highlightFrequency */,
|
|
107
|
-
);
|
|
108
|
-
};
|
|
109
|
-
recursion();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* js计算高亮渐暗颜色的循环
|
|
114
|
-
*/
|
|
115
|
-
function calcRowHighlightLoopJs() {
|
|
116
|
-
if (calcHighlightDimLoopJs) return;
|
|
117
|
-
calcHighlightDimLoopJs = true;
|
|
118
|
-
// js计算gradient
|
|
119
|
-
const recursion = () => {
|
|
120
|
-
window.setTimeout(() => {
|
|
121
|
-
const nowTs = Date.now();
|
|
122
|
-
highlightDimRowsJs.forEach((highlightStart, rowKeyValue) => {
|
|
123
|
-
/** 经过的时间 ÷ 高亮持续时间 计算出 颜色过渡进度 (0-1) */
|
|
124
|
-
const progress = (nowTs - highlightStart) / highlightDuration;
|
|
125
|
-
let bgc = '';
|
|
126
|
-
if (0 <= progress && progress <= 1) {
|
|
127
|
-
bgc = highlightInter.value(progress);
|
|
128
|
-
} else {
|
|
129
|
-
highlightDimRowsJs.delete(rowKeyValue);
|
|
130
|
-
}
|
|
131
|
-
updateRowBgcJs(rowKeyValue, bgc);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
if (highlightDimRowsJs.size > 0) {
|
|
135
|
-
// 还有高亮的行,则下一次循环
|
|
136
|
-
recursion();
|
|
137
|
-
} else {
|
|
138
|
-
// 没有则停止循环
|
|
139
|
-
calcHighlightDimLoopJs = false;
|
|
140
|
-
highlightDimRowsJs.clear(); // TODO: 是否需要 清除
|
|
141
|
-
}
|
|
142
|
-
}, highlightFrequency || HIGHLIGHT_FREQ);
|
|
143
|
-
};
|
|
144
|
-
recursion();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 高亮一个单元格。暂不支持虚拟滚动高亮状态记忆。
|
|
149
|
-
* @param rowKeyValue 一行的key
|
|
150
|
-
* @param colKeyValue 列key
|
|
151
|
-
* @param options.method css-使用css渲染,animation-使用animation api。默认animation;
|
|
152
|
-
* @param option.className 自定义css动画的class。
|
|
153
|
-
* @param option.keyframe 同Keyframe https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Animations_API/Keyframe_Formats
|
|
154
|
-
* @param option.duration 动画时长。method='css'状态下,用于移除class,如果传入了className则需要与自定义的动画时间一致。
|
|
155
|
-
*/
|
|
156
|
-
function setHighlightDimCell(rowKeyValue: UniqKey, colKeyValue: string, option: HighlightDimCellOption = {}) {
|
|
157
|
-
const cellEl = tableContainerRef.value?.querySelector<HTMLElement>(`[data-cell-key="${rowKeyValue}--${colKeyValue}"]`);
|
|
158
|
-
const { className, method, duration, keyframe } = {
|
|
159
|
-
className: HIGHLIGHT_CELL_CLASS,
|
|
160
|
-
method: 'animation',
|
|
161
|
-
...defaultHighlightDimOption,
|
|
162
|
-
...option,
|
|
163
|
-
};
|
|
164
|
-
if (!cellEl) return;
|
|
165
|
-
if (method === 'animation') {
|
|
166
|
-
cellEl.animate(keyframe, duration);
|
|
167
|
-
} else {
|
|
168
|
-
highlightCellsInCssKeyFrame(cellEl, rowKeyValue, className, duration);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* 高亮一行
|
|
174
|
-
* @param rowKeyValues 行唯一键的数组
|
|
175
|
-
* @param option.method css-使用css渲染,animation-使用animation api,js-使用js计算颜色。默认animation
|
|
176
|
-
* @param option.className 自定义css动画的class。
|
|
177
|
-
* @param option.keyframe 同Keyframe。 https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Animations_API/Keyframe_Formats
|
|
178
|
-
* @param option.duration 动画时长。method='css'状态下,用于移除class,如果传入了className则需要与自定义的动画时间一致。。
|
|
179
|
-
*/
|
|
180
|
-
function setHighlightDimRow(rowKeyValues: UniqKey[], option: HighlightDimRowOption = {}) {
|
|
181
|
-
if (!Array.isArray(rowKeyValues)) rowKeyValues = [rowKeyValues];
|
|
182
|
-
const { className, method, keyframe, duration } = {
|
|
183
|
-
className: HIGHLIGHT_ROW_CLASS,
|
|
184
|
-
method: 'animation',
|
|
185
|
-
...defaultHighlightDimOption,
|
|
186
|
-
...option,
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
if (method === 'css') {
|
|
190
|
-
// -------- use css keyframe
|
|
191
|
-
highlightRowsInCssKeyframe(rowKeyValues, className, duration);
|
|
192
|
-
} else if (method === 'animation') {
|
|
193
|
-
if (props.virtual) {
|
|
194
|
-
// -------- 用animation 接口实现动画
|
|
195
|
-
const nowTs = Date.now();
|
|
196
|
-
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
197
|
-
const rowKeyValue = rowKeyValues[i];
|
|
198
|
-
const store: HighlightDimRowStore = { ts: nowTs, visible: false, keyframe, duration };
|
|
199
|
-
highlightDimRowsAnimation.set(rowKeyValue, store);
|
|
200
|
-
updateRowAnimation(rowKeyValue, store, 0);
|
|
201
|
-
}
|
|
202
|
-
calcRowHighlightLoop();
|
|
203
|
-
} else {
|
|
204
|
-
// -------- use Element.animate
|
|
205
|
-
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
206
|
-
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValues[i])) as HTMLTableRowElement | null;
|
|
207
|
-
if (!rowEl) continue;
|
|
208
|
-
rowEl.animate(keyframe, duration);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
} else if (method === 'js') {
|
|
212
|
-
// -------- 用js计算颜色渐变的高亮方案
|
|
213
|
-
const nowTs = Date.now();
|
|
214
|
-
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
215
|
-
const rowKeyValue = rowKeyValues[i];
|
|
216
|
-
highlightDimRowsJs.set(rowKeyValue, nowTs);
|
|
217
|
-
updateRowBgcJs(rowKeyValue, highlightFrom.value);
|
|
218
|
-
}
|
|
219
|
-
calcRowHighlightLoopJs();
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* 使用css @keyframes动画,实现高亮行动画
|
|
225
|
-
* 此方案作为兼容方式。v0.3.4 将使用Element.animate 接口实现动画。
|
|
226
|
-
*/
|
|
227
|
-
function highlightRowsInCssKeyframe(rowKeyValues: UniqKey[], className: string, duration: number) {
|
|
228
|
-
/**是否需要重绘 */
|
|
229
|
-
let needRepaint = false;
|
|
230
|
-
const rowElTemp: HTMLTableRowElement[] = [];
|
|
231
|
-
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
232
|
-
const rowKeyValue = rowKeyValues[i];
|
|
233
|
-
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue)) as HTMLTableRowElement | null;
|
|
234
|
-
if (!rowEl) continue;
|
|
235
|
-
if (rowEl.classList.contains(className)) {
|
|
236
|
-
rowEl.classList.remove(className);
|
|
237
|
-
needRepaint = true;
|
|
238
|
-
}
|
|
239
|
-
rowElTemp.push(rowEl);
|
|
240
|
-
// 动画结束移除class
|
|
241
|
-
window.clearTimeout(highlightDimRowsTimeout.get(rowKeyValue));
|
|
242
|
-
highlightDimRowsTimeout.set(
|
|
243
|
-
rowKeyValue,
|
|
244
|
-
window.setTimeout(() => {
|
|
245
|
-
rowEl.classList.remove(className);
|
|
246
|
-
highlightDimRowsTimeout.delete(rowKeyValue); // 回收内存
|
|
247
|
-
}, duration),
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
if (needRepaint) {
|
|
251
|
-
void tableContainerRef.value?.offsetWidth; //强制浏览器重绘
|
|
252
|
-
}
|
|
253
|
-
rowElTemp.forEach(el => el.classList.add(className)); // 统一添加动画
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* 使用css @keyframes动画,实现高亮单元格动画
|
|
258
|
-
* 此方案作为兼容方式。v0.3.4 将使用Element.animate 接口实现动画。
|
|
259
|
-
*/
|
|
260
|
-
function highlightCellsInCssKeyFrame(cellEl: HTMLElement, rowKeyValue: UniqKey, className: string, duration: number) {
|
|
261
|
-
if (cellEl.classList.contains(className)) {
|
|
262
|
-
cellEl.classList.remove(className);
|
|
263
|
-
void cellEl.offsetHeight; // 通知浏览器重绘
|
|
264
|
-
}
|
|
265
|
-
cellEl.classList.add(className);
|
|
266
|
-
window.clearTimeout(highlightDimCellsTimeout.get(rowKeyValue));
|
|
267
|
-
highlightDimCellsTimeout.set(
|
|
268
|
-
rowKeyValue,
|
|
269
|
-
window.setTimeout(() => {
|
|
270
|
-
cellEl.classList.remove(className);
|
|
271
|
-
highlightDimCellsTimeout.delete(rowKeyValue);
|
|
272
|
-
}, duration),
|
|
273
|
-
);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* 更新行状态
|
|
278
|
-
* @param rowKeyValue 行唯一键
|
|
279
|
-
* @param store highlightDimRowStore 的引用对象
|
|
280
|
-
* @param timeOffset 距动画开始经过的时长
|
|
281
|
-
*/
|
|
282
|
-
function updateRowAnimation(rowKeyValue: UniqKey, store: HighlightDimRowStore, timeOffset: number) {
|
|
283
|
-
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue));
|
|
284
|
-
const { visible, keyframe, duration: initialDuration } = store;
|
|
285
|
-
if (!rowEl) {
|
|
286
|
-
if (visible) {
|
|
287
|
-
store.visible = false; // 标记为不可见
|
|
288
|
-
}
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
// 只有元素 不可见 -> 可见 时才需要更新
|
|
292
|
-
if (!visible) {
|
|
293
|
-
store.visible = true; // 标记为可见
|
|
294
|
-
/** 经过的时间 ÷ 高亮持续时间 计算出 颜色过渡进度 (0-1) */
|
|
295
|
-
const iterationStart = timeOffset / initialDuration;
|
|
296
|
-
rowEl.animate(keyframe, {
|
|
297
|
-
duration: initialDuration - timeOffset,
|
|
298
|
-
/** 从什么时候开始,0-1 */
|
|
299
|
-
iterationStart,
|
|
300
|
-
/** 持续多久 0-1 */
|
|
301
|
-
iterations: 1 - iterationStart,
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/** 更新行状态 */
|
|
307
|
-
function updateRowBgcJs(rowKeyValue: UniqKey, color: string) {
|
|
308
|
-
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue));
|
|
309
|
-
if (!rowEl) return;
|
|
310
|
-
rowEl.style.backgroundColor = color;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
return {
|
|
314
|
-
highlightSteps,
|
|
315
|
-
setHighlightDimRow,
|
|
316
|
-
setHighlightDimCell,
|
|
317
|
-
};
|
|
318
|
-
}
|
|
1
|
+
import { interpolateRgb } from 'd3-interpolate';
|
|
2
|
+
import { Ref, computed } from 'vue';
|
|
3
|
+
import { HIGHLIGHT_CELL_CLASS, HIGHLIGHT_COLOR, HIGHLIGHT_DURATION, HIGHLIGHT_FREQ, HIGHLIGHT_ROW_CLASS } from './const';
|
|
4
|
+
import { HighlightConfig, UniqKey } from './types';
|
|
5
|
+
import { HighlightDimCellOption, HighlightDimRowOption } from './types/highlightDimOptions';
|
|
6
|
+
|
|
7
|
+
type Params = {
|
|
8
|
+
props: any;
|
|
9
|
+
stkTableId: string;
|
|
10
|
+
tableContainerRef: Ref<HTMLDivElement | undefined>;
|
|
11
|
+
};
|
|
12
|
+
/** 存放高亮行信息 */
|
|
13
|
+
type HighlightDimRowStore = {
|
|
14
|
+
/** 动画开始时间戳 */
|
|
15
|
+
readonly ts: number;
|
|
16
|
+
/** 行是否可见 */
|
|
17
|
+
visible: boolean;
|
|
18
|
+
/** 动画关键帧 */
|
|
19
|
+
keyframe: Parameters<Animatable['animate']>['0'];
|
|
20
|
+
/** 动画初始持续时间 */
|
|
21
|
+
readonly duration: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 高亮单元格,行
|
|
26
|
+
*/
|
|
27
|
+
export function useHighlight({ props, stkTableId, tableContainerRef }: Params) {
|
|
28
|
+
const config: HighlightConfig = props.highlightConfig;
|
|
29
|
+
|
|
30
|
+
/** 高亮颜色 */
|
|
31
|
+
const highlightColor = {
|
|
32
|
+
light: HIGHLIGHT_COLOR.light,
|
|
33
|
+
dark: HIGHLIGHT_COLOR.dark,
|
|
34
|
+
};
|
|
35
|
+
/** 持续时间 */
|
|
36
|
+
const highlightDuration = config.duration ? config.duration * 1000 : HIGHLIGHT_DURATION;
|
|
37
|
+
/** 高亮频率*/
|
|
38
|
+
const highlightFrequency = config.fps && config.fps > 0 ? 1000 / config.fps : null;
|
|
39
|
+
/** 高亮帧数(非帧率),用于 timing-function: steps() */
|
|
40
|
+
const highlightSteps = highlightFrequency ? Math.round(highlightDuration / highlightFrequency) : null;
|
|
41
|
+
/** 高亮开始 */
|
|
42
|
+
const highlightFrom = computed(() => highlightColor[props.theme as 'light' | 'dark'].from);
|
|
43
|
+
/** 高亮结束 */
|
|
44
|
+
const highlightTo = computed(() => highlightColor[props.theme as 'light' | 'dark'].to);
|
|
45
|
+
const highlightInter = computed(() => interpolateRgb(highlightFrom.value, highlightTo.value));
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 存放高亮行的状态-使用js计算颜色
|
|
49
|
+
* @key 行唯一键
|
|
50
|
+
* @value 记录高亮开始时间
|
|
51
|
+
*/
|
|
52
|
+
const highlightDimRowsJs = new Map<UniqKey, number>();
|
|
53
|
+
/** 是否正在计算高亮行的循环-使用js计算颜色 */
|
|
54
|
+
let calcHighlightDimLoopJs = false;
|
|
55
|
+
/**
|
|
56
|
+
* 存放高亮行的状态-使用animation api实现
|
|
57
|
+
* @key 行唯一键
|
|
58
|
+
* @value 记录高亮配置
|
|
59
|
+
*/
|
|
60
|
+
const highlightDimRowsAnimation = new Map<UniqKey, HighlightDimRowStore>();
|
|
61
|
+
/** 是否正在计算高亮行的循环-使用animation api实现 */
|
|
62
|
+
let calcHighlightDimLoopAnimation = false;
|
|
63
|
+
|
|
64
|
+
/** 高亮后渐暗的行定时器 */
|
|
65
|
+
const highlightDimRowsTimeout = new Map();
|
|
66
|
+
/** 高亮后渐暗的单元格定时器 */
|
|
67
|
+
const highlightDimCellsTimeout = new Map();
|
|
68
|
+
|
|
69
|
+
/** 高亮函数的默认参数 */
|
|
70
|
+
const defaultHighlightDimOption = (() => {
|
|
71
|
+
const keyframe: PropertyIndexedKeyframes = { backgroundColor: [highlightFrom.value, ''] };
|
|
72
|
+
if (highlightSteps) {
|
|
73
|
+
keyframe.easing = `steps(${highlightSteps})`;
|
|
74
|
+
}
|
|
75
|
+
return { duration: highlightDuration, keyframe };
|
|
76
|
+
})();
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 计算高亮渐暗颜色的循环
|
|
80
|
+
*/
|
|
81
|
+
function calcRowHighlightLoop() {
|
|
82
|
+
if (calcHighlightDimLoopAnimation) return;
|
|
83
|
+
calcHighlightDimLoopAnimation = true;
|
|
84
|
+
const recursion = () => {
|
|
85
|
+
window.requestAnimationFrame(
|
|
86
|
+
() => {
|
|
87
|
+
const nowTs = Date.now();
|
|
88
|
+
highlightDimRowsAnimation.forEach((store, rowKeyValue) => {
|
|
89
|
+
const { ts, duration } = store;
|
|
90
|
+
const timeOffset = nowTs - ts;
|
|
91
|
+
if (nowTs - ts < duration) {
|
|
92
|
+
updateRowAnimation(rowKeyValue, store, timeOffset);
|
|
93
|
+
} else {
|
|
94
|
+
highlightDimRowsAnimation.delete(rowKeyValue);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
if (highlightDimRowsAnimation.size > 0) {
|
|
99
|
+
// 还有高亮的行,则下一次循环
|
|
100
|
+
recursion();
|
|
101
|
+
} else {
|
|
102
|
+
// 没有则停止循环
|
|
103
|
+
calcHighlightDimLoopAnimation = false;
|
|
104
|
+
highlightDimRowsAnimation.clear();
|
|
105
|
+
}
|
|
106
|
+
} /* , highlightFrequency */,
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
recursion();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* js计算高亮渐暗颜色的循环
|
|
114
|
+
*/
|
|
115
|
+
function calcRowHighlightLoopJs() {
|
|
116
|
+
if (calcHighlightDimLoopJs) return;
|
|
117
|
+
calcHighlightDimLoopJs = true;
|
|
118
|
+
// js计算gradient
|
|
119
|
+
const recursion = () => {
|
|
120
|
+
window.setTimeout(() => {
|
|
121
|
+
const nowTs = Date.now();
|
|
122
|
+
highlightDimRowsJs.forEach((highlightStart, rowKeyValue) => {
|
|
123
|
+
/** 经过的时间 ÷ 高亮持续时间 计算出 颜色过渡进度 (0-1) */
|
|
124
|
+
const progress = (nowTs - highlightStart) / highlightDuration;
|
|
125
|
+
let bgc = '';
|
|
126
|
+
if (0 <= progress && progress <= 1) {
|
|
127
|
+
bgc = highlightInter.value(progress);
|
|
128
|
+
} else {
|
|
129
|
+
highlightDimRowsJs.delete(rowKeyValue);
|
|
130
|
+
}
|
|
131
|
+
updateRowBgcJs(rowKeyValue, bgc);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
if (highlightDimRowsJs.size > 0) {
|
|
135
|
+
// 还有高亮的行,则下一次循环
|
|
136
|
+
recursion();
|
|
137
|
+
} else {
|
|
138
|
+
// 没有则停止循环
|
|
139
|
+
calcHighlightDimLoopJs = false;
|
|
140
|
+
highlightDimRowsJs.clear(); // TODO: 是否需要 清除
|
|
141
|
+
}
|
|
142
|
+
}, highlightFrequency || HIGHLIGHT_FREQ);
|
|
143
|
+
};
|
|
144
|
+
recursion();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 高亮一个单元格。暂不支持虚拟滚动高亮状态记忆。
|
|
149
|
+
* @param rowKeyValue 一行的key
|
|
150
|
+
* @param colKeyValue 列key
|
|
151
|
+
* @param options.method css-使用css渲染,animation-使用animation api。默认animation;
|
|
152
|
+
* @param option.className 自定义css动画的class。
|
|
153
|
+
* @param option.keyframe 同Keyframe https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Animations_API/Keyframe_Formats
|
|
154
|
+
* @param option.duration 动画时长。method='css'状态下,用于移除class,如果传入了className则需要与自定义的动画时间一致。
|
|
155
|
+
*/
|
|
156
|
+
function setHighlightDimCell(rowKeyValue: UniqKey, colKeyValue: string, option: HighlightDimCellOption = {}) {
|
|
157
|
+
const cellEl = tableContainerRef.value?.querySelector<HTMLElement>(`[data-cell-key="${rowKeyValue}--${colKeyValue}"]`);
|
|
158
|
+
const { className, method, duration, keyframe } = {
|
|
159
|
+
className: HIGHLIGHT_CELL_CLASS,
|
|
160
|
+
method: 'animation',
|
|
161
|
+
...defaultHighlightDimOption,
|
|
162
|
+
...option,
|
|
163
|
+
};
|
|
164
|
+
if (!cellEl) return;
|
|
165
|
+
if (method === 'animation') {
|
|
166
|
+
cellEl.animate(keyframe, duration);
|
|
167
|
+
} else {
|
|
168
|
+
highlightCellsInCssKeyFrame(cellEl, rowKeyValue, className, duration);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 高亮一行
|
|
174
|
+
* @param rowKeyValues 行唯一键的数组
|
|
175
|
+
* @param option.method css-使用css渲染,animation-使用animation api,js-使用js计算颜色。默认animation
|
|
176
|
+
* @param option.className 自定义css动画的class。
|
|
177
|
+
* @param option.keyframe 同Keyframe。 https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Animations_API/Keyframe_Formats
|
|
178
|
+
* @param option.duration 动画时长。method='css'状态下,用于移除class,如果传入了className则需要与自定义的动画时间一致。。
|
|
179
|
+
*/
|
|
180
|
+
function setHighlightDimRow(rowKeyValues: UniqKey[], option: HighlightDimRowOption = {}) {
|
|
181
|
+
if (!Array.isArray(rowKeyValues)) rowKeyValues = [rowKeyValues];
|
|
182
|
+
const { className, method, keyframe, duration } = {
|
|
183
|
+
className: HIGHLIGHT_ROW_CLASS,
|
|
184
|
+
method: 'animation',
|
|
185
|
+
...defaultHighlightDimOption,
|
|
186
|
+
...option,
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
if (method === 'css') {
|
|
190
|
+
// -------- use css keyframe
|
|
191
|
+
highlightRowsInCssKeyframe(rowKeyValues, className, duration);
|
|
192
|
+
} else if (method === 'animation') {
|
|
193
|
+
if (props.virtual) {
|
|
194
|
+
// -------- 用animation 接口实现动画
|
|
195
|
+
const nowTs = Date.now();
|
|
196
|
+
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
197
|
+
const rowKeyValue = rowKeyValues[i];
|
|
198
|
+
const store: HighlightDimRowStore = { ts: nowTs, visible: false, keyframe, duration };
|
|
199
|
+
highlightDimRowsAnimation.set(rowKeyValue, store);
|
|
200
|
+
updateRowAnimation(rowKeyValue, store, 0);
|
|
201
|
+
}
|
|
202
|
+
calcRowHighlightLoop();
|
|
203
|
+
} else {
|
|
204
|
+
// -------- use Element.animate
|
|
205
|
+
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
206
|
+
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValues[i])) as HTMLTableRowElement | null;
|
|
207
|
+
if (!rowEl) continue;
|
|
208
|
+
rowEl.animate(keyframe, duration);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
} else if (method === 'js') {
|
|
212
|
+
// -------- 用js计算颜色渐变的高亮方案
|
|
213
|
+
const nowTs = Date.now();
|
|
214
|
+
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
215
|
+
const rowKeyValue = rowKeyValues[i];
|
|
216
|
+
highlightDimRowsJs.set(rowKeyValue, nowTs);
|
|
217
|
+
updateRowBgcJs(rowKeyValue, highlightFrom.value);
|
|
218
|
+
}
|
|
219
|
+
calcRowHighlightLoopJs();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 使用css @keyframes动画,实现高亮行动画
|
|
225
|
+
* 此方案作为兼容方式。v0.3.4 将使用Element.animate 接口实现动画。
|
|
226
|
+
*/
|
|
227
|
+
function highlightRowsInCssKeyframe(rowKeyValues: UniqKey[], className: string, duration: number) {
|
|
228
|
+
/**是否需要重绘 */
|
|
229
|
+
let needRepaint = false;
|
|
230
|
+
const rowElTemp: HTMLTableRowElement[] = [];
|
|
231
|
+
for (let i = 0; i < rowKeyValues.length; i++) {
|
|
232
|
+
const rowKeyValue = rowKeyValues[i];
|
|
233
|
+
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue)) as HTMLTableRowElement | null;
|
|
234
|
+
if (!rowEl) continue;
|
|
235
|
+
if (rowEl.classList.contains(className)) {
|
|
236
|
+
rowEl.classList.remove(className);
|
|
237
|
+
needRepaint = true;
|
|
238
|
+
}
|
|
239
|
+
rowElTemp.push(rowEl);
|
|
240
|
+
// 动画结束移除class
|
|
241
|
+
window.clearTimeout(highlightDimRowsTimeout.get(rowKeyValue));
|
|
242
|
+
highlightDimRowsTimeout.set(
|
|
243
|
+
rowKeyValue,
|
|
244
|
+
window.setTimeout(() => {
|
|
245
|
+
rowEl.classList.remove(className);
|
|
246
|
+
highlightDimRowsTimeout.delete(rowKeyValue); // 回收内存
|
|
247
|
+
}, duration),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
if (needRepaint) {
|
|
251
|
+
void tableContainerRef.value?.offsetWidth; //强制浏览器重绘
|
|
252
|
+
}
|
|
253
|
+
rowElTemp.forEach(el => el.classList.add(className)); // 统一添加动画
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 使用css @keyframes动画,实现高亮单元格动画
|
|
258
|
+
* 此方案作为兼容方式。v0.3.4 将使用Element.animate 接口实现动画。
|
|
259
|
+
*/
|
|
260
|
+
function highlightCellsInCssKeyFrame(cellEl: HTMLElement, rowKeyValue: UniqKey, className: string, duration: number) {
|
|
261
|
+
if (cellEl.classList.contains(className)) {
|
|
262
|
+
cellEl.classList.remove(className);
|
|
263
|
+
void cellEl.offsetHeight; // 通知浏览器重绘
|
|
264
|
+
}
|
|
265
|
+
cellEl.classList.add(className);
|
|
266
|
+
window.clearTimeout(highlightDimCellsTimeout.get(rowKeyValue));
|
|
267
|
+
highlightDimCellsTimeout.set(
|
|
268
|
+
rowKeyValue,
|
|
269
|
+
window.setTimeout(() => {
|
|
270
|
+
cellEl.classList.remove(className);
|
|
271
|
+
highlightDimCellsTimeout.delete(rowKeyValue);
|
|
272
|
+
}, duration),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 更新行状态
|
|
278
|
+
* @param rowKeyValue 行唯一键
|
|
279
|
+
* @param store highlightDimRowStore 的引用对象
|
|
280
|
+
* @param timeOffset 距动画开始经过的时长
|
|
281
|
+
*/
|
|
282
|
+
function updateRowAnimation(rowKeyValue: UniqKey, store: HighlightDimRowStore, timeOffset: number) {
|
|
283
|
+
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue));
|
|
284
|
+
const { visible, keyframe, duration: initialDuration } = store;
|
|
285
|
+
if (!rowEl) {
|
|
286
|
+
if (visible) {
|
|
287
|
+
store.visible = false; // 标记为不可见
|
|
288
|
+
}
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
// 只有元素 不可见 -> 可见 时才需要更新
|
|
292
|
+
if (!visible) {
|
|
293
|
+
store.visible = true; // 标记为可见
|
|
294
|
+
/** 经过的时间 ÷ 高亮持续时间 计算出 颜色过渡进度 (0-1) */
|
|
295
|
+
const iterationStart = timeOffset / initialDuration;
|
|
296
|
+
rowEl.animate(keyframe, {
|
|
297
|
+
duration: initialDuration - timeOffset,
|
|
298
|
+
/** 从什么时候开始,0-1 */
|
|
299
|
+
iterationStart,
|
|
300
|
+
/** 持续多久 0-1 */
|
|
301
|
+
iterations: 1 - iterationStart,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** 更新行状态 */
|
|
307
|
+
function updateRowBgcJs(rowKeyValue: UniqKey, color: string) {
|
|
308
|
+
const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue));
|
|
309
|
+
if (!rowEl) return;
|
|
310
|
+
rowEl.style.backgroundColor = color;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
highlightSteps,
|
|
315
|
+
setHighlightDimRow,
|
|
316
|
+
setHighlightDimCell,
|
|
317
|
+
};
|
|
318
|
+
}
|