-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
93 lines (74 loc) · 2.54 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
// @ts-nocheck
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface MyPluginSettings {
lines: number;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
lines: 1
}
//From the default stylesheet. Themes can probably change this. It's not that big of an issue.
const LINE_HEIGHT = 1.5
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
this.addSettingTab(new MySettingTab(this.app, this));
await this.loadSettings();
this.registerDomEvent(document, 'keydown', (ev: KeyboardEvent) => {
let isFocused = document.querySelector('.cm-editor.cm-focused')
if (!isFocused) return
let fontSize = this.app.vault.getConfig("baseFontSize") //Not public API. But someone on the discord suggested.
let editor = this.app.workspace.activeEditor?.editor
if (!editor) return
switch (ev.code) {
case 'ArrowUp':
if (ev.ctrlKey) {
ev.preventDefault()
//Don't need to bother calculating extreme values. The editor stops scrolling automatically at its limits.
let scrollPos = editor.getScrollInfo().top - this.settings.lines * fontSize * LINE_HEIGHT
editor.scrollTo(null, scrollPos)
}
break;
case 'ArrowDown':
if (ev.ctrlKey) {
ev.preventDefault()
let scrollPos = editor.getScrollInfo().top + this.settings.lines * fontSize * LINE_HEIGHT
editor.scrollTo(null, scrollPos)
}
break;
default:
break;
}
})
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class MySettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
//Class is recreated every time you visit the settings so it should always be correct.
const currentLineHeight = this.app.vault.getConfig("baseFontSize") * LINE_HEIGHT
new Setting(containerEl)
.setName("Lines")
.setDesc(`How many lines does each keypress scroll? It isn't entirely accurate (some lines are bigger) and themes might break this slightly, but eh ¯\\_(ツ)_/¯. The current base line height is ${currentLineHeight} pixels.`)
.addText(text => text
.setPlaceholder("Number of lines")
.setValue(this.plugin.settings.lines.toString())
.onChange(async (value) => {
this.plugin.settings.lines = Number(value);
await this.plugin.saveSettings();
}));
}
}