wujingjing
2024-08-29 19b778d2d04bed31ce2e1f167c6ff2fda9906421
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import type { TableInstance } from 'element-plus';
import { onActivated, ref, type Ref } from 'vue';
import { onBeforeRouteLeave } from 'vue-router';
 
// 其他特殊滚动项
// type ExtItemContainer = TableInstance;
export const enum ExtItemType {
    ElTable,
}
export const enum ScrollType {
    X,
    Y,
    All,
}
 
type ExtItem = { container: any; type: ExtItemType };
export type ScrollKind = Ref<HTMLElement> | string | ExtItem;
type ScrollItem = { item: ScrollKind; scrollType: ScrollType } | ScrollKind;
type ScrollTarget = Array<ScrollItem> | ScrollItem;
 
type DomItem = { dom: HTMLElement; scrollType: ScrollType; scrollKind: ScrollKind };
/** @description 判断 ExtItemType 类型 */
function isExtItemType(param: any): param is ExtItem {
    return !!param?.type || param?.type === 0;
}
 
/** @description 判断 ExtItemType 类型 */
function isHtmlElement(param: any): param is Ref<HTMLElement> {
    return !!param?.value;
}
 
function isScrollKind(param: any): param is ScrollKind {
    return typeof param.scrollType === 'undefined';
}
 
const getDomList = (target: ScrollItem[]): DomItem[] => {
    const domList = target.map((item) => {
        let scrollItem: ScrollKind;
        let scrollType: ScrollType;
        if (isScrollKind(item)) {
            scrollItem = item;
            // 取默认值 All
            scrollType = ScrollType.All;
        } else {
            scrollItem = item.item;
            scrollType = item.scrollType;
        }
        let dom: HTMLElement;
        if (typeof scrollItem === 'string') {
            dom = document.querySelector(scrollItem);
        } else {
            if (isExtItemType(scrollItem)) {
                switch (scrollItem.type) {
                    case ExtItemType.ElTable:
                        dom = scrollItem.container.value.$el.querySelector('.el-scrollbar__wrap');
                        break;
                    default:
                        dom = null;
                        break;
                }
            } else if (isHtmlElement(scrollItem)) {
                dom = scrollItem.value;
            } else {
                dom = null;
            }
        }
 
        return {
            dom,
            scrollType: scrollType,
            scrollKind: scrollItem,
        };
    });
    return domList;
};
 
const extFixLayout = (domItem: DomItem) => {
    const kind = domItem.scrollKind as any;
    if (!kind.type) return;
    switch (kind.type) {
        case ExtItemType.ElTable:
            const tableInstanceRef = kind.container as Ref<TableInstance>;
            // tableInstanceRef.value.doLayout();
            break;
    }
};
export const useSaveScroll = (target: ScrollTarget) => {
    const targetArr = Array.isArray(target) ? target : [target];
 
    const savedScroll = ref<
        {
            top: number;
            left: number;
        }[]
    >(
        Array(targetArr.length).fill({
            top: null,
            left: null,
        })
    );
    onBeforeRouteLeave((to, from, next) => {
        const domList = getDomList(targetArr);
        domList.map((item, index) => {
            switch (item.scrollType) {
                case ScrollType.X:
                    savedScroll.value[index].left = item.dom?.scrollLeft ?? 0;
                    break;
                case ScrollType.Y:
                    savedScroll.value[index].top = item.dom?.scrollTop ?? 0;
                    break;
 
                case ScrollType.All:
                    savedScroll.value[index].left = item.dom?.scrollLeft ?? 0;
                    savedScroll.value[index].top = item.dom?.scrollTop ?? 0;
 
                    break;
            }
        });
        next();
    });
 
    onActivated(() => {
        const domList = getDomList(targetArr);
        domList.map((item, index) => {
            if (item.dom) {
                switch (item.scrollType) {
                    case ScrollType.X:
                        item.dom.scrollLeft = savedScroll.value[index].left;
 
                        break;
                    case ScrollType.Y:
                        item.dom.scrollTop = savedScroll.value[index].top;
                        break;
 
                    case ScrollType.All:
                        item.dom.scrollLeft = savedScroll.value[index].left;
                        item.dom.scrollTop = savedScroll.value[index].top;
                        break;
                }
                // extFixLayout(item);
            }
        });
    });
};