Delete Content Operation

Delete content from the document model in a range.

Overview

The delete content operation removes content from the document at a specific path and length. Always store deleted content for efficient undo.

Interface

interface DeleteContentOperation extends Operation {
  type: 'deleteContent';
  path: Path;
  length: number; // Number of characters/nodes to delete
  deletedContent?: any; // Store deleted content for undo
}

Usage

// Delete selected text
function deleteSelection(editor: Editor, selection: Selection) {
  const operation: DeleteContentOperation = {
    type: 'deleteContent',
    path: selection.start,
    length: selection.length,
    deletedContent: editor.getContent(selection) // Store for undo
  };
  
  editor.applyOperation(operation);
}

// In transaction
function deleteInTransaction(editor: Editor, selection: Selection) {
  const tx = editor.beginTransaction();
  tx.add({
    type: 'deleteContent',
    path: selection.start,
    length: selection.length,
    deletedContent: editor.getContent(selection)
  });
  tx.commit();
}

Inverse Operation

function getInverse(operation: DeleteContentOperation): InsertTextOperation {
  return {
    type: 'insertText',
    path: operation.path,
    text: operation.deletedContent
  };
}