edit.js: can move text cursor left/right

This commit is contained in:
minjaesong
2021-04-16 10:19:09 +09:00
parent 6862604562
commit 36e043fd16
2 changed files with 24 additions and 16 deletions

View File

@@ -16,12 +16,12 @@ let cursorRow = 0;
let cursorCol = 0;
let exit = false;
let scene = -1; // -1: main, 0: filemenu, 1: editmenu , ...
let bulletinShown = false;
// load existing file if it's there
let editingExistingFile = filesystem.open(driveLetter, filePath, "R");
if (editingExistingFile) {
textbuffer = filesystem.readAll(driveLetter).split("\n");
serial.println(textbuffer);
}
let windowWidth = con.getmaxyx()[1];
@@ -218,8 +218,11 @@ function hitAny() {
function appendText(code) {
if (textbuffer[cursorRow] === undefined)
textbuffer[cursorRow] = String.fromCharCode(code);
else
textbuffer[cursorRow] += String.fromCharCode(code);
else {
let s = textbuffer[cursorRow].substring(0);
textbuffer[cursorRow] = s.substring(0, cursorCol) + String.fromCharCode(code) + s.substring(cursorCol);
//textbuffer[cursorRow] += String.fromCharCode(code);
}
cursorCol += 1;
drawTextLine(cursorRow - scroll);
@@ -247,19 +250,20 @@ function appendLine() {
drawMain();
drawTextbuffer();
let bulletinShown = false;
// show "welcome" message
if (!editingExistingFile)
displayBulletin(`New File`);
else
else {
// move to right end of the first line
cursorCol = textbuffer[0].length;
drawLnCol();
displayBulletin(`Read ${textbuffer.length} Lines`);
}
while (!exit) {
let key = con.getch();
serial.println(`[edit.js] keycode = ${key}`);
if (bulletinShown) dismissBulletin();
if (key == 24) // Ctrl-X
exit = true;
@@ -272,11 +276,13 @@ while (!exit) {
else if (key == con.KEY_RETURN) { // Return
appendLine();
}
else if (key >= 32) { // printables (excludes \n)
if (bulletinShown) {
dismissBulletin();
}
else if (key == con.KEY_LEFT && cursorCol > 0) {
cursorCol -= 1; drawLnCol(); gotoText();
}
else if (key == con.KEY_RIGHT && cursorCol < textbuffer[cursorRow].length) {
cursorCol += 1; drawLnCol(); gotoText();
}
else if (key >= 32 && key < 128) { // printables (excludes \n)
appendText(key);
}
}

View File

@@ -3,17 +3,19 @@
let menu = [
{label:"BASIC",pwd:"\\",exec:"basic",args:""},
{label:"DOS",pwd:"\\",exec:"command",args:""},
{label:"TEXT",pwd:"\\home",exec:"undefined",args:""},
{label:"TEXT",pwd:"\\home",exec:"edit",args:""},
{label:"TELCOM",pwd:"\\home",exec:"undefined",args:""}
];
const MENU_COLS = 4;
const MENU_ROWS = 6;
const COL_SIZE = 10;
function redraw() {
con.clear();
for (let i = 0; i < MENU_COLS*6; i++) {
for (let i = 0; i < MENU_COLS*MENU_ROWS; i++) {
let m = menu[i];
con.move(2+((i/MENU_COLS)|0),2+((i%MENU_COLS)*10));
con.move(2+((i / MENU_COLS)|0),2+((i % MENU_COLS)*COL_SIZE));
print(m ? m.label : "-.-");
}
}