generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 103
/
main.ts
259 lines (207 loc) · 8.38 KB
/
main.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
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
import { Plugin, } from 'obsidian'
import HeatmapCalendarSettingsTab from "settings"
interface CalendarData {
year: number
colors: {
[index: string | number]: string[]
} | string
entries: Entry[]
showCurrentDayBorder: boolean
defaultEntryIntensity: number
intensityScaleStart: number
intensityScaleEnd: number
}
interface CalendarSettings extends CalendarData {
colors: {
[index: string | number]: string[]
},
weekStartDay: number,
}
interface Entry {
date: string
intensity?: number
color: string
content: string
}
const DEFAULT_SETTINGS: CalendarSettings = {
year: new Date().getFullYear(),
colors: {
default: ["#c6e48b", "#7bc96f", "#49af5d", "#2e8840", "#196127",],
},
entries: [{ date: "1900-01-01", color: "#7bc96f", intensity: 5, content: "",},],
showCurrentDayBorder: true,
defaultEntryIntensity: 4,
intensityScaleStart: 1,
intensityScaleEnd: 5,
weekStartDay: 1,
}
export default class HeatmapCalendar extends Plugin {
settings: CalendarSettings
/**
* Returns a number representing how many days into the year the supplied date is.
* Example: first of january is 1, third of february is 34 (31+3)
* @param date
*/
getHowManyDaysIntoYear(date: Date): number {
return (
(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()) -
Date.UTC(date.getUTCFullYear(), 0, 0)) / 24 / 60 / 60 / 1000
)
}
getHowManyDaysIntoYearLocal(date: Date): number {
return (
(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) -
Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000
)
}
/**
* Removes HTMLElements passed as entry.content and outside of the displayed year from rendering above the calendar
*/
removeHtmlElementsNotInYear(entries: Entry[], year: number) {
const calEntriesNotInDisplayedYear = entries.filter(e => new Date(e.date).getFullYear() !== year) ?? this.settings.entries
//@ts-ignore
calEntriesNotInDisplayedYear.forEach(e => e.content instanceof HTMLElement && e.content.remove())
}
clamp(input: number, min: number, max: number): number {
return input < min ? min : input > max ? max : input
}
map(current: number, inMin: number, inMax: number, outMin: number, outMax: number): number {
const mapped: number = ((current - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin
return this.clamp(mapped, outMin, outMax)
}
getWeekdayShort(dayNumber: number): string {
return new Date(1970, 0, dayNumber + this.settings.weekStartDay + 4).toLocaleDateString('en-US', { weekday: 'short' });
}
async onload() {
await this.loadSettings()
this.addSettingTab(new HeatmapCalendarSettingsTab(this.app, this))
//@ts-ignore
window.renderHeatmapCalendar = (el: HTMLElement, calendarData: CalendarData): void => {
const year = calendarData.year ?? this.settings.year
const colors = typeof calendarData.colors === "string"
? this.settings.colors[calendarData.colors]
? { [calendarData.colors]: this.settings.colors[calendarData.colors], }
: this.settings.colors
: calendarData.colors ?? this.settings.colors
this.removeHtmlElementsNotInYear(calendarData.entries, year)
const calEntries = calendarData.entries.filter(e => new Date(e.date + "T00:00").getFullYear() === year) ?? this.settings.entries
const showCurrentDayBorder = calendarData.showCurrentDayBorder ?? this.settings.showCurrentDayBorder
const defaultEntryIntensity = calendarData.defaultEntryIntensity ?? this.settings.defaultEntryIntensity
const intensities = calEntries.filter(e => e.intensity).map(e => e.intensity as number)
const minimumIntensity = intensities.length ? Math.min(...intensities) : this.settings.intensityScaleStart
const maximumIntensity = intensities.length ? Math.max(...intensities) : this.settings.intensityScaleEnd
const intensityScaleStart = calendarData.intensityScaleStart ?? minimumIntensity
const intensityScaleEnd = calendarData.intensityScaleEnd ?? maximumIntensity
const mappedEntries: Entry[] = []
calEntries.forEach(e => {
const newEntry = {
intensity: defaultEntryIntensity,
...e,
}
const colorIntensities = typeof colors === "string"
? this.settings.colors[colors]
: colors[e.color] ?? colors[Object.keys(colors)[0]]
const numOfColorIntensities = Object.keys(colorIntensities).length
if(minimumIntensity === maximumIntensity && intensityScaleStart === intensityScaleEnd) newEntry.intensity = numOfColorIntensities
else newEntry.intensity = Math.round(this.map(newEntry.intensity, intensityScaleStart, intensityScaleEnd, 1, numOfColorIntensities))
mappedEntries[this.getHowManyDaysIntoYear(new Date(e.date))] = newEntry
})
const firstDayOfYear = new Date(Date.UTC(year, 0, 1))
let numberOfEmptyDaysBeforeYearBegins = (firstDayOfYear.getUTCDay() + 7 - this.settings.weekStartDay) % 7
interface Box {
backgroundColor?: string;
date?: string;
content?: string;
classNames?: string[];
}
const boxes: Array<Box> = []
while (numberOfEmptyDaysBeforeYearBegins) {
boxes.push({ backgroundColor: "transparent", })
numberOfEmptyDaysBeforeYearBegins--
}
const lastDayOfYear = new Date(Date.UTC(year, 11, 31))
const numberOfDaysInYear = this.getHowManyDaysIntoYear(lastDayOfYear) //eg 365 or 366
const todaysDayNumberLocal = this.getHowManyDaysIntoYearLocal(new Date())
for (let day = 1; day <= numberOfDaysInYear; day++) {
const box: Box = {
classNames: [],
}
// determine the date and month for the current box
const currentDate = new Date(year, 0, day);
const month = currentDate.toLocaleString('en-us', { month: 'short' });
// Add the month class name to the box
box.classNames?.push(`month-${month.toLowerCase()}`); // e.g., "month-jan", "month-feb", etc.
if (day === todaysDayNumberLocal && showCurrentDayBorder) box.classNames?.push("today")
if (mappedEntries[day]) {
box.classNames?.push("hasData")
const entry = mappedEntries[day]
box.date = entry.date
if (entry.content) box.content = entry.content
const currentDayColors = entry.color ? colors[entry.color] : colors[Object.keys(colors)[0]]
box.backgroundColor = currentDayColors[entry.intensity as number - 1]
} else box.classNames?.push("isEmpty")
boxes.push(box)
}
const heatmapCalendarGraphDiv = createDiv({
cls: "heatmap-calendar-graph",
parent: el,
})
createDiv({
cls: "heatmap-calendar-year",
text: String(year).slice(2),
parent: heatmapCalendarGraphDiv,
})
const heatmapCalendarMonthsUl = createEl("ul", {
cls: "heatmap-calendar-months",
parent: heatmapCalendarGraphDiv,
})
createEl("li", { text: "Jan", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Feb", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Mar", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Apr", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "May", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Jun", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Jul", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Aug", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Sep", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Oct", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Nov", parent: heatmapCalendarMonthsUl, })
createEl("li", { text: "Dec", parent: heatmapCalendarMonthsUl, })
const heatmapCalendarDaysUl = createEl("ul", {
cls: "heatmap-calendar-days",
parent: heatmapCalendarGraphDiv,
})
for (let i = 0; i < 7; i++) {
createEl("li", { text: this.getWeekdayShort(i), parent: heatmapCalendarDaysUl, })
}
const heatmapCalendarBoxesUl = createEl("ul", {
cls: "heatmap-calendar-boxes",
parent: heatmapCalendarGraphDiv,
})
boxes.forEach(e => {
const entry = createEl("li", {
attr: {
...e.backgroundColor && { style: `background-color: ${e.backgroundColor};`, },
...e.date && { "data-date": e.date, },
},
cls: e.classNames,
parent: heatmapCalendarBoxesUl,
})
createSpan({
cls: "heatmap-calendar-content",
parent: entry,
text: e.content,
})
})
}
}
onunload() {
}
async loadSettings() {
console.log( "heyoh", await this.loadData() );
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData())
}
async saveSettings() {
await this.saveData(this.settings)
}
}