stk-table-vue 0.6.17 → 0.7.1

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.
Files changed (40) hide show
  1. package/README.md +211 -213
  2. package/lib/src/StkTable/StkTable.vue.d.ts +43 -24
  3. package/lib/src/StkTable/components/TriangleIcon.vue.d.ts +2 -0
  4. package/lib/src/StkTable/const.d.ts +0 -1
  5. package/lib/src/StkTable/types/highlightDimOptions.d.ts +1 -5
  6. package/lib/src/StkTable/types/index.d.ts +27 -7
  7. package/lib/src/StkTable/useHighlight.d.ts +1 -1
  8. package/lib/src/StkTable/useRowExpand.d.ts +17 -0
  9. package/lib/src/StkTable/useTree.d.ts +20 -0
  10. package/lib/src/StkTable/utils/index.d.ts +3 -1
  11. package/lib/stk-table-vue.js +320 -167
  12. package/lib/style.css +29 -20
  13. package/package.json +74 -75
  14. package/src/StkTable/StkTable.vue +1557 -1550
  15. package/src/StkTable/components/DragHandle.vue +9 -9
  16. package/src/StkTable/components/SortIcon.vue +6 -6
  17. package/src/StkTable/components/TriangleIcon.vue +3 -0
  18. package/src/StkTable/const.ts +37 -37
  19. package/src/StkTable/index.ts +4 -4
  20. package/src/StkTable/style.less +567 -553
  21. package/src/StkTable/types/highlightDimOptions.ts +26 -26
  22. package/src/StkTable/types/index.ts +260 -239
  23. package/src/StkTable/useAutoResize.ts +91 -91
  24. package/src/StkTable/useColResize.ts +216 -216
  25. package/src/StkTable/useFixedCol.ts +148 -148
  26. package/src/StkTable/useFixedStyle.ts +75 -75
  27. package/src/StkTable/useGetFixedColPosition.ts +65 -65
  28. package/src/StkTable/useHighlight.ts +320 -318
  29. package/src/StkTable/useKeyboardArrowScroll.ts +112 -112
  30. package/src/StkTable/useRowExpand.ts +78 -0
  31. package/src/StkTable/useThDrag.ts +102 -102
  32. package/src/StkTable/useTrDrag.ts +118 -118
  33. package/src/StkTable/useTree.ts +158 -0
  34. package/src/StkTable/useVirtualScroll.ts +462 -462
  35. package/src/StkTable/utils/constRefUtils.ts +29 -29
  36. package/src/StkTable/utils/index.ts +224 -212
  37. package/src/StkTable/utils/useTriggerRef.ts +33 -33
  38. package/src/VirtualTree.vue +622 -622
  39. package/src/VirtualTreeSelect.vue +367 -367
  40. package/src/vite-env.d.ts +10 -10
@@ -1,118 +1,118 @@
1
- import { computed, ShallowRef } from 'vue';
2
- import { DragRowConfig } from './types';
3
-
4
- type Params = {
5
- props: any;
6
- emits: any;
7
- dataSourceCopy: ShallowRef<any[]>;
8
- };
9
-
10
- const TR_DRAGGING_CLASS = 'tr-dragging';
11
- const TR_DRAG_OVER_CLASS = 'tr-dragging-over';
12
- const DATA_TRANSFER_FORMAT = 'text/plain';
13
-
14
- /**
15
- * 拖拽行
16
- * TODO: 不在虚拟滚动的情况下,从上面拖拽到下面,跨页的时候,滚动条会自适应位置。没有顶上去
17
- */
18
- export function useTrDrag({ props, emits, dataSourceCopy }: Params) {
19
- let trDragFlag = false;
20
-
21
- const dragRowConfig = computed<DragRowConfig>(() => {
22
- return { mode: 'insert', ...props.dragRowConfig };
23
- });
24
-
25
- function getClosestTr(e: DragEvent) {
26
- const target = e.target as HTMLElement;
27
- const tr = target?.closest('tr');
28
- return tr;
29
- }
30
-
31
- function onTrDragStart(e: DragEvent, rowIndex: number) {
32
- const tr = getClosestTr(e);
33
- if (tr) {
34
- const trRect = tr.getBoundingClientRect();
35
- const x = e.clientX - (trRect.left ?? 0);
36
- e.dataTransfer?.setDragImage(tr, x, trRect.height / 2);
37
- tr.classList.add(TR_DRAGGING_CLASS);
38
- }
39
- const dt = e.dataTransfer;
40
- if (dt) {
41
- dt.effectAllowed = 'move';
42
- dt.setData(DATA_TRANSFER_FORMAT, String(rowIndex));
43
- }
44
- trDragFlag = true;
45
- }
46
-
47
- function onTrDragOver(e: DragEvent) {
48
- if (!trDragFlag) return;
49
- e.preventDefault(); // 阻止默认行为,否则不会触发 drop 事件
50
-
51
- const dt = e.dataTransfer;
52
- if (dt) {
53
- dt.dropEffect = 'move';
54
- }
55
- }
56
-
57
- let oldTr: HTMLElement | null = null;
58
- function onTrDragEnter(e: DragEvent) {
59
- if (!trDragFlag) return;
60
- e.preventDefault();
61
- const tr = getClosestTr(e);
62
- if (oldTr && oldTr !== tr) {
63
- // 两个tr不一样说明移动到了另一个tr中
64
- oldTr.classList.remove(TR_DRAG_OVER_CLASS);
65
- }
66
- if (tr) {
67
- oldTr = tr;
68
- tr.classList.add(TR_DRAG_OVER_CLASS);
69
- }
70
- }
71
-
72
- function onTrDragEnd(e: DragEvent) {
73
- if (!trDragFlag) return;
74
- const tr = getClosestTr(e);
75
- if (tr) {
76
- tr.classList.remove(TR_DRAGGING_CLASS);
77
- }
78
- if (oldTr) {
79
- oldTr.classList.remove(TR_DRAG_OVER_CLASS);
80
- oldTr = null;
81
- }
82
- trDragFlag = false;
83
- }
84
-
85
- function onTrDrop(e: DragEvent, rowIndex: number) {
86
- if (!trDragFlag) return;
87
- const dt = e.dataTransfer;
88
- if (!dt) return;
89
- const mode = dragRowConfig.value.mode;
90
- const sourceIndex = Number(dt.getData(DATA_TRANSFER_FORMAT));
91
- // dt.clearData(DATA_TRANSFER_FORMAT); // firefox not work
92
- const endIndex = rowIndex;
93
- if (sourceIndex === endIndex) return;
94
-
95
- if (mode !== 'none') {
96
- const dataSourceTemp = dataSourceCopy.value.slice();
97
- const sourceRow = dataSourceTemp[sourceIndex];
98
- if (mode === 'swap') {
99
- dataSourceTemp[sourceIndex] = dataSourceTemp[endIndex];
100
- dataSourceTemp[endIndex] = sourceRow;
101
- } else {
102
- dataSourceTemp.splice(sourceIndex, 1);
103
- dataSourceTemp.splice(endIndex, 0, sourceRow);
104
- }
105
- dataSourceCopy.value = dataSourceTemp;
106
- }
107
- emits('row-order-change', sourceIndex, endIndex);
108
- }
109
-
110
- return {
111
- dragRowConfig,
112
- onTrDragStart,
113
- onTrDragEnter,
114
- onTrDragOver,
115
- onTrDrop,
116
- onTrDragEnd,
117
- };
118
- }
1
+ import { computed, ShallowRef } from 'vue';
2
+ import { DragRowConfig } from './types';
3
+
4
+ type Params = {
5
+ props: any;
6
+ emits: any;
7
+ dataSourceCopy: ShallowRef<any[]>;
8
+ };
9
+
10
+ const TR_DRAGGING_CLASS = 'tr-dragging';
11
+ const TR_DRAG_OVER_CLASS = 'tr-dragging-over';
12
+ const DATA_TRANSFER_FORMAT = 'text/plain';
13
+
14
+ /**
15
+ * 拖拽行
16
+ * TODO: 不在虚拟滚动的情况下,从上面拖拽到下面,跨页的时候,滚动条会自适应位置。没有顶上去
17
+ */
18
+ export function useTrDrag({ props, emits, dataSourceCopy }: Params) {
19
+ let trDragFlag = false;
20
+
21
+ const dragRowConfig = computed<DragRowConfig>(() => {
22
+ return { mode: 'insert', ...props.dragRowConfig };
23
+ });
24
+
25
+ function getClosestTr(e: DragEvent) {
26
+ const target = e.target as HTMLElement;
27
+ const tr = target?.closest('tr');
28
+ return tr;
29
+ }
30
+
31
+ function onTrDragStart(e: DragEvent, rowIndex: number) {
32
+ const tr = getClosestTr(e);
33
+ if (tr) {
34
+ const trRect = tr.getBoundingClientRect();
35
+ const x = e.clientX - (trRect.left ?? 0);
36
+ e.dataTransfer?.setDragImage(tr, x, trRect.height / 2);
37
+ tr.classList.add(TR_DRAGGING_CLASS);
38
+ }
39
+ const dt = e.dataTransfer;
40
+ if (dt) {
41
+ dt.effectAllowed = 'move';
42
+ dt.setData(DATA_TRANSFER_FORMAT, String(rowIndex));
43
+ }
44
+ trDragFlag = true;
45
+ }
46
+
47
+ function onTrDragOver(e: DragEvent) {
48
+ if (!trDragFlag) return;
49
+ e.preventDefault(); // 阻止默认行为,否则不会触发 drop 事件
50
+
51
+ const dt = e.dataTransfer;
52
+ if (dt) {
53
+ dt.dropEffect = 'move';
54
+ }
55
+ }
56
+
57
+ let oldTr: HTMLElement | null = null;
58
+ function onTrDragEnter(e: DragEvent) {
59
+ if (!trDragFlag) return;
60
+ e.preventDefault();
61
+ const tr = getClosestTr(e);
62
+ if (oldTr && oldTr !== tr) {
63
+ // 两个tr不一样说明移动到了另一个tr中
64
+ oldTr.classList.remove(TR_DRAG_OVER_CLASS);
65
+ }
66
+ if (tr) {
67
+ oldTr = tr;
68
+ tr.classList.add(TR_DRAG_OVER_CLASS);
69
+ }
70
+ }
71
+
72
+ function onTrDragEnd(e: DragEvent) {
73
+ if (!trDragFlag) return;
74
+ const tr = getClosestTr(e);
75
+ if (tr) {
76
+ tr.classList.remove(TR_DRAGGING_CLASS);
77
+ }
78
+ if (oldTr) {
79
+ oldTr.classList.remove(TR_DRAG_OVER_CLASS);
80
+ oldTr = null;
81
+ }
82
+ trDragFlag = false;
83
+ }
84
+
85
+ function onTrDrop(e: DragEvent, rowIndex: number) {
86
+ if (!trDragFlag) return;
87
+ const dt = e.dataTransfer;
88
+ if (!dt) return;
89
+ const mode = dragRowConfig.value.mode;
90
+ const sourceIndex = Number(dt.getData(DATA_TRANSFER_FORMAT));
91
+ // dt.clearData(DATA_TRANSFER_FORMAT); // firefox not work
92
+ const endIndex = rowIndex;
93
+ if (sourceIndex === endIndex) return;
94
+
95
+ if (mode !== 'none') {
96
+ const dataSourceTemp = dataSourceCopy.value.slice();
97
+ const sourceRow = dataSourceTemp[sourceIndex];
98
+ if (mode === 'swap') {
99
+ dataSourceTemp[sourceIndex] = dataSourceTemp[endIndex];
100
+ dataSourceTemp[endIndex] = sourceRow;
101
+ } else {
102
+ dataSourceTemp.splice(sourceIndex, 1);
103
+ dataSourceTemp.splice(endIndex, 0, sourceRow);
104
+ }
105
+ dataSourceCopy.value = dataSourceTemp;
106
+ }
107
+ emits('row-order-change', sourceIndex, endIndex);
108
+ }
109
+
110
+ return {
111
+ dragRowConfig,
112
+ onTrDragStart,
113
+ onTrDragEnter,
114
+ onTrDragOver,
115
+ onTrDrop,
116
+ onTrDragEnd,
117
+ };
118
+ }
@@ -0,0 +1,158 @@
1
+ import { ShallowRef } from 'vue';
2
+ import { PrivateRowDT, TreeConfig, UniqKey } from './types';
3
+
4
+ type DT = PrivateRowDT & { children?: DT[] };
5
+ type Option<DT extends Record<string, any>> = {
6
+ props: any;
7
+ rowKeyGen: (row: any) => UniqKey;
8
+ dataSourceCopy: ShallowRef<DT[]>;
9
+ emits: any;
10
+ };
11
+
12
+ export function useTree({ props, dataSourceCopy, rowKeyGen, emits }: Option<DT>) {
13
+ const { defaultExpandAll, defaultExpandKeys, defaultExpandLevel }: TreeConfig = props.treeConfig;
14
+
15
+ /** click expended icon to toggle expand row */
16
+ function toggleTreeNode(row: DT, col: any) {
17
+ const expand = row ? !row.__T_EXPANDED__ : false;
18
+ privateSetTreeExpand(row, { expand, col, isClick: true });
19
+ }
20
+
21
+ /**
22
+ *
23
+ * @param row rowKey or row
24
+ * @param option
25
+ * @param option.expand expand or collapse
26
+ * @param option.silent if set true, not emit `toggle-tree-expand`, default:false
27
+ */
28
+ function privateSetTreeExpand(row: (UniqKey | DT) | (UniqKey | DT)[], option: { expand?: boolean; col?: any; isClick: boolean }) {
29
+ const rowKeyOrRowArr: (UniqKey | DT)[] = Array.isArray(row) ? row : [row];
30
+
31
+ const tempData = dataSourceCopy.value.slice();
32
+ for (let i = 0; i < rowKeyOrRowArr.length; i++) {
33
+ const rowKeyOrRow = rowKeyOrRowArr[i];
34
+ let rowKey: UniqKey;
35
+ if (typeof rowKeyOrRow === 'string') {
36
+ rowKey = rowKeyOrRow;
37
+ } else {
38
+ rowKey = rowKeyGen(rowKeyOrRow);
39
+ }
40
+ const index = tempData.findIndex(it => rowKeyGen(it) === rowKey);
41
+ if (index === -1) {
42
+ console.warn('treeExpandRow failed.rowKey:', rowKey);
43
+ return;
44
+ }
45
+
46
+ const row = tempData[index];
47
+ const level = row.__T_LV__ || 0;
48
+ let expanded = option?.expand;
49
+ if (expanded === void 0) {
50
+ expanded = !row.__T_EXPANDED__;
51
+ }
52
+ if (expanded) {
53
+ const children = expandNode(row, level);
54
+ tempData.splice(index + 1, 0, ...children);
55
+ } else {
56
+ // delete all child nodes from i
57
+ const deleteCount = foldNode(index, tempData, level);
58
+ tempData.splice(index + 1, deleteCount);
59
+ }
60
+
61
+ setNodeExpanded(row, expanded, level);
62
+
63
+ if (option.isClick) {
64
+ emits('toggle-tree-expand', { expanded: Boolean(expanded), row, col: option.col });
65
+ }
66
+ }
67
+
68
+ dataSourceCopy.value = tempData;
69
+ }
70
+
71
+ function setTreeExpand(row: (UniqKey | DT) | (UniqKey | DT)[], option?: { expand?: boolean }) {
72
+ privateSetTreeExpand(row, { ...option, isClick: false });
73
+ }
74
+
75
+ function setNodeExpanded(row: DT, expanded: boolean, level?: number, parent?: DT) {
76
+ row.__T_EXPANDED__ = expanded;
77
+ if (level !== void 0) {
78
+ row.__T_LV__ = level;
79
+ }
80
+ if (parent) {
81
+ row.__T_PARENT_K__ = rowKeyGen(parent);
82
+ }
83
+ }
84
+
85
+ /**
86
+ * 根据保存的展开状态,深度遍历,展平树形数据。
87
+ * @param data
88
+ * @returns
89
+ */
90
+ function flatTreeData(data: DT[]) {
91
+ const result: DT[] = [];
92
+ (function recursion(data: DT[] | undefined, level: number, parent?: DT) {
93
+ if (!data) return;
94
+ for (let i = 0; i < data.length; i++) {
95
+ const item = data[i];
96
+ result.push(item);
97
+ const isExpanded = Boolean(item.__T_EXPANDED__);
98
+ setNodeExpanded(item, isExpanded, level, parent);
99
+ if (!isExpanded) {
100
+ if (defaultExpandAll) {
101
+ setNodeExpanded(item, true);
102
+ } else {
103
+ if (defaultExpandLevel) {
104
+ if (level < defaultExpandLevel) {
105
+ setNodeExpanded(item, true);
106
+ }
107
+ }
108
+ if (defaultExpandKeys) {
109
+ if (defaultExpandKeys.includes(rowKeyGen(item))) {
110
+ setNodeExpanded(item, true);
111
+ }
112
+ }
113
+ if (!item.__T_EXPANDED__) {
114
+ continue;
115
+ }
116
+ }
117
+ }
118
+ recursion(item.children, level + 1, item);
119
+ }
120
+ })(data, 0);
121
+ return result;
122
+ }
123
+
124
+ function expandNode(row: DT, level: number) {
125
+ let result: DT[] = [];
126
+ row.children &&
127
+ row.children.forEach(child => {
128
+ result.push(child);
129
+ const childLv = level + 1;
130
+ if (child.__T_EXPANDED__ && child.children) {
131
+ const res = expandNode(child, childLv);
132
+ result = result.concat(res);
133
+ } else {
134
+ setNodeExpanded(child, false, childLv, row);
135
+ }
136
+ });
137
+ return result;
138
+ }
139
+
140
+ function foldNode(index: number, tempData: DT[], level: number) {
141
+ let deleteCount = 0;
142
+ for (let i = index + 1; i < tempData.length; i++) {
143
+ const child = tempData[i];
144
+ if (child.__T_LV__ && child.__T_LV__ > level) {
145
+ deleteCount++;
146
+ } else {
147
+ break;
148
+ }
149
+ }
150
+ return deleteCount;
151
+ }
152
+
153
+ return {
154
+ toggleTreeNode,
155
+ setTreeExpand,
156
+ flatTreeData,
157
+ };
158
+ }