-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
150 lines (132 loc) · 3.46 KB
/
Copy pathmain.ts
File metadata and controls
150 lines (132 loc) · 3.46 KB
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
150
import {Editor, MarkdownView, Plugin} from 'obsidian';
export default class MyPlugin extends Plugin {
private symbolMode:boolean = false;
private cmEditors: CodeMirror.Editor[];
private symbols: { [key:string]:string;} = {
'a': 'alpha',
'b': 'beta',
'c': 'chi',
'd': 'delta',
'e': 'epsilon',
'f': 'phi',
'g': 'gamma',
'h': 'eta',
'i': 'iota',
'k': 'kappa',
'l': 'lambda',
'm': 'mu',
'n': 'nu',
'o': 'omicron',
'p': 'pi',
'q': 'theta',
'r': 'rho',
's': 'sigma',
't': 'tau',
'u': 'upsilon',
'w': 'omega',
'x': 'xi',
'y': 'psi',
'z': 'zeta',
};
private status: HTMLElement;
async onload() {
console.log('loading plugin');
this.cmEditors = [];
this.registerCodeMirror((cm) => {
this.cmEditors.push(cm);
// the callback has to be called through another function in order for 'this' to work
cm.on('keydown', (cm, event) => this.handleKeyDown(cm, event));
})
this.addCommand({
id:"insert-math",
name:"Insert math wrapper",
callback: () => this.insertMath(),
hotkeys: [
{
modifiers: ["Mod"],
key: "m"
}],
})
this.addCommand({
id:"insert-mhchem",
name:"Insert chemistry wrapper",
callback: () => this.insertChem(),
hotkeys: [
{
modifiers: ["Mod", "Alt"],
key: "m"
}]
})
this.status = this.addStatusBarItem()
this.status.setText('sym:[ ]')
this.addCommand({
id:"toggle-symbol-mode",
name:"Toggle symbol mode",
callback: () => this.toggleSymbolMode(),
hotkeys: [
{
modifiers: ["Mod"],
key: "w"
}]
})
}
private readonly handleKeyDown = (cm: CodeMirror.Editor, event: KeyboardEvent): void => {
if (this.symbolMode) {
let key = event.key.toLowerCase();
if (this.symbols.hasOwnProperty(key)) {
let symbol = this.symbols[key];
if (event.key.charAt(0) === event.key.charAt(0).toUpperCase()) {
symbol = symbol.charAt(0).toUpperCase() + symbol.slice(1);
}
this.insertSymbol(symbol);
event.preventDefault();
this.toggleSymbolMode();
};
};
}
insertSymbol(symbol: string): void {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
editor.replaceSelection(`\\${symbol}`);
}
toggleSymbolMode(): void {
this.symbolMode = !this.symbolMode;
this.symbolMode ? this.status.setText('symbol mode: on') : this.status.setText('symbol mode: off');
}
insertMath(): void {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
let selectedText = editor.somethingSelected()
? editor.getSelection()
: false;
if (selectedText) {
editor.replaceSelection(`\$${selectedText}\$`);
} else {
editor.replaceSelection("\$\$");
editor.exec("goLeft");
}
}
insertChem(): void {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
let selectedText = editor.somethingSelected()
? editor.getSelection()
: false;
if (selectedText) {
editor.replaceSelection(`\$\\ce{${selectedText}}\$`);
} else {
editor.replaceSelection("\$\\ce{}\$");
editor.exec("goLeft");
editor.exec("goLeft");
}
}
onunload() {
console.log('unloading symbols plugin');
this.cmEditors.forEach((cm) => {
cm.off('keydown', (cm, event) => this.handleKeyDown(cm, event));
})
}
}