-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
59 lines (47 loc) · 1.49 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
import { Plugin, Notice } from 'obsidian';
import * as path from 'path';
import { ServerManager } from './serverManager';
export default class ManicTime extends Plugin {
serverManager: ServerManager;
async onload() {
this.serverManager = new ServerManager();
this.serverManager.init();
const statusBar = this.addStatusBarItem();
statusBar.setText('ManicTime');
statusBar.addEventListener('click', () => this.onClick());
this.registerEvent(this.app.workspace.on('active-leaf-change', () => this.sendData()));
this.registerEvent(this.app.workspace.on('file-open' , () =>this.sendData()));
this.registerEvent(this.app.workspace.on('window-open', () =>this.sendData()));
this.registerEvent(this.app.workspace.on('window-close',() => this.sendData()));
this.registerInterval(window.setInterval(() => this.sendData(), 30 * 1000));
}
async onunload() {
this.serverManager.dispose();
}
getMetadata() {
const activeFile = this.app.workspace.getActiveFile();
const adapter = this.app.vault.adapter as any;
if (activeFile) {
return {
fileName: activeFile.name,
filePath: path.join(adapter.basePath, activeFile.path)
};
}
}
onClick() {
const tooltip = this.serverManager.getServerInfo();
new Notice(tooltip);
}
sendData() {
const data = this.getMetadata();
if (!data) {
return null;
}
let text = '';
try {
this.serverManager.send("Obsidian", "ManicTime/Files", data.filePath, data.fileName);
} catch (error) {
text = error;
}
}
}