stk-table-vue 0.8.12 → 0.8.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.
@@ -35,20 +35,22 @@ export function useMergeCells({ rowActiveProp, tableHeaderLast, rowKeyGen, colKe
35
35
  /**
36
36
  * abstract the logic of hiding cells
37
37
  */
38
- function hideCells(rowKey: UniqKey, startIndex: number, count: number, isSelfRow = false, mergeCellKey: string) {
39
- for (let i = startIndex; i < startIndex + count; i++) {
40
- if (!isSelfRow || i !== startIndex) {
41
- // self row does not need to be hidden
42
- const nextCol = tableHeaderLast.value[i];
43
- if (!nextCol) break;
44
- const nextColKey = colKeyGen.value(nextCol);
45
- if (!hiddenCellMap.value[rowKey]) hiddenCellMap.value[rowKey] = new Set();
46
- hiddenCellMap.value[rowKey].add(nextColKey);
47
- }
38
+ function hideCells(rowKey: UniqKey, colKey: UniqKey, colspan: number, isSelfRow = false, mergeCellKey: string) {
39
+ const startIndex = tableHeaderLast.value.findIndex(item => colKeyGen.value(item) === colKey);
48
40
 
41
+ for (let i = startIndex; i < startIndex + colspan; i++) {
49
42
  // if other row hovered, the rowspan cell need to be highlight
50
43
  if (!hoverRowMap.value[rowKey]) hoverRowMap.value[rowKey] = new Set();
51
44
  hoverRowMap.value[rowKey].add(mergeCellKey);
45
+ if (isSelfRow && i === startIndex) {
46
+ // self row start cell does not need to be hidden
47
+ continue;
48
+ }
49
+ const nextCol = tableHeaderLast.value[i];
50
+ if (!nextCol) break;
51
+ const nextColKey = colKeyGen.value(nextCol);
52
+ if (!hiddenCellMap.value[rowKey]) hiddenCellMap.value[rowKey] = new Set();
53
+ hiddenCellMap.value[rowKey].add(nextColKey);
52
54
  }
53
55
  }
54
56
 
@@ -78,31 +80,30 @@ export function useMergeCells({ rowActiveProp, tableHeaderLast, rowKeyGen, colKe
78
80
 
79
81
  const rowKey = rowKeyGen(row);
80
82
 
81
- const colKey = colKeyGen.value(col);
82
- const curColIndex = tableHeaderLast.value.findIndex(item => colKeyGen.value(item) === colKey);
83
83
  const curRowIndex = virtual_dataSourcePart.value.findIndex(item => rowKeyGen(item) === rowKey);
84
- const mergedCellKey = pureCellKeyGen(rowKey, colKey);
85
-
86
84
  if (curRowIndex === -1) return;
87
85
 
86
+ const colKey = colKeyGen.value(col);
87
+ const mergedCellKey = pureCellKeyGen(rowKey, colKey);
88
+
88
89
  for (let i = curRowIndex; i < curRowIndex + rowspan; i++) {
89
90
  const row = virtual_dataSourcePart.value[i];
90
91
  if (!row) break;
91
- hideCells(rowKeyGen(row), curColIndex, colspan, i === curRowIndex, mergedCellKey);
92
+ hideCells(rowKeyGen(row), colKey, colspan, i === curRowIndex, mergedCellKey);
92
93
  }
93
94
 
94
95
  return { colspan, rowspan };
95
96
  }
96
97
 
98
+ const emptySet = new Set<string>();
97
99
  function updateHoverMergedCells(rowKey: UniqKey | undefined) {
98
- const set = rowKey === void 0 ? null : hoverRowMap.value[rowKey];
99
- hoverMergedCells.value = set || new Set();
100
+ hoverMergedCells.value = rowKey === void 0 ? emptySet : hoverRowMap.value[rowKey] || emptySet;
100
101
  }
101
102
 
102
103
  function updateActiveMergedCells(clear?: boolean, rowKey?: UniqKey) {
103
104
  if (!rowActiveProp.value.enabled) return;
104
105
  if (clear) {
105
- activeMergedCells.value.clear();
106
+ activeMergedCells.value = new Set();
106
107
  return;
107
108
  }
108
109
  activeMergedCells.value = (rowKey !== void 0 && hoverRowMap.value[rowKey]) || new Set(hoverMergedCells.value);
@@ -1,4 +1,4 @@
1
- import { computed, Ref, ref, onMounted, onUnmounted, watch } from 'vue';
1
+ import { computed, customRef, onMounted, onUnmounted, Ref, watch } from 'vue';
2
2
 
3
3
  type Params = {
4
4
  props: any;
@@ -6,13 +6,43 @@ type Params = {
6
6
  };
7
7
 
8
8
  export function useScrollRowByRow({ props, tableContainerRef }: Params) {
9
- let isMouseDown = false;
9
+ // let isMouseDown = false;
10
10
  let isAddListeners = false;
11
11
  /** record the last scroll bar position */
12
- let lastScrollTop = 0;
12
+ // let lastScrollTop = 0;
13
13
 
14
- /** record is the scroll bar is dragging */
15
- const isDragScroll = ref(false);
14
+ /** record is the scroll bar is dragging.debounce true to false */
15
+ const isDragScroll = customRef((track, trigger) => {
16
+ let value = false;
17
+ let debounceTimer = 0;
18
+
19
+ return {
20
+ get() {
21
+ track();
22
+ return value;
23
+ },
24
+ set(newValue) {
25
+ // Add debounce when change from true to false
26
+ if (value && !newValue) {
27
+ if (debounceTimer) {
28
+ window.clearTimeout(debounceTimer);
29
+ }
30
+ debounceTimer = window.setTimeout(() => {
31
+ value = newValue;
32
+ trigger();
33
+ debounceTimer = 0;
34
+ }, 300);
35
+ } else {
36
+ if (debounceTimer) {
37
+ window.clearTimeout(debounceTimer);
38
+ debounceTimer = 0;
39
+ }
40
+ value = newValue;
41
+ trigger();
42
+ }
43
+ }
44
+ }
45
+ });
16
46
  const onlyDragScroll = computed(() => props.scrollRowByRow === 'scrollbar');
17
47
 
18
48
  /** is ScrollRowByRow active */
@@ -45,7 +75,7 @@ export function useScrollRowByRow({ props, tableContainerRef }: Params) {
45
75
  if (!container) return;
46
76
  container.addEventListener('mousedown', handleMouseDown);
47
77
  container.addEventListener('mouseup', handleMouseUp);
48
- container.addEventListener('scroll', handleScroll);
78
+ // container.addEventListener('scroll', handleScroll);
49
79
  isAddListeners = true;
50
80
  }
51
81
 
@@ -54,26 +84,31 @@ export function useScrollRowByRow({ props, tableContainerRef }: Params) {
54
84
  if (!container) return;
55
85
  container.removeEventListener('mousedown', handleMouseDown);
56
86
  container.removeEventListener('mouseup', handleMouseUp);
57
- container.removeEventListener('scroll', handleScroll);
87
+ // container.removeEventListener('scroll', handleScroll);
58
88
  isAddListeners = false;
59
89
  }
60
90
 
61
91
  function handleMouseDown(e: Event) {
62
- isMouseDown = true;
63
- lastScrollTop = (e.target as HTMLElement).scrollTop;
92
+ // console.log('mousedown target:', e.target)
93
+ const el = e.target as HTMLElement;
94
+ if(el.classList.contains('stk-table')){
95
+ isDragScroll.value = true;
96
+ }
97
+ // isMouseDown = true;
98
+ // lastScrollTop = (e.target as HTMLElement).scrollTop;
64
99
  }
65
100
 
66
101
  function handleMouseUp() {
67
- isMouseDown = false;
102
+ // isMouseDown = false;
68
103
  isDragScroll.value = false;
69
- lastScrollTop = 0;
104
+ // lastScrollTop = 0;
70
105
  }
71
106
 
72
- function handleScroll(e: Event) {
73
- const scrollTop = (e.target as HTMLElement).scrollTop;
74
- // if scrollTop === lastScrollTop means horizontal scroll
75
- if (!isMouseDown || scrollTop === lastScrollTop) return;
76
- isDragScroll.value = true;
77
- }
107
+ // function handleScroll(e: Event) {
108
+ // const scrollTop = (e.target as HTMLElement).scrollTop;
109
+ // // if scrollTop === lastScrollTop means horizontal scroll
110
+ // if (!isMouseDown || scrollTop === lastScrollTop) return;
111
+ // isDragScroll.value = true;
112
+ // }
78
113
  return { isSRBRActive, isDragScroll };
79
- }
114
+ }
@@ -1,5 +1,6 @@
1
1
  import { computed, ShallowRef } from 'vue';
2
2
  import { DragRowConfig } from './types';
3
+ import { getClosestTr } from './utils';
3
4
 
4
5
  type Params = {
5
6
  props: any;
@@ -22,12 +23,6 @@ export function useTrDrag({ props, emits, dataSourceCopy }: Params) {
22
23
  return { mode: 'insert', ...props.dragRowConfig };
23
24
  });
24
25
 
25
- function getClosestTr(e: DragEvent) {
26
- const target = e.target as HTMLElement;
27
- const tr = target?.closest('tr');
28
- return tr;
29
- }
30
-
31
26
  function onTrDragStart(e: DragEvent, rowIndex: number) {
32
27
  const tr = getClosestTr(e);
33
28
  if (tr) {
@@ -240,3 +240,19 @@ export function getBrowsersVersion(browserName: string) {
240
240
  export function pureCellKeyGen(rowKey: UniqKey, colKey: UniqKey) {
241
241
  return rowKey + CELL_KEY_SEPARATE + colKey;
242
242
  }
243
+
244
+ export function getClosestTr(e: MouseEvent) {
245
+ const target = e.target as HTMLElement;
246
+ const tr = target?.closest('tr');
247
+ return tr;
248
+ }
249
+
250
+ export function getClosestTrIndex(e: MouseEvent) {
251
+ const tr = getClosestTr(e);
252
+ if (!tr) return -1;
253
+ return Number(tr.dataset.rowI);
254
+ }
255
+
256
+ export function getClosestColKey(e: MouseEvent) {
257
+ return (e.target as HTMLElement)?.closest('td')?.dataset.colKey;
258
+ }