Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scripts/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export type Repo = {
files: RepoFile[];
}

export type History = {
lineCorrectness: boolean[];
line: string;
}

export const repoOptions: Repo[] = [
linuxRepo,
reactRepo,
Expand Down
21 changes: 21 additions & 0 deletions scripts/editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { backspaceKey, enterKey, tabKey } from "./keyboard";
import { separatorLine } from "./terminal";
import { History } from "./data/index"

const editorElement = document.getElementById("editor") as HTMLElement;
const statsElement = document.getElementById("stats") as HTMLElement;
Expand Down Expand Up @@ -68,6 +69,7 @@ export const startEditor = (code: string) => {
let correctCharacters = 0;
let pageLines = allLines.slice(firstLineIndex, linesPerPage);
let line = pageLines[lineIndex];
let lineHistory: History[] = []
let element = printCode(pageLines);
let startTime = 0;
let timeoutHandle = 0;
Expand Down Expand Up @@ -97,6 +99,7 @@ export const startEditor = (code: string) => {
}

const advanceLine = () => {
lineHistory.push({ lineCorrectness, line })
line = pageLines[++lineIndex];
charIndex = 0;
lineCorrectness = []
Expand All @@ -107,7 +110,23 @@ export const startEditor = (code: string) => {
advanceWhitespace();
}

const backspaceLine = () => {
if (lineHistory.length > 0) {
--lineIndex
const history = lineHistory.pop() as History
line = history.line
lineCorrectness = history.lineCorrectness
charIndex = line.length - 1
element.classList.remove(cursorClassName);
element.classList.add(nextClassName);
// because there is a <br> element at the beginning line
element = element.previousElementSibling?.previousElementSibling as Element;
element.classList.add(cursorClassName);
}
}

const advancePage = () => {
lineHistory = []
firstLineIndex += pageLines.length;
pageLines = allLines.slice(firstLineIndex, firstLineIndex + linesPerPage);
lineIndex = 0;
Expand Down Expand Up @@ -165,6 +184,8 @@ export const startEditor = (code: string) => {
}
} else if (charIndex > 0 && key === backspaceKey) {
applyBackspace();
} else if ( charIndex === 0 && lineHistory.length > 0 ) {
backspaceLine()
} else if (charIndex === line.length - 1 && key === enterKey) {
totalCharacters++;
correctCharacters++;
Expand Down