System Design: Input Processing and Text Reconstruction
Context
You are designing a cross-platform input processing layer for a text editor that reconstructs exactly what a user typed from raw keyboard and mouse events. The system must support standard keyboards, mice/trackpads, and IME/composition for non-Latin input. Assume a single-user session per editor instance, possibly with multiple input devices (e.g., external keyboard + laptop keyboard + mouse).
Functional Requirements
-
Event capture and handling
-
Keyboard: keydown/keyup, key repeat, modifiers (Shift/Ctrl/Alt/Meta/CapsLock), backspace/delete.
-
Pointer: cursor movement, mouse down/up/move, selection via drag, double/triple click.
-
Clipboard: copy, cut, paste.
-
IME/composition: compositionstart/update/end and pre-edit text.
-
Ordering and time
-
Maintain precise timestamps and a total order across devices (keyboard, mouse).
-
APIs
-
recordEvent(event)
-
getCurrentText()
-
getSelectionRange()
-
undo()
-
redo()
-
Persistence and replay
-
Persist an event log and support deterministic replay to derive final text.
-
Reliability and resilience
-
Debounce where appropriate; handle lost/duplicated events.
-
Define data models for events and sessions.
Deliverables
-
High-level components and their responsibilities.
-
Core data structures (document, selection, history, logging).
-
Recovery strategy after crashes.
-
Testing strategy (functional, property-based, fuzzing, IME).
-
Performance considerations (latency, throughput, memory).