Skip to content

Commit

Permalink
add line breaking support
Browse files Browse the repository at this point in the history
  • Loading branch information
kjloveless committed Nov 20, 2024
1 parent 9128fee commit e4cd884
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,10 @@ function editorUpdateRow(at: number) {
}
}

function editorAppendRow(msg: string) {
const at = e.numRows;
e.row[at] = msg;
function editorInsertRow(at: number, msg: string) {
if (at < 0 || at > e.numRows) return;

e.row.splice(at, 0, msg);

editorUpdateRow(at);

Expand Down Expand Up @@ -250,12 +251,25 @@ function editorRowDelChar(row: number, at: number) {

function editorInsertChar(char: string) {
if (e.cursorY == e.numRows) {
editorAppendRow("");
editorInsertRow(e.numRows, "");
}
editorRowInsertChar(e.cursorY, e.cursorX, char);
e.cursorX++;
}

function editorInsertNewline() {
if (e.cursorX == 0) {
editorInsertRow(e.cursorY, "");
} else {
const row = e.row[e.cursorY];
editorInsertRow(e.cursorY + 1, row.slice(e.cursorX));
e.row[e.cursorY] = e.row[e.cursorY].slice(0, e.cursorX);
editorUpdateRow(e.cursorY);
}
e.cursorY++;
e.cursorX = 0;
}

function editorDelChar() {
if (e.cursorY == e.numRows) return;
if (e.cursorX == 0 && e.cursorY == 0) return;
Expand Down Expand Up @@ -304,7 +318,7 @@ function editorOpen(filename: string) {

let i = 0;
while (i < lines.length) {
editorAppendRow(lines[i]);
editorInsertRow(e.numRows, lines[i]);
i++;
}
e.dirty = 0;
Expand Down Expand Up @@ -472,7 +486,7 @@ async function editorProcessKeypress() {

switch (char) {
case "\r".charCodeAt(0):
// TODO
editorInsertNewline();
break;
case ctrlKey("q"):
if (e.dirty != 0 && e.quitTimes > 0) {
Expand Down

0 comments on commit e4cd884

Please sign in to comment.