1
1
import { Platform , App , MarkdownFileInfo , Hotkey , Plugin , PluginSettingTab , Setting } from 'obsidian' ;
2
2
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' ;
6
6
7
7
interface EditorScrollCommandsSettings {
8
8
offset : number ;
9
9
interval : number ;
10
+ accelerate : boolean ;
11
+ accelRate : number ;
12
+ maxAccel : number ;
10
13
}
11
14
12
15
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 ,
15
21
}
16
22
17
- const ModToEventProp = {
23
+ const MODIFIER_TO_EVENT_PROP = {
18
24
'Alt' : 'altKey' ,
19
25
'Shift' : 'shiftKey' ,
20
26
'Ctrl' : 'ctrlKey' ,
@@ -26,11 +32,13 @@ export default class EditorScrollCommandsPlugin extends Plugin {
26
32
settings : EditorScrollCommandsSettings ;
27
33
intervalId : number ;
28
34
editorScrolling : MarkdownFileInfo | null ;
35
+ scrollCoef = 1 ;
29
36
30
37
clearInterval ( ) {
31
38
window . clearInterval ( this . intervalId ) ;
32
39
this . intervalId = 0 ;
33
40
this . editorScrolling = null ;
41
+ this . scrollCoef = 1 ;
34
42
}
35
43
36
44
scroll ( offset : number ) {
@@ -40,12 +48,12 @@ export default class EditorScrollCommandsPlugin extends Plugin {
40
48
if ( ! this . intervalId ) {
41
49
this . editorScrolling = this . app . workspace . activeEditor ;
42
50
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 ) ;
46
54
}
47
55
// @ts -expect-error
48
- editor ?. cm . scrollDOM . scrollBy ( 0 , offset ) ;
56
+ editor ?. cm . scrollDOM . scrollBy ( 0 , offset * this . scrollCoef ) ;
49
57
} , this . settings . interval ) ;
50
58
this . registerInterval ( this . intervalId ) ;
51
59
}
@@ -60,18 +68,16 @@ export default class EditorScrollCommandsPlugin extends Plugin {
60
68
}
61
69
62
70
isScrollHotkeyEvent ( event : KeyboardEvent , hotkeys : Hotkey [ ] ) {
63
- return hotkeys . some ( ( hotkey : Hotkey ) => {
71
+ return hotkeys ? .some ( ( hotkey : Hotkey ) => {
64
72
let keyMatched =
65
73
( hotkey . key == event . code ) ||
66
74
( 'Key' + hotkey . key == event . code ) ;
67
75
68
76
let allModsMatched = hotkey . modifiers . every ( m => {
69
- let propName = ModToEventProp [ m ] ;
77
+ let modifierPressed = MODIFIER_TO_EVENT_PROP [ m ] ;
70
78
71
79
// @ts -expect-error
72
- if ( ! event [ propName ] ) { return false ; }
73
-
74
- return true ;
80
+ return event [ modifierPressed ] ;
75
81
} ) ;
76
82
77
83
return keyMatched && allModsMatched ;
@@ -80,18 +86,14 @@ export default class EditorScrollCommandsPlugin extends Plugin {
80
86
81
87
isScrollUpHotkeyEvent ( event : KeyboardEvent ) {
82
88
// @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 } ` ] ;
86
90
87
91
return this . isScrollHotkeyEvent ( event , scrollUpHotkeys ) ;
88
92
}
89
93
90
94
isScrollDownHotkeyEvent ( event : KeyboardEvent ) {
91
95
// @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 } ` ] ;
95
97
96
98
return this . isScrollHotkeyEvent ( event , scrollDownHotkeys ) ;
97
99
}
@@ -100,13 +102,13 @@ export default class EditorScrollCommandsPlugin extends Plugin {
100
102
await this . loadSettings ( ) ;
101
103
102
104
this . addCommand ( {
103
- id : ScrollUpCommandId ,
105
+ id : SCROLL_UP_COMMAND_ID ,
104
106
name : 'Scroll up' ,
105
107
editorCallback : ( ) => { } ,
106
108
} ) ;
107
109
108
110
this . addCommand ( {
109
- id : ScrollDownCommandId ,
111
+ id : SCROLL_DOWN_COMMAND_ID ,
110
112
name : 'Scroll down' ,
111
113
editorCallback : ( ) => { } ,
112
114
} ) ;
@@ -170,32 +172,72 @@ class EditorScrollCommandsSettingTab extends PluginSettingTab {
170
172
171
173
containerEl . empty ( ) ;
172
174
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
+ ) ;
200
242
}
201
243
}
0 commit comments