edit.js: adding/deleting a text or a line works again

This commit is contained in:
minjaesong
2021-04-22 10:56:20 +09:00
parent 77ff22f968
commit e862edd67f

View File

@@ -185,8 +185,10 @@ function drawTextLineAbsolute(rowNumber, paintOffsetX) {
} }
} }
function drawTextbuffer() { function drawTextbuffer(from, toExclusive) {
for (let k = scroll; k < scroll + paintHeight; k++) { let lineStart = from || scroll;
let lineEnd = toExclusive || scroll + paintHeight;
for (let k = lineStart; k < lineEnd; k++) {
drawTextLineAbsolute(k, (k == cursorRow) ? scrollHor : 0); drawTextLineAbsolute(k, (k == cursorRow) ? scrollHor : 0);
} }
gotoText(); gotoText();
@@ -260,12 +262,10 @@ function appendLine() {
cursorCol = 0; cursorCol = 0;
cursoringCol = cursorCol; cursoringCol = cursorCol;
if (cursorRow >= windowHeight - scrollPeek) { if (cursorRow >= windowHeight - scrollPeek)
scroll += 1; scroll += 1;
drawLineNumbers();
}
drawTextbuffer(); drawTextbuffer(); drawLineNumbers();
} }
function backspaceOnce() { function backspaceOnce() {
@@ -274,13 +274,24 @@ function backspaceOnce() {
let s1 = textbuffer[cursorRow - 1]; let s1 = textbuffer[cursorRow - 1];
let s2 = textbuffer[cursorRow]; let s2 = textbuffer[cursorRow];
textbuffer.splice(cursorRow - 1, 2, s1+s2); textbuffer.splice(cursorRow - 1, 2, s1+s2);
cursorMoveRelative(0,-1); cursorMoveRelative(Number.MAX_SAFE_INTEGER, 0); cursorHorAbsolute(s1.length); // move cursor and possibly scrollHor
cursorMoveRelative(0,-1); cursorMoveRelative(Number.MAX_SAFE_INTEGER, 0);
cursorHorAbsolute(s1.length);
cursoringCol = cursorCol;
// end of repositioning
drawTextbuffer(cursorRow);
drawLineNumbers();
} }
// delete a character // delete a character
else if (cursorCol > 0) { else if (cursorCol > 0) {
let s = textbuffer[cursorRow].substring(0); let s = textbuffer[cursorRow].substring(0);
textbuffer[cursorRow] = s.substring(0, cursorCol - 1) + s.substring(cursorCol); textbuffer[cursorRow] = s.substring(0, cursorCol - 1) + s.substring(cursorCol);
// identical to con.KEY_LEFT
cursoringCol = cursorCol - 1;
if (cursoringCol < 0) cursoringCol = 0;
cursorMoveRelative(-1,0); cursorMoveRelative(-1,0);
// end of con.KEY_LEFT
drawTextLineAbsolute(cursorRow, scrollHor);
} }
} }
@@ -413,10 +424,11 @@ while (!exit) {
displayBulletin(`Wrote ${textbuffer.length} lines`); displayBulletin(`Wrote ${textbuffer.length} lines`);
} }
else if (key == con.KEY_BACKSPACE) { // Bksp else if (key == con.KEY_BACKSPACE) { // Bksp
backspaceOnce(); drawLnCol(); gotoText(); backspaceOnce();
drawLnCol(); gotoText();
} }
else if (key == con.KEY_RETURN) { // Return else if (key == con.KEY_RETURN) { // Return
appendLine(); drawLineNumbers(); drawLnCol(); gotoText(); appendLine(); drawLnCol(); gotoText();
} }
else if (key == con.KEY_LEFT) { else if (key == con.KEY_LEFT) {
cursoringCol = cursorCol - 1; cursoringCol = cursorCol - 1;