Skip to content

Refactor TextareaController to reactive .svelte.ts module #624

Closed

Description

@malibio

Overview

Convert the imperative TextareaController class to a reactive .svelte.ts module that participates in Svelte 5's reactivity system, eliminating the need for manual prop synchronization via $effect blocks.

Problem Statement

The current TextareaController is a plain TypeScript class that manages textarea state imperatively. This forces base-node.svelte to use multiple $effect blocks to keep the controller in sync with reactive props:

// Current anti-pattern: Manual sync effects
$effect(() => {
  if (controller && content !== undefined) {
    controller.updateContent(content);  // Manual sync
  }
});

$effect(() => {
  if (controller && editableConfig) {
    controller.updateConfig(editableConfig);  // Manual sync
  }
});

Effects to eliminate:

  • Line 712-739: Controller initialization based on element existence
  • Line 796-800: Content prop sync
  • Line 803-807: Config prop sync
  • Line 814-818: Slash command dropdown state sync
  • Line 820-825: Autocomplete dropdown state sync

Proposed Solution

Convert TextareaController to a .svelte.ts module using Svelte 5 runes internally:

// textarea-controller.svelte.ts
export function createTextareaController(
  element: () => HTMLTextAreaElement | undefined,
  nodeId: () => string,
  content: () => string,
  config: () => TextareaControllerConfig
) {
  // Internal reactive state
  let initialized = $state(false);
  
  // Derived values that auto-update
  let currentContent = $derived(content());
  
  // Effects only for true side effects (DOM manipulation)
  $effect(() => {
    const el = element();
    if (el && !initialized) {
      // Initialize once
      initialized = true;
    }
  });
  
  return {
    // Expose reactive getters
    get isInitialized() { return initialized; },
    // Methods for user actions
    focus() { ... },
    insertText(text: string) { ... }
  };
}

Acceptance Criteria

  • TextareaController converted to .svelte.ts with internal runes
  • Props passed as getter functions for reactivity
  • Content sync effect eliminated (controller reads reactive prop directly)
  • Config sync effect eliminated
  • Dropdown state sync effects eliminated
  • Controller initialization effect simplified or eliminated
  • All existing textarea tests pass
  • Edit mode, focus, cursor positioning work correctly
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Technical Specifications

Files to Modify

  • textarea-controller.ts: packages/desktop-app/src/lib/design/components/textarea-controller.ts - Convert to .svelte.ts
  • base-node.svelte: packages/desktop-app/src/lib/design/components/base-node.svelte - Update to use new reactive controller

Pattern Reference

  • focus-manager.svelte.ts: packages/desktop-app/src/lib/services/focus-manager.svelte.ts - Existing .svelte.ts pattern in codebase

Effects to Eliminate (5 total)

  1. Controller initialization (lines 712-739)
  2. Content prop sync (lines 796-800)
  3. Config prop sync (lines 803-807)
  4. Slash command dropdown sync (lines 814-818)
  5. Autocomplete dropdown sync (lines 820-825)

Testing Requirements

  • Existing textarea controller tests must pass
  • Manual testing of edit mode transitions
  • Verify cursor positioning after slash commands
  • Verify autocomplete dropdown interaction

Related Issues

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