forked from petr-nazarov/obsidian-autoscroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
146 lines (122 loc) · 3.89 KB
/
Copy pathmain.ts
File metadata and controls
146 lines (122 loc) · 3.89 KB
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
import { App, Editor, Notice, Plugin, PluginSettingTab, Setting, addIcon, MarkdownView } from 'obsidian';
// Remember to rename these classes and interfaces!
interface AutoScrollSettings {
speed: string;
}
const DEFAULT_SETTINGS: AutoScrollSettings = {
speed: '0.2'
}
const allowedSpeeds = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2];
const ribbonActiveClassName = 'autoscroll-ribbon-active';
export default class AutoScrollPlugin extends Plugin {
settings: AutoScrollSettings;
active: boolean = false;
intervalId: number;
ribbonIconEl: HTMLElement;
currentTop: number = 0;
nextTop: number =0;
/**
* The ammount of pixels to pass in 10 ms
*/
speed: number
private stopScroll(text: string = 'Stopping Auto Scroller') {
window.clearInterval(this.intervalId);
this.active = false;
this.ribbonIconEl.removeClass(ribbonActiveClassName);
new Notice(text);
}
private performScroll() {
const view = app.workspace.getActiveViewOfType(MarkdownView);
if(view){
const editor = view.editor;
const {top, left} = editor.getScrollInfo();
// console.log({
// currentTop: this.currentTop,
// nextTop :this.nextTop,
// })
if (this.nextTop - this.currentTop > 1) {
// console.log('scrolling')
editor.scrollTo(left, this.nextTop);
const {top:newTop, left: newLeft} = editor.getScrollInfo();
this.currentTop = newTop;
if (newTop === this.nextTop) {
this.stopScroll('Scrolled to the end!');
}
} else{
this.currentTop = top;
this.nextTop += this.speed;
}
}
}
private increaseSpeed() {
const currentSpeedIndex = allowedSpeeds.indexOf(this.speed) || allowedSpeeds.indexOf(0.5);
if(currentSpeedIndex === allowedSpeeds.length - 1) {
this.speed = allowedSpeeds[0];
} else {
this.speed = allowedSpeeds[currentSpeedIndex + 1];
}
new Notice('Setting speed to ' + this.speed);
}
async onload() {
this.active = false;
await this.loadSettings();
this.speed = parseFloat(this.settings.speed);
// This creates an icon in the left ribbon.
this.ribbonIconEl = this.addRibbonIcon('double-down-arrow-glyph', `Auto Scroller (speed ${this.speed})`, (evt: MouseEvent) => {
// Clicked with left mouse button
if (evt.button === 0) {
const currentState = this.active;
if (currentState) {
this.stopScroll()
} else{
new Notice('Starting Auto Scroller');
this.ribbonIconEl.addClass(ribbonActiveClassName);
this.active = true;
this.speed = parseFloat(this.settings.speed);
this.intervalId = this.registerInterval(window.setInterval(() => this.performScroll(), 10));
}
} else{
// Clicked with right mouse button
this.increaseSpeed();
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new AutoScrollSettingTab(this.app, this));
}
onunload() {
window.clearInterval(this.intervalId);
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class AutoScrollSettingTab extends PluginSettingTab {
plugin: AutoScrollPlugin;
constructor(app: App, plugin: AutoScrollPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
const speedOptions = allowedSpeeds.reduce((acc, speed) => {
const strSpeed = `${speed}`
acc[strSpeed] = strSpeed;
return acc;
}, {} as Record<string, string>);
containerEl.createEl('h2', {text: 'Settings for Autoscroll Plugin'});
new Setting(containerEl)
.setName('Default scrolling speed')
.setDesc('The amount of pixels to pass in 10 ms')
.addDropdown((dropdown) => dropdown.addOptions(speedOptions)
.onChange(async (value) => {
// console.log('Secret: ' + value);
this.plugin.settings.speed = value;
await this.plugin.saveSettings();
})
)
}
}