A code editor in plain JavaScript and the DOM, no libraries: a document model, emoji-safe cursor movement, word jumps, editing commands as pure functions, undo that groups typing, a hidden textarea for every kind of keyboard input, syntax highlighting that carries state across lines, and rendering only the visible lines so a 20,000-line file stays fast.
What you'll build
A working code editor with selections, undo, highlighting and line numbers that opens a 20,000-line file and renders each keystroke in about 1 ms.
In this project you'll build a working code editor in plain JavaScript and the DOM, without any editor library. By the end it has:
Editors like VS Code (Monaco), CodeMirror and Google Docs are built on the same ideas. After this project you'll understand what they do and why.
The obvious shortcut, and why editors don't take it. Browsers have a built-in editable element: <div contenteditable>. Type into it and text appears. But for a code editor it's a trap:
So serious editors flip it around. Your code owns the text; the DOM is just a picture of it. They differ in how they receive input:
<textarea> and draw everything themselves. That's what you'll build.contenteditable, but only as an input surface: they let the browser apply an edit, read back what changed, turn it into a transaction on their own state, and redraw from that state. The browser never owns the document.Step 16 compares how these libraries are built.
1 keydown / input / mouse events
2 │
3 ▼
4 state ──▶ command ──▶ transaction ──▶ new state ──▶ view draws it
5 { doc, selection } { change, selection } (lines, cursor, selection)
6 │
7 └──▶ history (for undo)The parts, in the order you'll build them:
(state) => transaction. Easy to test, easy to undo (step 6).<textarea> (steps 9–11).Everything except the view is plain functions that run anywhere, so you'll test them in Node before anything is on screen.