Apply Format Operation

Apply formatting to text range in the document model.

Overview

The apply format operation modifies text styling (bold, italic, underline, etc.) without changing content.

Interface

interface ApplyFormatOperation extends Operation {
  type: 'applyFormat';
  path: Path;
  length: number; // Range to format
  format: string; // Format name (e.g., 'bold', 'italic')
  value: any; // Format value
}

Usage

// Apply bold formatting
function applyBold(editor: Editor, selection: Selection) {
  const operation: ApplyFormatOperation = {
    type: 'applyFormat',
    path: selection.start,
    length: selection.length,
    format: 'bold',
    value: true
  };
  
  editor.applyOperation(operation);
}

// Apply multiple formats in transaction
function applyFormats(editor: Editor, selection: Selection, formats: string[]) {
  const tx = editor.beginTransaction();
  
  for (const format of formats) {
    tx.add({
      type: 'applyFormat',
      path: selection.start,
      length: selection.length,
      format: format,
      value: true
    });
  }
  
  tx.commit(); // Single undo entry
}

Inverse Operation

function getInverse(operation: ApplyFormatOperation): RemoveFormatOperation {
  return {
    type: 'removeFormat',
    path: operation.path,
    length: operation.length,
    format: operation.format
  };
}