0520. In-place text editing: hidden textarea, doc-side layout, one gesture per edit
Status: Accepted (2026-11-28, ED1). Builds on 0014 (shared metrics) and 0502 (text glyphs from TextView).
Context
The text tool must create and edit text on the canvas, Figma-style. Requirements:
- IME, clipboard and keyboard behaviour that people expect;
- the canvas keeps drawing the text with the engine’s SDF glyphs, with no DOM text over the scene;
- multiplayer-safe and undoable per user;
- ADR 0014: the author measures the box and writes it in the same op;
- no per-node JS objects, and nothing that slows the frame loop.
Decision
- Input is a hidden
<textarea>. The textarea is invisible and positioned at the caret, so IME candidate windows open there.
- The browser handles typing, composition, word moves, the clipboard and in-edit undo (Cmd+Z inside the edit).
- Every
input event writes the textarea’s value to the doc. During composition nothing reprograms the textarea (IME-safe), and the overlay underlines the composing range.
contenteditable was rejected: it would need its own visible layout of the text, which would duplicate (and disagree with) the engine’s glyphs.
- Layout on the doc side.
text/layout.ts mirrors the engine’s textLine:
- line breaks come from
@linorahq/doc/text forEachLine on the UTF-8 bytes, mapped to UTF-16 indices;
- line i spans y in [ilh, (i+1)lh);
- alignment offsets match the engine’s;
- advances come from
advanceUnits.
Caret, selection rectangles, click-to-index and Up/Down/Home/End/Cmd+Left/Right use this layout, not the textarea’s. Caret and text therefore always agree, at any zoom or rotation. EN2 confirmed the contract and will announce any change to text layout.
- Drawing. The overlay canvas draws the box outline, the selection and the composition underline in the node’s page matrix.
- The caret is a DOM element with a Web Animations blink, restarted on every move. Blinking never redraws the WebGL scene.
- Inline styles go through the CSSOM (
element.style), so a strict CSP needs no 'unsafe-inline'.
- Ops. An edit is one
Gesture:
- each change is one
SET {text, w, h, x, y}, sized by measureBox (auto width: no wrap; auto height: wrap at w; fixed: unchanged);
- the origin is anchored so an auto-width box keeps its alignment edge (left, center or right) and an auto-height box keeps its top, rotation included;
- peers see typing live (the session coalesces per frame), and the whole edit is one undo step.
Text stays whole-string last-writer-wins (ADR 0004). A remote change to the edited node is adopted into the textarea unless a composition is in progress. A remote delete ends the edit.
- Lifecycle.
- Click with the text tool: auto-width text, with the click at the middle of the first line.
- Drag with the text tool: fixed-width (auto-height) text at the dragged width.
- Double-click a text with the move tool, or press Enter on one: edit it. Enter selects all.
- Escape, Cmd+Enter, a click outside, a tool switch or focus moving to another control ends the edit. A window blur does not.
- An empty new text is cancelled and leaves no undo entry. An existing text emptied by the edit is deleted, as one undo step that restores it.
- New text goes into the frame under the click (ADR 0521).
- Cmd+B toggles bold. The properties panel’s text controls apply inside the open edit when they don’t take focus (the segmented buttons), otherwise to the selection after the edit ends.
- Bounded memory. A gesture keeps every update’s inverse until it ends. After 256 updates the edit closes that undo step and continues in a new gesture. Typing into a 64 KiB text therefore holds at most 256 inverses, not one per keystroke. The UTF-8 size is capped at 64 KiB before writing (the PROP_INFO limit).
Alternatives
- Rendering the edited text in DOM over the canvas: an exact IME caret, but two text renderers disagreeing about breaks (kerning, subpixel), and a visible swap on enter and exit.
- Character-level ops or a CRDT for text: concurrent typing in one node would merge. That is a large protocol change; LWW whole strings are the accepted model for now (ADR 0004).
- Committing one transaction per keystroke: per-keystroke undo after the edit, and more wire batches. Figma undoes a whole edit.
Consequences
- Non-ASCII text renders as ‘?’ until the atlas and metrics cover more scripts (ADR 0014). The caret and layout already count code points correctly.
- Typing growth needs engine slack. EN2 is replacing the tail layout on text growth with chunk insertion. Until it lands, typing into a text near the start of a huge page costs one tail layout per keystroke that outgrows the node’s slot.
- Tested:
test/text-layout.test.ts covers breaks identical to wrapLines, caret, hit and surrogate pairs, sizing and anchoring. The browser checks cover create, type, select, navigate, in-edit undo, one-step undo after the edit, wrap, click and double-click, style changes, and dropping empty text.