CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lexical

Lexical is an extensible text editor framework that provides excellent reliability, accessible and performance.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

editor-management.mddocs/

Editor Management

Core editor functionality for creating, configuring, and managing Lexical editor instances. The editor serves as the central hub for all text editing operations and state management.

Capabilities

Editor Creation

Creates a new Lexical editor instance with optional configuration.

/**
 * Creates a new Lexical editor instance
 * @param config - Optional configuration for the editor
 * @returns A new LexicalEditor instance
 */
function createEditor(config?: CreateEditorArgs): LexicalEditor;

interface CreateEditorArgs {
  /** Unique namespace for the editor instance */
  namespace?: string;
  /** Theme configuration for styling editor content */
  theme?: EditorThemeClasses;
  /** Error handler for editor errors */
  onError?: (error: Error) => void;
  /** Array of node types and replacements to register */
  nodes?: ReadonlyArray<Klass<LexicalNode> | LexicalNodeReplacement>;
  /** Initial editor state */
  editorState?: EditorState;
  /** HTML import/export configuration */
  html?: HTMLConfig;
  /** Whether the editor is initially editable */
  editable?: boolean;
}

interface EditorThemeClasses {
  /** CSS class for paragraph elements */
  paragraph?: string;
  /** CSS classes for text formatting */
  text?: {
    bold?: string;
    italic?: string;
    underline?: string;
    strikethrough?: string;
    underlineStrikethrough?: string;
    code?: string;
    highlight?: string;
    subscript?: string;
    superscript?: string;
  };
  /** CSS classes for other node types */
  [key: string]: string | Record<string, string> | undefined;
}

Usage Examples:

import { createEditor } from "lexical";

// Basic editor
const editor = createEditor();

// Editor with configuration
const editor = createEditor({
  namespace: 'MyEditor',
  theme: {
    paragraph: 'my-paragraph',
    text: {
      bold: 'my-bold',
      italic: 'my-italic',
    }
  },
  onError: (error) => {
    console.error('Editor error:', error);
  },
  editable: true,
});

Editor Interface

The main editor interface providing methods for DOM binding, state management, and updates.

interface LexicalEditor {
  /** Associate editor with a DOM element */
  setRootElement(rootElement: null | HTMLElement): void;
  /** Get the current immutable editor state */
  getEditorState(): EditorState;
  /** Set a new editor state */
  setEditorState(editorState: EditorState, options?: EditorSetOptions): void;
  /** Perform an update to the editor state */
  update(updateFn: () => void, options?: EditorUpdateOptions): void;
  /** Read from the current editor state */
  read<T>(readFn: () => T): T;
  /** Focus the editor */
  focus(callbackFn?: () => void): void;
  /** Blur the editor */
  blur(): void;
  /** Check if editor is editable */
  isEditable(): boolean;
  /** Set editor editable state */
  setEditable(editable: boolean): void;
  /** Parse a serialized editor state */
  parseEditorState(maybeStringifiedEditorState: string): EditorState;
  /** Register a command listener */
  registerCommand<P>(
    command: LexicalCommand<P>,
    listener: CommandListener<P>,
    priority: CommandListenerPriority
  ): () => void;
  /** Dispatch a command */
  dispatchCommand<P>(command: LexicalCommand<P>, payload: P): boolean;
  /** Register an update listener */
  registerUpdateListener(listener: UpdateListener): () => void;
  /** Register a root listener */
  registerRootListener(listener: RootListener): () => void;
  /** Register an editable listener */
  registerEditableListener(listener: EditableListener): () => void;
  /** Register a mutation listener */
  registerMutationListener(
    klass: Klass<LexicalNode>,
    listener: MutationListener
  ): () => void;
  /** Register a node transform */
  registerNodeTransform<T extends LexicalNode>(
    klass: Klass<T>,
    listener: Transform<T>
  ): () => void;
}

interface EditorSetOptions {
  /** Tag to identify the update */
  tag?: string;
}

interface EditorUpdateOptions {
  /** Tag to identify the update */
  tag?: string;
  /** Called when the update is applied to the DOM */
  onUpdate?: () => void;
  /** Skip collaboration for this update */
  skipTransforms?: boolean;
  /** Disable DOM updates */
  discrete?: boolean;
}

Usage Examples:

import { createEditor, $getRoot, $createParagraphNode, $createTextNode } from "lexical";

const editor = createEditor();
const contentEditable = document.getElementById('editor');

// Associate with DOM
editor.setRootElement(contentEditable);

// Perform an update
editor.update(() => {
  const root = $getRoot();
  const paragraph = $createParagraphNode();
  const text = $createTextNode('Hello, world!');
  
  paragraph.append(text);
  root.append(paragraph);
});

// Read from editor state
const textContent = editor.read(() => {
  const root = $getRoot();
  return root.getTextContent();
});

// Listen for updates
const removeUpdateListener = editor.registerUpdateListener(({editorState}) => {
  console.log('Editor updated');
  editorState.read(() => {
    // Process the new state
  });
});

// Focus the editor
editor.focus();

// Make editor read-only
editor.setEditable(false);

Command Priority Constants

Priority levels for command listeners determining execution order.

/** Editor priority - core editor functionality (lowest priority) */
const COMMAND_PRIORITY_EDITOR: 0;
/** Low priority - basic plugin functionality */
const COMMAND_PRIORITY_LOW: 1;
/** Normal priority - default for most listeners */
const COMMAND_PRIORITY_NORMAL: 2;
/** High priority - for important plugins */
const COMMAND_PRIORITY_HIGH: 3;
/** Critical priority - highest execution priority */
const COMMAND_PRIORITY_CRITICAL: 4;

type CommandListenerPriority = 0 | 1 | 2 | 3 | 4;

Listener Types

Type definitions for various editor event listeners.

/**
 * Listener for editor updates
 * @param payload - Update information including new editor state
 */
type UpdateListener = (payload: {
  editorState: EditorState;
  prevEditorState: EditorState;
  tags: Set<string>;
}) => void;

/**
 * Listener for root element changes
 * @param rootElement - New root element (null if removed)
 * @param prevRootElement - Previous root element
 */
type RootListener = (
  rootElement: null | HTMLElement,
  prevRootElement: null | HTMLElement
) => void;

/**
 * Listener for editable state changes
 * @param editable - New editable state
 */
type EditableListener = (editable: boolean) => void;

/**
 * Listener for node mutations
 * @param mutations - Map of node keys to mutation types
 * @param payload - Update payload with editor states
 */
type MutationListener = (
  mutations: Map<NodeKey, NodeMutation>,
  payload: {
    prevEditorState: EditorState;
    updateTags: Set<string>;
  }
) => void;

type NodeMutation = 'created' | 'updated' | 'destroyed';

/**
 * Transform function for node changes
 * @param node - The node being transformed
 */
type Transform<T extends LexicalNode> = (node: T) => void;

docs

caret-system.md

command-system.md

editor-management.md

index.md

node-system.md

selection-system.md

state-management.md

utilities-helpers.md

tile.json