-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdata.ts
176 lines (153 loc) · 4.49 KB
/
data.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
interface RecordBase {
/** 阅读历史总时长,单位秒 */
totalS?: number;
/** 第一次阅读的时间戳 */
firstTime?: number;
/** 最后一次阅读的时间戳 */
lastTime?: number;
/**
* 获取当前用户的阅读时长
* @param userID 使用{@link Zotero.Users.getCurrentUserID()}
*/
getUserTotalSeconds(userID: number): number | undefined;
mergeJSON(json: object): void;
}
export class PageRecord implements RecordBase {
period?: { [timestamp: number]: number };
userSeconds?: { [user: number]: number };
totalSeconds?: number;
selectText?: number;
constructor(json?: object) {
if (json) this.mergeJSON(json);
}
mergeJSON(json: _ZoteroTypes.anyObj): void {
if (json.p) {
this.period ??= {};
for (const t in json.p)
if (parseInt(t) > 0 && Object.prototype.hasOwnProperty.call(json.p, t))
this.period[parseInt(t)] = json.p[t]; // 时间戳是唯一的,这里就不再求和了
}
typeof json.t == "number" &&
(this.totalSeconds = (this.totalSeconds ?? 0) + json.t);
typeof json.s == "number" &&
(this.selectText = (this.selectText ?? 0) + json.s);
typeof json.u == "object" &&
(this.userSeconds = { ...this.userSeconds ?? {}, ...json.u });
}
toJSON() {
return {
t: this.totalSeconds,
s: this.selectText,
p: this.period,
u: this.userSeconds,
};
}
public get totalS() {
return (
this.totalSeconds ??
(this.period && Object.values(this.period).reduce((sum, p) => sum + p))
);
}
public get firstTime(): number | undefined {
if (!this.period) return;
const arr = Object.keys(this.period);
if (arr.length) return parseInt(arr.sort()[0]);
}
public get lastTime(): number | undefined {
if (!this.period) return;
const arr = Object.keys(this.period);
if (arr.length)
return arr.reduce(
(result, timestamp) => Math.max(result, parseInt(timestamp)),
0
);
}
public get userIDs(): number[] {
return this.userSeconds ? Object.keys(this.userSeconds).map(Number) : [];
}
getUserTotalSeconds(userID: number): number | undefined {
if (this.userSeconds) return this.userSeconds[userID];
}
}
export class AttachmentRecord implements RecordBase {
[key: string]: unknown;
pages: { [page: number]: PageRecord };
numPages?: number;
constructor(numberOfPages?: number);
constructor(json?: object);
constructor(arg?: number | object) {
this.pages = {};
switch (typeof arg) {
case "number":
this.numPages = arg;
break;
case "object":
this.mergeJSON(arg);
break;
default:
break;
}
}
mergeJSON(json: _ZoteroTypes.anyObj): void {
for (const key in json)
if (Object.prototype.hasOwnProperty.call(json, key)) {
if (key === "pages") {
for (const page in json.pages)
if (parseInt(page) >= 0)
(this.pages[parseInt(page)] ??= new PageRecord()).mergeJSON(
json.pages[page]
);
} else this[key] = json[key];
}
}
get pageArr() {
return Object.values(this.pages);
}
/** 获取有阅读记录的页数 */
public get readPages(): number {
const completeThreshold = addon.getPref("completeThreshold");
return this.pageArr.filter(page => (page.totalS ?? 0) > completeThreshold).length;
}
get firstPage() {
return Object.keys(this.pages)
.map((p) => parseInt(p))
.sort((a, b) => a - b)[0];
}
get lastPage() {
return Object.keys(this.pages).reduce(
(last, page) => Math.max(last, parseInt(page)),
0
);
}
public get firstTime() {
const arr = this.pageArr
.map((p) => p.firstTime)
.filter((p) => p) as number[];
if (arr.length)
return arr.reduce(
(result, timestamp) => Math.min(result, timestamp),
Infinity
);
}
public get lastTime() {
const arr = this.pageArr
.map((p) => p.lastTime)
.filter((p) => p) as number[];
if (arr.length)
return arr.reduce((result, timestamp) => Math.max(result, timestamp), 0);
}
public get totalS() {
return this.pageArr.reduce((sum, p) => sum + (p.totalS ?? 0), 0);
}
public get userIDs() {
return Array.from(new Set(
this.pageArr.reduce((arr, page) => arr.concat(page.userIDs), [] as number[])
));
}
getUserTotalSeconds(userID: number): number {
return this.pageArr.reduce(
(sum, page) => (page.getUserTotalSeconds(userID) ?? 0) + sum,
0
);
}
}