2026-04-09 18:14:28 +08:00
|
|
|
|
import { getConfiguredTableDefaultPageSize, normalizeTablePageSize } from '@/utils/tablePreferences'
|
|
|
|
|
|
|
2026-03-18 10:53:27 +08:00
|
|
|
|
const STORAGE_KEY = 'table-page-size'
|
2026-04-09 18:14:28 +08:00
|
|
|
|
const SOURCE_KEY = 'table-page-size-source'
|
2026-03-18 10:53:27 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从 localStorage 读取/写入 pageSize
|
|
|
|
|
|
* 全局共享一个 key,所有表格统一偏好
|
|
|
|
|
|
*/
|
2026-04-09 18:14:28 +08:00
|
|
|
|
export function getPersistedPageSize(fallback = getConfiguredTableDefaultPageSize()): number {
|
2026-03-18 10:53:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const stored = localStorage.getItem(STORAGE_KEY)
|
|
|
|
|
|
if (stored) {
|
2026-04-09 18:14:28 +08:00
|
|
|
|
return normalizeTablePageSize(stored)
|
2026-03-18 10:53:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// localStorage 不可用(隐私模式等)
|
|
|
|
|
|
}
|
2026-04-09 18:14:28 +08:00
|
|
|
|
return normalizeTablePageSize(fallback)
|
2026-03-18 10:53:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function setPersistedPageSize(size: number): void {
|
|
|
|
|
|
try {
|
2026-04-09 18:14:28 +08:00
|
|
|
|
localStorage.setItem(STORAGE_KEY, String(normalizeTablePageSize(size)))
|
|
|
|
|
|
localStorage.setItem(SOURCE_KEY, 'user')
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// 静默失败
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function syncPersistedPageSizeWithSystemDefault(defaultSize = getConfiguredTableDefaultPageSize()): void {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const normalizedDefault = normalizeTablePageSize(defaultSize)
|
|
|
|
|
|
const stored = localStorage.getItem(STORAGE_KEY)
|
|
|
|
|
|
const source = localStorage.getItem(SOURCE_KEY)
|
|
|
|
|
|
const normalizedStored = stored ? normalizeTablePageSize(stored) : null
|
|
|
|
|
|
|
|
|
|
|
|
if ((source === 'user' || (source === null && stored !== null)) && stored) {
|
|
|
|
|
|
localStorage.setItem(STORAGE_KEY, String(normalizedStored ?? normalizedDefault))
|
|
|
|
|
|
localStorage.setItem(SOURCE_KEY, 'user')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
localStorage.setItem(STORAGE_KEY, String(normalizedDefault))
|
|
|
|
|
|
localStorage.setItem(SOURCE_KEY, 'system')
|
2026-03-18 10:53:27 +08:00
|
|
|
|
} catch {
|
|
|
|
|
|
// 静默失败
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|