Skip to content

Commit 0d9d851

Browse files
committed
feat: add scroll acceleration
1 parent b44de56 commit 0d9d851

File tree

6 files changed

+100
-57
lines changed

6 files changed

+100
-57
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The commands itself do nothing, so use the hotkeys to scroll the editor.
1111

1212
![Editor Scroll Commands Hotkeys](media/editor-scroll-commands-hotkeys.png "Editor Scroll Commands Hotkeys")
1313

14-
The settings allow you to change the scroll offset and the interval.
14+
The settings allow you to change the scroll offset, the interval, and some acceleration parameters.
1515

1616
![Editor Scroll Commands Settings](media/editor-scroll-commands-settings.png "Editor Scroll Commands Settings")
1717

@@ -26,4 +26,4 @@ The settings allow you to change the scroll offset and the interval.
2626

2727
- Copy over `main.js`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/obsidian-editor-scroll-commands/`.
2828

29-
See [latest release](https://github.com/PhoSor/obsidian-editor-scroll-commands/releases/latest) in **Releases** section.
29+
See the [latest release](https://github.com/PhoSor/obsidian-editor-scroll-commands/releases/latest) in the **Releases** section.

main.ts

Lines changed: 92 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import { Platform, App, MarkdownFileInfo, Hotkey, Plugin, PluginSettingTab, Setting } from 'obsidian';
22

3-
const PluginId = 'editor-scroll-commands';
4-
const ScrollUpCommandId = 'scroll-up';
5-
const ScrollDownCommandId = 'scroll-down';
3+
const PLUGIN_ID = 'editor-scroll-commands';
4+
const SCROLL_UP_COMMAND_ID = 'scroll-up';
5+
const SCROLL_DOWN_COMMAND_ID = 'scroll-down';
66

77
interface EditorScrollCommandsSettings {
88
offset: number;
99
interval: number;
10+
accelerate: boolean;
11+
accelRate: number;
12+
maxAccel: number;
1013
}
1114

1215
const DEFAULT_SETTINGS: EditorScrollCommandsSettings = {
13-
offset: 11,
14-
interval: 8,
16+
offset: 2,
17+
interval: 5,
18+
accelerate: true,
19+
accelRate: 0.024,
20+
maxAccel: 4,
1521
}
1622

17-
const ModToEventProp = {
23+
const MODIFIER_TO_EVENT_PROP = {
1824
'Alt': 'altKey',
1925
'Shift': 'shiftKey',
2026
'Ctrl': 'ctrlKey',
@@ -26,11 +32,13 @@ export default class EditorScrollCommandsPlugin extends Plugin {
2632
settings: EditorScrollCommandsSettings;
2733
intervalId: number;
2834
editorScrolling: MarkdownFileInfo | null;
35+
scrollCoef = 1;
2936

3037
clearInterval() {
3138
window.clearInterval(this.intervalId);
3239
this.intervalId = 0;
3340
this.editorScrolling = null;
41+
this.scrollCoef = 1;
3442
}
3543

3644
scroll(offset: number) {
@@ -40,12 +48,12 @@ export default class EditorScrollCommandsPlugin extends Plugin {
4048
if (!this.intervalId) {
4149
this.editorScrolling = this.app.workspace.activeEditor;
4250
this.intervalId = window.setInterval(() => {
43-
if (this.app.workspace.activeEditor != this.editorScrolling) {
44-
this.clearInterval();
45-
return;
51+
if (this.settings.accelerate) {
52+
this.scrollCoef += ((this.scrollCoef < this.settings.maxAccel) ?
53+
this.settings.accelRate : 0);
4654
}
4755
// @ts-expect-error
48-
editor?.cm.scrollDOM.scrollBy(0, offset);
56+
editor?.cm.scrollDOM.scrollBy(0, offset * this.scrollCoef);
4957
}, this.settings.interval);
5058
this.registerInterval(this.intervalId);
5159
}
@@ -60,18 +68,16 @@ export default class EditorScrollCommandsPlugin extends Plugin {
6068
}
6169

6270
isScrollHotkeyEvent(event: KeyboardEvent, hotkeys: Hotkey[]) {
63-
return hotkeys.some((hotkey: Hotkey) => {
71+
return hotkeys?.some((hotkey: Hotkey) => {
6472
let keyMatched =
6573
(hotkey.key == event.code) ||
6674
('Key' + hotkey.key == event.code);
6775

6876
let allModsMatched = hotkey.modifiers.every(m => {
69-
let propName = ModToEventProp[m];
77+
let modifierPressed = MODIFIER_TO_EVENT_PROP[m];
7078

7179
// @ts-expect-error
72-
if (!event[propName]) { return false; }
73-
74-
return true;
80+
return event[modifierPressed];
7581
});
7682

7783
return keyMatched && allModsMatched;
@@ -80,18 +86,14 @@ export default class EditorScrollCommandsPlugin extends Plugin {
8086

8187
isScrollUpHotkeyEvent(event: KeyboardEvent) {
8288
// @ts-expect-error
83-
let scrollUpHotkeys = this.app.hotkeyManager.customKeys[`${PluginId}:${ScrollUpCommandId}`];
84-
85-
if (!scrollUpHotkeys) { return false; }
89+
let scrollUpHotkeys = this.app.hotkeyManager.customKeys[`${PLUGIN_ID}:${SCROLL_UP_COMMAND_ID}`];
8690

8791
return this.isScrollHotkeyEvent(event, scrollUpHotkeys);
8892
}
8993

9094
isScrollDownHotkeyEvent(event: KeyboardEvent) {
9195
// @ts-expect-error
92-
let scrollDownHotkeys = this.app.hotkeyManager.customKeys[`${PluginId}:${ScrollDownCommandId}`];
93-
94-
if (!scrollDownHotkeys) { return false; }
96+
let scrollDownHotkeys = this.app.hotkeyManager.customKeys[`${PLUGIN_ID}:${SCROLL_DOWN_COMMAND_ID}`];
9597

9698
return this.isScrollHotkeyEvent(event, scrollDownHotkeys);
9799
}
@@ -100,13 +102,13 @@ export default class EditorScrollCommandsPlugin extends Plugin {
100102
await this.loadSettings();
101103

102104
this.addCommand({
103-
id: ScrollUpCommandId,
105+
id: SCROLL_UP_COMMAND_ID,
104106
name: 'Scroll up',
105107
editorCallback: () => {},
106108
});
107109

108110
this.addCommand({
109-
id: ScrollDownCommandId,
111+
id: SCROLL_DOWN_COMMAND_ID,
110112
name: 'Scroll down',
111113
editorCallback: () => {},
112114
});
@@ -170,32 +172,72 @@ class EditorScrollCommandsSettingTab extends PluginSettingTab {
170172

171173
containerEl.empty();
172174

173-
new Setting(containerEl)
174-
.setHeading()
175-
.setName('Editor Scroll Commands');
176-
177-
new Setting(containerEl)
178-
.setName('Scroll offset')
179-
.setDesc('The number of pixels to scroll per interval, px')
180-
.addText(text => text
181-
.setPlaceholder('Enter the scroll offset number')
182-
.setValue(this.plugin.settings.offset?.toString())
183-
.onChange(async (value) => {
184-
this.plugin.settings.offset = value ?
185-
parseInt(value, 10) : DEFAULT_SETTINGS.offset;
186-
await this.plugin.saveSettings();
187-
}));
188-
189-
new Setting(containerEl)
190-
.setName('Scroll interval')
191-
.setDesc('The scroll offset interval in milliseconds, ms')
192-
.addText(text => text
193-
.setPlaceholder('Enter the interval amount')
194-
.setValue(this.plugin.settings.interval?.toString())
195-
.onChange(async (value) => {
196-
this.plugin.settings.interval = value ?
197-
parseInt(value, 10) : DEFAULT_SETTINGS.interval;
198-
await this.plugin.saveSettings();
199-
}));
175+
new Setting(containerEl).
176+
setName('Scroll offset').
177+
setDesc('The number of pixels to scroll per interval, px').
178+
addText(text => text.
179+
setPlaceholder('Enter the scroll offset number').
180+
setValue(this.plugin.settings.offset?.toString()).
181+
onChange(async (value) => {
182+
this.plugin.settings.offset =
183+
parseInt(value, 10) || DEFAULT_SETTINGS.offset;
184+
await this.plugin.saveSettings();
185+
})
186+
);
187+
188+
new Setting(containerEl).
189+
setName('Scroll interval').
190+
setDesc('The scroll offset interval in milliseconds, ms').
191+
addText(text => text.
192+
setPlaceholder('Enter the interval amount').
193+
setValue(this.plugin.settings.interval?.toString()).
194+
onChange(async (value) => {
195+
this.plugin.settings.interval =
196+
parseInt(value, 10) || DEFAULT_SETTINGS.interval;
197+
await this.plugin.saveSettings();
198+
})
199+
);
200+
201+
new Setting(containerEl).
202+
setHeading().
203+
setName('Scroll acceleration');
204+
205+
new Setting(containerEl).
206+
setName('Enable scroll acceleration').
207+
addToggle(text => text.
208+
setValue(this.plugin.settings.accelerate).
209+
onChange(async (value) => {
210+
this.plugin.settings.accelerate = value;
211+
await this.plugin.saveSettings();
212+
if (increaseRate && maxRate) {
213+
increaseRate.setDisabled(!value);
214+
maxRate.setDisabled(!value);
215+
}
216+
})
217+
);
218+
219+
let increaseRate = new Setting(containerEl).
220+
setName('Scroll rate increase').
221+
setDesc('The step of the increase of the scroll acceleration').
222+
addText(text => text.
223+
setValue(this.plugin.settings.accelRate.toString()).
224+
onChange(async (value) => {
225+
this.plugin.settings.accelRate =
226+
parseFloat(value) || DEFAULT_SETTINGS.accelRate;
227+
await this.plugin.saveSettings();
228+
})
229+
);
230+
231+
let maxRate = new Setting(containerEl).
232+
setName('Max scroll rate').
233+
setDesc('Max rate of the scroll acceleration').
234+
addText(text => text.
235+
setValue(this.plugin.settings.maxAccel.toString()).
236+
onChange(async (value) => {
237+
this.plugin.settings.maxAccel =
238+
parseFloat(value) || DEFAULT_SETTINGS.maxAccel;
239+
await this.plugin.saveSettings();
240+
})
241+
);
200242
}
201243
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "editor-scroll-commands",
33
"name": "Editor Scroll Commands",
4-
"version": "1.0.3",
4+
"version": "1.1.0",
55
"minAppVersion": "0.15.0",
66
"description": "Add scroll commands for editor, so you can assign hotkeys.",
77
"author": "Andrey Sorokin <[email protected]>",
14 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "editor-scroll-commands",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "Add scroll commands for editor, so you can assign hotkeys.",
55
"main": "main.js",
66
"scripts": {

versions.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2-
"1.0.0": "0.15.0",
3-
"1.0.3": "0.15.0"
4-
}
2+
"1.0.0": "0.15.0",
3+
"1.0.3": "0.15.0",
4+
"1.1.0": "0.15.0"
5+
}

0 commit comments

Comments
 (0)