|
| 1 | +import type React from 'react'; |
| 2 | +import { useRef, useEffect, useState } from 'react'; |
| 3 | +import { useTabStore } from '@/stores/tabStore'; |
| 4 | + |
| 5 | +interface KeepAliveProps { |
| 6 | + children: React.ReactNode; |
| 7 | +} |
| 8 | + |
| 9 | +interface CacheItem { |
| 10 | + component: React.ReactElement; |
| 11 | + scrollTop: number; |
| 12 | + scrollLeft: number; |
| 13 | +} |
| 14 | + |
| 15 | +const KeepAlive: React.FC<KeepAliveProps> = ({ children }) => { |
| 16 | + const { tabs, activeKey } = useTabStore(); |
| 17 | + const cacheRef = useRef<Map<string, CacheItem>>(new Map()); |
| 18 | + const [currentComponent, setCurrentComponent] = useState<React.ReactElement | null>(null); |
| 19 | + const containerRef = useRef<HTMLDivElement>(null); |
| 20 | + |
| 21 | + // 保存当前页面的滚动位置 |
| 22 | + const saveScrollPosition = (key: string) => { |
| 23 | + if (containerRef.current) { |
| 24 | + const scrollTop = containerRef.current.scrollTop; |
| 25 | + const scrollLeft = containerRef.current.scrollLeft; |
| 26 | + |
| 27 | + const cached = cacheRef.current.get(key); |
| 28 | + if (cached) { |
| 29 | + cached.scrollTop = scrollTop; |
| 30 | + cached.scrollLeft = scrollLeft; |
| 31 | + } |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + // 恢复页面的滚动位置 |
| 36 | + const restoreScrollPosition = (key: string) => { |
| 37 | + const cached = cacheRef.current.get(key); |
| 38 | + if (cached && containerRef.current) { |
| 39 | + // 使用setTimeout确保DOM更新后再设置滚动位置 |
| 40 | + setTimeout(() => { |
| 41 | + if (containerRef.current) { |
| 42 | + containerRef.current.scrollTop = cached.scrollTop; |
| 43 | + containerRef.current.scrollLeft = cached.scrollLeft; |
| 44 | + } |
| 45 | + }, 0); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (!activeKey) return; |
| 51 | + |
| 52 | + // 保存之前页面的滚动位置 |
| 53 | + const previousActiveKey = Object.keys(cacheRef.current).find(key => key !== activeKey); |
| 54 | + if (previousActiveKey) { |
| 55 | + saveScrollPosition(previousActiveKey); |
| 56 | + } |
| 57 | + |
| 58 | + // 检查缓存中是否已有当前页面 |
| 59 | + let cached = cacheRef.current.get(activeKey); |
| 60 | + |
| 61 | + if (!cached) { |
| 62 | + // 如果没有缓存,创建新的缓存项 |
| 63 | + cached = { |
| 64 | + component: children as React.ReactElement, |
| 65 | + scrollTop: 0, |
| 66 | + scrollLeft: 0 |
| 67 | + }; |
| 68 | + cacheRef.current.set(activeKey, cached); |
| 69 | + } |
| 70 | + |
| 71 | + // 设置当前显示的组件 |
| 72 | + setCurrentComponent(cached.component); |
| 73 | + |
| 74 | + // 恢复滚动位置 |
| 75 | + restoreScrollPosition(activeKey); |
| 76 | + |
| 77 | + }, [activeKey, children]); |
| 78 | + |
| 79 | + // 清理不存在的tab对应的缓存 |
| 80 | + useEffect(() => { |
| 81 | + const tabKeys = tabs.map(tab => tab.key); |
| 82 | + const cacheKeys = Array.from(cacheRef.current.keys()); |
| 83 | + |
| 84 | + cacheKeys.forEach(key => { |
| 85 | + if (!tabKeys.includes(key)) { |
| 86 | + cacheRef.current.delete(key); |
| 87 | + } |
| 88 | + }); |
| 89 | + }, [tabs]); |
| 90 | + |
| 91 | + if (!currentComponent) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + return ( |
| 96 | + <div |
| 97 | + ref={containerRef} |
| 98 | + className="keep-alive-container" |
| 99 | + style={{ |
| 100 | + height: '100%', |
| 101 | + overflow: 'auto', |
| 102 | + display: 'flex', |
| 103 | + flexDirection: 'column', |
| 104 | + position: 'relative' |
| 105 | + }} |
| 106 | + > |
| 107 | + {currentComponent} |
| 108 | + </div> |
| 109 | + ); |
| 110 | +}; |
| 111 | + |
| 112 | +export default KeepAlive; |
0 commit comments