-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
218 lines (212 loc) · 6.6 KB
/
main.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
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
default: () => RpgClock,
urlRegex: () => urlRegex
});
module.exports = __toCommonJS(main_exports);
var import_obsidian4 = require("obsidian");
// src/clock.ts
var import_obsidian = require("obsidian");
var Clock = class extends import_obsidian.MarkdownRenderChild {
constructor(plugin, settings, containerEl, input) {
super(containerEl);
this.plugin = plugin;
this.settings = settings;
this.input = input;
this.clocks = [];
}
onload() {
console.log("onload: clean");
this.containerEl.innerHTML = "";
this.clocks = this.input.split("\n");
this.clocks.forEach((clock) => {
const [clockName, clockValue, clockColor] = clock.split(":");
const [filled, total] = clockValue ? clockValue.split("/") : clockName.split("/");
this.createClock(clockValue ? clockName : "", parseInt(filled, 10), parseInt(total, 10), clockColor);
});
}
unload() {
var _a;
if (this.clocks[0] !== "Invalid Clock") {
(_a = this.plugin.clocks) == null ? void 0 : _a.remove(this);
}
}
refresh() {
this.containerEl.empty();
console.log("refresh");
}
/**
* Add a new clock to html element
* @param clockName
* @param filled
* @param totalCount
*/
createClock(clockName, filled, totalCount, color) {
let { clockColor } = this.settings;
if (color) {
clockColor = color;
}
console.log("Create clock: ", clockName, `${filled}/${totalCount}`);
this.containerEl.innerHTML += `<div class="clock" n="${totalCount}" style="--n: ${totalCount}; --clock-color:${clockColor}">
<div class="description">${clockName}</div>
<div class="widget">
<div class="core">
${this._slices(totalCount, filled)}
${this._bars(totalCount)}
</div>
</div>
</div>`;
}
/**
* Generate slices
* @param count
* @param filled
* @returns
*/
_slices(count, filled) {
return new Array(count).fill(0).reduce((a, _, i) => [...a, `<div class="slice" i="${i}" ${filled <= i ? "" : "filled"} style="--i: ${i};"></div>`], []).join("\n");
}
/**
* Generate Bars
* @param count
* @returns
*/
_bars(count) {
return new Array(count).fill(0).reduce((a, _, i) => [...a, ` <div class="bar" i="${i}" style="--i: ${i};">
<div class="paint"></div>
</div>`], []).join("\n");
}
};
// src/settings.ts
var import_obsidian2 = require("obsidian");
var DefaultSettings = {
clockSize: 150,
clockColor: "#ff5757"
};
var SettingsTab = class extends import_obsidian2.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
let { settings } = this.plugin;
containerEl.empty();
new import_obsidian2.Setting(containerEl).setName("Clock Size").setDesc("How big the clock should be").addText((text) => {
text.setValue(settings.clockSize.toString()).onChange(async (value) => {
settings.clockSize = Number(value);
await this.plugin.saveSettings();
});
});
new import_obsidian2.Setting(containerEl).setName("Clock Color").setDesc("Which color should be the clock").addColorPicker((colorPicker) => {
colorPicker.setValue(settings.clockColor).onChange(async (value) => {
settings.clockColor = value;
await this.plugin.saveSettings();
});
});
}
hide() {
var _a;
if ((_a = this.plugin) == null ? void 0 : _a.clocks) {
for (let palette of this.plugin.clocks) {
palette.refresh();
}
}
}
};
// src/commandInput.ts
var import_obsidian3 = require("obsidian");
var CommandInput = class extends import_obsidian3.Modal {
constructor(app, onSubmit) {
super(app);
this.onSubmit = onSubmit;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl("h1", { text: "Clock name" });
new import_obsidian3.Setting(contentEl).setName("name").addText((text) => {
text.onChange((value) => {
this.result = value;
});
});
new import_obsidian3.Setting(contentEl).addButton(
(btn) => btn.setButtonText("enter").setCta().onClick(() => {
this.close();
this.onSubmit(this.result);
})
);
}
onClose() {
let { contentEl } = this;
contentEl.empty();
}
};
// src/main.ts
var urlRegex = /\/([^\/]+)\/?$/;
var RpgClock = class extends import_obsidian4.Plugin {
async onload() {
this.clocks = [];
await this.loadSettings();
this.registerMarkdownCodeBlockProcessor(
"clock",
async (source, el, ctx) => {
ctx.addChild(new Clock(this, this.settings, el, source.trim()));
}
);
this.addCommand({
id: "insert-clock",
name: "Insert Clock",
editorCallback: (editor) => {
new CommandInput(this.app, (result) => {
console.log("aniuest");
try {
const codeBlock = `\`\`\`clock
${result}:0/4
\`\`\`
`;
const cursor = editor.getCursor();
editor.transaction({
changes: [{ from: cursor, text: codeBlock }]
});
editor.setCursor({
line: cursor.line + codeBlock.split("\n").length,
ch: 0
});
new import_obsidian4.Notice(`Added ${result}`);
} catch (error) {
new import_obsidian4.Notice(error);
}
}).open();
}
});
this.addSettingTab(new SettingsTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DefaultSettings, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
};