forked from deathau/cm-typewriter-scroll-obsidian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typewriter-scrolling.js
45 lines (45 loc) · 1.83 KB
/
typewriter-scrolling.js
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
// all credit to azu: https://github.com/azu/codemirror-typewriter-scrolling/blob/b0ac076d72c9445c96182de87d974de2e8cc56e2/typewriter-scrolling.js
"use strict";
CodeMirror.commands.scrollSelectionToCenter = function (cm) {
var cursor = cm.getCursor('head');
var charCoords = cm.charCoords(cursor, "local");
var top = charCoords.top;
var halfLineHeight = (charCoords.bottom - top) / 2;
var halfWindowHeight = cm.getWrapperElement().offsetHeight / 2;
var scrollTo = Math.round((top - halfWindowHeight + halfLineHeight));
cm.scrollTo(null, scrollTo);
};
CodeMirror.defineOption("typewriterScrolling", false, function (cm, val, old) {
if (old && old != CodeMirror.Init) {
const linesEl = cm.getScrollerElement().querySelector('.CodeMirror-lines');
linesEl.style.paddingTop = null;
linesEl.style.paddingBottom = null;
cm.off("cursorActivity", onCursorActivity);
cm.off("refresh", onRefresh);
}
if (val) {
onRefresh(cm);
cm.on("cursorActivity", onCursorActivity);
cm.on("refresh", onRefresh);
}
});
function onCursorActivity(cm) {
const linesEl = cm.getScrollerElement().querySelector('.CodeMirror-lines');
if (cm.getSelection().length !== 0) {
linesEl.classList.add("selecting");
}
else {
linesEl.classList.remove("selecting");
cm.execCommand("scrollSelectionToCenter");
console.log(cm.getOption('mode'));
}
}
function onRefresh(cm) {
const halfWindowHeight = cm.getWrapperElement().offsetHeight / 2;
const linesEl = cm.getScrollerElement().querySelector('.CodeMirror-lines');
linesEl.style.paddingTop = `${halfWindowHeight}px`;
linesEl.style.paddingBottom = `${halfWindowHeight}px`; // Thanks @walulula!
if (cm.getSelection().length === 0) {
cm.execCommand("scrollSelectionToCenter");
}
}