-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.vue
276 lines (267 loc) · 9.37 KB
/
main.vue
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<script lang="ts">
import Skyline from './components/skyline.vue';
import HistoryAnalyzer from '$/history/analyzer';
import { toTimeString, accumulate } from '$/utils';
import { splitOtherData } from '@/utils';
import { Dashboards } from '@/highcharts';
import type {
SeriesVariablepieOptions,
SeriesColumnOptions,
SeriesSplineOptions,
PointOptionsObject,
SeriesPieOptions
} from 'highcharts';
const excludedTags = addon.getPref('excludedTags'),
libraryHistory = addon.history.getInLibrary(),
analyzer = new HistoryAnalyzer(libraryHistory),
Zotero = addon.getGlobal('Zotero');
function drawSchedule() {
const weekData = new Array(7).fill(0),
hourData = new Array(24).fill(0);
analyzer.forEachPeriod(
(date, time) => {
weekData[date.getDay()] += time;
hourData[date.getHours()] += time;
}
);
return [
{
name: addon.locale.scheduleWeek ?? 'week',
type: 'column',
data: weekData
} as SeriesColumnOptions,
{
name: addon.locale.scheduleHour ?? 'hour',
type: 'spline',
data: hourData,
tooltip: { headerFormat: '{point.x}:00~{add point.x 1}:00<br/>' },
xAxis: 1
} as SeriesSplineOptions
];
}
async function drawVariablePie() {
function getTime(item: Zotero.Item) {
return new HistoryAnalyzer(item).totalS;
}
function process(
arr: Array<PointOptionsObject>,
item: Zotero.Item
) { // 将item的数组转换为饼图数据
const tags = item.getTags().filter(t => t.type).map(t => t.tag).filter(t => {
const id = Zotero.Tags.getID(t);
return id && !excludedTags.includes(id);
}); // 标签字符串的数组
const time = getTime(item);
for (const tag of tags) {
const fan = arr.find(i => i.name === tag); // 0代表名字
if (fan) { // 该标签已记录
++fan.y!; // 1代表弧度
fan.z! += time; // 2代表厚度
}
else
arr.push({ name: tag, y: 1, z: time });
}
return arr;
}
const data = new Array<PointOptionsObject>(),
series = new Array<SeriesVariablepieOptions | SeriesPieOptions>(),
userLib = Zotero.Libraries.userLibraryID,
collections: Array<Zotero.Collection | Zotero.Search> =
Zotero.Collections.getByLibrary(userLib, true),
unfiled = new Zotero.Search({
libraryID: userLib,
name: addon.locale.unfiled
});
// 添加未分类条目
unfiled.addCondition('unfiled', 'true');
unfiled.addCondition('itemType', 'isNot', 'note');
collections.push(unfiled);
for (const collection of collections) {
const items = collection instanceof Zotero.Collection
? collection.getChildItems()
: Zotero.Items.get(await collection.search()),
drilldownData = items.reduce(process, []),
[major, minor] = splitOtherData(drilldownData);
if (major.length < 2 || minor.length < 2)
major.push(...minor);
else
major.push({
name: addon.locale.others,
sliced: true,
y: accumulate(minor, 'y'),
z: accumulate(minor, 'z')
});
data.push({
name: collection.name,
drilldown: collection.name,
y: items.length, // 条目数作为扇形角度
z: items.reduce((sum, item) =>
sum + getTime(item), 0) // 从0开始加
});
series.push({
name: collection.name,
id: collection.name,
type: data.at(-1)!.z! > 0 ? 'variablepie' : 'pie',
data: major
});
}
return { data, series };
}
export default {
components: { Skyline },
data() {
return {
overallProgress: analyzer.progress,
locale: addon.locale,
theme: 'light' // TODO: dark
};
},
async mounted() {
const { data, series } = await drawVariablePie();
const board = await Dashboards.board('container', {
gui: {
layouts: [{
rows: [{
cells: [
{ id: 'cell-schedule' },
{
id: 'cell-r1-c2',
layout: {
rows: [
{ cells: [{ id: 'cell-skyline' }] },
{
cells: [
{ id: 'cell-pie' },
// { id: 'cell-kpi' },
{ id: 'cell-progress' }
]
}
]
}
}
]
}]
}]
},
components: [{
cell: 'cell-schedule',
type: 'Highcharts',
chartOptions: {
title: { text: addon.locale.chartTitle.schedule },
exporting: { enabled: false },
xAxis: [
{
opposite: true,
categories: addon.locale.weekdays,
crosshair: true,
},
{ opposite: false },
],
yAxis: {
title: { text: addon.locale.time },
labels: { formatter: ctx => toTimeString(ctx.value) },
},
tooltip: {
pointFormatter () {
return toTimeString(this.y ?? 0);
},
},
series: drawSchedule()
} as Highcharts.Options
}, {
cell: 'cell-skyline',
type: 'HTML',
elements: [{ tagName: 'div' }]
}, {
cell: 'cell-kpi',
type: 'KPI',
title: addon.locale.chartTitle.readToday,
value: analyzer.getByDay(new Date().getDay()),
valueFormatter: toTimeString
}, {
cell: 'cell-pie',
type: 'Highcharts',
chartOptions: {
title: { text: addon.locale.chartTitle.pie },
subtitle: { text: addon.locale.chartTitle.pieSub },
exporting: { enabled: false },
tooltip: {
useHTML: true,
pointFormatter () {
const dot = `<span style="color: var(--highcharts-color-${
this.colorIndex
})">\u25CF</span>`;
return `
${dot} ${addon.locale.itemsCount}: <b>${this.y}</b><br/>
${dot} ${addon.locale.totalTime}: <b>${toTimeString((this as any).z)}</b>
`;
}
},
drilldown: { series },
series: [{
name: Zotero.Libraries.userLibrary.name,
type: 'variablepie',
minPointSize: 10,
innerSize: '20%',
zMin: 0,
colorByPoint: true,
allowPointSelect: false,
data
} as SeriesVariablepieOptions]
} as Highcharts.Options
}, {
cell: 'cell-progress',
type: 'HTML',
elements: [{
tagName: 'div',
children: [{
tagName: 'h2',
textContent: addon.locale.overallProgress,
style: { textAlign: 'center' }
}]
}]
}]
}, true);
// 等待DOM挂载后把组件移动到对应位置
for (const component of board.mountedComponents)
switch (component.options.cell) {
case 'cell-skyline':
component.component.contentElement.appendChild(document.getElementById('skyline')!);
break;
case 'cell-progress':
component.component.contentElement.appendChild(document.getElementById('progress')!);
break;
default:
break;
}
addon.log('overview board loaded', board);
}
};
</script>
<template>
<div id="container" />
<Skyline id="skyline" />
<TProgress id="progress" theme="circle" size="large" :percentage="overallProgress" />
</template>
<style scoped>
#progress {
position: relative;
left: calc(50% - 80px);
top: calc(50% - 160px);
}
</style>
<style>
#cell-r1-c2 {
flex: revert;
width: 700px;
}
#cell-skyline {
height: 135px;
}
#cell-pie {
flex: 3;
}
#cell-progress {
flex: 2;
}
</style>