stk-table-vue 0.6.8 → 0.6.10

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,106 +1,106 @@
1
- import { ComputedRef, Ref, ShallowRef, onBeforeUnmount, onMounted, watch } from 'vue';
2
- import { StkTableColumn } from './types';
3
- import { VirtualScrollStore, VirtualScrollXStore } from './useVirtualScroll';
4
-
5
- /** 翻页按键 */
6
- enum ScrollCodes {
7
- ArrowUp = 'ArrowUp',
8
- ArrowRight = 'ArrowRight',
9
- ArrowDown = 'ArrowDown',
10
- ArrowLeft = 'ArrowLeft',
11
- PageUp = 'PageUp',
12
- PageDown = 'PageDown',
13
- }
14
- /** 所有翻页按键数组 */
15
- const ScrollCodesValues = Object.values(ScrollCodes);
16
-
17
- type Options<DT extends Record<string, any>> = {
18
- props: any;
19
- scrollTo: (y: number | null, x: number | null) => void;
20
- virtualScroll: Ref<VirtualScrollStore>;
21
- virtualScrollX: Ref<VirtualScrollXStore>;
22
- tableHeaders: ShallowRef<StkTableColumn<DT>[][]>;
23
- virtual_on: ComputedRef<boolean>;
24
- };
25
- /**
26
- * 按下键盘箭头滚动。只有悬浮在表体上才能生效键盘。
27
- *
28
- * 在低版本浏览器中,虚拟滚动时,使用键盘滚动,等选中的行消失在视口外时,滚动会失效。
29
- */
30
- export function useKeyboardArrowScroll<DT extends Record<string, any>>(
31
- targetElement: Ref<HTMLElement | undefined>,
32
- { props, scrollTo, virtualScroll, virtualScrollX, tableHeaders, virtual_on }: Options<DT>,
33
- ) {
34
- /** 检测鼠标是否悬浮在表格体上 */
35
- let isMouseOver = false;
36
- watch(virtual_on, val => {
37
- if (!val) {
38
- removeListeners();
39
- } else {
40
- addEventListeners();
41
- }
42
- });
43
-
44
- onMounted(addEventListeners);
45
-
46
- onBeforeUnmount(removeListeners);
47
-
48
- function addEventListeners() {
49
- window.addEventListener('keydown', handleKeydown);
50
- targetElement.value?.addEventListener('mouseenter', handleMouseEnter);
51
- targetElement.value?.addEventListener('mouseleave', handleMouseLeave);
52
- targetElement.value?.addEventListener('mousedown', handleMouseDown);
53
- }
54
-
55
- function removeListeners() {
56
- window.removeEventListener('keydown', handleKeydown);
57
- targetElement.value?.removeEventListener('mouseenter', handleMouseEnter);
58
- targetElement.value?.removeEventListener('mouseleave', handleMouseLeave);
59
- targetElement.value?.removeEventListener('mousedown', handleMouseDown);
60
- }
61
-
62
- /** 键盘按下事件 */
63
- function handleKeydown(e: KeyboardEvent) {
64
- if (!virtual_on.value) return; // 非虚拟滚动使用浏览器默认滚动
65
- if (!ScrollCodesValues.includes(e.code as any)) return;
66
- if (!isMouseOver) return; // 不悬浮还是要触发键盘事件的
67
- e.preventDefault(); // 不触发键盘默认的箭头事件
68
-
69
- const { scrollTop, rowHeight, containerHeight } = virtualScroll.value;
70
- const { scrollLeft } = virtualScrollX.value;
71
- const { headless, headerRowHeight } = props;
72
-
73
- // 这里不用virtualScroll 中的pageSize,因为我需要上一页的最后一条放在下一页的第一条
74
- const headerHeight = headless ? 0 : tableHeaders.value.length * (headerRowHeight || rowHeight);
75
- /** 表体的page */
76
- const bodyPageSize = Math.floor((containerHeight - headerHeight) / rowHeight);
77
-
78
- if (e.code === ScrollCodes.ArrowUp) {
79
- scrollTo(scrollTop - rowHeight, null);
80
- } else if (e.code === ScrollCodes.ArrowRight) {
81
- scrollTo(null, scrollLeft + rowHeight);
82
- } else if (e.code === ScrollCodes.ArrowDown) {
83
- scrollTo(scrollTop + rowHeight, null);
84
- } else if (e.code === ScrollCodes.ArrowLeft) {
85
- scrollTo(null, scrollLeft - rowHeight);
86
- } else if (e.code === ScrollCodes.PageUp) {
87
- scrollTo(scrollTop - rowHeight * bodyPageSize + headerHeight, null);
88
- } else if (e.code === ScrollCodes.PageDown) {
89
- scrollTo(scrollTop + rowHeight * bodyPageSize - headerHeight, null);
90
- }
91
- }
92
-
93
- function handleMouseEnter() {
94
- isMouseOver = true;
95
- }
96
- function handleMouseLeave() {
97
- isMouseOver = false;
98
- }
99
- /**
100
- * 兜底。
101
- * 是否存在不触发mouseEnter的时候?
102
- */
103
- function handleMouseDown() {
104
- if (!isMouseOver) isMouseOver = true;
105
- }
106
- }
1
+ import { ComputedRef, Ref, ShallowRef, onBeforeUnmount, onMounted, watch } from 'vue';
2
+ import { StkTableColumn } from './types';
3
+ import { VirtualScrollStore, VirtualScrollXStore } from './useVirtualScroll';
4
+
5
+ /** 翻页按键 */
6
+ enum ScrollCodes {
7
+ ArrowUp = 'ArrowUp',
8
+ ArrowRight = 'ArrowRight',
9
+ ArrowDown = 'ArrowDown',
10
+ ArrowLeft = 'ArrowLeft',
11
+ PageUp = 'PageUp',
12
+ PageDown = 'PageDown',
13
+ }
14
+ /** 所有翻页按键数组 */
15
+ const ScrollCodesValues = Object.values(ScrollCodes);
16
+
17
+ type Options<DT extends Record<string, any>> = {
18
+ props: any;
19
+ scrollTo: (y: number | null, x: number | null) => void;
20
+ virtualScroll: Ref<VirtualScrollStore>;
21
+ virtualScrollX: Ref<VirtualScrollXStore>;
22
+ tableHeaders: ShallowRef<StkTableColumn<DT>[][]>;
23
+ virtual_on: ComputedRef<boolean>;
24
+ };
25
+ /**
26
+ * 按下键盘箭头滚动。只有悬浮在表体上才能生效键盘。
27
+ *
28
+ * 在低版本浏览器中,虚拟滚动时,使用键盘滚动,等选中的行消失在视口外时,滚动会失效。
29
+ */
30
+ export function useKeyboardArrowScroll<DT extends Record<string, any>>(
31
+ targetElement: Ref<HTMLElement | undefined>,
32
+ { props, scrollTo, virtualScroll, virtualScrollX, tableHeaders, virtual_on }: Options<DT>,
33
+ ) {
34
+ /** 检测鼠标是否悬浮在表格体上 */
35
+ let isMouseOver = false;
36
+ watch(virtual_on, val => {
37
+ if (!val) {
38
+ removeListeners();
39
+ } else {
40
+ addEventListeners();
41
+ }
42
+ });
43
+
44
+ onMounted(addEventListeners);
45
+
46
+ onBeforeUnmount(removeListeners);
47
+
48
+ function addEventListeners() {
49
+ window.addEventListener('keydown', handleKeydown);
50
+ targetElement.value?.addEventListener('mouseenter', handleMouseEnter);
51
+ targetElement.value?.addEventListener('mouseleave', handleMouseLeave);
52
+ targetElement.value?.addEventListener('mousedown', handleMouseDown);
53
+ }
54
+
55
+ function removeListeners() {
56
+ window.removeEventListener('keydown', handleKeydown);
57
+ targetElement.value?.removeEventListener('mouseenter', handleMouseEnter);
58
+ targetElement.value?.removeEventListener('mouseleave', handleMouseLeave);
59
+ targetElement.value?.removeEventListener('mousedown', handleMouseDown);
60
+ }
61
+
62
+ /** 键盘按下事件 */
63
+ function handleKeydown(e: KeyboardEvent) {
64
+ if (!virtual_on.value) return; // 非虚拟滚动使用浏览器默认滚动
65
+ if (!ScrollCodesValues.includes(e.code as any)) return;
66
+ if (!isMouseOver) return; // 不悬浮还是要触发键盘事件的
67
+ e.preventDefault(); // 不触发键盘默认的箭头事件
68
+
69
+ const { scrollTop, rowHeight, containerHeight } = virtualScroll.value;
70
+ const { scrollLeft } = virtualScrollX.value;
71
+ const { headless, headerRowHeight } = props;
72
+
73
+ // 这里不用virtualScroll 中的pageSize,因为我需要上一页的最后一条放在下一页的第一条
74
+ const headerHeight = headless ? 0 : tableHeaders.value.length * (headerRowHeight || rowHeight);
75
+ /** 表体的page */
76
+ const bodyPageSize = Math.floor((containerHeight - headerHeight) / rowHeight);
77
+
78
+ if (e.code === ScrollCodes.ArrowUp) {
79
+ scrollTo(scrollTop - rowHeight, null);
80
+ } else if (e.code === ScrollCodes.ArrowRight) {
81
+ scrollTo(null, scrollLeft + rowHeight);
82
+ } else if (e.code === ScrollCodes.ArrowDown) {
83
+ scrollTo(scrollTop + rowHeight, null);
84
+ } else if (e.code === ScrollCodes.ArrowLeft) {
85
+ scrollTo(null, scrollLeft - rowHeight);
86
+ } else if (e.code === ScrollCodes.PageUp) {
87
+ scrollTo(scrollTop - rowHeight * bodyPageSize + headerHeight, null);
88
+ } else if (e.code === ScrollCodes.PageDown) {
89
+ scrollTo(scrollTop + rowHeight * bodyPageSize - headerHeight, null);
90
+ }
91
+ }
92
+
93
+ function handleMouseEnter() {
94
+ isMouseOver = true;
95
+ }
96
+ function handleMouseLeave() {
97
+ isMouseOver = false;
98
+ }
99
+ /**
100
+ * 兜底。
101
+ * 是否存在不触发mouseEnter的时候?
102
+ */
103
+ function handleMouseDown() {
104
+ if (!isMouseOver) isMouseOver = true;
105
+ }
106
+ }
@@ -1,102 +1,102 @@
1
- import { computed, ComputedRef } from 'vue';
2
- import { StkTableColumn, UniqKey } from './types';
3
- import { isEmptyValue } from './utils';
4
-
5
- type Params<T extends Record<string, any>> = {
6
- props: any;
7
- emits: any;
8
- colKeyGen: ComputedRef<(col: StkTableColumn<T>) => UniqKey>;
9
- };
10
- /**
11
- * 列顺序拖动
12
- * @returns
13
- */
14
- export function useThDrag<DT extends Record<string, any>>({ props, emits, colKeyGen }: Params<DT>) {
15
- const findParentTH = (e: DragEvent) => (e.target as HTMLElement).closest('th');
16
-
17
- const dragConfig = computed(() => {
18
- const headerDrag = props.headerDrag;
19
- const draggable = headerDrag !== false; // true or object
20
- return {
21
- draggable,
22
- mode: 'insert',
23
- disabled: () => false,
24
- ...headerDrag,
25
- };
26
- });
27
-
28
- /** 开始拖动记录th位置 */
29
- function onThDragStart(e: DragEvent) {
30
- const th = findParentTH(e);
31
- if (!th) return;
32
- const dragStartKey = th.dataset.colKey || '';
33
- const dt = e.dataTransfer;
34
- if (dt) {
35
- dt.effectAllowed = 'move';
36
- dt.setData('text/plain', dragStartKey);
37
- }
38
-
39
- emits('th-drag-start', dragStartKey);
40
- }
41
-
42
- function onThDragOver(e: DragEvent) {
43
- const th = findParentTH(e);
44
- if (!th) return;
45
-
46
- const isHeaderDraggable = th.getAttribute('draggable') === 'true';
47
- if (!isHeaderDraggable) return;
48
-
49
- const dt = e.dataTransfer;
50
- if (dt) {
51
- dt.dropEffect = 'move';
52
- }
53
- e.preventDefault();
54
- }
55
-
56
- /** th拖动释放时 */
57
- function onThDrop(e: DragEvent) {
58
- const th = findParentTH(e);
59
- if (!th) return;
60
- const dragStartKey = e.dataTransfer?.getData('text');
61
- if (dragStartKey !== th.dataset.colKey) {
62
- handleColOrderChange(dragStartKey, th.dataset.colKey);
63
- }
64
- emits('th-drop', th.dataset.colKey);
65
- }
66
-
67
- /** 列拖动交换顺序 */
68
- function handleColOrderChange(dragStartKey: string | undefined, dragEndKey: string | undefined) {
69
- if (isEmptyValue(dragStartKey) || isEmptyValue(dragEndKey)) return;
70
-
71
- if (dragConfig.value.mode !== 'none') {
72
- const columns = [...props.columns];
73
-
74
- const dragStartIndex = columns.findIndex(col => colKeyGen.value(col) === dragStartKey);
75
- const dragEndIndex = columns.findIndex(col => colKeyGen.value(col) === dragEndKey);
76
-
77
- if (dragStartIndex === -1 || dragEndIndex === -1) return;
78
-
79
- const dragStartCol = columns[dragStartIndex];
80
- // if mode is none, do nothing
81
- if (dragConfig.value.mode === 'swap') {
82
- columns[dragStartIndex] = columns[dragEndIndex];
83
- columns[dragEndIndex] = dragStartCol;
84
- } else {
85
- // default is insert
86
- columns.splice(dragStartIndex, 1);
87
- columns.splice(dragEndIndex, 0, dragStartCol);
88
- }
89
- emits('update:columns', columns);
90
- }
91
-
92
- emits('col-order-change', dragStartKey, dragEndKey);
93
- }
94
-
95
- return {
96
- onThDragStart,
97
- onThDragOver,
98
- onThDrop,
99
- /** 是否可拖拽 */
100
- isHeaderDraggable: (col: StkTableColumn<DT>) => dragConfig.value.draggable && !dragConfig.value.disabled(col),
101
- };
102
- }
1
+ import { computed, ComputedRef } from 'vue';
2
+ import { StkTableColumn, UniqKey } from './types';
3
+ import { isEmptyValue } from './utils';
4
+
5
+ type Params<T extends Record<string, any>> = {
6
+ props: any;
7
+ emits: any;
8
+ colKeyGen: ComputedRef<(col: StkTableColumn<T>) => UniqKey>;
9
+ };
10
+ /**
11
+ * 列顺序拖动
12
+ * @returns
13
+ */
14
+ export function useThDrag<DT extends Record<string, any>>({ props, emits, colKeyGen }: Params<DT>) {
15
+ const findParentTH = (e: DragEvent) => (e.target as HTMLElement).closest('th');
16
+
17
+ const dragConfig = computed(() => {
18
+ const headerDrag = props.headerDrag;
19
+ const draggable = headerDrag !== false; // true or object
20
+ return {
21
+ draggable,
22
+ mode: 'insert',
23
+ disabled: () => false,
24
+ ...headerDrag,
25
+ };
26
+ });
27
+
28
+ /** 开始拖动记录th位置 */
29
+ function onThDragStart(e: DragEvent) {
30
+ const th = findParentTH(e);
31
+ if (!th) return;
32
+ const dragStartKey = th.dataset.colKey || '';
33
+ const dt = e.dataTransfer;
34
+ if (dt) {
35
+ dt.effectAllowed = 'move';
36
+ dt.setData('text/plain', dragStartKey);
37
+ }
38
+
39
+ emits('th-drag-start', dragStartKey);
40
+ }
41
+
42
+ function onThDragOver(e: DragEvent) {
43
+ const th = findParentTH(e);
44
+ if (!th) return;
45
+
46
+ const isHeaderDraggable = th.getAttribute('draggable') === 'true';
47
+ if (!isHeaderDraggable) return;
48
+
49
+ const dt = e.dataTransfer;
50
+ if (dt) {
51
+ dt.dropEffect = 'move';
52
+ }
53
+ e.preventDefault();
54
+ }
55
+
56
+ /** th拖动释放时 */
57
+ function onThDrop(e: DragEvent) {
58
+ const th = findParentTH(e);
59
+ if (!th) return;
60
+ const dragStartKey = e.dataTransfer?.getData('text');
61
+ if (dragStartKey !== th.dataset.colKey) {
62
+ handleColOrderChange(dragStartKey, th.dataset.colKey);
63
+ }
64
+ emits('th-drop', th.dataset.colKey);
65
+ }
66
+
67
+ /** 列拖动交换顺序 */
68
+ function handleColOrderChange(dragStartKey: string | undefined, dragEndKey: string | undefined) {
69
+ if (isEmptyValue(dragStartKey) || isEmptyValue(dragEndKey)) return;
70
+
71
+ if (dragConfig.value.mode !== 'none') {
72
+ const columns: StkTableColumn<any>[] = props.columns.slice();
73
+
74
+ const dragStartIndex = columns.findIndex(col => colKeyGen.value(col) === dragStartKey);
75
+ const dragEndIndex = columns.findIndex(col => colKeyGen.value(col) === dragEndKey);
76
+
77
+ if (dragStartIndex === -1 || dragEndIndex === -1) return;
78
+
79
+ const dragStartCol = columns[dragStartIndex];
80
+ // if mode is none, do nothing
81
+ if (dragConfig.value.mode === 'swap') {
82
+ columns[dragStartIndex] = columns[dragEndIndex];
83
+ columns[dragEndIndex] = dragStartCol;
84
+ } else {
85
+ // default is insert
86
+ columns.splice(dragStartIndex, 1);
87
+ columns.splice(dragEndIndex, 0, dragStartCol);
88
+ }
89
+ emits('update:columns', columns);
90
+ }
91
+
92
+ emits('col-order-change', dragStartKey, dragEndKey);
93
+ }
94
+
95
+ return {
96
+ onThDragStart,
97
+ onThDragOver,
98
+ onThDrop,
99
+ /** 是否可拖拽 */
100
+ isHeaderDraggable: (col: StkTableColumn<DT>) => dragConfig.value.draggable && !dragConfig.value.disabled(col),
101
+ };
102
+ }
@@ -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];
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
+ }