Skip to content

Commit 652a7c8

Browse files
committed
tested plugin successfully. Tested with valid and invalid json, tested turning on and off plugin too. && git push
1 parent d2bdbf3 commit 652a7c8

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

main.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
22

33
interface CustomFileExtensionsSettings {
44
additionalFileTypes: string;
5-
previousAdditionalFileTypes: string;
5+
previousAdditionalFileTypes: object;
6+
currentValueIsInvalidJson: boolean;
67
}
78

89
const DEFAULT_SETTINGS: CustomFileExtensionsSettings = {
9-
additionalFileTypes: '{markdown: ["", "txt", "html", "js", "css", "ts", "yaml"]}',
10-
previousAdditionalFileTypes: '{markdown: ["", "txt", "html", "js", "css", "ts", "yaml"]}'
10+
additionalFileTypes: '{"markdown": ["", "txt", "html", "js", "css", "ts", "yaml"]}',
11+
previousAdditionalFileTypes: { markdown: ["", "txt", "html", "js", "css", "ts", "yaml"] },
12+
currentValueIsInvalidJson: false
1113
}
1214

1315
export default class CustomFileExtensions extends Plugin {
@@ -17,7 +19,6 @@ export default class CustomFileExtensions extends Plugin {
1719
super.onload();
1820
await this.loadSettings();
1921
this.addSettingTab(new CustomFileExtensionsSettingTab(this.app, this));
20-
2122
this.apply();
2223
}
2324

@@ -31,24 +32,32 @@ export default class CustomFileExtensions extends Plugin {
3132

3233
async saveSettings() {
3334
await this.saveData(this.settings);
35+
this.revert();
3436
this.apply();
3537
}
3638

3739
apply() {
38-
this.revert();
39-
4040
// apply new types:
4141
const views = JSON.parse(this.settings.additionalFileTypes);
4242
for (const view in views) {
43-
this.registerExtensions([views[view]], view);
43+
for (const fileType of views[view]) {
44+
this.registerExtensions([fileType], view);
45+
}
4446
}
4547
}
4648

4749
revert() {
48-
for (const view in Object.values(this.plugin.settings.additionalFileTypes).flat()) {
49-
this.registerExtensions(view, "");
50+
for (const view of Object.values(this.settings.previousAdditionalFileTypes).flat()) {
51+
try {
52+
this.app.viewRegistry.unregisterExtensions([view]);
53+
} catch {
54+
console.log("ERROR");
55+
}
5056
}
51-
this.registerExtensions([".md"], 'markdown');
57+
58+
try {
59+
this.registerExtensions([".md"], 'markdown');
60+
} catch {}
5261
}
5362
}
5463

@@ -72,11 +81,19 @@ class CustomFileExtensionsSettingTab extends PluginSettingTab {
7281
.setDesc("Valid entry is a JSON object with properties named after the desired view, containing the file types to assign to that view. EX: " + DEFAULT_SETTINGS.additionalFileTypes)
7382
.addText(text => text
7483
.setPlaceholder(DEFAULT_SETTINGS.additionalFileTypes)
75-
.setValue(this.plugin.settings.additionalFileTypes)
84+
.setValue(this.plugin.settings.additionalFileTypes)
7685
.onChange(async (value) => {
86+
try {
87+
JSON.parse(value);
88+
this.plugin.settings.currentValueIsInvalidJson = false;
89+
} catch {
90+
this.plugin.settings.currentValueIsInvalidJson = true;
91+
return;
92+
}
93+
7794
this.plugin.settings.previousAdditionalFileTypes = JSON.parse(this.plugin.settings.additionalFileTypes);
7895
this.plugin.settings.additionalFileTypes = value;
7996
await this.plugin.saveSettings();
80-
}));
97+
}));
8198
}
8299
}

0 commit comments

Comments
 (0)