generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathsettings.ts
188 lines (141 loc) · 5.07 KB
/
settings.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
import HeatmapCalendar from "main"
import { App, PluginSettingTab, setIcon, Setting, } from "obsidian"
export default class HeatmapCalendarSettingsTab extends PluginSettingTab {
plugin: HeatmapCalendar
constructor(app: App, plugin: HeatmapCalendar) {
super(app, plugin)
this.plugin = plugin
}
private async addColorMap(color: { key: string, value: string }) {
const isValid = { key: true, value: true, }
if (!color.key) isValid.key = false
const validatedArray = this.validateColorInput(color.value)
if (!validatedArray) isValid.value = false
if (isValid.key && isValid.value) {
this.plugin.settings.colors[color.key] = validatedArray as string[]
await this.plugin.saveSettings()
this.display()
}
return isValid
}
private async deleteColorMap(key: keyof typeof this.plugin.settings.colors) {
delete this.plugin.settings.colors[key]
await this.plugin.saveSettings()
this.display()
}
private displayColorSettings() {
const { containerEl, } = this
containerEl.createEl("h3", { text: "Colors", })
this.displayColorHelp(containerEl)
for (const [key, colors,] of Object.entries(this.plugin.settings.colors)) {
const colorEntryContainer = containerEl.createDiv({
cls: "heatmap-calendar-settings-colors__container",
})
const colorDataContainer = colorEntryContainer.createDiv({
cls: "heatmap-calendar-settings-colors__data-container",
})
colorDataContainer.createEl("h4", { text: key, })
const colorRow = colorDataContainer.createDiv({ cls: "heatmap-calendar-settings-colors__row", })
const colorsContainer = colorRow.createDiv({ cls: "heatmap-calendar-settings-colors__color-container", })
for (const color of colors) {
colorsContainer.createEl("div", {
cls: "heatmap-calendar-settings-colors__color-box",
attr: {
style: `background-color: ${color}`,
},
})
colorsContainer.createEl("pre", {
cls: "heatmap-calendar-settings-colors__color-name",
text: color,
})
}
if (key !== "default") {
const deleteColorButton = colorEntryContainer.createEl("button", {
cls: "mod-warning heatmap-calendar-settings-colors__delete",
})
setIcon(deleteColorButton, "trash")
deleteColorButton.addEventListener("click", () => this.deleteColorMap(key))
}
}
this.displayColorInput(containerEl)
}
private displayColorInput(parent: HTMLElement) {
const inputContainer = parent.createDiv({ cls: "heatmap-calendar-settings-colors__new-color-input-container", })
const colorNameInput = inputContainer.createEl("input", {
cls: "heatmap-calendar-settings-colors__new-color-input-name",
attr: { placeholder: "Color name", type: "text", },
})
const colorValueInput = inputContainer.createEl("input", {
cls: "heatmap-calendar-settings-colors__new-color-input-value",
attr: { placeholder: "Colors array", type: "text", },
})
const addColorButton = inputContainer.createEl("button", {
cls: "mod-cta heatmap-calendar-settings-colors__new-color-button",
})
setIcon(addColorButton, "plus")
addColorButton.addEventListener("click", async () => {
const isValid = await this.addColorMap({
key: colorNameInput.value,
value: colorValueInput.value,
})
this.reportInputValidity(colorNameInput, isValid.key, "Please input a name for your color")
this.reportInputValidity(colorValueInput, isValid.value, "Color is not a valid JSON array of colors")
})
}
private displayColorHelp(parent: HTMLElement) {
parent.createEl("p", {
text: "Add lists of colors which will be globally available on your heatmaps.",
})
parent.createEl("p", {
text: "You can use those colors by referencing their name in your heatmap render settings.",
})
}
private reportInputValidity(input: HTMLInputElement, isValid: boolean, msg: string) {
if (!isValid) {
input.classList.add("has-error")
input.setCustomValidity(msg)
} else input.setCustomValidity("")
input.reportValidity()
}
private validateColorInput(value: string) {
const colorRegex = /^(#[0-9a-f]{3,6}|rgba?\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*(,\s*\d+(\.\d+)?%?)?\s*\))$/i;
try {
const data: string[] = JSON.parse(value)
if (!Array.isArray(data)) return false
return data.every(color => colorRegex.test(color)) ? data : false
} catch (e) {
return false
}
}
private displayWeekStartDaySettings() {
const { containerEl, } = this
new Setting(containerEl)
.setName("Week Start Day")
.setDesc("Select the day on which your week starts.")
.addDropdown(dropdown =>
dropdown
.addOptions({
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
})
.setValue(this.plugin.settings.weekStartDay.toString())
.onChange(async value => {
this.plugin.settings.weekStartDay = +value
await this.plugin.saveSettings()
})
)
}
display() {
const { containerEl, } = this
containerEl.empty()
containerEl.createEl("h2", { text: "Heatmap Calendar Settings", })
this.displayWeekStartDaySettings()
this.displayColorSettings()
console.log( "settings", this.plugin.settings )
}
}