Skip to content

Commit 544f926

Browse files
committed
Fixing comments and validations
1 parent 6d1f627 commit 544f926

20 files changed

+721
-110
lines changed

.editorconfig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# top-most EditorConfig file
2-
root = true
3-
4-
[*]
5-
charset = utf-8
6-
end_of_line = lf
7-
insert_final_newline = true
8-
indent_style = tab
9-
indent_size = 4
10-
tab_width = 4
1+
# top-most EditorConfig file
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 2
10+
tab_width = 2

main.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "obsidian-custom-file-extensions-plugin",
2+
"id": "custom-file-extensions",
33
"name": "Custom File Extensions Plugin",
44
"version": "1.0.0",
55
"minAppVersion": "0.10.12",

src/main.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { App, Plugin, PluginSettingTab, Setting, TextAreaComponent } from 'obsidian';
2+
3+
interface CustomFileExtensionsSettings {
4+
additionalFileTypes: Record<string, Array<string>>;
5+
currentValueIsInvalidJson: boolean;
6+
}
7+
8+
const DEFAULT_SETTINGS: CustomFileExtensionsSettings = {
9+
additionalFileTypes: {
10+
"markdown": [
11+
"", "txt", "html",
12+
"js", "css", "ts",
13+
"jsx", "tsx", "yaml",
14+
"yml", "sass", "scss"
15+
]
16+
},
17+
currentValueIsInvalidJson: false
18+
}
19+
20+
export default class CustomFileExtensions extends Plugin {
21+
private _settings: CustomFileExtensionsSettings;
22+
public get settings(): CustomFileExtensionsSettings {
23+
return this._settings;
24+
}
25+
26+
async onload() {
27+
super.onload();
28+
await this.loadSettings();
29+
this.addSettingTab(new CustomFileExtensionsSettingTab(this.app, this));
30+
this._apply(this.settings.additionalFileTypes);
31+
}
32+
33+
onunload() {
34+
this._unapply(this._settings.additionalFileTypes);
35+
36+
// reset the defaults:
37+
try {
38+
this.registerExtensions([".md"], 'markdown');
39+
} catch { }
40+
}
41+
42+
async loadSettings() {
43+
this._settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
44+
}
45+
46+
async updateSettings(newSettings: CustomFileExtensionsSettings) {
47+
this._unapply(this._settings.additionalFileTypes);
48+
this._settings = newSettings;
49+
50+
await this.saveData(this.settings);
51+
this._apply(this.settings.additionalFileTypes);
52+
}
53+
54+
private _apply(extensionsByViewType: Record<string, Array<string>>) {
55+
for (const view in extensionsByViewType) {
56+
for (const fileType of this.settings.additionalFileTypes[view]) {
57+
this.registerExtensions([fileType], view);
58+
}
59+
}
60+
}
61+
62+
private _unapply(extensionsByViewType: Record<string, Array<string>>) {
63+
for (const view of Object.values(extensionsByViewType).flat()) {
64+
try {
65+
/**@ts-expect-error */
66+
this.app.viewRegistry.unregisterExtensions([view]);
67+
} catch {
68+
console.log("ERROR");
69+
}
70+
}
71+
}
72+
}
73+
74+
class CustomFileExtensionsSettingTab extends PluginSettingTab {
75+
plugin: CustomFileExtensions;
76+
private _defaults?: {
77+
color: string;
78+
borderColor: string;
79+
borderWidth: string;
80+
} = undefined;
81+
82+
constructor(app: App, plugin: CustomFileExtensions) {
83+
super(app, plugin);
84+
this.plugin = plugin;
85+
}
86+
87+
display(): void {
88+
const { containerEl } = this;
89+
90+
containerEl.empty();
91+
92+
containerEl.createEl('h2', { text: 'Custom File Extensions Settings' });
93+
94+
const settings = new Setting(containerEl)
95+
.setName('Config')
96+
.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)
97+
.addTextArea(text => {
98+
text = text
99+
.setPlaceholder(JSON.stringify(DEFAULT_SETTINGS.additionalFileTypes))
100+
.setValue(JSON.stringify(this.plugin.settings.additionalFileTypes))
101+
.onChange(async (value) => {
102+
let parsed: any = null;
103+
try {
104+
parsed = JSON.parse(value);
105+
this.updateErrorState(text, false);
106+
} catch {
107+
this.updateErrorState(text, true);
108+
return;
109+
}
110+
111+
this.plugin.settings.additionalFileTypes = parsed;
112+
await this.plugin.updateSettings(this.plugin.settings);
113+
});
114+
115+
return text;
116+
});
117+
}
118+
119+
updateErrorState(text: TextAreaComponent, to: boolean) {
120+
if (this.plugin.settings.currentValueIsInvalidJson !== to) {
121+
this.plugin.settings.currentValueIsInvalidJson = to;
122+
123+
if (this.plugin.settings.currentValueIsInvalidJson) {
124+
if (!this._defaults) {
125+
this._defaults = {
126+
color: text.inputEl.style.color,
127+
borderColor: text.inputEl.style.borderColor,
128+
borderWidth: text.inputEl.style.borderWidth
129+
}
130+
}
131+
132+
text.inputEl.style.color = "var(--text-error)"// "red";
133+
text.inputEl.style.borderColor = "var(--background-modifier-error-rgb)" // "red";
134+
text.inputEl.style.borderWidth = "3px";
135+
} else if (this._defaults) {
136+
text.inputEl.style.color = this._defaults.color;
137+
text.inputEl.style.borderColor = this._defaults.borderColor;
138+
text.inputEl.style.borderWidth = this._defaults.borderWidth;
139+
}
140+
}
141+
}
142+
}

tests/vault/.obsidian/app.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"showUnsupportedFiles": true,
3+
"tabSize": 2,
4+
"alwaysUpdateLinks": true
5+
}

tests/vault/.obsidian/appearance.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"accentColor": "",
3+
"baseFontSize": 15
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"custom-file-extensions"
3+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"file-explorer": true,
3+
"global-search": true,
4+
"switcher": true,
5+
"graph": true,
6+
"backlink": true,
7+
"outgoing-link": true,
8+
"tag-pane": true,
9+
"page-preview": true,
10+
"daily-notes": true,
11+
"templates": true,
12+
"note-composer": true,
13+
"command-palette": true,
14+
"slash-command": false,
15+
"editor-status": true,
16+
"starred": true,
17+
"markdown-importer": false,
18+
"zk-prefixer": false,
19+
"random-note": false,
20+
"outline": true,
21+
"word-count": true,
22+
"slides": false,
23+
"audio-recorder": false,
24+
"workspaces": false,
25+
"file-recovery": true,
26+
"publish": false,
27+
"sync": false,
28+
"canvas": true
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
"file-explorer",
3+
"global-search",
4+
"switcher",
5+
"graph",
6+
"backlink",
7+
"canvas",
8+
"outgoing-link",
9+
"tag-pane",
10+
"page-preview",
11+
"daily-notes",
12+
"templates",
13+
"note-composer",
14+
"command-palette",
15+
"editor-status",
16+
"starred",
17+
"outline",
18+
"word-count",
19+
"file-recovery"
20+
]

tests/vault/.obsidian/graph.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"collapse-filter": true,
3+
"search": "",
4+
"showTags": false,
5+
"showAttachments": false,
6+
"hideUnresolved": false,
7+
"showOrphans": true,
8+
"collapse-color-groups": true,
9+
"colorGroups": [],
10+
"collapse-display": true,
11+
"showArrow": false,
12+
"textFadeMultiplier": 0,
13+
"nodeSizeMultiplier": 1,
14+
"lineSizeMultiplier": 1,
15+
"collapse-forces": true,
16+
"centerStrength": 0.518713248970312,
17+
"repelStrength": 10,
18+
"linkStrength": 1,
19+
"linkDistance": 250,
20+
"scale": 1.0000000000000029,
21+
"close": false
22+
}

tests/vault/.obsidian/hotkeys.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)