stk-table-vue 0.2.8 → 0.3.0

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.
@@ -1,19 +1,19 @@
1
- import { computed, ref, Ref, shallowRef } from 'vue';
1
+ import { computed, ref, Ref, ShallowRef, shallowRef } from 'vue';
2
2
  import { StkTableColumn } from './types';
3
3
 
4
4
  type Params<T extends Record<string, any>> = {
5
5
  props: any;
6
6
  colKeyGen: (col: StkTableColumn<T>) => string;
7
- tableHeaders: Ref<StkTableColumn<T>[][]>;
8
- tableHeaderLast: Ref<StkTableColumn<T>[]>;
9
- tableContainer: Ref<HTMLDivElement | undefined>;
7
+ tableHeaders: ShallowRef<StkTableColumn<T>[][]>;
8
+ tableHeaderLast: ShallowRef<StkTableColumn<T>[]>;
9
+ tableContainerRef: Ref<HTMLDivElement | undefined>;
10
10
  };
11
11
 
12
12
  /**
13
13
  * 固定列处理
14
14
  * @returns
15
15
  */
16
- export function useFixedCol<DT extends Record<string, any>>({ props, colKeyGen, tableHeaders, tableHeaderLast, tableContainer }: Params<DT>) {
16
+ export function useFixedCol<DT extends Record<string, any>>({ props, colKeyGen, tableHeaders, tableHeaderLast, tableContainerRef }: Params<DT>) {
17
17
  /** 固定列阴影 */
18
18
  const fixedShadow = ref<{
19
19
  /** 是否展示左侧固定列阴影 */
@@ -82,7 +82,7 @@ export function useFixedCol<DT extends Record<string, any>>({ props, colKeyGen,
82
82
  /** 滚动条变化时,更新需要展示阴影的列 */
83
83
  function updateFixedShadow() {
84
84
  if (!props.fixedColShadow) return;
85
- const { clientWidth, scrollWidth, scrollLeft } = tableContainer.value as HTMLDivElement;
85
+ const { clientWidth, scrollWidth, scrollLeft } = tableContainerRef.value as HTMLDivElement;
86
86
  fixedShadow.value.showL = Boolean(scrollLeft);
87
87
  fixedShadow.value.showR = Math.abs(scrollWidth - scrollLeft - clientWidth) > 0.5;
88
88
  }
@@ -1,12 +1,12 @@
1
- import { CSSProperties, Ref, computed } from 'vue';
2
- import { Is_Legacy_Mode } from './const';
1
+ import { CSSProperties, Ref, ShallowRef, computed } from 'vue';
2
+ import { IS_LEGACY_MODE } from './const';
3
3
  import { StkTableColumn, TagType } from './types';
4
4
  import { VirtualScrollStore, VirtualScrollXStore } from './useVirtualScroll';
5
- import { getColWidth } from './utils';
5
+ import { getCalculatedColWidth } from './utils';
6
6
 
7
7
  type Options<T extends Record<string, any>> = {
8
8
  props: any;
9
- tableHeaders: Ref<StkTableColumn<T>[][]>;
9
+ tableHeaders: ShallowRef<StkTableColumn<T>[][]>;
10
10
  virtualScroll: Ref<VirtualScrollStore>;
11
11
  virtualScrollX: Ref<VirtualScrollXStore>;
12
12
  virtualX_on: Ref<boolean>;
@@ -42,7 +42,7 @@ export function useFixedStyle<DT extends Record<string, any>>({
42
42
  } else {
43
43
  refStore.set(item, left);
44
44
  }
45
- left += getColWidth(item);
45
+ left += getCalculatedColWidth(item);
46
46
  }
47
47
  if (!rightStartIndex && item.fixed === 'right') {
48
48
  rightStartIndex = i;
@@ -57,7 +57,7 @@ export function useFixedStyle<DT extends Record<string, any>>({
57
57
  } else {
58
58
  refStore.set(item, right);
59
59
  }
60
- right += getColWidth(item);
60
+ right += getCalculatedColWidth(item);
61
61
  }
62
62
  }
63
63
  });
@@ -71,32 +71,35 @@ export function useFixedStyle<DT extends Record<string, any>>({
71
71
  * @param col
72
72
  * @param depth 深度。tagType = 1时使用
73
73
  */
74
- function getFixedStyle(tagType: TagType, col: StkTableColumn<DT>, depth = 0): CSSProperties {
74
+ function getFixedStyle(tagType: TagType, col: StkTableColumn<DT>, depth = 0): CSSProperties | null {
75
75
  const { fixed } = col;
76
+ if (tagType === TagType.TD && !fixed) return null;
77
+
76
78
  const isFixedLeft = fixed === 'left';
77
79
  const style: CSSProperties = {};
78
80
  const { colKeyStore, refStore } = fixedColumnsPositionStore.value;
79
81
 
80
- if (Is_Legacy_Mode) {
82
+ if (IS_LEGACY_MODE) {
81
83
  style.position = 'relative';
82
84
  } else {
83
85
  style.position = 'sticky';
84
86
  }
87
+
85
88
  if (tagType === TagType.TH) {
86
89
  // TH
87
- if (Is_Legacy_Mode) {
90
+ if (IS_LEGACY_MODE) {
88
91
  style.top = virtualScroll.value.scrollTop + depth * props.rowHeight + 'px';
89
92
  } else {
90
93
  style.top = depth * props.rowHeight + 'px';
91
94
  }
92
- style.zIndex = isFixedLeft ? '5' : '4'; // 保证固定列高于其他单元格
95
+ style.zIndex = isFixedLeft ? '3' : '2'; // 保证固定列高于其他单元格
93
96
  } else {
94
97
  // TD
95
- style.zIndex = isFixedLeft ? '3' : '2';
98
+ style.zIndex = isFixedLeft ? '2' : '1';
96
99
  }
97
100
 
98
101
  if (fixed === 'left' || fixed === 'right') {
99
- if (Is_Legacy_Mode) {
102
+ if (IS_LEGACY_MODE) {
100
103
  if (isFixedLeft) {
101
104
  if (virtualX_on.value) style.left = virtualScrollX.value.scrollLeft - virtualScrollX.value.offsetLeft + 'px';
102
105
  else style.left = virtualScrollX.value.scrollLeft + 'px';
@@ -1,170 +1,328 @@
1
1
  import { interpolateRgb } from 'd3-interpolate';
2
- import { Ref, computed, ref } from 'vue';
3
- import { Highlight_Color, Highlight_Color_Change_Freq, Highlight_Duration } from './const';
4
- import { UniqKey } from './types';
2
+ import { Ref, computed } from 'vue';
3
+ import { HIGHLIGHT_CELL_CLASS, HIGHLIGHT_COLOR, HIGHLIGHT_DURATION, HIGHLIGHT_FREQ, HIGHLIGHT_ROW_CLASS, IS_LEGACY_MODE } from './const';
4
+ import { HighlightConfig, UniqKey } from './types';
5
5
 
6
6
  type Params = {
7
- props: { theme: 'light' | 'dark'; virtual: boolean; dataSource: any[] };
8
- tableContainer: Ref<HTMLElement | undefined>;
7
+ props: any;
8
+ stkTableId: string;
9
+ tableContainerRef: Ref<HTMLDivElement | undefined>;
9
10
  };
10
-
11
- /** 高亮行保存的东西 */
12
- type HighlightRowStore = {
13
- bgc: string;
14
- bgc_progress_ms: number;
15
- bgc_progress: number;
11
+ /** 存放高亮行信息 */
12
+ type HighlightDimRowStore = {
13
+ /** 动画开始时间戳 */
14
+ readonly ts: number;
15
+ /** 行是否可见 */
16
+ visible: boolean;
17
+ /** 动画关键帧 */
18
+ keyframe: Parameters<Animatable['animate']>['0'];
19
+ /** 动画初始持续时间 */
20
+ readonly duration: number;
16
21
  };
17
22
 
18
- /** 高亮行class */
19
- const HIGHLIGHT_ROW_CLASS = 'highlight-row';
20
- /** 高连单元格class */
21
- const HIGHLIGHT_CELL_CLASS = 'highlight-cell';
22
-
23
23
  /**
24
24
  * 高亮单元格,行
25
- * row中新增_bgc_progress_ms 属性控制高亮状态,_bgc控制颜色
26
25
  */
27
- export function useHighlight({ props, tableContainer }: Params) {
28
- /**
29
- * 高亮行记录 key-rowKey, value-obj
30
- */
31
- const highlightRowStore = ref<Record<UniqKey, HighlightRowStore>>({});
26
+ export function useHighlight({ props, stkTableId, tableContainerRef }: Params) {
27
+ const config: HighlightConfig = props.highlightConfig;
32
28
 
33
- const highlightFrom = computed(() => Highlight_Color[props.theme].from);
34
- const highlightTo = computed(() => Highlight_Color[props.theme].to);
29
+ /** 持续时间 */
30
+ const highlightDuration = config.duration ? config.duration * 1000 : HIGHLIGHT_DURATION;
31
+ /** 高亮频率(仅虚拟滚动生效) */
32
+ const highlightFrequency = config.fps ? 1000 / config.fps : HIGHLIGHT_FREQ;
33
+ /** 高亮颜色 */
34
+ const highlightColor = {
35
+ light: HIGHLIGHT_COLOR.light,
36
+ dark: HIGHLIGHT_COLOR.dark,
37
+ };
38
+
39
+ /** css 高亮的次数,用于css animation steps() */
40
+ const highlightSteps = highlightDuration / highlightFrequency;
41
+ /** 高亮开始 */
42
+ const highlightFrom = computed(() => highlightColor[props.theme as 'light' | 'dark'].from);
43
+ /** 高亮结束 */
44
+ const highlightTo = computed(() => highlightColor[props.theme as 'light' | 'dark'].to);
35
45
  const highlightInter = computed(() => interpolateRgb(highlightFrom.value, highlightTo.value));
36
46
 
37
- /** 存放高亮行的key*/
38
- const highlightDimRowKeys = new Set<UniqKey>();
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
+
39
64
  /** 高亮后渐暗的行定时器 */
40
65
  const highlightDimRowsTimeout = new Map();
41
66
  /** 高亮后渐暗的单元格定时器 */
42
67
  const highlightDimCellsTimeout = new Map();
43
- /** 是否正在计算高亮行的循环*/
44
- let calcHighlightDimLoop = false;
45
68
 
46
69
  /**
47
70
  * 计算高亮渐暗颜色的循环
48
- * FIXME: 相同数据源,相同引用的情况,将颜色值挂在数据源对象上,在多个表格使用相同数据源时会出问题。
49
71
  */
50
- function calcHighlightLoop() {
51
- if (calcHighlightDimLoop) return;
52
- calcHighlightDimLoop = true;
72
+ function calcRowHighlightLoop() {
73
+ if (calcHighlightDimLoopAnimation) return;
74
+ calcHighlightDimLoopAnimation = true;
75
+ const recursion = () => {
76
+ window.requestAnimationFrame(
77
+ () => {
78
+ const nowTs = Date.now();
79
+ highlightDimRowsAnimation.forEach((store, rowKeyValue) => {
80
+ const { ts, duration } = store;
81
+ const timeOffset = nowTs - ts;
82
+ if (nowTs - ts < duration) {
83
+ updateRowBgc(rowKeyValue, store, timeOffset);
84
+ } else {
85
+ highlightDimRowsAnimation.delete(rowKeyValue);
86
+ }
87
+ });
88
+
89
+ if (highlightDimRowsAnimation.size > 0) {
90
+ // 还有高亮的行,则下一次循环
91
+ recursion();
92
+ } else {
93
+ // 没有则停止循环
94
+ calcHighlightDimLoopAnimation = false;
95
+ highlightDimRowsAnimation.clear();
96
+ }
97
+ } /* , highlightFrequency */,
98
+ );
99
+ };
100
+ recursion();
101
+ }
102
+
103
+ /**
104
+ * js计算高亮渐暗颜色的循环
105
+ */
106
+ function calcRowHighlightLoopJs() {
107
+ if (calcHighlightDimLoopJs) return;
108
+ calcHighlightDimLoopJs = true;
53
109
  // js计算gradient
54
- // raf 太频繁。TODO: 考虑setTimeout分段设置颜色,过渡靠css transition 补间
55
110
  const recursion = () => {
56
111
  window.setTimeout(() => {
57
112
  const nowTs = Date.now();
58
-
59
- highlightDimRowKeys.forEach(rowKeyValue => {
60
- // const rowKeyValue = rowKeyGen(row);
61
- // const rowEl = tableContainer.value?.querySelector<HTMLElement>(`[data-row-key="${rowKeyValue}"]`);
62
- // if (rowEl && row._bgc_progress === 0) {
63
- // // 开始css transition 补间
64
- // rowEl.classList.remove('highlight-row-transition');
65
- // void rowEl.offsetHeight; // reflow
66
- // rowEl.classList.add('highlight-row-transition');
67
- // }
68
- const highlightItem = highlightRowStore.value[rowKeyValue];
113
+ highlightDimRowsJs.forEach((highlightStart, rowKeyValue) => {
69
114
  /** 经过的时间 ÷ 高亮持续时间 计算出 颜色过渡进度 (0-1) */
70
- const progress = (nowTs - highlightItem.bgc_progress_ms) / Highlight_Duration;
115
+ const progress = (nowTs - highlightStart) / highlightDuration;
116
+ let bgc = '';
71
117
  if (0 < progress && progress < 1) {
72
- highlightItem.bgc = highlightInter.value(progress);
118
+ bgc = highlightInter.value(progress);
73
119
  } else {
74
- highlightItem.bgc = ''; // 清空颜色
75
- highlightDimRowKeys.delete(rowKeyValue);
120
+ highlightDimRowsJs.delete(rowKeyValue);
76
121
  }
122
+ updateRowBgcJs(rowKeyValue, bgc);
77
123
  });
78
124
 
79
- // $*$ 兼容vue2响应
80
- highlightRowStore.value = { ...highlightRowStore.value };
81
-
82
- if (highlightDimRowKeys.size > 0) {
125
+ if (highlightDimRowsJs.size > 0) {
83
126
  // 还有高亮的行,则下一次循环
84
127
  recursion();
85
128
  } else {
86
129
  // 没有则停止循环
87
- calcHighlightDimLoop = false;
130
+ calcHighlightDimLoopJs = false;
131
+ highlightDimRowsJs.clear();
88
132
  }
89
- }, Highlight_Color_Change_Freq);
133
+ }, highlightFrequency);
90
134
  };
91
135
  recursion();
92
136
  }
93
137
 
94
- /** 高亮一个单元格 */
95
- function setHighlightDimCell(rowKeyValue: string, dataIndex: string) {
138
+ /** 高亮函数的默认参数 */
139
+ const defaultHighlightDimOption = {
140
+ keyframe: [{ backgroundColor: highlightFrom.value }, { backgroundColor: highlightTo.value }],
141
+ duration: highlightDuration,
142
+ };
143
+
144
+ /**
145
+ * 高亮一个单元格
146
+ * @param rowKeyValue 一行的key
147
+ * @param dataIndex 列key
148
+ * @param options.method css-使用css渲染,animation-使用animation api。默认css;
149
+ * @param option.className 自定义css动画的class。
150
+ * @param option.keyframe 同Keyframe https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Animations_API/Keyframe_Formats
151
+ * @param option.duration 动画时长。method='css'状态下,用于移除class,如果传入了className则需要与自定义的动画时间一致。
152
+ */
153
+ function setHighlightDimCell(
154
+ rowKeyValue: string,
155
+ dataIndex: string,
156
+ option: { className?: string; method?: 'css' | 'animation'; keyframe?: Parameters<Animatable['animate']>['0']; duration?: number } = {},
157
+ ) {
96
158
  // TODO: 支持动态计算高亮颜色。不易实现。需记录每一个单元格的颜色情况。
97
- const cellEl = tableContainer.value?.querySelector<HTMLElement>(`[data-row-key="${rowKeyValue}"]>[data-index="${dataIndex}"]`);
159
+ const cellEl = tableContainerRef.value?.querySelector<HTMLElement>(`[data-row-key="${rowKeyValue}"]>[data-index="${dataIndex}"]`);
160
+ const { className, method, duration, keyframe } = {
161
+ className: HIGHLIGHT_CELL_CLASS,
162
+ method: 'css',
163
+ ...defaultHighlightDimOption,
164
+ ...option,
165
+ };
98
166
  if (!cellEl) return;
99
- if (cellEl.classList.contains(HIGHLIGHT_CELL_CLASS)) {
100
- cellEl.classList.remove(HIGHLIGHT_CELL_CLASS);
101
- void cellEl.offsetHeight; // 通知浏览器重绘
167
+ if (method === 'animation') {
168
+ cellEl.animate(keyframe, duration);
169
+ } else {
170
+ highlightCellsInCssKeyFrame(cellEl, rowKeyValue, className, duration);
102
171
  }
103
- cellEl.classList.add(HIGHLIGHT_CELL_CLASS);
104
- window.clearTimeout(highlightDimCellsTimeout.get(rowKeyValue));
105
- highlightDimCellsTimeout.set(
106
- rowKeyValue,
107
- window.setTimeout(() => {
108
- cellEl.classList.remove(HIGHLIGHT_CELL_CLASS);
109
- highlightDimCellsTimeout.delete(rowKeyValue);
110
- }, Highlight_Duration),
111
- );
112
172
  }
113
173
 
114
174
  /**
115
175
  * 高亮一行
116
- * @param rowKeyValues
117
- * @param option.useCss 虚拟滚动时,高亮由js控制。如果仍想使用css 关键帧控制,则配置此项
176
+ * @param rowKeyValues 行唯一键的数组
177
+ * @param option.method css-使用css渲染,animation-使用animation api,js-使用js计算颜色
178
+ * @param option.className 自定义css动画的class。
179
+ * @param option.keyframe 同Keyframe,无法控制帧率。 https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Animations_API/Keyframe_Formats
180
+ * @param option.duration 动画时长。method='css'状态下,用于移除class,如果传入了className则需要与自定义的动画时间一致。。
118
181
  */
119
- function setHighlightDimRow(rowKeyValues: UniqKey[], option: { useCss?: boolean } = {}) {
182
+ function setHighlightDimRow(
183
+ rowKeyValues: UniqKey[],
184
+ option: {
185
+ method?: 'css' | 'animation' | 'js';
186
+ /** @deprecated 请使用method */
187
+ useCss?: boolean;
188
+ className?: string;
189
+ keyframe?: Parameters<Animatable['animate']>['0'];
190
+ duration?: number;
191
+ } = {},
192
+ ) {
120
193
  if (!Array.isArray(rowKeyValues)) rowKeyValues = [rowKeyValues];
121
- if (props.virtual && !option.useCss) {
122
- // --------虚拟滚动用js计算颜色渐变的高亮方案
123
- const nowTs = Date.now(); // 重置渐变进度
194
+ const { className, method, useCss, keyframe, duration } = {
195
+ className: HIGHLIGHT_ROW_CLASS,
196
+ method: props.virtual ? 'js' : 'css',
197
+ ...defaultHighlightDimOption,
198
+ ...option,
199
+ };
200
+
201
+ const nowTs = Date.now();
202
+ if (method === 'css' || useCss) {
203
+ // -------- use css keyframe
204
+ highlightRowsInCssKeyframe(rowKeyValues, className, duration);
205
+ } else if (method === 'animation') {
206
+ if (props.virtual) {
207
+ // -------- 用animation 接口实现动画
208
+ for (let i = 0; i < rowKeyValues.length; i++) {
209
+ const rowKeyValue = rowKeyValues[i];
210
+ const store: HighlightDimRowStore = { ts: nowTs, visible: false, keyframe, duration };
211
+ highlightDimRowsAnimation.set(rowKeyValue, store);
212
+ updateRowBgc(rowKeyValue, store, 0);
213
+ }
214
+ calcRowHighlightLoop();
215
+ } else {
216
+ // -------- use Element.animate
217
+ for (let i = 0; i < rowKeyValues.length; i++) {
218
+ const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValues[i])) as HTMLTableRowElement | null;
219
+ if (!rowEl) continue;
220
+ rowEl.animate(keyframe, duration);
221
+ }
222
+ }
223
+ } else if (method === 'js') {
224
+ // -------- 用js计算颜色渐变的高亮方案
124
225
  for (let i = 0; i < rowKeyValues.length; i++) {
125
226
  const rowKeyValue = rowKeyValues[i];
126
- highlightRowStore.value[rowKeyValue] = {
127
- bgc: highlightFrom.value,
128
- bgc_progress: 0,
129
- bgc_progress_ms: nowTs,
130
- };
131
- highlightDimRowKeys.add(rowKeyValue);
227
+ highlightDimRowsJs.set(rowKeyValue, nowTs);
228
+ updateRowBgcJs(rowKeyValue, highlightFrom.value);
132
229
  }
133
- calcHighlightLoop();
134
- } else {
135
- // -------- 普通滚动用css @keyframes动画,实现高亮
136
- /**是否需要重绘 */
137
- let needRepaint = false;
230
+ calcRowHighlightLoopJs();
231
+ }
232
+ }
138
233
 
139
- const rowElTemp: HTMLTableRowElement[] = [];
140
- for (let i = 0; i < rowKeyValues.length; i++) {
141
- const rowKeyValue = rowKeyValues[i];
142
- const rowEl = tableContainer.value?.querySelector<HTMLTableRowElement>(`[data-row-key="${rowKeyValue}"]`);
143
- if (!rowEl) continue;
144
- if (rowEl.classList.contains(HIGHLIGHT_ROW_CLASS)) {
145
- rowEl.classList.remove(HIGHLIGHT_ROW_CLASS);
146
- needRepaint = true;
147
- }
148
- rowElTemp.push(rowEl);
149
- // 动画结束移除class
150
- window.clearTimeout(highlightDimRowsTimeout.get(rowKeyValue));
151
- highlightDimRowsTimeout.set(
152
- rowKeyValue,
153
- window.setTimeout(() => {
154
- rowEl.classList.remove(HIGHLIGHT_ROW_CLASS);
155
- highlightDimRowsTimeout.delete(rowKeyValue); // 回收内存
156
- }, Highlight_Duration),
157
- );
234
+ /**
235
+ * 使用css @keyframes动画,实现高亮行动画
236
+ * 此方案作为兼容方式。v0.3.0 将使用Element.animate 接口实现动画。
237
+ */
238
+ function highlightRowsInCssKeyframe(rowKeyValues: UniqKey[], className: string, duration: number) {
239
+ /**是否需要重绘 */
240
+ let needRepaint = false;
241
+ const rowElTemp: HTMLTableRowElement[] = [];
242
+ for (let i = 0; i < rowKeyValues.length; i++) {
243
+ const rowKeyValue = rowKeyValues[i];
244
+ const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue)) as HTMLTableRowElement | null;
245
+ if (!rowEl) continue;
246
+ if (rowEl.classList.contains(className)) {
247
+ rowEl.classList.remove(className);
248
+ needRepaint = true;
158
249
  }
159
- if (needRepaint) {
160
- void tableContainer.value?.offsetWidth; //强制浏览器重绘
250
+ rowElTemp.push(rowEl);
251
+ // 动画结束移除class
252
+ window.clearTimeout(highlightDimRowsTimeout.get(rowKeyValue));
253
+ highlightDimRowsTimeout.set(
254
+ rowKeyValue,
255
+ window.setTimeout(() => {
256
+ rowEl.classList.remove(className);
257
+ highlightDimRowsTimeout.delete(rowKeyValue); // 回收内存
258
+ }, duration),
259
+ );
260
+ }
261
+ if (needRepaint) {
262
+ void tableContainerRef.value?.offsetWidth; //强制浏览器重绘
263
+ }
264
+ rowElTemp.forEach(el => el.classList.add(className)); // 统一添加动画
265
+ }
266
+
267
+ /**
268
+ * 使用css @keyframes动画,实现高亮单元格动画
269
+ * 此方案作为兼容方式。v0.3.0 将使用Element.animate 接口实现动画。
270
+ */
271
+ function highlightCellsInCssKeyFrame(cellEl: HTMLElement, rowKeyValue: UniqKey, className: string, duration: number) {
272
+ if (cellEl.classList.contains(className)) {
273
+ cellEl.classList.remove(className);
274
+ void cellEl.offsetHeight; // 通知浏览器重绘
275
+ }
276
+ cellEl.classList.add(className);
277
+ window.clearTimeout(highlightDimCellsTimeout.get(rowKeyValue));
278
+ highlightDimCellsTimeout.set(
279
+ rowKeyValue,
280
+ window.setTimeout(() => {
281
+ cellEl.classList.remove(className);
282
+ highlightDimCellsTimeout.delete(rowKeyValue);
283
+ }, duration),
284
+ );
285
+ }
286
+
287
+ /**
288
+ * 更新行状态
289
+ * @param rowKeyValue 行唯一键
290
+ * @param store highlightDimRowStore 的引用对象
291
+ * @param timeOffset 距动画开始经过的时长
292
+ */
293
+ function updateRowBgc(rowKeyValue: UniqKey, store: HighlightDimRowStore, timeOffset: number) {
294
+ const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue));
295
+ const { visible, keyframe, duration: initialDuration } = store;
296
+ if (!rowEl) {
297
+ if (visible) {
298
+ store.visible = false; // 标记为不可见
161
299
  }
162
- rowElTemp.forEach(el => el.classList.add(HIGHLIGHT_ROW_CLASS)); // 统一添加动画
300
+ return;
301
+ }
302
+ // 只有元素 不可见 -> 可见 时才需要更新
303
+ if (!visible) {
304
+ store.visible = true; // 标记为可见
305
+ /** 经过的时间 ÷ 高亮持续时间 计算出 颜色过渡进度 (0-1) */
306
+ const iterationStart = timeOffset / initialDuration;
307
+ rowEl.animate(keyframe, {
308
+ duration: initialDuration - timeOffset,
309
+ /** 从什么时候开始,0-1 */
310
+ iterationStart,
311
+ /** 持续多久 0-1 */
312
+ iterations: 1 - iterationStart,
313
+ });
163
314
  }
164
315
  }
165
316
 
317
+ /** 更新行状态 */
318
+ function updateRowBgcJs(rowKeyValue: UniqKey, color: string) {
319
+ const rowEl = document.getElementById(stkTableId + '-' + String(rowKeyValue));
320
+ if (!rowEl) return;
321
+ rowEl.style.backgroundColor = color;
322
+ }
323
+
166
324
  return {
167
- highlightRowStore,
325
+ highlightSteps,
168
326
  setHighlightDimRow,
169
327
  setHighlightDimCell,
170
328
  };
@@ -1,4 +1,4 @@
1
- import { Ref, onBeforeUnmount, onMounted } from 'vue';
1
+ import { ComputedRef, Ref, ShallowRef, onBeforeUnmount, onMounted, watch } from 'vue';
2
2
  import { StkTableColumn } from './types';
3
3
  import { VirtualScrollStore, VirtualScrollXStore } from './useVirtualScroll';
4
4
 
@@ -10,7 +10,8 @@ type Options<DT extends Record<string, any>> = {
10
10
  scrollTo: (y: number | null, x: number | null) => void;
11
11
  virtualScroll: Ref<VirtualScrollStore>;
12
12
  virtualScrollX: Ref<VirtualScrollXStore>;
13
- tableHeaders: Ref<StkTableColumn<DT>[][]>;
13
+ tableHeaders: ShallowRef<StkTableColumn<DT>[][]>;
14
+ virtual_on: ComputedRef<boolean>;
14
15
  };
15
16
  /**
16
17
  * 按下键盘箭头滚动。只有悬浮在表体上才能生效键盘。
@@ -19,24 +20,35 @@ type Options<DT extends Record<string, any>> = {
19
20
  */
20
21
  export function useKeyboardArrowScroll<DT extends Record<string, any>>(
21
22
  targetElement: Ref<HTMLElement | undefined>,
22
- { props, scrollTo, virtualScroll, virtualScrollX, tableHeaders }: Options<DT>,
23
+ { props, scrollTo, virtualScroll, virtualScrollX, tableHeaders, virtual_on }: Options<DT>,
23
24
  ) {
24
25
  /** 检测鼠标是否悬浮在表格体上 */
25
26
  let isMouseOver = false;
27
+ watch(virtual_on, val => {
28
+ if (!val) {
29
+ removeListeners();
30
+ } else {
31
+ addEventListeners();
32
+ }
33
+ });
34
+
35
+ onMounted(addEventListeners);
36
+
37
+ onBeforeUnmount(removeListeners);
26
38
 
27
- onMounted(() => {
39
+ function addEventListeners() {
28
40
  window.addEventListener('keydown', handleKeydown);
29
41
  targetElement.value?.addEventListener('mouseenter', handleMouseEnter);
30
42
  targetElement.value?.addEventListener('mouseleave', handleMouseLeave);
31
43
  targetElement.value?.addEventListener('mousedown', handleMouseDown);
32
- });
44
+ }
33
45
 
34
- onBeforeUnmount(() => {
46
+ function removeListeners() {
35
47
  window.removeEventListener('keydown', handleKeydown);
36
48
  targetElement.value?.removeEventListener('mouseenter', handleMouseEnter);
37
49
  targetElement.value?.removeEventListener('mouseleave', handleMouseLeave);
38
50
  targetElement.value?.removeEventListener('mousedown', handleMouseDown);
39
- });
51
+ }
40
52
 
41
53
  /** 键盘按下事件 */
42
54
  function handleKeydown(e: KeyboardEvent) {