Linora

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:

Decision

  1. 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.
  2. 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.
  3. 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'.
  4. 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.
  5. 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.
  6. 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

Consequences