210 lines
5.2 KiB
TypeScript
210 lines
5.2 KiB
TypeScript
|
|
/* ==================================================================
|
|||
|
|
* 已读 / 未读
|
|||
|
|
*
|
|||
|
|
* 存在 localStorage,不写 data.json:
|
|||
|
|
* - 阅读状态是高频写入的数据,塞进设置里会让每次点文章都重写整个配置文件;
|
|||
|
|
* - 用户手改设置时也不会被几千个 id 淹没。
|
|||
|
|
*
|
|||
|
|
* 两个索引:
|
|||
|
|
* read —— 全局已读表(id → 时间戳),决定某一行为「已读」样式
|
|||
|
|
* scope —— 按范围(全部 / 分类 / 网站)存的 id 数组,用来算「已读 (x)」
|
|||
|
|
*
|
|||
|
|
* 标记一篇已读时,同时写进 all / cat:<分类> / site:<网站> 三个范围,
|
|||
|
|
* 这样在哪一层看统计都对得上。范围数组只追加不重排,超出上限砍掉最早的。
|
|||
|
|
* ================================================================== */
|
|||
|
|
|
|||
|
|
const STORAGE_KEY = "ruu-read-v1";
|
|||
|
|
const MAX_SCOPE_ITEMS = 8000;
|
|||
|
|
const MAX_GLOBAL_ITEMS = 20000;
|
|||
|
|
|
|||
|
|
interface ReadStore {
|
|||
|
|
v: number;
|
|||
|
|
read: Record<string, number>;
|
|||
|
|
scope: Record<string, number[]>;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export interface ReadableItem {
|
|||
|
|
id: number;
|
|||
|
|
categoryId?: number;
|
|||
|
|
urlId?: number;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
let store: ReadStore | null = null;
|
|||
|
|
let saveTimer: number | null = null;
|
|||
|
|
|
|||
|
|
function emptyStore(): ReadStore {
|
|||
|
|
return { v: 1, read: {}, scope: {} };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function load(): ReadStore {
|
|||
|
|
if (store) return store;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|||
|
|
const parsed = raw ? JSON.parse(raw) : null;
|
|||
|
|
|
|||
|
|
if (parsed && typeof parsed === "object" && parsed.read && parsed.scope) {
|
|||
|
|
store = {
|
|||
|
|
v: 1,
|
|||
|
|
read: parsed.read || {},
|
|||
|
|
scope: parsed.scope || {},
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return store;
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
/* 存的内容坏了就当空的,别让阅读列表因为一个坏 JSON 打不开 */
|
|||
|
|
console.warn("RssUnion: 已读记录解析失败,已重置", error);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
store = emptyStore();
|
|||
|
|
|
|||
|
|
return store;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 攒一下再写盘:连点几篇只落一次 */
|
|||
|
|
function persistSoon(): void {
|
|||
|
|
if (saveTimer !== null) return;
|
|||
|
|
|
|||
|
|
saveTimer = window.setTimeout(() => {
|
|||
|
|
saveTimer = null;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(load()));
|
|||
|
|
} catch (error) {
|
|||
|
|
console.warn("RssUnion: 已读记录写入失败", error);
|
|||
|
|
}
|
|||
|
|
}, 400);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 立刻落盘(面板关闭时用,别把最后的记录丢掉) */
|
|||
|
|
export function flushReadState(): void {
|
|||
|
|
if (saveTimer !== null) {
|
|||
|
|
window.clearTimeout(saveTimer);
|
|||
|
|
saveTimer = null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(load()));
|
|||
|
|
} catch (error) {
|
|||
|
|
// 存不进去也不影响当前会话
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 当前选中范围对应的 scope 键 */
|
|||
|
|
export function scopeKeyOf(type: string, id: number): string {
|
|||
|
|
if (type === "cat") return `cat:${id}`;
|
|||
|
|
if (type === "site") return `site:${id}`;
|
|||
|
|
|
|||
|
|
return "all";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 一篇已读文章要记进哪几个范围 */
|
|||
|
|
function scopeKeysOfItem(item: ReadableItem): string[] {
|
|||
|
|
const keys = ["all"];
|
|||
|
|
|
|||
|
|
if (item.categoryId) keys.push(`cat:${item.categoryId}`);
|
|||
|
|
if (item.urlId) keys.push(`site:${item.urlId}`);
|
|||
|
|
|
|||
|
|
return keys;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function isRead(id: number): boolean {
|
|||
|
|
const data = load();
|
|||
|
|
|
|||
|
|
return Object.prototype.hasOwnProperty.call(data.read, String(id));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 把一篇写进数据(不落盘);返回 true 表示这是新标记(之前未读) */
|
|||
|
|
function applyMark(data: ReadStore, item: ReadableItem): boolean {
|
|||
|
|
if (!item || !item.id) return false;
|
|||
|
|
|
|||
|
|
const key = String(item.id);
|
|||
|
|
const fresh = !data.read[key];
|
|||
|
|
|
|||
|
|
if (fresh) data.read[key] = Date.now();
|
|||
|
|
|
|||
|
|
/* 已读过的也补一次范围索引,防止早期版本只写了全局没写范围 */
|
|||
|
|
for (const scope of scopeKeysOfItem(item)) {
|
|||
|
|
if (!data.scope[scope]) data.scope[scope] = [];
|
|||
|
|
|
|||
|
|
const list = data.scope[scope];
|
|||
|
|
|
|||
|
|
if (list.indexOf(item.id) >= 0) continue;
|
|||
|
|
|
|||
|
|
list.push(item.id);
|
|||
|
|
|
|||
|
|
if (list.length > MAX_SCOPE_ITEMS) {
|
|||
|
|
data.scope[scope] = list.slice(list.length - MAX_SCOPE_ITEMS);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return fresh;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 标记已读;返回 true 表示这是新标记(之前未读) */
|
|||
|
|
export function markRead(item: ReadableItem): boolean {
|
|||
|
|
const data = load();
|
|||
|
|
const fresh = applyMark(data, item);
|
|||
|
|
|
|||
|
|
if (fresh) {
|
|||
|
|
trimGlobal(data);
|
|||
|
|
persistSoon();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return fresh;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 批量标记已读(多选「设为已读」用)
|
|||
|
|
*
|
|||
|
|
* @returns 真正从「未读」变成「已读」的篇数(原本就读过的不计入)
|
|||
|
|
*/
|
|||
|
|
export function markReadMany(items: ReadableItem[]): number {
|
|||
|
|
if (!items || items.length === 0) return 0;
|
|||
|
|
|
|||
|
|
const data = load();
|
|||
|
|
let changed = 0;
|
|||
|
|
|
|||
|
|
for (const item of items) {
|
|||
|
|
if (applyMark(data, item)) changed++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (changed > 0) {
|
|||
|
|
trimGlobal(data);
|
|||
|
|
persistSoon();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return changed;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function trimGlobal(data: ReadStore): void {
|
|||
|
|
const keys = Object.keys(data.read);
|
|||
|
|
|
|||
|
|
if (keys.length <= MAX_GLOBAL_ITEMS) return;
|
|||
|
|
|
|||
|
|
keys.sort((a, b) => (data.read[a] || 0) - (data.read[b] || 0));
|
|||
|
|
|
|||
|
|
for (const key of keys.slice(0, keys.length - MAX_GLOBAL_ITEMS)) {
|
|||
|
|
delete data.read[key];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 某个范围里已读了多少篇 */
|
|||
|
|
export function countRead(scope: string): number {
|
|||
|
|
const list = load().scope[scope];
|
|||
|
|
|
|||
|
|
return list ? list.length : 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 清空已读记录(设置页里给个「重置」用) */
|
|||
|
|
export function clearReadState(): void {
|
|||
|
|
store = emptyStore();
|
|||
|
|
flushReadState();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 把 localStorage 里的记录读出来(调试 / 测试用) */
|
|||
|
|
export function debugSnapshot(): ReadStore {
|
|||
|
|
return load();
|
|||
|
|
}
|