Back to projects
Projectadvanced 17 steps ~170 min

Build a Code Editor from Scratch

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.

  • JavaScript
  • DOM
  • Intl.Segmenter
  • IME input
  • tokenizer
  • virtualization
  • undo history
Project briefrequirements and design decisions

Requirements

  • Type, select with keyboard and mouse, jump by words, smart Home
  • Enter keeps indentation; Tab and Shift+Tab work on selections
  • Undo and redo that group typing sensibly
  • Works with input methods, paste and mobile keyboards
  • Syntax highlighting and line numbers
  • Stays fast with a 20,000-line file

Architecture decisions

  • The state (text and selection) is the source of truth; the DOM is only a picture of it
  • Commands are pure functions that return transactions; one change per user action
  • Text input goes through a hidden textarea, not keydown
  • A per-line tokenizer state so highlighting is lazy and incremental
  • Only the visible lines are in the DOM

Stretch goals

  • Multiple cursors
  • Bracket matching and auto-closing brackets
  • Search and replace
  • Line wrapping with a height map

Production checklist

  • A piece table or rope for very large files
  • Measure text with the DOM instead of assuming monospace widths
  • Screen reader support beyond the hidden textarea
  • Right-to-left and bidirectional text

1What You're Building, and Why Not Plain contenteditable

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:

  • a cursor and selections you control with the keyboard and the mouse,
  • word jumps, smart Home, Enter that keeps indentation, Tab and Shift+Tab on selections,
  • undo and redo that group your typing the way VS Code does,
  • input that works with any keyboard, including input methods for Chinese and Japanese, dictation and paste,
  • syntax highlighting and line numbers,
  • a 20,000-line file that scrolls and edits as fast as a 20-line one.

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:

  • You don't own the document. The browser edits the DOM however it likes (and differently in Chrome, Safari and Firefox), and you have to reverse-engineer what changed.
  • Undo is the browser's, and it breaks as soon as your code also changes the DOM.
  • Highlighting fights the cursor: re-rendering colored spans moves or loses the selection.
  • Big files are slow: every line has to be in the DOM.

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:

  • Monaco (VS Code) and Ace capture typing with a hidden <textarea> and draw everything themselves. That's what you'll build.
  • CodeMirror 6, ProseMirror and Lexical do use 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:

  1. Document: the text as an array of lines, with positions as offsets (steps 2–3).
  2. Selection and movement: a cursor that respects emoji and remembers its column (steps 4–5).
  3. Commands: pure functions, (state) => transaction. Easy to test, easy to undo (step 6).
  4. History: undo and redo with sensible grouping (step 7).
  5. View: draws the state, and turns keyboard and mouse events into commands. Typing goes through a hidden <textarea> (steps 9–11).
  6. Highlighting and performance: a tokenizer that carries state across lines, and drawing only the visible lines (steps 13–14).

Everything except the view is plain functions that run anywhere, so you'll test them in Node before anything is on screen.

Key points

  • 1Plain contenteditable lets the browser own your document; editors keep it in their own state
  • 2The state (text plus selection) is the source of truth; the DOM is just a picture of it
  • 3Commands are pure functions that return transactions; the view applies them
  • 4Typing goes through a hidden textarea so every kind of keyboard input works
Code Editor from Scratch project | Bloom