Skip to content

Refactor BaseNodeViewer to be fully type-agnostic #698

Closed

Description

@malibio

Overview

BaseNodeViewer currently contains hardcoded node-type-specific logic that violates the plugin architecture pattern. This refactoring will make BaseNodeViewer fully type-agnostic by moving all type-specific handling to the plugin registry or individual node components.

Problem Statement

BaseNodeViewer has several instances of type-specific code that should be delegated to the plugin system:

  1. Task-specific metadata extraction (lines 376-395):

    // Task nodes: Map schema status to taskState for icon rendering
    if (node.nodeType === 'task') {
      // ... task-specific logic
    }
  2. Task-specific state mapping (lines 402-413):

    function mapNodeStateToSchemaStatus(state: string): CoreTaskStatus {
      // ... hardcoded task state mapping
    }
  3. Hardcoded structured node types (lines 348-357):

    const STRUCTURED_NODE_TYPES = ['code-block', 'quote-block'] as const;

Self-Contained Scope

  • Remove all hardcoded nodeType === 'task' checks from BaseNodeViewer
  • Remove hardcoded STRUCTURED_NODE_TYPES array
  • Create plugin registry interfaces for type-specific behaviors
  • Migrate task-specific logic to TaskNode plugin
  • Migrate structured node logic to respective plugins
  • Ensure all existing functionality continues to work

Implementation Approach

Phase 1: Define Plugin Interfaces

Add new optional methods to the plugin interface in plugin-registry.ts:

interface NodePluginDefinition {
  // Existing properties...
  
  // NEW: Type-specific metadata extraction
  extractMetadata?: (node: Node) => Record<string, unknown>;
  
  // NEW: Type-specific state mapping (for schema fields)
  mapStateToSchema?: (state: string, fieldName: string) => unknown;
  
  // NEW: Whether this node type accepts content merges
  acceptsContentMerge?: boolean; // default: true
}

Phase 2: Migrate Task-Specific Logic

  1. Move extractNodeMetadata task logic to TaskNode plugin
  2. Move mapNodeStateToSchemaStatus to TaskNode plugin
  3. Update BaseNodeViewer to call plugin registry for metadata extraction

Phase 3: Migrate Structured Node Logic

  1. Add acceptsContentMerge: false to code-block and quote-block plugins
  2. Replace isStructuredNode() with plugin registry lookup
  3. Remove STRUCTURED_NODE_TYPES constant

Phase 4: Update BaseNodeViewer

  1. Replace direct type checks with plugin registry calls
  2. Add fallback behavior for plugins without optional methods
  3. Ensure backward compatibility during transition

Acceptance Criteria

  • No hardcoded nodeType === 'task' checks in BaseNodeViewer
  • No hardcoded STRUCTURED_NODE_TYPES array in BaseNodeViewer
  • Plugin interface extended with optional type-specific methods
  • TaskNode plugin implements extractMetadata and mapStateToSchema
  • Code-block and quote-block plugins implement acceptsContentMerge: false
  • isStructuredNode() replaced with plugin registry lookup
  • All existing task state cycling functionality works
  • All existing backspace/merge protection works for structured nodes
  • All tests pass
  • Code passes bun run quality:fix

Technical Implementation Notes

Files to Modify

  1. src/lib/plugins/plugin-registry.ts

    • Extend NodePluginDefinition interface
    • Add helper methods for type-specific lookups
  2. src/lib/design/components/base-node-viewer.svelte

    • Remove extractNodeMetadata task-specific logic
    • Remove mapNodeStateToSchemaStatus function
    • Remove STRUCTURED_NODE_TYPES constant
    • Remove isStructuredNode function
    • Replace with plugin registry calls
  3. src/lib/plugins/definitions/task-node.ts

    • Add extractMetadata implementation
    • Add mapStateToSchema implementation
  4. src/lib/plugins/definitions/code-block-node.ts

    • Add acceptsContentMerge: false
  5. src/lib/plugins/definitions/quote-block-node.ts

    • Add acceptsContentMerge: false

Patterns to Follow

  • Reference existing plugin registration patterns in src/lib/plugins/definitions/
  • Follow the lazy-loading pattern already established
  • Use optional chaining for backward compatibility with existing plugins

Testing Approach

  1. Verify task state cycling (pending → inProgress → completed) still works
  2. Verify backspace at start of node after code-block is blocked
  3. Verify backspace at start of node after quote-block is blocked
  4. Run full test suite: bun run test:all

Resources & References

  • Plugin Registry: src/lib/plugins/plugin-registry.ts
  • BaseNodeViewer: src/lib/design/components/base-node-viewer.svelte
  • Task Plugin: src/lib/plugins/definitions/task-node.ts
  • Architecture Guide: docs/architecture/components/component-architecture-guide.md

Definition of Done

  • All type-specific logic removed from BaseNodeViewer
  • Plugin interface properly extended
  • All plugins updated with new optional methods where needed
  • All tests pass (bun run test:all)
  • Code quality verified (bun run quality:fix)
  • Existing functionality unchanged from user perspective
  • Code review completed

Related Issues

Activity

  1. malibio commented on Dec 1, 2025

    @malibio
    CollaboratorAuthor

    🧪 Test Baseline Recorded

    Frontend Tests: 728 passed
    Rust Tests: Running... (checking completion)

    Starting implementation of type-agnostic BaseNodeViewer refactoring.

  2. self-assigned this
    on Dec 1, 2025
  3. added a commit that references this issue on Dec 1, 2025
    31d84b9
  4. added 2 commits that reference this issue on Feb 4, 2026
    5ae83ee
    2c0660b
  5. added a commit that references this issue on Feb 26, 2026
    2f709fd
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