generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.ts
103 lines (87 loc) · 2.77 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
import { App, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { helix } from 'codemirror-helix';
import { Extension, Prec } from '@codemirror/state';
interface HelixSettings {
enableHelixKeybindings: boolean;
cursorInInsertMode: "block" | "bar";
}
const DEFAULT_SETTINGS: HelixSettings = {
enableHelixKeybindings: false,
// Following the defualt Obsidian behavior, instead of the Helix one.
cursorInInsertMode: "bar",
}
export default class HelixPlugin extends Plugin {
settings: HelixSettings;
extensions: Extension[]
async onload() {
await this.loadSettings();
this.extensions = [];
this.addSettingTab(new HelixSettingsTab(this.app, this));
this.setEnabled(this.settings.enableHelixKeybindings, false);
this.registerEditorExtension(this.extensions);
this.addCommand({
id: "toggle-helix-keybindings",
name: "Toggle Helix keybindings",
callback: async () => this.setEnabled(!this.settings.enableHelixKeybindings, true, true),
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async setEnabled(value: boolean, reload: boolean = true, print: boolean = false) {
this.settings.enableHelixKeybindings = value;
this.extensions.length = 0;
if (value) this.extensions.push(Prec.high(helix({
config: {
"editor.cursor-shape.insert": this.settings.cursorInInsertMode,
}
})));
await this.saveSettings();
if (reload) this.app.workspace.updateOptions();
if (print) {
const msg = value ? "Enabled" : "Disabled";
new Notice(`${msg} Helix keybindings`);
}
}
async reload() {
await this.setEnabled(this.settings.enableHelixKeybindings);
}
}
class HelixSettingsTab extends PluginSettingTab {
plugin: HelixPlugin;
constructor(app: App, plugin: HelixPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("p", { text: "Vim keybindings must be disabled for the plugin to work" });
new Setting(containerEl)
.setName('Enable Helix keybindings')
.addToggle(async (value) => {
value
.setValue(this.plugin.settings.enableHelixKeybindings)
.onChange(async (value) => this.plugin.setEnabled(value))
});
new Setting(containerEl)
.setName('Cursor in insert mode')
.addDropdown(dropDown => {
dropDown.addOption('block', 'Block');
dropDown.addOption('bar', 'Bar');
dropDown.setValue(this.plugin.settings.cursorInInsertMode)
dropDown.onChange(async (value) => {
if (value == "block" || value == "bar") {
this.plugin.settings.cursorInInsertMode = value;
await this.plugin.saveSettings();
await this.plugin.reload();
}
});
});
}
}