forked from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
340 lines (315 loc) · 9.83 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
class HotKey {
public meta: boolean;
public shift: boolean;
public ctrl: boolean;
public alt: boolean;
public key: string;
public constructor (dict) {
this.meta = dict.meta;
this.shift = dict.shift;
this.ctrl = dict.ctrl;
this.alt = dict.alt;
this.key = dict.key.toLowerCase();
}
public toString(): string {
var name = "";
if (this.alt == true) {
name += "A-";
}
if (this.ctrl == true) {
name += "C-";
}
if (this.meta == true) {
name += "M-";
}
if (this.shift == true) {
name += "S-";
}
return name + this.key;
}
}
class Chord {
public sequence: HotKey[];
public command: string;
public constructor (dict) {
this.sequence = dict.sequence;
this.command = dict.command;
}
// Check if other is a prefix of the current Chord
// Returns: "NO", "YES", "FULL" when it is a complete match
public checkPrefix(other: HotKey[]): string {
for (const [index, hotkey] of this.sequence.entries()) {
let otherkey = other[index];
if (otherkey === undefined) {
return "YES";
}
if ((hotkey.meta != otherkey.meta) ||
(hotkey.shift != otherkey.shift) ||
(hotkey.ctrl != otherkey.ctrl) ||
(hotkey.alt != otherkey.alt) ||
(hotkey.key != otherkey.key)) {
return "NO";
}
}
return "FULL";
}
public chordToString(): string {
return this.sequence.map(hk => hk.toString()).join(" ");
}
}
interface Settings {
hotkeys: Chord[];
}
const DEFAULT_SETTINGS: Settings = {
hotkeys: [
new Chord({ sequence: [
new HotKey({ key: 'x', meta: false, shift: false, ctrl: true, alt: false }),
new HotKey({ key: '3', meta: false, shift: false, ctrl: false, alt: false }),
], command: "workspace:split-vertical" }),
new Chord({ sequence: [
new HotKey({ key: 'x', meta: false, shift: false, ctrl: true, alt: false }),
new HotKey({ key: '2', meta: false, shift: false, ctrl: false, alt: false }),
], command: "workspace:split-horizontal" }),
]
}
export default class HotkeysChordPlugin extends Plugin {
public settings: Settings;
private statusbar; // Points to our custom status bar
private currentseq: HotKey[]; // List of currently pressed chords
async onload() {
// Convert each data to Chord and to HotKey
var data = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
data.hotkeys = data.hotkeys.map(chord => new Chord({
command: chord.command,
sequence: chord.sequence.map(hotkey => new HotKey(hotkey)),
}));
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
this.statusbar = this.addStatusBarItem();
this.currentseq = [];
this.updateStatusBar();
this.addSettingTab(new HotkeysChordPluginSettingsTab(this.app, this));
window.addEventListener("keydown", this.handleKeyDown, {capture: true});
}
async onunload() {
window.removeEventListener("keydown", this.handleKeyDown);
}
async saveSettings() {
await this.saveData(this.settings);
}
private updateStatusBar(): void {
var chord = "None";
if (this.currentseq.length > 0) {
chord = this.currentseq.map(hk => hk.toString()).join(" ");
}
this.statusbar.setText("Chord: " + chord);
}
private readonly handleKeyDown = (
event: KeyboardEvent,
cm: CodeMirror.Editor,
) => {
if (event.key === 'Shift' || event.key === 'Meta' || event.key === 'Control' || event.key == 'Alt') {
console.debug("Skipping meta key: " + event.key);
return;
}
// Add the pressed keys to the current sequence and update on-screen
let hotkey = new HotKey ({
key: event.key,
shift: event.shiftKey,
meta: event.metaKey,
ctrl: event.ctrlKey,
alt: event.altKey,
});
this.currentseq.push(hotkey);
// We check whether the current sequence can be found in the hotkey database
var partialMatch = false;
for (let chord of this.settings.hotkeys) {
let result = chord.checkPrefix(this.currentseq);
if (result == "FULL") {
(this.app as any).commands.executeCommandById(chord.command);
event.preventDefault();
event.stopPropagation();
// new Notice(`Chord triggered ${chord.command}`);
partialMatch = false;
break;
} else if (result == "YES") {
partialMatch = true;
}
}
// We also want to prevent default if this is a key inside a previous sequence or a partial match
if ((this.currentseq.length > 1) || partialMatch) {
event.preventDefault();
event.stopPropagation();
}
if (!partialMatch) {
// No patial match, we get back to zero sequence
this.currentseq = [];
}
this.updateStatusBar();
}
}
class HotkeysChordPluginSettingsTab extends PluginSettingTab {
private readonly plugin: HotkeysChordPlugin;
constructor(app: App, plugin: HotkeysChordPlugin) {
super(app, plugin);
this.plugin = plugin;
}
// Returns { name, id } for each of the application commands
private readonly generateCommandList = (app: App): Command[] => {
const commands: Command[] = [];
for (const [key, value] of Object.entries((app as any).commands.commands)) {
commands.push({ name: value.name, id: value.id });
}
return commands;
};
public display(): void {
const commands = this.generateCommandList(this.app);
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Hotkeys Chord Plugin - Settings' });
containerEl.createEl('p', { text: "Click on the buttons to change chords. Click again to end chord composition." });
containerEl.createEl('h3', { text: 'Existing Hotkeys Chords' });
// We do want to create a thing for each chord...
this.plugin.settings.hotkeys.forEach((chord, index) => {
new ChordSetting(containerEl, "existing", chord, commands, async state => {
if (state === undefined) {
this.plugin.settings.hotkeys.splice(index, 1); // Remove the element at index
await this.plugin.saveSettings();
} else {
this.plugin.settings.hotkeys[index] = state;
await this.plugin.saveSettings();
}
this.display();
});
});
containerEl.createEl('h3', { text: 'Create new Chord' });
// Important that it is a variable that is persisted across
var newchord;
if (newchord == undefined)
newchord = new Chord({ sequence: [], command: "invalid-placeholder" });
new ChordSetting(containerEl, "new", newchord, commands, async state => {
this.plugin.settings.hotkeys.push(state);
await this.plugin.saveSettings();
newchord = new Chord({ sequence: [], command: "invalid-placeholder" });
this.display();
});
}
}
class ChordSetting extends Setting {
private ctype;
private chord;
private commands;
private cb;
constructor (container, ctype, chord: Chord, commands, cb) {
super(container);
this.ctype = ctype;
this.chord = chord;
this.cb = cb;
this.commands = commands;
this.display();
}
public display(): void {
// Do a search on the given command
let cmdname = `${this.chord.command}`;
for (let command of this.commands) {
if (command.id == this.chord.command)
cmdname = command.name;
}
this.clear()
.addButton(btn => {
var state = "inactive";
var stopper = undefined;
var text = "";
if (this.chord.sequence.length > 0) {
text = this.chord.chordToString();
} else {
text = "Choose a Chord";
}
btn.setButtonText(text)
.setTooltip("Change the Chord")
.onClick(() => {
if (state === "inactive") {
state = "active";
btn.setButtonText("Type the Chord");
stopper = new ChordCapture((sequence, final) => {
if (final) {
this.chord.sequence = sequence;
if (this.ctype == "existing")
this.cb(this.chord);
else
this.display();
} else {
btn.setButtonText("*** " + sequence.map(hk => hk.toString()).join(" ") + " ***");
}
});
} else {
stopper();
state = "inactive";
}
});
})
.addDropdown(dropdown => {
dropdown.addOption("invalid-placeholder", "Select a Command");
this.commands.forEach(cmd => dropdown.addOption(cmd.id, cmd.name));
dropdown.onChange(newcmd => {
this.chord.command = newcmd;
if (this.ctype == "existing")
this.cb(this.chord);
else
this.display();
});
dropdown.setValue(this.chord.command);
})
.addExtraButton(btn => {
if (this.ctype == "existing") {
btn.setIcon("cross")
.setTooltip("Delete shortcut")
.onClick(() => {
this.cb(undefined);
});
} else {
btn.setIcon("enter")
.setTooltip("Add shortcut")
.onClick(() => {
if ((this.chord.command === "invalid-placeholder") ||
(this.chord.sequence.length == 0)) {
new Notice("Please choose both a command and a chord!!!");
} else {
this.cb(this.chord);
}
});
}
});
}
}
function ChordCapture (cb) {
var sequence = [];
var keydownhandler = event => {
if (["Shift", "Meta", "Alt", "Control"].contains(event.key))
return;
// We always want to remove further things for such events
event.preventDefault();
event.stopPropagation();
if (event.key === "Escape") {
document.removeEventListener("keydown", keydownhandler);
cb(sequence, true);
} else {
let hotkey = new HotKey({
key: event.key,
meta: event.metaKey,
ctrl: event.ctrlKey,
alt: event.altKey,
shift: event.shiftKey,
});
sequence.push(hotkey);
cb(sequence, false);
};
};
// Stop when this method is called
var stopper = () => {
document.removeEventListener("keydown", keydownhandler);
cb(sequence, true);
};
document.addEventListener("keydown", keydownhandler);
return stopper;
}