generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
129 lines (113 loc) · 4.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
import { Editor, Plugin, TFile, Notice } from 'obsidian';
import { BbsPluginSettings, DEFAULT_SETTINGS, BbsSettingTab } from '@src/settings';
import { CustomStampModal } from '@modals/custom-stamp';
import { Stamp } from '@src/stamp';
import { insertAtCursor, replacePlaceholders } from '@utils/functions';
import { Replacements } from '@utils/types';
export default class BbsPlugin extends Plugin {
settings: BbsPluginSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new BbsSettingTab(this.app, this));
this.addRibbonIcon('bitcoin', 'Insert Bitcoin block stamp', () => {
try {
new CustomStampModal(this.app, this).open();
} catch (error) {
console.error(error);
new Notice('🛑 An error occurred while adding a custom stamp.');
}
});
this.addCommand({
id: 'insert-historical-block-stamp',
name: 'Insert custom block stamp',
editorCallback: () => {
try {
new CustomStampModal(this.app, this).open();
} catch (error) {
console.error(error);
new Notice('🛑 An error occurred while adding a custom stamp.');
}
}
});
this.addCommand({
id: 'insert-current-block-height',
name: 'Insert current block height',
editorCallback: async (editor: Editor) => {
try {
const blockHeight: string = await new Stamp().blockHeight(this.settings.formats.blockHeight, this.settings.blockExplorer);
insertAtCursor(blockHeight, editor);
} catch (error) {
console.error(error);
new Notice('🛑 An error occurred while adding the stamp.');
}
}
});
this.addCommand({
id: 'insert-current-moscow-time',
name: 'Insert current Moscow time',
editorCallback: async (editor: Editor) => {
try {
const moscowTime: string = await new Stamp().moscowTime(this.settings.formats.moscowTime);
insertAtCursor(moscowTime, editor);
} catch (error) {
console.error(error);
new Notice('🛑 An error occurred while adding the stamp.');
}
}
});
this.addCommand({
id: 'insert-current-moscow-time-at-block-height',
name: 'Insert current Moscow time @ block height',
editorCallback: async (editor: Editor) => {
try{
const moscowTimeAtBlockHeight: string = await new Stamp().moscowTimeAtBlockHeight(this.settings.formats.moscowTime, this.settings.formats.blockHeight, this.settings.blockExplorer);
insertAtCursor(moscowTimeAtBlockHeight, editor);
} catch (error) {
console.error(error);
new Notice('🛑 An error occurred while adding the stamp.');
}
}
});
this.addCommand({
id: 'replace-stamp-placeholders',
name: 'Replace stamp placeholders',
callback: () => {
try {
const activeFile = this.app.workspace.getActiveFile();
this.replaceStampPlaceholders(activeFile as TFile);
} catch (error) {
console.error(error);
new Notice('🛑 An error occurred while replacing stamp placeholders.');
}
}
});
this.app.workspace.onLayoutReady(() => {
this.app.vault.on('create', (file: TFile) => {
try {
this.replaceStampPlaceholders(file);
} catch (error) {
console.error(error);
new Notice('🛑 An error occurred while replacing stamp placeholders.');
}
})
})
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async replaceStampPlaceholders (file: TFile) {
let replacements: Replacements = {};
const stamp = new Stamp();
replacements = {
[this.settings.placeholders.blockHeight]: await stamp.blockHeight(this.settings.formats.blockHeight, this.settings.blockExplorer),
[this.settings.placeholders.moscowTime]: await stamp.moscowTime(this.settings.formats.moscowTime),
[this.settings.placeholders.moscowTimeAtBlockHeight]: await stamp.moscowTimeAtBlockHeight(this.settings.formats.moscowTime, this.settings.formats.blockHeight, this.settings.blockExplorer)
}
replacePlaceholders(this.app.vault, file, replacements);
}
}