-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathactivity_tracker.js
287 lines (287 loc) · 9.49 KB
/
activity_tracker.js
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
277
278
279
280
281
282
283
284
285
286
287
(() => {
let styles, dialog, action, running, importDialog, exportDialog, interval, timeout, notification, state
const id = "activity_tracker"
const name = "Activity Tracker"
const icon = "trending_up"
const description = "Track how long you spend using Blockbench and working on each project."
const activity = JSON.parse(localStorage.getItem(id) ?? '{ "clock": 0 }')
let pauseOnLostFocus = localStorage.getItem(`${id}_pause_lost_focus`) ?? "0"
Plugin.register(id, {
title: name,
icon,
author: "Ewan Howell",
description,
about: "This plugin allows you to keep track of how much time you spend using Blockbench and how long you spend working on each project.\n\nTo view your activity stats, go to `Tools > Activity Tracker`. From here you can see all your stats for both Blockbench and the current project.",
tags: ["Tracking", "Stats", "Utility"],
version: "1.0.1",
min_version: "4.6.5",
variant: "both",
onload() {
activity.session = activity.clock
styles = Blockbench.addCSS(`
#tracker-container {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.tracker-row {
display: flex;
gap: 20px;
width: 100%;
margin: 10px 0 20px;
justify-content: center;
}
.tracker-col {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.tracker-col > p {
color: var(--color-light);
font-size: 1.5rem;
}
.tracker-line {
width: 100%;
border-top: 2px solid var(--color-border);
margin: 10px 0 20px;
}
#tracker-container .button-row {
display: flex;
justify-content: flex-end;
width: 100%;
}
#tracker-import {
margin: 20px 0 10px;
}
#tracker-export {
user-select: all;
}
.tracker-row-center {
align-items: center;
gap: 10px;
}
#tracker-container i {
cursor: pointer;
}
#tracker-container i:hover {
color: var(--color-light);
}
`)
property = new Property(ModelProject, "number", id, {
default: 0,
exposed: false
})
ModelProject.all.forEach(e => {
e.activity_tracker = 0
e.activity_tracker_session = 0
})
importDialog = new Dialog({
id: `${id}_import`,
title: `${name} Import`,
buttons: [],
component: {
data: {
seconds: 0
},
methods: {
input(e) {
this.seconds = Math.min(parseInt(e.target.value) || 0, 9999999999)
},
durationString,
confirm() {
activity.clock = this.seconds
activity.session = this.seconds
importDialog.close()
}
},
template: `
<div id="tracker-container">
<h2>Enter number of seconds:</h2>
<input id="tracker-import" class="dark_bordered half" type="number" placeholder="123456789" @input="input"></input>
<p>Preview: {{ durationString(seconds) }}</p>
<div class="button-row">
<button @click="confirm">Confirm</button>
</div>
</div>
`
}
})
exportDialog = new Dialog({
id: `${id}_export`,
title: `${name} Export`,
buttons: [],
component: {
data: {
clock: activity.clock
},
methods: {
copy() {
navigator.clipboard.writeText(this.clock)
const text = $("#tracker-export").text("Copied...")
setTimeout(() => text.text(this.clock), 1000)
}
},
template: `
<div id="tracker-container">
<h2>Your total activity in seconds:</h2>
<div class="tracker-row tracker-row-center">
<p id="tracker-export" class="dark_bordered half">{{ clock }}</p>
<i class="material-icons icon" title="Copy to clipboard" @click="copy">content_copy</i>
</div>
</div>
`
}
})
dialog = new Dialog({
id,
title: name,
buttons: [],
component: {
data: {
activity,
importDialog,
exportDialog,
options: {
"0": "Immediately",
"10": "After 10 seconds",
"30": "After 30 seconds",
"60": "After 1 minute",
"300": "After 5 minutes",
"600": "After 10 minutes",
"1200": "After 20 minutes",
"1800": "After 30 minutes",
"2700": "After 45 minutes",
"3600": "After 1 hour",
"never": "Never"
},
option: pauseOnLostFocus
},
methods: {
durationString,
set(e) {
pauseOnLostFocus = e
localStorage.setItem(`${id}_pause_lost_focus`, e)
}
},
template: `
<div id="tracker-container">
<h1>Global Activity</h1>
<div class="tracker-row">
<div class="tracker-col">
<h2>Total</h2>
<p>{{ durationString(activity.clock) }}</p>
</div>
<div class="tracker-col">
<h2>Current session</h2>
<p>{{ durationString(activity.clock - activity.session) }}</p>
</div>
</div>
<div v-if="Project" id="project-tracker">
<div class="tracker-line"></div>
<h1>Current Project Activity</h1>
<p>Models must be saved as a bbmodel to preserve per project activity tracking.</p>
<div class="tracker-row">
<div class="tracker-col">
<h2>Total</h2>
<p>{{ durationString(Project.${id}) }}</p>
</div>
<div class="tracker-col">
<h2>Current session</h2>
<p>{{ durationString(Project.${id} - Project.${id}_session) }}</p>
</div>
</div>
</div>
<div class="tracker-line"></div>
<div id="pause-tracking-select-container" class="tracker-row tracker-row-center">
<p>Pause tracking when tabbed out:</p>
<select-input v-model="option" :options="options" @input="set" />
</div>
<div class="tracker-line"></div>
<div class="tracker-row">
<button @click="importDialog.show()">Import time</button>
<button @click="exportDialog.show()">Export time</button>
</div>
</div>
`
}
})
action = new Action({
name,
id,
description,
icon,
click: () => dialog.show()
})
MenuBar.addAction(action, "tools")
window.addEventListener("focus", focus)
window.addEventListener("blur", blur)
if (Blockbench.isWeb) document.addEventListener("visibilitychange", visibilityChange)
Blockbench.on("select_project", selectProject)
selectProject()
focus()
},
onunload() {
window.removeEventListener("focus", focus)
window.removeEventListener("blur", blur)
if (Blockbench.isWeb) document.removeEventListener("visibilitychange", visibilityChange)
Blockbench.removeListener("select_project", selectProject)
clearTimeout(timeout)
clearInterval(interval)
styles.delete()
property.delete()
action.delete()
dialog.close()
importDialog.close()
exportDialog.close()
}
})
function durationString(num) {
num = num * 1000
const years = Math.floor(num / 3.1536e10)
let days = Math.floor(num / 8.64e7) % 365
const weeks = Math.floor(days / 7)
days %= 7
const hours = Math.floor(num / 3.6e6) % 24
const minutes = Math.floor(num / 6e4) % 60
const seconds = Math.round(num / 1000 % 60)
return `${years} year${years === 1 ? "" : "s"}, ${weeks} week${weeks === 1 ? "" : "s"}, ${days} day${days === 1 ? "" : "s"}, ${hours.toString().padStart(2, 0)}:${minutes.toString().padStart(2, 0)}:${seconds.toString().padStart(2, 0)}`.replace(/(?<!\d)0\s[a-z]+,\s/g, "").replace(/(, 00:00:00)/, "")
}
function selectProject() {
setTimeout(() => {
Project.activity_tracker_session ||= Project.activity_tracker
}, 0)
}
function visibilityChange() {
if (document.visibilityState === "hidden") blur()
else focus()
}
function focus() {
if (state === "focus") return
state = "focus"
clearTimeout(timeout)
notification?.delete()
if (running) return
running = true
interval = setInterval(() => {
activity.clock += 1
Project.activity_tracker += 1
localStorage.setItem(id, JSON.stringify(activity))
}, 1000)
}
function blur() {
if (state === "blur") return
state = "blur"
if (pauseOnLostFocus === "never") return
timeout = setTimeout(() => {
clearInterval(interval)
running = false
notification = Blockbench.showToastNotification({
text: "Activity tracking paused. Tab back in to resume.",
icon
})
}, parseInt(pauseOnLostFocus) * 1000)
}
})()