Skip to content

Commit

Permalink
modified scroll so it won't work on mouse selection.
Browse files Browse the repository at this point in the history
Instead, it specifically only works on 'up', 'down', 'left' and 'right'
keyboard presses (alongside any typing edits).
Also, changed the cursor position used for the calculation so that
shift+arrow selection is visible.
  • Loading branch information
gordonped committed May 10, 2021
1 parent b595057 commit eb375ca
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions typewriter-scrolling.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CodeMirror.commands.scrollSelectionToCenter = function (cm) {
if (cm.getOption("disableInput")) {
return CodeMirror.Pass;
}
var cursor = cm.getCursor('anchor');
var cursor = cm.getCursor('head');
var charCoords = cm.charCoords(cursor, "local");
var top = charCoords.top;
var halfLineHeight = (charCoords.bottom - top) / 2;
Expand All @@ -18,12 +18,14 @@ CodeMirror.defineOption("typewriterScrolling", false, function (cm, val, old) {
linesEl.style.paddingTop = null;
cm.off("changes", onChanges);
cm.off("cursorActivity", onCursorActivity);
cm.off("keyHandled", onKeyHandled);
cm.off("refresh", onRefresh);
}
if (val) {
onRefresh(cm);
cm.on("changes", onChanges);
cm.on("cursorActivity", onCursorActivity);
cm.on("keyHandled", onKeyHandled);
cm.on("refresh", onRefresh);
}
});
Expand All @@ -47,7 +49,6 @@ function onCursorActivity(cm) {
else {
linesEl.classList.remove("selecting")
}
cm.execCommand("scrollSelectionToCenter");
}
function onRefresh(cm) {
const halfWindowHeight = cm.getWrapperElement().offsetHeight / 2;
Expand All @@ -56,4 +57,10 @@ function onRefresh(cm) {
if (cm.getSelection().length === 0) {
cm.execCommand("scrollSelectionToCenter");
}
}
function onKeyHandled(cm, name, event) {
console.log(name);
if (name === "Up" || name === "Down" || name === "Left" || name === "Right") {
cm.execCommand("scrollSelectionToCenter");
}
}

0 comments on commit eb375ca

Please sign in to comment.