From 0b9962c6c63aab4bbf546b4d594960ecdbfdd733 Mon Sep 17 00:00:00 2001 From: John McLear Date: Mon, 28 Dec 2020 14:12:24 +0000 Subject: [PATCH] working going to end of the line and beginning of first --- src/static/js/ace2_inner.js | 36 +++++++++++++++++++++++------------- src/static/js/scroll.js | 3 ++- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index a2fd165a8..453b64c89 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -1,3 +1,4 @@ +'use strict'; /** * This code is mostly from the old Etherpad. Please help us to comment this code. * This helps other people to understand this code better and helps them to improve it. @@ -3059,32 +3060,41 @@ function Ace2Inner() { const linesLength = rep.lines.length(); if (isPageUp) { - top.console.log('page up'); // go to the top of the visible content - rep.selStart[0] -= visibleLineRange[1] - visibleLineRange[0] + 1; - rep.selEnd[0] -= visibleLineRange[1] - visibleLineRange[0] + 1; - // if the new rep is beyond the viewport, put the caret on the last line - if (rep.selStart[0] < 0) { + rep.selStart[0] -= visibleLineRange[1] - visibleLineRange[0]; + rep.selEnd[0] -= visibleLineRange[1] - visibleLineRange[0]; + // if the new rep is beyond the viewport or first line, + if (rep.selStart[0] <= 0) { + // put the caret on the first line in the first position rep.selStart = [0, 0]; rep.selEnd = [0, 0]; } } if (isPageDown) { - top.console.log('pag edown'); // go to the bottom of the last visible content if (rep.selStart[0] === 0) { rep.selStart[0] += visibleLineRange[1] - visibleLineRange[0] - 1; rep.selEnd[0] += visibleLineRange[1] - visibleLineRange[0] - 1; } else { - rep.selStart[0] += visibleLineRange[1] - visibleLineRange[0]; - rep.selEnd[0] += visibleLineRange[1] - visibleLineRange[0]; + rep.selStart[0] = visibleLineRange[1] - visibleLineRange[0]; + rep.selEnd[0] = visibleLineRange[1] - visibleLineRange[0]; } - // if the new rep is beyond the viewport, put the caret on the last line - if (rep.selStart[0] > linesLength) { - top.console.log('beyond scope'); - rep.selStart = [linesLength - 1, 0]; - rep.selEnd = [linesLength - 1, 0]; + if (rep.selStart[0] === -1) rep.selStart[0] = 0; + if (rep.selEnd[0] === -1) rep.selEnd[0] = 0; + // if the new rep is beyond the viewport + // put the caret on the last line at the end of the line + if (rep.selStart[0] >= (linesLength - 1)) { + // need current character length of line + const line = rep.lines.atIndex(rep.selEnd[0]); + let lineLength; + if (line) { // need to test if this is needed or not. + lineLength = line.width; + } else { + lineLength = 0; + } + rep.selStart = [linesLength - 1, lineLength]; + rep.selEnd = [linesLength - 1, lineLength]; } // TODO: Handle if character is X offset from content } diff --git a/src/static/js/scroll.js b/src/static/js/scroll.js index 1fd77cdfc..e69111912 100644 --- a/src/static/js/scroll.js +++ b/src/static/js/scroll.js @@ -365,7 +365,8 @@ Scroll.prototype.getVisibleLineRange = function (rep) { Scroll.prototype.getVisibleCharRange = function (rep) { const lineRange = this.getVisibleLineRange(rep); - // top.console.log(rep.lines); + // top.console.log('char range', 0, rep.lines.offsetOfIndex(lineRange[0])); + // top.console.log('char range', 1, rep.lines.offsetOfIndex(lineRange[1])); return [rep.lines.offsetOfIndex(lineRange[0]), rep.lines.offsetOfIndex(lineRange[1])]; };