Skip to content

Migrate to textarea-based editor (Logseq approach) #274

Closed

Description

@malibio

Problem

Current contenteditable-based editor has critical state synchronization bugs:

  • Markdown syntax lost on focus/blur/input events
  • originalContent vs DOM innerHTML sync issues
  • Multiline content losing formatting
  • Complex cursor positioning with markdown offsets
  • Each fix breaks other functionality

Root cause: Maintaining two representations (DOM HTML + markdown string) simultaneously.

Solution

Adopt Logseq's proven approach: textarea for editing + separate rendering on blur

Architecture Change

Current (contenteditable):

  • Dual state: originalContent (markdown) + element.innerHTML (formatted HTML)
  • Real-time inline formatting (bold/italic visible while typing)
  • Complex synchronization logic
  • ~2000+ lines in ContentEditableController

New (textarea):

  • Single source of truth: textarea.value (raw markdown)
  • Inline syntax visible during edit mode
  • Formatted rendering only on blur (syntax hidden)
  • ~500-800 lines (simpler TextareaController)

Key Benefits

  1. Zero synchronization bugs - textarea.value is the only state
  2. Native cursor APIs - selectionStart/selectionEnd instead of DOM traversal
  3. Simpler testing - string assertions instead of DOM queries
  4. Better AI integration - agents work with plain markdown
  5. Proven approach - Used by Logseq (millions of users)

Visual Changes

Editing mode:

  • Inline markdown syntax visible: **bold**, *italic*
  • Block syntax triggers node type conversion: # → HeaderNode

View mode (blur):

  • Inline syntax hidden, formatting rendered: bold, italic
  • Block nodes styled appropriately (headers, quotes, code blocks)

Preserved Functionality

  • ✅ Enter creates new nodes
  • ✅ Backspace merges/deletes nodes
  • ✅ Arrow navigation within/between nodes
  • ✅ Slash commands
  • ✅ @ mentions
  • ✅ Keyboard shortcuts
  • ✅ Clean UI (no borders, minimal padding)
  • ✅ Auto-resize height

Migration Steps

  1. Create TextareaController (replaces ContentEditableController)
  2. Update BaseNode to use <textarea> instead of <div contenteditable>
  3. Update keyboard commands for string operations
  4. Implement markdown rendering on blur
  5. Adapt cursor positioning for view→edit transitions
  6. Update ALL existing tests (see Testing Requirements below)
  7. Delete ContentEditableController (~580 lines removed)

Acceptance Criteria

  • TextareaController implements all keyboard commands
  • Node creation/deletion works identically
  • Arrow navigation works within/between nodes
  • Click on view mode positions cursor correctly in edit mode
  • Slash commands work
  • @ mentions work
  • Inline markdown renders on blur (syntax hidden)
  • All existing tests updated and passing (see Testing Requirements)
  • Code review via /pragmatic-code-review passes
  • No contenteditable code remains
  • Performance: handles 100+ nodes smoothly

Testing Requirements

CRITICAL: All existing tests must be updated for textarea

Tests affected (~50+ test files):

  • ContentEditableController tests → TextareaController tests (complete rewrite)
  • Keyboard command tests (update element queries)
  • Integration tests (update DOM queries and assertions)

Changes needed:

  • Replace querySelector('[contenteditable]') with querySelector('textarea')
  • Replace element.innerHTML assertions with textarea.value assertions
  • Simpler assertions (string comparisons instead of HTML parsing)
  • Tests should be easier to write and more reliable

All tests must pass before PR is ready for review.

References

  • Analysis: docs/architecture/research/logseq-editor-analysis.md
  • Design system: docs/design-system/components.html

Activity

  1. self-assigned this
    on Oct 15, 2025
  2. 19 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions