or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

conversion-utilities.mddata-models.mdeditor-components.mdentity-system.mdindex.mdkey-bindings-utilities.mdtext-modification.md
tile.json

data-models.mddocs/

Data Models

Immutable data structures that represent editor state, content, selections, and character metadata. These are the core building blocks of Draft.js's functional approach to text editing.

Capabilities

EditorState

The central immutable state container for the entire editor. Contains content, selection, undo/redo stack, and editor configuration.

/**
 * Immutable editor state containing content, selection, and configuration
 */
class EditorState {
  // Static creation methods
  /**
   * Create an empty editor state
   * @param decorator - Optional decorator for text ranges
   * @returns New EditorState instance
   */
  static createEmpty(decorator?: ?DraftDecoratorType): EditorState;
  
  /**
   * Create editor state from plain text
   * @param text - Initial text content
   * @param decorator - Optional decorator for text ranges
   * @returns New EditorState instance
   */
  static createWithText(text: string, decorator?: ?DraftDecoratorType): EditorState;
  
  /**
   * Create editor state from content state
   * @param contentState - Initial content state
   * @param decorator - Optional decorator for text ranges
   * @returns New EditorState instance
   */
  static createWithContent(contentState: ContentState, decorator?: ?DraftDecoratorType): EditorState;
  
  /**
   * Create editor state with full configuration
   * @param config - Complete editor state configuration
   * @returns New EditorState instance
   */
  static create(config: {
    currentContent: ContentState,
    decorator?: ?DraftDecoratorType,
    selection?: ?SelectionState,
    allowUndo?: boolean,
    inlineStyleOverride?: ?DraftInlineStyle,
    forceSelection?: boolean,
    inCompositionMode?: boolean,
    lastChangeType?: ?EditorChangeType,
    nativelyRenderedContent?: ?ContentState,
    redoStack?: Stack<ContentState>,
    undoStack?: Stack<ContentState>,
    treeMap?: ?OrderedMap<string, List<any>>,
    directionMap?: ?OrderedMap<string, string>
  }): EditorState;
  
  // Static modification methods
  /**
   * Apply content changes to editor state
   * @param editorState - Current editor state
   * @param contentState - New content state
   * @param changeType - Type of change being made
   * @param forceSelection - Whether to force selection update
   * @returns New EditorState instance
   */
  static push(editorState: EditorState, contentState: ContentState, changeType: EditorChangeType, forceSelection?: boolean): EditorState;
  
  /**
   * Move selection to end of content
   * @param editorState - Current editor state
   * @returns New EditorState with selection at end
   */
  static moveSelectionToEnd(editorState: EditorState): EditorState;
  
  /**
   * Move focus to end of content
   * @param editorState - Current editor state
   * @returns New EditorState with focus at end
   */
  static moveFocusToEnd(editorState: EditorState): EditorState;
  
  /**
   * Accept selection change without triggering re-render
   * @param editorState - Current editor state
   * @param selection - New selection state
   * @returns New EditorState with updated selection
   */
  static acceptSelection(editorState: EditorState, selection: SelectionState): EditorState;
  
  /**
   * Force selection change with re-render
   * @param editorState - Current editor state
   * @param selection - Selection state to force
   * @returns New EditorState with forced selection
   */
  static forceSelection(editorState: EditorState, selection: SelectionState): EditorState;
  
  /**
   * Undo the last change
   * @param editorState - Current editor state
   * @returns New EditorState with previous content
   */
  static undo(editorState: EditorState): EditorState;
  
  /**
   * Redo the last undone change
   * @param editorState - Current editor state
   * @returns New EditorState with restored content
   */
  static redo(editorState: EditorState): EditorState;
  
  // Instance methods - Content access
  /**
   * Get current content state
   * @returns Current ContentState
   */
  getCurrentContent(): ContentState;
  
  /**
   * Get current selection state
   * @returns Current SelectionState
   */
  getSelection(): SelectionState;
  
  /**
   * Get decorator instance
   * @returns Current decorator or null
   */
  getDecorator(): ?DraftDecoratorType;
  
  /**
   * Get inline styles at current selection
   * @returns Set of active inline styles
   */
  getCurrentInlineStyle(): DraftInlineStyle;
  
  /**
   * Get the type of the last change
   * @returns Last change type or null
   */
  getLastChangeType(): ?EditorChangeType;
  
  /**
   * Check if undo is allowed
   * @returns True if undo operations are enabled
   */
  getAllowUndo(): boolean;
  
  /**
   * Get decorator tree for a specific block
   * @param blockKey - Key of the block
   * @returns Decorator tree for the block
   */
  getBlockTree(blockKey: string): List<any>;
  
  /**
   * Check if editor is in composition mode (IME input)
   * @returns True if in composition mode
   */
  isInCompositionMode(): boolean;
  
  /**
   * Check if selection update should be forced
   * @returns True if selection update is needed
   */
  mustForceSelection(): boolean;
  
  // Instance methods - State transitions
  /**
   * Create new state with updated content
   * @param contentState - New content state
   * @param changeType - Type of change being made
   * @returns New EditorState instance
   */
  push(contentState: ContentState, changeType: EditorChangeType): EditorState;
  
  /**
   * Create new state with arbitrary changes
   * @param config - Properties to update
   * @returns New EditorState instance
   */
  set(config: {
    currentContent?: ?ContentState,
    decorator?: ?DraftDecoratorType,
    selection?: ?SelectionState,
    allowUndo?: boolean,
    inlineStyleOverride?: ?DraftInlineStyle,
    forceSelection?: boolean,
    inCompositionMode?: boolean,
    lastChangeType?: ?EditorChangeType,
    nativelyRenderedContent?: ?ContentState,
    redoStack?: Stack<ContentState>,
    undoStack?: Stack<ContentState>,
    treeMap?: ?OrderedMap<string, List<any>>,
    directionMap?: ?OrderedMap<string, string>
  }): EditorState;
  
  /**
   * Undo the last change
   * @returns New EditorState with previous content
   */
  undo(): EditorState;
  
  /**
   * Redo the last undone change
   * @returns New EditorState with restored content
   */
  redo(): EditorState;
  
  /**
   * Move selection to start of content
   * @param content - Optional content state (uses current if not provided)
   * @returns New EditorState with selection at start
   */
  moveSelectionToStart(content?: ContentState): EditorState;
  
  /**
   * Move selection to end of content
   * @param content - Optional content state (uses current if not provided)
   * @returns New EditorState with selection at end
   */
  moveSelectionToEnd(content?: ContentState): EditorState;
  
  /**
   * Accept current selection state
   * @param selection - New selection state
   * @returns New EditorState with updated selection
   */
  acceptSelection(selection: SelectionState): EditorState;
  
  /**
   * Force selection state (bypassing composition)
   * @param selection - Selection state to force
   * @returns New EditorState with forced selection
   */
  forceSelection(selection: SelectionState): EditorState;
}

Usage Examples:

import { EditorState, ContentState, SelectionState } from "draft-js";

// Create empty editor state
const emptyState = EditorState.createEmpty();

// Create from text
const textState = EditorState.createWithText("Hello world!");

// Create from content state
const contentState = ContentState.createFromText("Initial content");
const editorState = EditorState.createWithContent(contentState);

// Working with editor state
const currentContent = editorState.getCurrentContent();
const selection = editorState.getSelection();
const inlineStyles = editorState.getCurrentInlineStyle();

// Making changes
const newContentState = Modifier.insertText(
  currentContent,
  selection,
  "inserted text"
);
const newEditorState = EditorState.push(
  editorState,
  newContentState,
  'insert-characters'
);

// Undo/redo
const undoneState = editorState.undo();
const redoneState = undoneState.redo();

ContentState

Immutable representation of the editor's content including all blocks, entities, and metadata.

/**
 * Immutable content state containing blocks and entities
 */
class ContentState {
  // Static creation methods
  /**
   * Create content state from plain text
   * @param text - Text to convert
   * @param delimiter - Line separator (default: newline)
   * @returns New ContentState instance
   */
  static createFromText(text: string, delimiter?: string | RegExp): ContentState;
  
  /**
   * Create content state from block array
   * @param blocks - Array of content blocks or config object
   * @param entityMap - Optional entity map
   * @returns New ContentState instance
   */
  static createFromBlockArray(
    blocks: Array<ContentBlock> | {contentBlocks: Array<ContentBlock>, entityMap?: any},
    entityMap?: any
  ): ContentState;
  
  // Instance methods - Content access
  /**
   * Get all content blocks as OrderedMap
   * @returns BlockMap containing all blocks
   */
  getBlockMap(): BlockMap;
  
  /**
   * Get selection state before this content change
   * @returns Previous selection state
   */
  getSelectionBefore(): SelectionState;
  
  /**
   * Get selection state after this content change
   * @returns New selection state
   */
  getSelectionAfter(): SelectionState;
  
  /**
   * Get block by key
   * @param key - Block key to find
   * @returns ContentBlock instance or null
   */
  getBlockForKey(key: string): ContentBlock;
  
  /**
   * Get key of block before specified key
   * @param key - Reference block key
   * @returns Previous block key or null
   */
  getKeyBefore(key: string): ?string;
  
  /**
   * Get key of block after specified key
   * @param key - Reference block key
   * @returns Next block key or null
   */
  getKeyAfter(key: string): ?string;
  
  /**
   * Get block before specified key
   * @param key - Reference block key
   * @returns Previous ContentBlock or null
   */
  getBlockBefore(key: string): ?ContentBlock;
  
  /**
   * Get block after specified key
   * @param key - Reference block key
   * @returns Next ContentBlock or null
   */
  getBlockAfter(key: string): ?ContentBlock;
  
  /**
   * Get all blocks as array
   * @returns Array of ContentBlock instances
   */
  getBlocksAsArray(): Array<ContentBlock>;
  
  /**
   * Get first block
   * @returns First ContentBlock
   */
  getFirstBlock(): ContentBlock;
  
  /**
   * Get last block
   * @returns Last ContentBlock
   */
  getLastBlock(): ContentBlock;
  
  /**
   * Get plain text representation
   * @param delimiter - Text to join blocks (default: newline)
   * @returns Plain text string
   */
  getPlainText(delimiter?: string): string;
  
  /**
   * Check if content has any text
   * @returns True if content contains text
   */
  hasText(): boolean;
  
  /**
   * Get entity map (legacy API)
   * @returns Entity storage object
   */
  getEntityMap(): any;
  
  /**
   * Get block size (character count)
   * @returns Total character count
   */
  getBlockSize(): number;
  
  // Entity methods
  /**
   * Create a new entity
   * @param type - Entity type (e.g., 'LINK', 'IMAGE')
   * @param mutability - Entity mutability ('MUTABLE', 'IMMUTABLE', 'SEGMENTED')
   * @param data - Entity data object
   * @returns Entity key string
   */
  createEntity(type: DraftEntityType, mutability: DraftEntityMutability, data?: any): string;
  
  /**
   * Get entity instance
   * @param key - Entity key
   * @returns DraftEntityInstance
   */
  getEntity(key: string): DraftEntityInstance;
  
  /**
   * Merge data into existing entity
   * @param key - Entity key
   * @param toMerge - Data to merge
   * @returns New ContentState with updated entity
   */
  mergeEntityData(key: string, toMerge: {[key: string]: any}): ContentState;
  
  /**
   * Replace entity data completely
   * @param key - Entity key
   * @param newData - New data object
   * @returns New ContentState with updated entity
   */
  replaceEntityData(key: string, newData: {[key: string]: any}): ContentState;
  
  /**
   * Add entity to content state
   * @param instance - Entity instance to add
   * @returns Entity key string
   */
  addEntity(instance: DraftEntityInstance): string;
}

SelectionState

Immutable representation of a selection or cursor position within the editor content.

/**
 * Immutable selection state representing cursor or selection range
 */
class SelectionState {
  // Static creation methods
  /**
   * Create selection state
   * @param config - Selection configuration
   * @returns New SelectionState instance
   */
  static createEmpty(blockKey: string): SelectionState;
  
  // Instance methods - Position access
  /**
   * Get start block key
   * @returns Block key where selection starts
   */
  getStartKey(): string;
  
  /**
   * Get start character offset
   * @returns Character offset where selection starts
   */
  getStartOffset(): number;
  
  /**
   * Get end block key
   * @returns Block key where selection ends
   */
  getEndKey(): string;
  
  /**
   * Get end character offset
   * @returns Character offset where selection ends
   */
  getEndOffset(): number;
  
  /**
   * Get anchor block key (selection anchor point)
   * @returns Anchor block key
   */
  getAnchorKey(): string;
  
  /**
   * Get anchor character offset
   * @returns Anchor character offset
   */
  getAnchorOffset(): number;
  
  /**
   * Get focus block key (selection focus point)
   * @returns Focus block key
   */
  getFocusKey(): string;
  
  /**
   * Get focus character offset
   * @returns Focus character offset
   */
  getFocusOffset(): number;
  
  // Instance methods - Selection properties
  /**
   * Check if selection is backwards (focus before anchor)
   * @returns True if selection is backwards
   */
  getIsBackward(): boolean;
  
  /**
   * Check if selection is collapsed (cursor, not range)
   * @returns True if selection is collapsed
   */
  isCollapsed(): boolean;
  
  /**
   * Check if selection has focus within specified range
   * @param blockKey - Block to check
   * @param start - Start offset
   * @param end - End offset
   * @returns True if focus is within range
   */
  getHasFocus(): boolean;
  
  /**
   * Check if either selection edge is within specified range
   * @param blockKey - Block to check
   * @param start - Start offset
   * @param end - End offset
   * @returns True if selection overlaps range
   */
  hasEdgeWithin(blockKey: string, start: number, end: number): boolean;
  
  // Instance methods - Manipulation
  /**
   * Create new selection with updated properties
   * @param config - Properties to update
   * @returns New SelectionState instance
   */
  merge(config: {
    anchorKey?: string,
    anchorOffset?: number,
    focusKey?: string,
    focusOffset?: number,
    isBackward?: boolean,
    hasFocus?: boolean
  }): SelectionState;
  
  /**
   * Create new selection collapsed at start
   * @returns New collapsed SelectionState
   */
  collapseToStart(): SelectionState;
  
  /**
   * Create new selection collapsed at end
   * @returns New collapsed SelectionState
   */
  collapseToEnd(): SelectionState;
}

ContentBlock

Immutable representation of a single content block (paragraph, header, list item, etc.).

/**
 * Immutable content block representing a single block of text
 */
class ContentBlock {
  // Instance methods - Basic properties
  /**
   * Get unique block key
   * @returns Block key string
   */
  getKey(): string;
  
  /**
   * Get block type
   * @returns Block type string
   */
  getType(): DraftBlockType;
  
  /**
   * Get plain text content
   * @returns Block text
   */
  getText(): string;
  
  /**
   * Get character metadata list
   * @returns List of CharacterMetadata
   */
  getCharacterList(): List<CharacterMetadata>;
  
  /**
   * Get text length
   * @returns Number of characters
   */
  getLength(): number;
  
  /**
   * Get nesting depth (for lists)
   * @returns Depth number (0-based)
   */
  getDepth(): number;
  
  /**
   * Get block data map
   * @returns Immutable Map of block data
   */
  getData(): Map<any, any>;
  
  // Instance methods - Character-level access
  /**
   * Get inline styles at character offset
   * @param offset - Character position
   * @returns Set of inline style names
   */
  getInlineStyleAt(offset: number): DraftInlineStyle;
  
  /**
   * Get entity key at character offset
   * @param offset - Character position
   * @returns Entity key or null
   */
  getEntityAt(offset: number): ?string;
  
  // Instance methods - Text search
  /**
   * Find ranges of text matching pattern
   * @param pattern - Text or regex to find
   * @returns Array of [start, end] offset pairs
   */
  findStyleRanges(filterFn: (metadata: CharacterMetadata) => boolean, callback: (start: number, end: number) => void): void;
  
  /**
   * Find entity ranges in block
   * @param entityKey - Entity key to find
   * @param callback - Function called for each range
   */
  findEntityRanges(filterFn: (metadata: CharacterMetadata) => boolean, callback: (start: number, end: number) => void): void;
}

CharacterMetadata

Immutable representation of formatting metadata for a single character (styles, entities).

/**
 * Immutable character metadata containing style and entity information
 */
class CharacterMetadata {
  // Static creation methods
  /**
   * Create character metadata
   * @param config - Metadata configuration
   * @returns New CharacterMetadata instance
   */
  static create(config?: {
    style?: DraftInlineStyle,
    entity?: ?string
  }): CharacterMetadata;
  
  /**
   * Create empty character metadata
   * @returns New empty CharacterMetadata instance
   */
  static EMPTY: CharacterMetadata;
  
  // Instance methods
  /**
   * Get inline styles
   * @returns Set of style names
   */
  getStyle(): DraftInlineStyle;
  
  /**
   * Get entity key
   * @returns Entity key or null
   */
  getEntity(): ?string;
  
  /**
   * Check if character has specific style
   * @param style - Style name to check
   * @returns True if style is present
   */
  hasStyle(style: string): boolean;
  
  // Instance methods - Manipulation
  /**
   * Apply inline style
   * @param style - Style name to apply
   * @returns New CharacterMetadata with style
   */
  applyStyle(style: string): CharacterMetadata;
  
  /**
   * Remove inline style
   * @param style - Style name to remove
   * @returns New CharacterMetadata without style
   */
  removeStyle(style: string): CharacterMetadata;
  
  /**
   * Apply entity
   * @param entityKey - Entity key to apply
   * @returns New CharacterMetadata with entity
   */
  applyEntity(entityKey: ?string): CharacterMetadata;
}

Usage Examples:

import { ContentState, SelectionState, ContentBlock, CharacterMetadata } from "draft-js";

// Working with ContentState
const contentState = ContentState.createFromText("Hello\nWorld");
const blocks = contentState.getBlocksAsArray();
const plainText = contentState.getPlainText(" ");
const firstBlock = contentState.getFirstBlock();

// Working with SelectionState
const selection = SelectionState.createEmpty(firstBlock.getKey());
const expandedSelection = selection.merge({
  anchorOffset: 0,
  focusOffset: 5
});
const isCollapsed = selection.isCollapsed(); // true
const isBackward = expandedSelection.getIsBackward(); // false

// Working with ContentBlock
const blockText = firstBlock.getText();
const blockType = firstBlock.getType();
const blockLength = firstBlock.getLength();
const styleAtOffset = firstBlock.getInlineStyleAt(0);

// Working with CharacterMetadata
const charMeta = CharacterMetadata.create({
  style: OrderedSet(['BOLD', 'ITALIC']),
  entity: 'entity-key-123'
});
const hasBold = charMeta.hasStyle('BOLD');
const entity = charMeta.getEntity();

Types

Data Model Types

// Block types
type DraftBlockType = 'paragraph' | 'header-one' | 'header-two' | 'header-three' | 
  'header-four' | 'header-five' | 'header-six' | 'unordered-list-item' | 
  'ordered-list-item' | 'blockquote' | 'code-block' | 'atomic' | 'unstyled';

// Inline styles
type DraftInlineStyle = OrderedSet<string>;

// Change types
type EditorChangeType = 'adjust-depth' | 'apply-entity' | 'backspace-character' |
  'change-block-data' | 'change-block-type' | 'change-inline-style' | 'move-block' |
  'delete-character' | 'insert-characters' | 'insert-fragment' | 'redo' | 'remove-range' |
  'spellcheck-change' | 'split-block' | 'undo';

// Block map (OrderedMap of blocks keyed by string)
type BlockMap = OrderedMap<string, ContentBlock>;

// Decorator interface
interface DraftDecoratorType {
  getDecorations(block: ContentBlock, contentState: ContentState): ?Array<?string>;
  getComponentForKey(key: string): Function;
  getPropsForKey(key: string): any;
}