-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
130 lines (104 loc) · 3 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
import {
Notice,
Plugin,
PluginSettingTab,
Setting,
WorkspaceLeaf,
MarkdownView
} from 'obsidian';
import DynasyncSettings from './settings';
import Store from './store';
import DynasyncView from './view';
import { exportDocument } from './src/utils';
function extractFrontmatter(source: string): string {
if (!source.startsWith('---')) {
return '';
}
const end = source.indexOf("\n---\n", 4);
if (end < 0) {
return '';
}
return source.substring(0, end + 5) + "\n";
}
export default class DynasyncPlugin extends Plugin {
store: Store;
view: DynasyncView;
async onload() {
console.log('loading DynaSync');
const settings = (await this.loadData()) || new DynasyncSettings();
this.store = new Store(settings);
this.addRibbonIcon('dice', 'Dynasync Plugin', () => {
new Notice('This is a notice!');
});
// this.addStatusBarItem().setText('Status Bar Text');
this.addCommand({
id: 'refresh-dynasync',
name: 'Refresh DynaSync',
callback: () => {
this.store.trigger('sync');
},
});
this.addSettingTab(new DynasyncSettingTab(this.app, this));
this.registerView('dynasync', (leaf: WorkspaceLeaf) =>
(this.view = new DynasyncView(leaf, this.store))
);
this.initLeaf();
this.store.on('sync', () => {
console.log('syncing');
this.sync();
});
}
initLeaf(): void {
if (!this.app.workspace.getLeavesOfType('dynasync').length) {
this.app.workspace.getRightLeaf(false).setViewState({
type: 'dynasync',
});
}
}
onunload() {
console.log('unloading plugin');
this.app.workspace
.getLeavesOfType('dynasync')
.forEach((leaf) => leaf.detach());
}
async sync(): Promise<void> {
this.store.setStatus('syncing ...');
const view = this.app.workspace.activeLeaf.view;
if (!(view instanceof MarkdownView)) {
this.store.setStatus('failed');
return;
}
const content = view.sourceMode.get();
const frontmatter = extractFrontmatter(content);
const file = this.app.workspace.getActiveFile();
const metadata = this.app.metadataCache.getFileCache(file);
const documentId = metadata.frontmatter && metadata.frontmatter.dynasync;
if (!documentId) {
this.store.setStatus('disabled');
return;
}
const body = await this.store.client.readDocument(documentId);
const doc = exportDocument(body);
const newContent = frontmatter + doc;
view.sourceMode.set(newContent, true);
this.store.setStatus('synced');
}
}
class DynasyncSettingTab extends PluginSettingTab {
display(): void {
let {containerEl} = this;
const plugin: DynasyncPlugin = (this as any).plugin;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
new Setting(containerEl)
.setName('API token')
.setDesc('Dynalist API token')
.addText(text => text.setPlaceholder('Enter your secret')
.setValue(plugin.store.settings.apiToken)
.onChange(async (value) => {
plugin.store.settings.apiToken = value;
await plugin.saveData(plugin.store.settings);
console.log('saved', plugin.store.settings);
}));
}
}