generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ts
119 lines (101 loc) · 3.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
import { App, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import staticServer, { StaticServer } from './static-server';
interface StaticFileServerPluginSettings {
vaultMaps: Record<string, string>;
}
const DEFAULT_SETTINGS: StaticFileServerPluginSettings = {
vaultMaps: {}
};
const isValidPort = (port: string) => Number(port) && 0 <= Number(port) && Number(port) <= 65535;
export default class StaticFileServerPlugin extends Plugin {
settings: StaticFileServerPluginSettings;
webservers: StaticServer[];
async onload() {
this.webservers = [];
await this.loadSettings();
this.restartServers();
this.addSettingTab(new SettingTab(this.app, this));
}
shutDownServers() {
this.webservers.forEach(s => s.close());
this.webservers = [];
}
restartServers() {
this.shutDownServers();
for (const [port, path] of Object.entries(this.settings.vaultMaps)) {
if (!isValidPort(port)) {
new Notice(`Static File Server Plugin: Invalid port '${port}'`);
continue;
}
const ws = staticServer(path, port, this);
ws.listen();
this.webservers.push(ws);
}
}
onunload() {
this.shutDownServers();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SettingTab extends PluginSettingTab {
plugin: StaticFileServerPlugin;
constructor(app: App, plugin: StaticFileServerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Static file server settings' });
let i = 1;
[...Object.entries(this.plugin.settings.vaultMaps), ['', '']].forEach(([port, path]) => {
new Setting(containerEl)
.setName(`port #${i}`)
.setDesc('The port number')
.addText(text =>
text
.setPlaceholder('e.g. 1337')
.setValue(port)
.onChange(async newPort => {
if (isValidPort(newPort)) {
delete this.plugin.settings.vaultMaps[port];
port = newPort;
this.plugin.settings.vaultMaps[port] = path;
}
})
);
new Setting(containerEl)
.setName(`folder #${i}`)
.setDesc(`The vault folder served for port #${i}`)
.addText(text =>
text
.setPlaceholder('e.g. FolderName')
.setValue(path)
.onChange(async newPath => {
this.plugin.settings.vaultMaps[port] = newPath;
path = newPath;
})
);
i++;
});
new Setting(containerEl).addButton(button =>
button.setButtonText('Revert').onClick(async () => {
await this.plugin.loadSettings();
this.plugin.restartServers();
this.display();
})
);
new Setting(containerEl).addButton(button =>
button.setButtonText('Apply').onClick(async () => {
await this.plugin.saveSettings();
this.plugin.restartServers();
this.display();
})
);
}
}