generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
399 lines (358 loc) · 10.8 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
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
import { App, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
interface FontPluginSettings {
font_folder: string;
font: string;
force_mode: boolean;
custom_css_mode: boolean;
custom_css: string;
}
const DEFAULT_SETTINGS: FontPluginSettings = {
font_folder: "",
font: "None",
force_mode: false,
custom_css_mode: false,
custom_css: "",
};
function get_default_css(font_family_name: string, css_class = ":root *") {
return `${css_class} {
--font-default: ${font_family_name};
--default-font: ${font_family_name};
--font-family-editor: ${font_family_name};
--font-monospace-default: ${font_family_name},
--font-interface-override: ${font_family_name},
--font-text-override: ${font_family_name},
--font-monospace-override: ${font_family_name},
}
`;
}
function get_custom_css(font_family_name: string, css_class = ":root *") {
return `${css_class} * {
font-family: ${font_family_name} !important;
}`;
}
function arrayBufferToBase64(buffer: ArrayBuffer) {
let binary = "";
const bytes = new Uint8Array(buffer);
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function applyCss(css: string, css_id: string, appendMode = false) {
// Check if style tag with the given ID already exists
const existingStyle = document.getElementById(css_id);
if (existingStyle && appendMode) {
// Append CSS content to existing style tag
existingStyle.innerHTML += css;
} else {
// Create style tag
const style = document.createElement("style");
// Add CSS content
style.innerHTML = css;
// Append style tag to head
document.head.appendChild(style);
// Optional: Remove existing custom CSS
if (existingStyle) {
existingStyle.remove();
}
// Give ID to new style tag
style.id = css_id;
}
}
export default class FontPlugin extends Plugin {
settings: FontPluginSettings;
config_dir: string = this.app.vault.configDir;
plugin_folder_path = `${this.config_dir}/plugins/custom-font-loader`;
async load_plugin() {
await this.loadSettings();
try {
const font_file_name: string = this.settings.font;
if (font_file_name && font_file_name.toLowerCase() != "none") {
if (font_file_name != "all") {
await this.process_and_load_font(font_file_name, false);
} else {
applyCss("", "custom_font_base64");
const files = await this.app.vault.adapter.list(
this.settings.font_folder
);
for (const file of files.files) {
const file_name = file.replace(
this.settings.font_folder,
""
);
await this.process_and_load_font(file_name, true);
}
}
} else {
applyCss("", "custom_font_base64");
applyCss("", "custom_font_general");
}
} catch (error) {
new Notice(error);
}
}
private async process_and_load_font(
font_file_name: string,
load_all_fonts: boolean
) {
console.log("loading %s", font_file_name);
const css_font_path = `${this.plugin_folder_path}/${font_file_name
.toLowerCase()
.replace(".", "_")}.css`;
if (!(await this.app.vault.adapter.exists(css_font_path))) {
await this.convert_font_to_css(font_file_name, css_font_path);
} else {
await this.load_font(css_font_path, load_all_fonts);
await this.load_css(font_file_name);
}
}
private async load_font(css_font_path: string, appendMode: boolean) {
const content = await this.app.vault.adapter.read(css_font_path);
applyCss(content, "custom_font_base64", appendMode);
}
private async load_css(font_file_name: string) {
let css_string = "";
const font_family_name: string = font_file_name.split(".")[0];
if (this.settings.custom_css_mode) {
css_string = this.settings.custom_css;
} else {
css_string = get_default_css(font_family_name);
}
if (this.settings.force_mode)
css_string += `
* {
font-family: ${font_family_name} !important;
}
`;
applyCss(css_string, "custom_font_general");
}
private async convert_font_to_css(
font_file_name: string,
css_font_path: string
) {
new Notice("Processing Font files");
const file = `${this.settings.font_folder}/${font_file_name}`;
const arrayBuffer = await this.app.vault.adapter.readBinary(file);
// Convert to base64
const base64 = arrayBufferToBase64(arrayBuffer);
const font_family_name: string = font_file_name
.split(".")[0]
.toLowerCase();
const font_extension_name: string = font_file_name
.split(".")[1]
.toLowerCase();
let css_type = "";
switch (font_extension_name) {
case "woff":
css_type = "font/woff";
break;
case "ttf":
css_type = "font/truetype";
break;
case "woff2":
css_type = "font/woff2";
break;
case "otf":
css_type = "font/opentype";
break;
default:
css_type = "font";
}
const base64_css = `@font-face{
font-family: '${font_family_name}';
src: url(data:${css_type};base64,${base64});
}`;
this.app.vault.adapter.write(css_font_path, base64_css);
console.log("saved font %s into %s", font_family_name, css_font_path);
console.log("Font CSS Saved into %s", css_font_path);
await this.load_plugin();
}
async onload() {
this.load_plugin();
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new FontSettingTab(this.app, this));
}
async onunload() {
applyCss("", "custom_font_base64");
applyCss("", "custom_font_general");
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class FontSettingTab extends PluginSettingTab {
plugin: FontPlugin;
constructor(app: App, plugin: FontPlugin) {
super(app, plugin);
this.plugin = plugin;
}
async display() {
const { containerEl } = this;
containerEl.empty();
const infoContainer = containerEl.createDiv();
infoContainer.setText(
"In Order to set the font, copy your font into fonts directory that you set"
);
new Setting(containerEl)
.setName("Fonts Folder")
.setDesc("Folder to look for your custom fonts")
.addText((text) => {
text.onChange(async (value) => {
this.plugin.settings.font_folder = value;
await this.plugin.saveSettings();
await this.plugin.loadSettings();
});
if (this.plugin.settings.font_folder.trim() == "") {
this.plugin.settings.font_folder = `${this.app.vault.configDir}/fonts`;
}
if (!this.plugin.settings.font_folder.endsWith("/"))
this.plugin.settings.font_folder =
this.plugin.settings.font_folder + "/";
text.setValue(this.plugin.settings.font_folder);
});
const font_folder_path = this.plugin.settings.font_folder;
const options = [{ name: "none", value: "None" }];
try {
if (!(await this.app.vault.adapter.exists(font_folder_path))) {
await this.app.vault.adapter.mkdir(font_folder_path);
}
if (await this.app.vault.adapter.exists(font_folder_path)) {
const files = await this.app.vault.adapter.list(
font_folder_path
);
// Add files as options
for (const file of files.files) {
const file_name = file.replace(font_folder_path, "");
if (file_name.startsWith(".")) continue; //ignore hidden files
options.push({ name: file_name, value: file_name });
}
}
options.push({ name: "all", value: "Multiple fonts" });
} catch (error) {
console.log(error);
}
new Setting(containerEl)
.setName("Reload fonts from folder")
.setDesc(
"This button reloades from the folder you specified (it also creates the folder for you)"
)
.addButton((button) => {
button.setButtonText("Reload");
button.onClick((callback) => {
this.plugin.saveSettings();
this.plugin.load_plugin();
this.display();
});
});
this.containerEl.createDiv();
new Setting(containerEl)
.setName("Font")
.setDesc(
`Choose font (If you can't see your fonts, make sure your fonts are in the folder you specified and hit reload.
Also if you choose multiple fonts option, we will load and process all fonts in the folder for you. In that Case, enable Custom CSS Mode)`
)
.addDropdown((dropdown) => {
// Add options
for (const opt of options) {
dropdown.addOption(opt.name, opt.value);
}
dropdown
.setValue(this.plugin.settings.font)
.onChange(async (value) => {
this.plugin.settings.font = value;
await this.plugin.saveSettings();
await this.plugin.load_plugin();
});
});
new Setting(containerEl)
.setName("Force Style")
.setDesc(
"This option should only be used if you have installed a community theme and normal mode doesn't work"
)
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.force_mode);
toggle.onChange(async (value) => {
this.plugin.settings.force_mode = value;
await this.plugin.saveSettings();
await this.plugin.load_plugin();
});
});
new Setting(containerEl)
.setName("Custom CSS Mode")
.setDesc(
"If you want to apply a custom css style rather than default style, choose this."
)
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.custom_css_mode);
toggle.onChange(async (value) => {
if (this.plugin.settings.custom_css_mode == false) {
this.plugin.settings.custom_css = "";
}
this.plugin.settings.custom_css_mode = value;
this.plugin.saveSettings();
this.plugin.load_plugin();
this.display();
});
});
if (this.plugin.settings.custom_css_mode) {
new Setting(containerEl)
.setName("Custom CSS Style")
.setDesc("Input your custom css style")
.addTextArea(async (text) => {
text.onChange(async (new_value) => {
this.plugin.settings.custom_css = new_value;
await this.plugin.saveSettings();
await this.plugin.load_plugin();
});
text.setDisabled(!this.plugin.settings.custom_css_mode);
if (this.plugin.settings.custom_css == "") {
let font_family_name = "";
try {
font_family_name =
this.plugin.settings.font.split(".")[0];
} catch (error) {
console.log(error);
}
if (font_family_name == "all") {
if (
await this.app.vault.adapter.exists(
font_folder_path
)
) {
const files = await this.app.vault.adapter.list(
font_folder_path
);
let final_str = "";
// Add files as options
for (const file of files.files) {
const file_name = file.split("/")[2];
const font_family = file_name.split(".")[0];
final_str +=
"\n" +
get_custom_css(
font_family,
"." + font_family
);
}
text.setValue(final_str);
}
} else {
text.setValue(get_default_css(font_family_name));
}
} else {
text.setValue(this.plugin.settings.custom_css);
}
text.onChanged();
text.inputEl.style.width = "100%";
text.inputEl.style.height = "100px";
});
}
}
}