> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superdoc.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Events

Events let you respond to editor lifecycle and content changes.

## Lifecycle

### `onBeforeCreate`

Called before the editor view is created.

```javascript Usage theme={null}
onBeforeCreate: ({ editor }) => {
  // Set up external services
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onBeforeCreate: ({ editor }) => {
    // Set up external services
  },
});
```

### `onCreate`

Called when editor is fully initialized and ready.

```javascript Usage theme={null}
onCreate: ({ editor }) => {
  editor.focus();
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onCreate: ({ editor }) => {
    editor.focus();
  },
});
```

### `onDestroy`

Called when editor is being destroyed. Clean up resources here.

```javascript Usage theme={null}
onDestroy: () => {
  clearInterval(autoSaveTimer);
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onDestroy: () => {
    clearInterval(autoSaveTimer);
  },
});
```

### `onFirstRender`

Called after the first render completes.

```javascript Usage theme={null}
onFirstRender: () => {
  hideLoadingSpinner();
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onFirstRender: () => {
    hideLoadingSpinner();
  },
});
```

## Content

### `onUpdate`

Called when document content changes.

```javascript Usage theme={null}
onUpdate: ({ editor, transaction }) => {
  if (transaction.docChanged) {
    saveToBackend(editor.getJSON());
  }
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onUpdate: ({ editor, transaction }) => {
    if (transaction.docChanged) {
      saveToBackend(editor.getJSON());
    }
  },
});
```

### `onContentError`

Called when content processing fails.

```javascript Usage theme={null}
onContentError: ({ error, editor, documentId }) => {
  console.error('Document error:', error);
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onContentError: ({ error, editor, documentId }) => {
    console.error('Document error:', error);
  },
});
```

## Selection

### `onSelectionUpdate`

Called when selection changes (cursor movement).

```javascript Usage theme={null}
onSelectionUpdate: ({ editor }) => {
  toolbar.bold = editor.isActive('bold');
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onSelectionUpdate: ({ editor }) => {
    toolbar.bold = editor.isActive('bold');
  },
});
```

### `onFocus`

Called when editor gains focus.

```javascript Usage theme={null}
onFocus: ({ editor, event }) => {
  showFormattingToolbar();
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onFocus: ({ editor, event }) => {
    showFormattingToolbar();
  },
});
```

### `onBlur`

Called when editor loses focus.

```javascript Usage theme={null}
onBlur: ({ editor, event }) => {
  saveCurrentState();
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onBlur: ({ editor, event }) => {
    saveCurrentState();
  },
});
```

## Subscribing after initialization

Use `editor.on(...)` and `editor.off(...)` to subscribe to events at any time after the editor is created. This is useful for adding listeners from external code that does not control the initial configuration.

```javascript Usage theme={null}
editor.on('update', ({ editor }) => {
  const { counts } = editor.doc.info();
  updateDocumentStatsUI({
    words: counts.words,
    characters: counts.characters,
    trackedChanges: counts.trackedChanges,
    sdtFields: counts.sdtFields,
    lists: counts.lists,
  });
});
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
});

// Subscribe to updates after creation
const handler = ({ editor }) => {
  const { counts } = editor.doc.info();
  document.getElementById('stats').textContent =
    `${counts.words} words, ${counts.characters} characters, ` +
    `${counts.trackedChanges} tracked changes`;
};

editor.on('update', handler);

// Later, unsubscribe
editor.off('update', handler);
```

> [!NOTE]
>
> Constructor callbacks like `onUpdate` and runtime subscriptions like `editor.on('update', ...)` both fire on the same events. Use constructor callbacks when the listener is known at creation time, and `editor.on(...)` when adding listeners dynamically.

## Features

### `onCommentsUpdate`

```javascript Usage theme={null}
onCommentsUpdate: ({ editor }) => {
  updateCommentsSidebar();
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onCommentsUpdate: ({ editor }) => {
    updateCommentsSidebar();
  },
});
```

### `onCommentsLoaded`

```javascript Usage theme={null}
onCommentsLoaded: ({ editor, comments }) => {
  console.log(`Loaded ${comments.length} comments`);
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onCommentsLoaded: ({ editor, comments }) => {
    console.log(`Loaded ${comments.length} comments`);
  },
});
```

### `onTrackedChangesUpdate`

```javascript Usage theme={null}
onTrackedChangesUpdate: ({ editor }) => {
  updateReviewPanel();
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onTrackedChangesUpdate: ({ editor }) => {
    updateReviewPanel();
  },
});
```

### `onCollaborationReady`

```javascript Usage theme={null}
onCollaborationReady: ({ editor, ydoc }) => {
  showCollaboratorsCursors();
}
```

```javascript Full Example theme={null}
import { Editor } from 'superdoc/super-editor';

const editor = await Editor.open(yourFile, {
  element: document.querySelector('#editor'),
  onCollaborationReady: ({ editor, ydoc }) => {
    showCollaboratorsCursors();
  },
});
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.