-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
481 lines (428 loc) · 14.1 KB
/
main.js
File metadata and controls
481 lines (428 loc) · 14.1 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
const {
App,
Plugin,
PluginSettingTab,
Setting,
ItemView,
moment,
setIcon,
normalizePath,
TFolder
} = require("obsidian");
/** ========== GLOBAL CONSTANTS ========== */
const HEATMAP_VIEW_TYPE = "heatmap-view";
const DEFAULT_HEATMAP_LEAF_ICON = "activity";
const DEFAULT_HEATMAP_LEAF_TITLE = "Vault Heatmap";
const REFRESH_INTERVAL_MS = 1000 * 60 * 60;
/** ========== SETTINGS ========== */
const DEFAULT_SETTINGS = {
trackedFolderPath: "",
colourPresetsJSON: JSON.stringify([
{
"name": "Warm 1",
"colours": ["#ffc000", "#fda456", "#f7642e", "#ff4948"]
},
{
"name": "Warm 2",
"colours": ["#FFC000", "#FDA456", "#F7642E", "#FF4948", "#FE0000", "#FF0A6F", "#F95CB3", "#FF9ADE"]
},
{
"name": "Miami",
"colours": ["#4CC9F0", "#4361ee", "#3a0ca3", "#560bad", "#7209b7", "#b5179e", "#f72585"]
},
{
"name": "Github",
"colours": ["#ebedf0", "#9be9a8", "#40c463", "#30a14e", "#216e39"]
},
{
"name": "Cool",
"colours": ["#84DCC6", "#A5FFD6", "#00A7E1", "#007EA7", "#003459"]
},
{
"name": "Natural",
"colours": ["#d9ed92", "#b5e48c", "#99d98c", "#76c893", "#52b69a", "#34a0a4", "#168aad", "#1a759f", "#1e6091", "#184e77"]
}
]),
selectedColourway: "Natural",
heatmapLeafTitle: "Vault Heatmap",
heatmapLeafIcon: "activity"
};
class HeatmapSettingsTab extends PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
isValidFolder(folderPath) {
if (!folderPath.trim()) return true;
const file = this.app.vault.getAbstractFileByPath(folderPath.trim());
return file && file instanceof TFolder;
}
isValidIcon(iconName) {
return iconName.trim().length > 0;
}
display() {
const { containerEl } = this;
containerEl.empty();
/** -- Target Folder Setting -- */
new Setting(containerEl)
.setName("Target folder")
.setDesc("Track only files in this folder (and subfolders). Leave blank for entire vault.")
.addText((text) => {
text
.setPlaceholder("e.g. folderA/Subfolder")
.setValue(this.plugin.settings.trackedFolderPath)
.onChange(async (value) => {
let normalizedFolder = normalizePath(value.trim());
this.plugin.settings.trackedFolderPath = normalizedFolder;
if (!this.isValidFolder(normalizedFolder)) {
text.inputEl.classList.add("invalid-input");
} else {
text.inputEl.classList.remove("invalid-input");
}
await this.plugin.saveSettings();
this.plugin.refreshHeatmapView();
});
let normalizedFolder = normalizePath(this.plugin.settings.trackedFolderPath);
if (!this.isValidFolder(normalizedFolder)) {
text.inputEl.classList.add("invalid-input");
}
});
/** -- Leaf Name Setting -- */
new Setting(containerEl)
.setName("Leaf name")
.setDesc("Custom name used for the heatmap leaf title, panel tooltip, etc.")
.addText((text) =>
text
.setPlaceholder("Heatmap")
.setValue(this.plugin.settings.heatmapLeafTitle)
.onChange(async (value) => {
this.plugin.settings.heatmapLeafTitle = value.trim();
await this.plugin.saveSettings();
this.plugin.refreshHeatmapView();
})
);
/** -- Leaf Icon Setting -- */
const iconSetting = new Setting(containerEl)
.setName("Leaf icon")
.setDesc("Loading...")
.addText((text) => {
text
.setPlaceholder(DEFAULT_HEATMAP_LEAF_ICON)
.setValue(this.plugin.settings.heatmapLeafIcon)
.onChange(async (value) => {
this.plugin.settings.heatmapLeafIcon = value.trim();
if (!this.isValidIcon(this.plugin.settings.heatmapLeafIcon)) {
text.inputEl.classList.add("invalid-input");
} else {
text.inputEl.classList.remove("invalid-input");
}
await this.plugin.saveSettings();
this.plugin.refreshHeatmapView();
});
if (!this.isValidIcon(this.plugin.settings.heatmapLeafIcon)) {
text.inputEl.classList.add("invalid-input");
}
});
iconSetting.descEl.empty();
iconSetting.descEl.createEl("span", { text: "Type the exact " });
const link = iconSetting.descEl.createEl("a", { text: "Lucide icon" });
link.href = "https://lucide.dev/icons/";
link.target = "_blank";
link.rel = "noopener";
iconSetting.descEl.createEl("span", { text: " name" });
/** -- Actions Setting -- */
const buttonSetting = new Setting(containerEl)
.setName("Actions")
.setDesc("Apply or reset the settings.");
buttonSetting.addButton((btn) => {
btn.setButtonText("Apply changes")
.setCta()
.onClick(async () => {
await this.plugin.saveSettings();
this.plugin.updateActiveLeaf({
title: this.plugin.settings.heatmapLeafTitle,
icon: this.plugin.settings.heatmapLeafIcon
});
});
});
buttonSetting.addButton((btn) => {
btn.setButtonText("Reset to defaults")
.setCta()
.onClick(async () => {
Object.assign(this.plugin.settings, DEFAULT_SETTINGS);
await this.plugin.saveSettings();
this.plugin.refreshHeatmapView();
this.display();
});
});
/** -- Colour Settings Section -- */
new Setting(containerEl).setHeading("Colour");
new Setting(containerEl)
.setName("Colourways presets json")
.setDesc("Define the colourways (each as an object with a 'name' and 'colours' array). Best edited in a code editor.")
.addTextArea((textArea) => {
textArea
.setPlaceholder('e.g. [{"name": "Warm", "colours": ["#FF0000", "#FFA500", ...]}]')
.setValue(this.plugin.settings.colourPresetsJSON)
.onChange(async (value) => {
this.plugin.settings.colourPresetsJSON = value.trim();
await this.plugin.saveSettings();
this.display();
this.plugin.refreshHeatmapView();
});
textArea.inputEl.classList.add("settings-input-text-area");
});
let presetOptions = {};
try {
const presets = JSON.parse(this.plugin.settings.colourPresetsJSON);
presets.forEach((preset) => {
if (preset.name) presetOptions[preset.name] = preset.name;
});
} catch (error) {
presetOptions["Natural"] = "Natural";
}
new Setting(containerEl)
.setName("Select colourway")
.setDesc("Choose a colourway preset.")
.addDropdown((drop) =>
drop
.addOptions(presetOptions)
.setValue(this.plugin.settings.selectedColourway)
.onChange(async (value) => {
this.plugin.settings.selectedColourway = value;
await this.plugin.saveSettings();
this.plugin.refreshHeatmapView();
})
);
}
}
/** ========== HEATMAP VIEW ========== */
// const HEATMAP_VIEW_TYPE = "heatmap-view";
class HeatmapView extends ItemView {
constructor(leaf, plugin) {
super(leaf);
this.plugin = plugin;
this.dailyCounts = null;
this.resizeObserver = null;
this.lastWidth = 0;
}
getViewType() {
return HEATMAP_VIEW_TYPE;
}
getDisplayText() {
return this.plugin.settings.heatmapLeafTitle || DEFAULT_HEATMAP_LEAF_TITLE;
}
getIcon() {
return this.plugin.settings.heatmapLeafIcon || DEFAULT_HEATMAP_LEAF_ICON;
}
/** ========== VIEW INITIALIZATION ========== */
async onOpen() {
this.containerEl.empty();
if (this.leaf && this.leaf.tabHeaderInnerEl) {
setIcon(this.leaf.tabHeaderInnerEl, this.plugin.settings.heatmapLeafIcon || DEFAULT_HEATMAP_LEAF_ICON);
}
this.containerEl.classList.add("heatmap-default-padding");
this.heatmapContainer = this.containerEl.createDiv({ cls: "heatmap-container" });
this.gridEl = this.heatmapContainer.createDiv({ cls: "heatmap-grid" });
this.dailyCounts = await this.gatherFileActivity();
this.setupResizeObserver();
}
onClose() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
}
}
setupResizeObserver() {
if (!this.heatmapContainer) return;
this.resizeObserver = new ResizeObserver((entries) => {
for (let entry of entries) {
const w = entry.contentRect.width;
if (Math.abs(w - this.lastWidth) >= 2) {
this.buildHeatmap(w);
this.lastWidth = w;
}
}
});
this.resizeObserver.observe(this.heatmapContainer);
const initialWidth = this.heatmapContainer.offsetWidth;
this.buildHeatmap(initialWidth);
this.lastWidth = initialWidth;
}
/** ========== HEATMAP GRID BUILDING ========== */
buildHeatmap(containerWidth) {
if (!this.gridEl || !this.dailyCounts) return;
this.gridEl.empty();
const COLUMN_WIDTH = 16;
let numWeeks = Math.floor(containerWidth / COLUMN_WIDTH);
if (numWeeks < 1) numWeeks = 1;
const today = moment().startOf("day");
const startDate = today.clone().subtract(numWeeks - 1, "weeks").startOf("week");
for (let w = 0; w < numWeeks; w++) {
const weekDiv = this.gridEl.createDiv({ cls: "heatmap-week" });
for (let d = 0; d < 7; d++) {
const currentDate = startDate.clone().add(w, "weeks").add(d, "days");
if (currentDate.isAfter(today)) continue;
const dateKey = currentDate.format("YYYY-MM-DD");
const count = this.dailyCounts[dateKey] || 0;
const dayCell = weekDiv.createDiv({ cls: "heatmap-day" });
dayCell.setAttr("data-date", dateKey);
dayCell.setAttr("data-count", count);
dayCell.setAttr("title", `${dateKey}: ${count} changes`);
dayCell.style.backgroundColor = this.getColor(count);
dayCell.addEventListener("click", (evt) => {
this.showDayPopup(evt, dateKey);
});
}
}
}
async gatherFileActivity() {
// cDate and mDate store creation and modification days. dailyCounts tallies file activity per date.
this.dailyFiles = {};
const dailyCounts = {};
const files = this.app.vault.getFiles();
const folderPath = this.plugin.settings.trackedFolderPath;
for (const file of files) {
if (!this.isInTargetFolder(file, folderPath)) continue;
const cDate = moment(file.stat.ctime).startOf("day").format("YYYY-MM-DD");
const mDate = moment(file.stat.mtime).startOf("day").format("YYYY-MM-DD");
dailyCounts[cDate] = (dailyCounts[cDate] || 0) + 1;
dailyCounts[mDate] = (dailyCounts[mDate] || 0) + 1;
this.dailyFiles[cDate] = this.dailyFiles[cDate] || [];
this.dailyFiles[mDate] = this.dailyFiles[mDate] || [];
this.dailyFiles[cDate].push(file.path);
this.dailyFiles[mDate].push(file.path);
}
return dailyCounts;
}
showDayPopup(evt, dateKey) {
// Creates and positions a small popup of file names for dateKey, and dismisses it if clicked outside.
document.querySelectorAll(".day-popup").forEach(el => el.remove());
const filesForDate = this.dailyFiles?.[dateKey] || [];
if (!filesForDate.length) return;
const popup = createDiv({ cls: "day-popup" });
popup.style.position = "absolute";
popup.style.left = evt.pageX + "px";
popup.style.top = evt.pageY + "px";
popup.style.zIndex = 9999;
filesForDate.forEach(path => {
const filename = path.split("/").pop();
const linkEl = popup.createEl("a", { text: filename });
linkEl.href = "#";
linkEl.style.display = "block";
linkEl.addEventListener("click", (e) => {
e.preventDefault();
this.openFile(path);
popup.remove();
});
});
document.body.appendChild(popup);
window.requestAnimationFrame(() => {
const rect = popup.getBoundingClientRect();
if (rect.bottom > window.innerHeight) {
popup.style.top = (window.innerHeight - rect.height - 10) + "px";
}
});
const removePopup = (e) => {
if (!popup.contains(e.target)) {
popup.remove();
window.removeEventListener("mousedown", removePopup);
}
};
window.addEventListener("mousedown", removePopup);
}
openFile(path) {
const file = this.app.vault.getAbstractFileByPath(path);
if (file) {
this.app.workspace.getLeaf(true).openFile(file);
}
}
isInTargetFolder(file, folderPath) {
// Checks whether folderPath is empty or if file path starts with folderPath (case-insensitive).
if (!folderPath) return true;
return file.path.toLowerCase().startsWith(folderPath.toLowerCase() + "/");
}
getColor(count) {
if (count === 0) return "#ebedf0";
let presetColours = [];
try {
let presets = JSON.parse(this.plugin.settings.colourPresetsJSON);
let preset = presets.find(p => p.name === this.plugin.settings.selectedColourway);
if (preset && Array.isArray(preset.colours)) {
presetColours = preset.colours.filter(c =>
typeof c === "string" && /^#(?:[0-9a-fA-F]{3}){1,2}$/.test(c)
);
}
} catch (err) {
console.error("Error parsing colourPresetsJSON:", err);
}
if (!presetColours.length) {
presetColours = ["#d9ed92", "#b5e48c", "#99d98c", "#76c893", "#52b69a", "#34a0a4", "#168aad", "#1a759f", "#1e6091", "#184e77"];
}
const index = Math.min(count - 1, presetColours.length - 1);
return presetColours[index];
}
}
/** ========== MAIN PLUGIN CLASS ========== */
module.exports = class HeatmapPlugin extends Plugin {
async onload() {
await this.loadSettings();
this.registerView(
HEATMAP_VIEW_TYPE,
(leaf) => new HeatmapView(leaf, this)
);
this.addSettingTab(new HeatmapSettingsTab(this.app, this));
this.addCommand({
id: "reopen-heatmap-pane",
name: "Reopen Heatmap Pane",
callback: () => {
this.activateView();
}
});
this.lastDateChecked = new Date().toDateString();
this.registerInterval(window.setInterval(() => {
const currentDate = new Date().toDateString();
if (currentDate !== this.lastDateChecked) {
this.lastDateChecked = currentDate;
this.refreshHeatmapView();
}
}, REFRESH_INTERVAL_MS));
this.activateView();
}
onunload() {
}
async activateView() {
this.app.workspace.detachLeavesOfType(HEATMAP_VIEW_TYPE);
const leaf = this.app.workspace.getLeftLeaf(false);
await leaf.setViewState({
type: HEATMAP_VIEW_TYPE,
active: true,
});
this.app.workspace.revealLeaf(leaf);
}
refreshHeatmapView() {
const leaves = this.app.workspace.getLeavesOfType(HEATMAP_VIEW_TYPE);
if (leaves.length) {
leaves[0].view.onOpen();
}
}
updateActiveLeaf(updates) {
const leaves = this.app.workspace.getLeavesOfType(HEATMAP_VIEW_TYPE);
if (leaves.length) {
const leaf = leaves[0];
const currentState = leaf.getViewState();
leaf.setViewState({
...currentState,
state: {
...currentState.state,
...updates
}
});
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
};