mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-26 23:30:09 +08:00
+1
-1
@@ -32,4 +32,4 @@
|
|||||||
|
|
||||||
### 👙 界面样式
|
### 👙 界面样式
|
||||||
|
|
||||||
- 代理组实现sticky scroll的效果
|
- 代理组实现 Sticky Scroll 效果
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ import {
|
|||||||
Snackbar,
|
Snackbar,
|
||||||
Typography,
|
Typography,
|
||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
|
import { useTheme } from '@mui/material/styles'
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { useVirtualizer } from '@tanstack/react-virtual'
|
import { defaultRangeExtractor, useVirtualizer } from '@tanstack/react-virtual'
|
||||||
import { useLockFn } from 'ahooks'
|
import { useLockFn } from 'ahooks'
|
||||||
import {
|
import {
|
||||||
type Key,
|
type Key,
|
||||||
@@ -18,11 +19,13 @@ import {
|
|||||||
type RefObject,
|
type RefObject,
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
useLayoutEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import { useLocation } from 'react-router'
|
||||||
import { delayGroup, healthcheckProxyProvider } from 'tauri-plugin-mihomo-api'
|
import { delayGroup, healthcheckProxyProvider } from 'tauri-plugin-mihomo-api'
|
||||||
|
|
||||||
import { BaseEmpty } from '@/components/base'
|
import { BaseEmpty } from '@/components/base'
|
||||||
@@ -65,6 +68,7 @@ interface ProxyChainItem {
|
|||||||
|
|
||||||
export const ProxyGroups = (props: Props) => {
|
export const ProxyGroups = (props: Props) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
const { pathname } = useLocation()
|
||||||
const { mode, isChainMode = false, chainConfigData } = props
|
const { mode, isChainMode = false, chainConfigData } = props
|
||||||
|
|
||||||
// Drive 3s polling on the shared TQ cache; data is read via granular context below
|
// Drive 3s polling on the shared TQ cache; data is read via granular context below
|
||||||
@@ -158,8 +162,40 @@ export const ProxyGroups = (props: Props) => {
|
|||||||
|
|
||||||
const parentRef = useRef<HTMLDivElement>(null)
|
const parentRef = useRef<HTMLDivElement>(null)
|
||||||
const scrollPositionRef = useRef<Record<string, number>>({})
|
const scrollPositionRef = useRef<Record<string, number>>({})
|
||||||
|
const scrollTopRef = useRef(0)
|
||||||
const showScrollTopRef = useRef(false)
|
const showScrollTopRef = useRef(false)
|
||||||
|
const activeStickyIndexRef = useRef<number | null>(null)
|
||||||
|
const restoredScrollKeyRef = useRef<string | null>(null)
|
||||||
const [showScrollTop, setShowScrollTop] = useState(false)
|
const [showScrollTop, setShowScrollTop] = useState(false)
|
||||||
|
const scrollPositionKey = useMemo(
|
||||||
|
() =>
|
||||||
|
isChainMode
|
||||||
|
? `${mode}:chain:${activeSelectedGroup ?? 'all'}`
|
||||||
|
: `${mode}:normal`,
|
||||||
|
[activeSelectedGroup, isChainMode, mode],
|
||||||
|
)
|
||||||
|
const stickyGroupIndexes = useMemo(
|
||||||
|
() =>
|
||||||
|
renderList.flatMap((item, index) =>
|
||||||
|
item.type === 0 && !item.group.hidden ? [index] : [],
|
||||||
|
),
|
||||||
|
[renderList],
|
||||||
|
)
|
||||||
|
|
||||||
|
const rangeExtractor = useCallback(
|
||||||
|
(range: Parameters<typeof defaultRangeExtractor>[0]) => {
|
||||||
|
const activeStickyIndex = [...stickyGroupIndexes]
|
||||||
|
.reverse()
|
||||||
|
.find((index) => index <= range.startIndex)
|
||||||
|
activeStickyIndexRef.current = activeStickyIndex ?? null
|
||||||
|
|
||||||
|
const indexes = defaultRangeExtractor(range)
|
||||||
|
return activeStickyIndex == null || indexes.includes(activeStickyIndex)
|
||||||
|
? indexes
|
||||||
|
: [activeStickyIndex, ...indexes]
|
||||||
|
},
|
||||||
|
[stickyGroupIndexes],
|
||||||
|
)
|
||||||
|
|
||||||
const virtualizer = useVirtualizer({
|
const virtualizer = useVirtualizer({
|
||||||
count: renderList.length,
|
count: renderList.length,
|
||||||
@@ -167,58 +203,49 @@ export const ProxyGroups = (props: Props) => {
|
|||||||
estimateSize: () => 56,
|
estimateSize: () => 56,
|
||||||
overscan: 15,
|
overscan: 15,
|
||||||
getItemKey: (index) => renderList[index]?.key ?? index,
|
getItemKey: (index) => renderList[index]?.key ?? index,
|
||||||
|
rangeExtractor,
|
||||||
})
|
})
|
||||||
const virtualItems = virtualizer.getVirtualItems()
|
const virtualItems = virtualizer.getVirtualItems()
|
||||||
const stickyGroupByIndex = useMemo(
|
const activeStickyIndex = activeStickyIndexRef.current
|
||||||
() => getStickyGroupByIndex(renderList),
|
|
||||||
[renderList],
|
|
||||||
)
|
|
||||||
const stickyGroupItem = getStickyGroupItem(
|
|
||||||
stickyGroupByIndex,
|
|
||||||
virtualItems,
|
|
||||||
virtualizer.scrollOffset ?? 0,
|
|
||||||
)
|
|
||||||
|
|
||||||
// 从 localStorage 恢复滚动位置
|
// 从 localStorage 恢复滚动位置
|
||||||
useEffect(() => {
|
useLayoutEffect(() => {
|
||||||
if (renderList.length === 0) return
|
if (renderList.length === 0) return
|
||||||
|
const node = parentRef.current
|
||||||
let restoreTimer: ReturnType<typeof setTimeout> | null = null
|
if (!node) return
|
||||||
|
if (
|
||||||
|
restoredScrollKeyRef.current === scrollPositionKey &&
|
||||||
|
node.scrollTop === scrollTopRef.current
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const savedPositions = localStorage.getItem('proxy-scroll-positions')
|
const savedPositions = localStorage.getItem('proxy-scroll-positions')
|
||||||
if (savedPositions) {
|
if (savedPositions) {
|
||||||
const positions = JSON.parse(savedPositions)
|
const positions = JSON.parse(savedPositions)
|
||||||
scrollPositionRef.current = positions
|
scrollPositionRef.current = positions
|
||||||
const savedPosition = positions[mode]
|
const savedPosition = positions[scrollPositionKey]
|
||||||
|
|
||||||
if (savedPosition !== undefined) {
|
if (savedPosition !== undefined) {
|
||||||
restoreTimer = setTimeout(() => {
|
node.scrollTop = savedPosition
|
||||||
if (parentRef.current) {
|
scrollTopRef.current = savedPosition
|
||||||
parentRef.current.scrollTop = savedPosition
|
const nextShowScrollTop = savedPosition > 100
|
||||||
const nextShowScrollTop = savedPosition > 100
|
showScrollTopRef.current = nextShowScrollTop
|
||||||
showScrollTopRef.current = nextShowScrollTop
|
queueMicrotask(() => setShowScrollTop(nextShowScrollTop))
|
||||||
setShowScrollTop(nextShowScrollTop)
|
|
||||||
}
|
|
||||||
}, 100)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error restoring scroll position:', e)
|
console.error('Error restoring scroll position:', e)
|
||||||
}
|
}
|
||||||
|
restoredScrollKeyRef.current = scrollPositionKey
|
||||||
return () => {
|
}, [pathname, renderList.length, scrollPositionKey])
|
||||||
if (restoreTimer) {
|
|
||||||
clearTimeout(restoreTimer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [mode, renderList.length])
|
|
||||||
|
|
||||||
// 改为使用节流函数保存滚动位置
|
// 改为使用节流函数保存滚动位置
|
||||||
const saveScrollPosition = useCallback(
|
const saveScrollPosition = useCallback(
|
||||||
(scrollTop: number) => {
|
(scrollTop: number) => {
|
||||||
try {
|
try {
|
||||||
scrollPositionRef.current[mode] = scrollTop
|
scrollPositionRef.current[scrollPositionKey] = scrollTop
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
'proxy-scroll-positions',
|
'proxy-scroll-positions',
|
||||||
JSON.stringify(scrollPositionRef.current),
|
JSON.stringify(scrollPositionRef.current),
|
||||||
@@ -227,7 +254,7 @@ export const ProxyGroups = (props: Props) => {
|
|||||||
console.error('Error saving scroll position:', e)
|
console.error('Error saving scroll position:', e)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[mode],
|
[scrollPositionKey],
|
||||||
)
|
)
|
||||||
|
|
||||||
const saveScrollPositionThrottled = useMemo(
|
const saveScrollPositionThrottled = useMemo(
|
||||||
@@ -240,6 +267,7 @@ export const ProxyGroups = (props: Props) => {
|
|||||||
const target = event.target as HTMLElement | null
|
const target = event.target as HTMLElement | null
|
||||||
const nextScrollTop = target?.scrollTop ?? 0
|
const nextScrollTop = target?.scrollTop ?? 0
|
||||||
const nextShowScrollTop = nextScrollTop > 100
|
const nextShowScrollTop = nextScrollTop > 100
|
||||||
|
scrollTopRef.current = nextScrollTop
|
||||||
|
|
||||||
if (showScrollTopRef.current !== nextShowScrollTop) {
|
if (showScrollTopRef.current !== nextShowScrollTop) {
|
||||||
showScrollTopRef.current = nextShowScrollTop
|
showScrollTopRef.current = nextShowScrollTop
|
||||||
@@ -262,9 +290,12 @@ export const ProxyGroups = (props: Props) => {
|
|||||||
node.addEventListener('scroll', listener, options)
|
node.addEventListener('scroll', listener, options)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
if (restoredScrollKeyRef.current === scrollPositionKey) {
|
||||||
|
saveScrollPosition(scrollTopRef.current)
|
||||||
|
}
|
||||||
node.removeEventListener('scroll', listener, options)
|
node.removeEventListener('scroll', listener, options)
|
||||||
}
|
}
|
||||||
}, [handleScroll])
|
}, [handleScroll, saveScrollPosition, scrollPositionKey])
|
||||||
|
|
||||||
// 滚动到顶部
|
// 滚动到顶部
|
||||||
const scrollToTop = useCallback(() => {
|
const scrollToTop = useCallback(() => {
|
||||||
@@ -272,6 +303,7 @@ export const ProxyGroups = (props: Props) => {
|
|||||||
top: 0,
|
top: 0,
|
||||||
behavior: 'smooth',
|
behavior: 'smooth',
|
||||||
})
|
})
|
||||||
|
scrollTopRef.current = 0
|
||||||
saveScrollPosition(0)
|
saveScrollPosition(0)
|
||||||
}, [saveScrollPosition])
|
}, [saveScrollPosition])
|
||||||
|
|
||||||
@@ -453,7 +485,7 @@ export const ProxyGroups = (props: Props) => {
|
|||||||
totalSize={virtualizer.getTotalSize()}
|
totalSize={virtualizer.getTotalSize()}
|
||||||
virtualItems={virtualItems}
|
virtualItems={virtualItems}
|
||||||
renderList={renderList}
|
renderList={renderList}
|
||||||
stickyItem={stickyGroupItem}
|
activeStickyIndex={activeStickyIndex}
|
||||||
indent={mode === 'rule' || mode === 'script'}
|
indent={mode === 'rule' || mode === 'script'}
|
||||||
isChainMode={isChainMode}
|
isChainMode={isChainMode}
|
||||||
measureElement={virtualizer.measureElement}
|
measureElement={virtualizer.measureElement}
|
||||||
@@ -564,7 +596,7 @@ interface ProxyVirtualListProps {
|
|||||||
totalSize: number
|
totalSize: number
|
||||||
virtualItems: VirtualListItem[]
|
virtualItems: VirtualListItem[]
|
||||||
renderList: IRenderItem[]
|
renderList: IRenderItem[]
|
||||||
stickyItem: IRenderItem | null
|
activeStickyIndex: number | null
|
||||||
indent: boolean
|
indent: boolean
|
||||||
isChainMode?: boolean
|
isChainMode?: boolean
|
||||||
measureElement: (node: Element | null) => void
|
measureElement: (node: Element | null) => void
|
||||||
@@ -728,7 +760,7 @@ function ProxyVirtualList({
|
|||||||
totalSize,
|
totalSize,
|
||||||
virtualItems,
|
virtualItems,
|
||||||
renderList,
|
renderList,
|
||||||
stickyItem,
|
activeStickyIndex,
|
||||||
indent,
|
indent,
|
||||||
isChainMode,
|
isChainMode,
|
||||||
measureElement,
|
measureElement,
|
||||||
@@ -737,39 +769,12 @@ function ProxyVirtualList({
|
|||||||
onHeadState,
|
onHeadState,
|
||||||
onChangeProxy,
|
onChangeProxy,
|
||||||
}: ProxyVirtualListProps) {
|
}: ProxyVirtualListProps) {
|
||||||
|
const theme = useTheme()
|
||||||
|
const stickyBackground =
|
||||||
|
theme.palette.mode === 'dark' ? '#1e1f27' : 'var(--background-color)'
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={parentRef} style={{ height, overflow: 'auto' }}>
|
<div ref={parentRef} style={{ height, overflow: 'auto' }}>
|
||||||
{stickyItem && (
|
|
||||||
<Box
|
|
||||||
sx={{
|
|
||||||
position: 'sticky',
|
|
||||||
top: 0,
|
|
||||||
height: 0,
|
|
||||||
zIndex: 5,
|
|
||||||
pointerEvents: 'none',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Box
|
|
||||||
sx={{
|
|
||||||
pointerEvents: 'auto',
|
|
||||||
'& .MuiListItemButton-root': {
|
|
||||||
boxShadow: 3,
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<ProxyRender
|
|
||||||
item={stickyItem}
|
|
||||||
indent={indent}
|
|
||||||
onLocation={onLocation}
|
|
||||||
onCheckAll={onCheckAll}
|
|
||||||
onHeadState={onHeadState}
|
|
||||||
onChangeProxy={onChangeProxy}
|
|
||||||
isChainMode={isChainMode}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<div style={{ height: totalSize, position: 'relative' }}>
|
<div style={{ height: totalSize, position: 'relative' }}>
|
||||||
{virtualItems.map((virtualItem) => (
|
{virtualItems.map((virtualItem) => (
|
||||||
<div
|
<div
|
||||||
@@ -777,11 +782,24 @@ function ProxyVirtualList({
|
|||||||
data-index={virtualItem.index}
|
data-index={virtualItem.index}
|
||||||
ref={measureElement}
|
ref={measureElement}
|
||||||
style={{
|
style={{
|
||||||
position: 'absolute',
|
position:
|
||||||
|
virtualItem.index === activeStickyIndex ? 'sticky' : 'absolute',
|
||||||
top: 0,
|
top: 0,
|
||||||
left: 0,
|
left: 0,
|
||||||
|
zIndex: virtualItem.index === activeStickyIndex ? 5 : undefined,
|
||||||
|
display:
|
||||||
|
virtualItem.index === activeStickyIndex
|
||||||
|
? 'flow-root'
|
||||||
|
: undefined,
|
||||||
|
backgroundColor:
|
||||||
|
virtualItem.index === activeStickyIndex
|
||||||
|
? stickyBackground
|
||||||
|
: undefined,
|
||||||
width: '100%',
|
width: '100%',
|
||||||
transform: `translateY(${virtualItem.start}px)`,
|
transform:
|
||||||
|
virtualItem.index === activeStickyIndex
|
||||||
|
? undefined
|
||||||
|
: `translateY(${virtualItem.start}px)`,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<ProxyRender
|
<ProxyRender
|
||||||
@@ -801,30 +819,6 @@ function ProxyVirtualList({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStickyGroupByIndex(renderList: IRenderItem[]) {
|
|
||||||
let stickyGroup: IRenderItem | null = null
|
|
||||||
|
|
||||||
return renderList.map((item) => {
|
|
||||||
if (item?.type === 0 && item.group && !item.group.hidden) {
|
|
||||||
stickyGroup = item
|
|
||||||
}
|
|
||||||
return stickyGroup
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStickyGroupItem(
|
|
||||||
stickyGroupByIndex: (IRenderItem | null)[],
|
|
||||||
virtualItems: VirtualListItem[],
|
|
||||||
scrollTop: number,
|
|
||||||
) {
|
|
||||||
const firstVisibleItem =
|
|
||||||
virtualItems.find((item) => item.end > scrollTop + 1) ?? virtualItems[0]
|
|
||||||
|
|
||||||
if (!firstVisibleItem) return null
|
|
||||||
|
|
||||||
return stickyGroupByIndex[firstVisibleItem.index] ?? null
|
|
||||||
}
|
|
||||||
|
|
||||||
// 替换简单防抖函数为更优的节流函数
|
// 替换简单防抖函数为更优的节流函数
|
||||||
function throttle<T extends (...args: any[]) => any>(
|
function throttle<T extends (...args: any[]) => any>(
|
||||||
func: T,
|
func: T,
|
||||||
@@ -832,23 +826,30 @@ function throttle<T extends (...args: any[]) => any>(
|
|||||||
): (...args: Parameters<T>) => void {
|
): (...args: Parameters<T>) => void {
|
||||||
let timer: ReturnType<typeof setTimeout> | null = null
|
let timer: ReturnType<typeof setTimeout> | null = null
|
||||||
let previous = 0
|
let previous = 0
|
||||||
|
let lastArgs: Parameters<T> | null = null
|
||||||
|
|
||||||
|
const run = (args: Parameters<T>) => {
|
||||||
|
previous = Date.now()
|
||||||
|
timer = null
|
||||||
|
lastArgs = null
|
||||||
|
func(...args)
|
||||||
|
}
|
||||||
|
|
||||||
return function (...args: Parameters<T>) {
|
return function (...args: Parameters<T>) {
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const remaining = wait - (now - previous)
|
const remaining = wait - (now - previous)
|
||||||
|
lastArgs = args
|
||||||
|
|
||||||
if (remaining <= 0 || remaining > wait) {
|
if (remaining <= 0 || remaining > wait) {
|
||||||
if (timer) {
|
if (timer) {
|
||||||
clearTimeout(timer)
|
clearTimeout(timer)
|
||||||
timer = null
|
|
||||||
}
|
}
|
||||||
previous = now
|
run(args)
|
||||||
func(...args)
|
|
||||||
} else if (!timer) {
|
} else if (!timer) {
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
previous = Date.now()
|
if (lastArgs) {
|
||||||
timer = null
|
run(lastArgs)
|
||||||
func(...args)
|
}
|
||||||
}, remaining)
|
}, remaining)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user