-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
149 lines (131 loc) · 3.81 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
import { Notice, Plugin, requestUrl } from "obsidian";
import { ReactView, BEAUTITAB_REACT_VIEW } from "./Views/ReactView";
import Observable from "src/Utils/Observable";
import {
BeautitabPluginSettingTab,
BeautitabPluginSettings,
DEFAULT_SETTINGS,
} from "src/Settings/Settings";
/**
* This allows a "live-reload" of Obsidian when developing the plugin.
* Any changes to the code will force reload Obsidian.
*/
if (process.env.NODE_ENV === "development") {
new EventSource("http://127.0.0.1:8000/esbuild").addEventListener(
"change",
() => location.reload()
);
}
export default class BeautitabPlugin extends Plugin {
settings: BeautitabPluginSettings;
settingsObservable: Observable;
async onload() {
await this.loadSettings();
this.versionCheck();
this.settingsObservable = new Observable(this.settings);
this.registerView(
BEAUTITAB_REACT_VIEW,
(leaf) =>
new ReactView(this.app, this.settingsObservable, leaf, this)
);
this.addSettingTab(new BeautitabPluginSettingTab(this.app, this));
this.registerEvent(
this.app.workspace.on(
"layout-change",
this.onLayoutChange.bind(this)
)
);
if (process.env.NODE_ENV === "development") {
// @ts-ignore
if (process.env.EMULATE_MOBILE && !this.app.isMobile) {
// @ts-ignore
this.app.emulateMobile(true);
}
// @ts-ignore
if (!process.env.EMULATE_MOBILE && this.app.isMobile) {
// @ts-ignore
this.app.emulateMobile(false);
}
}
}
onunload() {}
/**
* Load data from disk, stored in data.json in plugin folder
*/
async loadSettings() {
const data = (await this.loadData()) || {};
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
}
/**
* Save data to disk, stored in data.json in plugin folder
*/
async saveSettings() {
await this.saveData(this.settings);
}
/**
* Check the local plugin version against github. If there is a new version, notify the user.
*/
async versionCheck() {
const localVersion = process.env.PLUGIN_VERSION;
const stableVersion = await requestUrl(
"https://raw.githubusercontent.com/andrewmcgivery/obsidian-beautitab/main/package.json"
).then(async (res) => {
if (res.status === 200) {
const response = await res.json;
return response.version;
}
});
const betaVersion = await requestUrl(
"https://raw.githubusercontent.com/andrewmcgivery/obsidian-beautitab/beta/package.json"
).then(async (res) => {
if (res.status === 200) {
const response = await res.json;
return response.version;
}
});
if (localVersion?.indexOf("beta") !== -1) {
if (localVersion !== betaVersion) {
new Notice(
"There is a beta update available for the Beautitab plugin. Please update to to the latest version to get the latest features!",
0
);
}
} else if (localVersion !== stableVersion) {
new Notice(
"There is an update available for the Beautitab plugin. Please update to to the latest version to get the latest features!",
0
);
}
}
/**
* Hijack new tabs and show Beauitab
*/
private onLayoutChange(): void {
const leaf = this.app.workspace.getMostRecentLeaf();
if (leaf?.getViewState().type === "empty") {
leaf.setViewState({
type: BEAUTITAB_REACT_VIEW,
});
}
}
/**
* Check if the choosen provider is enabled
* If yes: open it by using executeCommandById
* If no: Notice the user and tell them to enable it in the settings
*/
openSwitcherCommand(command: string): void {
const pluginID = command.split(":")[0];
//@ts-ignore
const plugins = this.app.plugins.plugins;
//@ts-ignore
const internalPlugins = this.app.internalPlugins.plugins;
if (plugins[pluginID] || internalPlugins[pluginID]?.enabled) {
//@ts-ignore
this.app.commands.executeCommandById(command);
} else {
new Notice(
`Plugin ${pluginID} is not enabled. Please enable it in the settings.`
);
}
}
}