generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
243 lines (215 loc) · 7.14 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
import { App, Editor, MarkdownView, Menu, MenuItem, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, parseYaml, stringifyYaml } from 'obsidian';
import 'obsidian';
interface ProgressBarSettings {
setting: string;
}
const DEFAULT_SETTINGS: ProgressBarSettings = {
setting: 'default'
}
export default class ProgressBar extends Plugin {
// settings: ProgressBarSettings;
async onload() {
// await this.loadSettings();
// This adds a settings tab so the user can configure various aspects of the plugin
// this.addSettingTab(new ProgressBarSettingTab(this.app, this));
this.registerMarkdownCodeBlockProcessor("progressbar", (source, el, ctx) => {
let cfg;
try {
cfg = parseYaml(source);
} catch (e) {
newError(el, "Cannot parse the YAML Format");
return;
}
if (!cfg.kind && !cfg.value) {
newError(el, "No kind specified");
return;
}
if (cfg.kind === "day-custom" && !cfg.min && !cfg.max) {
newError(el, "Must specify min and max for day-custom");
return;
}
if ((cfg.kind && !(cfg.kind==="manual" || cfg.kind=== "other")) && cfg.button){
newError(el, "Can only use button with kind: manual/other");
return;
}
if (cfg.button && !cfg.id){
newError(el, "Can not use button without id");
return;
}
createProgressBar(el, cfg, source);
});
}
onunload() {
}
// async loadSettings() {
// this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
// }
//
// async saveSettings() {
// await this.saveData(this.settings);
// }
}
function newError(el: HTMLElement, msg: string) {
el.createEl("div", { text: 'ProgressBarError: ' + msg });
}
const generateUniqueID = (idLength: number) => [...Array(idLength).keys()].map((elem) => Math.random().toString(36).substr(2, 1)).join("")
function createProgressBar(el: HTMLElement, bar: any, source: string) {
switch (bar.kind) {
case "day-year":
return newDayYearProgressBar(el, bar);
case "day-month":
return newDayMonthProgressBar(el, bar);
case "month":
return newMonthProgressBar(el, bar);
case "day-week":
return newDayWeekProgressBar(el, bar);
case "day-custom":
return newDayCustomProgressBar(el, bar);
default:
return newProgressBar(el, bar, bar);
}
}
function daysIntoYear(date: Date) {
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())
- Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
function newDayWeekProgressBar(el: HTMLElement, bar: any) {
bar.max = 7;
bar.value = new Date().getDay() === 0 ? 7 : new Date().getDay();
newProgressBar(el, bar, bar);
}
function newMonthProgressBar(el: HTMLElement, bar: any) {
bar.max = 12;
bar.value = new Date().getMonth() + 1;
newProgressBar(el, bar, bar);
}
function newDayMonthProgressBar(el: HTMLElement, bar: any) {
const now = new Date()
bar.max = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
bar.value = now.getDate();
newProgressBar(el, bar, bar);
}
function newDayYearProgressBar(el: HTMLElement, bar: any) {
bar.max = new Date().getFullYear() % 4 == 0 ? 366 : 365;
bar.value = daysIntoYear(new Date());
newProgressBar(el, bar, bar);
}
// html progressbar has no min variable,
// both max and value should minus min
function newDayCustomProgressBar(el: HTMLElement, bar: any) {
let val = {
min: daysIntoYear(new Date(bar.min)),
max: daysIntoYear(new Date(bar.max)),
value: daysIntoYear(new Date()),
}
val.max = val.max - val.min;
val.value = val.value - val.min;
newProgressBar(el, bar, val);
}
interface Templater {
[index: string]: string;
max: string;
value: string;
percentage: string;
}
function applyTemplate(template: string, data: Templater) {
const pattern = /{\s*(\w+?)\s*}/g; // {property}
return template.replace(pattern, (_: any, token: string) => data[token] || "{" + token + "}");
}
function newProgressBar(el: HTMLElement, bar: any, val: any) {
const labelName = bar.name ? bar.name : bar.kind + "({percentage})";
const value: string = (Math.floor(bar.value * 10) / 10).toString();
const message = applyTemplate(labelName, {
min: bar.min,
max: bar.max,
value: value,
percentage: Math.round(val.value / val.max * 100) + "%",
});
const label = el.createEl("label", { text: message + ": " });
if (bar.button) {
const minus=el.createEl("button", { text: "-" });
minus.style.fontSize='larger'
minus.addEventListener("click", () => {
decrement(bar);
})
}
const progressbar = el.createEl("progress");
progressbar.value = val.value;
progressbar.max = val.max;
if (bar.width) {
progressbar.style.width = bar.width;
}
if (bar.button) {
const plus=el.createEl("button", { text: "+" });
plus.style.fontSize='larger'
plus.addEventListener("click", () => {
increment(bar);
})
}
el.style.padding="1px"
el.style.display = "flex";
el.style.alignItems = "center";
el.style.gap = "10px";
}
function increment(blockTextYAML: any){
if (blockTextYAML.value>=blockTextYAML.max) {return;}
const file = this.app.workspace.getActiveFile();
if (file) {
let doneOnce=false;
this.app.vault.process(file, (data: string) => {
const pattern=new RegExp(`\`{3}progressbar[a-zA-Z0-9\\s:{}#\\-"]*id:[\\s]${blockTextYAML.id}[a-zA-Z0-9\\s:{}#\\-"]*\`{3}`, "g")
return data.replace(pattern, (source: String)=>{
if (!doneOnce) {
blockTextYAML.value=blockTextYAML.value+1;
doneOnce=true;
}
return source.replace(/value: [0-9\-]*/g, `value: ${blockTextYAML.value}`)
})
})
}
}
function decrement(blockTextYAML: any){
if (blockTextYAML.value<=(blockTextYAML.min?blockTextYAML.min:0)) {return;}
const file = this.app.workspace.getActiveFile();
if (file) {
let doneOnce=false;
this.app.vault.process(file, (data: string) => {
const pattern=new RegExp(`\`{3}progressbar[a-zA-Z0-9\\s:{}#\\-"]*id:[\\s]${blockTextYAML.id}[a-zA-Z0-9\\s:{}#\\-"]*\`{3}`, "g")
return data.replace(pattern, (source: String)=>{
if (!doneOnce) {
blockTextYAML.value=blockTextYAML.value-1;
doneOnce=true;
}
return source.replace(/value: [0-9\-]*/g, `value: ${blockTextYAML.value}`)
})
})
}
}
// class ProgressBarSettingTab extends PluginSettingTab {
// plugin: ProgressBar;
//
// constructor(app: App, plugin: ProgressBar) {
// super(app, plugin);
// this.plugin = plugin;
// }
//
// display(): void {
// const {containerEl} = this;
//
// containerEl.empty();
//
// containerEl.createEl('h2', {text: 'Settings for ProgressBar plugin.'});
//
// new Setting(containerEl)
// .setName('TBD')
// .setDesc('Construction')
// .addText(text => text
// .setPlaceholder('Enter your secret')
// .setValue(this.plugin.settings.setting)
// .onChange(async (value) => {
// console.log('Secret: ' + value);
// this.plugin.settings.setting = value;
// await this.plugin.saveSettings();
// }));
// }
// }