Skip to main content

@barocss/editor-view-dom

The Editor-View-DOM package provides the view layer that connects the Editor to the DOM. It handles user input, selection synchronization, and triggers rendering.

Purpose

View layer connecting Editor and DOM. Bridges user interactions (typing, clicking, keyboard shortcuts) with editor commands.

Key Exports

  • EditorViewDOM - Main view class
  • Selection synchronization utilities
  • Input handling utilities
  • Keybinding dispatch

Basic Usage

import { EditorViewDOM } from '@barocss/editor-view-dom';

const container = document.getElementById('editor')!;
const view = new EditorViewDOM(editor, { container });

Core Features

Multi-Layer Architecture

EditorViewDOM renders into 5 independent layers, each with its own DOMRenderer:

Selection Synchronization

Bidirectional synchronization between DOM selection and editor selection:

Input Handling

Processes user input and converts it to editor commands:

Keybinding Dispatch

Dispatches keyboard shortcuts to editor commands:

// User presses Ctrl+B
// → View captures keyboard event
// → Dispatches to editor
// → Editor executes 'bold' command
// → Model updated
// → View re-renders

Rendering Integration

Triggers rendering when model changes:

// Model changes
// → View detects change
// → Calls renderer.build()
// → Calls renderer.render()
// → DOM updated

View Lifecycle

// 1. Create view (attaches event listeners, initial render)
const view = new EditorViewDOM(editor, { container });

// 3. User interacts with editor
// → View handles input
// → Updates editor
// → Triggers rendering

// 4. Unmount (cleanup)
view.unmount();

Decorator Management

The view manages decorators:

// Add decorator
view.addDecorator({
sid: 'highlight-1',
stype: 'highlight',
target: { nodeId: 'p1', range: [0, 5] }
});

// Remove decorator
view.removeDecorator('highlight-1');

When to Use

  • Editor Integration: Required to connect editor to DOM
  • User Input: Handles all user interactions
  • Selection Management: Manages selection synchronization

Integration

Editor-View-DOM connects:

  • Editor: Executes commands on editor
  • Renderer-DOM: Triggers rendering
  • DOM: Listens to DOM events and updates DOM