DocAuthEditor
DocAuthEditor:
{}The visual editor UI. Binds to a DOM element and lets users edit documents.
Create editors using createDocAuthSystem.
Methods
Section titled “Methods”setCurrentDocument()
Section titled “setCurrentDocument()”setCurrentDocument(
doc):void
Attaches the provided document as the current document to the editor.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”currentDocument()
Section titled “currentDocument()”currentDocument():
DocAuthDocument
Returns a reference to the currently attached document.
Returns
Section titled “Returns”destroy()
Section titled “destroy()”destroy():
void
Removes all DOM elements and releases resources held by the editor. Note: This does not release the underlying DocAuthSystem. Use
DocAuthSystem.destroy after calling DocAuthEditor.destroy for a complete teardown.
Returns
Section titled “Returns”docAuthSystem()
Section titled “docAuthSystem()”docAuthSystem():
DocAuthSystem
Retrieves the DocAuthSystem instance this editor is bound to.
Returns
Section titled “Returns”insertTextAtCursor(
text):void
Inserts text at the current cursor position. If there’s a selection, it will be replaced with the provided text.
Parameters
Section titled “Parameters”The text to insert at the cursor position
Returns
Section titled “Returns”hasActiveCursor():
boolean
Checks if the editor has an active cursor/insertion point. This is useful for custom actions to determine if they should be enabled.
Returns
Section titled “Returns”True if cursor is active and insertion is possible, false otherwise
setActions(
actions):void
Set all actions (replaces any existing actions). Allows setting and executing editor actions programmatically.
Parameters
Section titled “Parameters”actions
Section titled “actions”Action[]
Array of actions to register
Returns
Section titled “Returns”Example
Section titled “Example”
// Register custom actions alongside default actionseditor.setActions([ { id: 'custom.insert-signature', label: 'Insert Signature', handler: () => { editor.insertTextAtCursor('\n\nBest regards,\nJohn Doe'); }, shortcuts: ['Mod+Shift+S'], },]);setToolbarConfig(
config):void
Set the toolbar configuration. Use this to customize the editor’s toolbar.
Parameters
Section titled “Parameters”config
Section titled “config”Toolbar configuration object
Returns
Section titled “Returns”Example
Section titled “Example”
// Use default toolbar config but add a custom itemeditor.setToolbarConfig({ items: [...defaultToolbarConfig.items, { type: 'action', id: 'custom', actionId: 'custom.insert-signature' }],});
// Or create a minimal toolbareditor.setToolbarConfig({ items: [ { type: 'built-in', id: 'undo', builtInType: 'undo' }, { type: 'built-in', id: 'redo', builtInType: 'redo' }, { type: 'separator', id: 'sep-1' }, { type: 'built-in', id: 'bold', builtInType: 'bold' }, { type: 'built-in', id: 'italic', builtInType: 'italic' }, { type: 'action', id: 'custom', actionId: 'custom.insert-signature' }, ],});on(
event,handler):DocAuthEditor
Adds an event listener that will be called every time the specified event is emitted.
Parameters
Section titled “Parameters”EventName
handler
Section titled “handler”(payload: EventPayload) => void
Returns
Section titled “Returns”DocAuthEditor
The editor instance for method chaining
Example
Section titled “Example”// Auto-save on content changeseditor.on('content.change', async () => { const doc = await editor.currentDocument().saveDocument();});
// Track document loadeditor.on('document.load', () => { console.log('Document loaded successfully');});
// Debounced save to prevent excessive API callslet saveTimeout;editor.on('content.change', async () => { clearTimeout(saveTimeout); saveTimeout = setTimeout(async () => { const doc = await editor.currentDocument().saveDocument(); await fetch('/api/save', { method: 'POST', }); }, 1000);});
// Method chainingeditor.on('document.load', () => console.log('loaded')).on('content.change', () => console.log('changed'));Available Events
Section titled “Available Events”| Event name | Event payload | Description |
|---|---|---|
document.load | none | Fired when a document is initially loaded into the editor. This event is triggered once per document load, including when switching documents. |
content.change | none | Fired when the document content has changed due to user editing or programmatic modifications. Use this event to react to document changes, such as saving drafts or updating UI state. |
off(
event,handler?):DocAuthEditor
Removes an event listener. If no handler is provided, removes all listeners for the event.
Parameters
Section titled “Parameters”EventName
handler?
Section titled “handler?”(payload: EventPayload) => void
Returns
Section titled “Returns”DocAuthEditor
The editor instance for method chaining
Example
Section titled “Example”// Remove specific handler (prevent memory leaks)const handleChange = async () => { const doc = await editor.currentDocument().saveDocument();};editor.on('content.change', handleChange);// ... later ...editor.off('content.change', handleChange);
// Remove all handlers for an eventeditor.off('content.change');
// Cleanup patternconst setupEditor = (target) => { const editor = await system.createEditor(target);
const handleChange = () => console.log('changed'); const handleLoad = () => console.log('loaded');
editor.on('content.change', handleChange); editor.on('document.load', handleLoad);
return () => { editor.off('content.change', handleChange); editor.off('document.load', handleLoad); editor.destroy(); };};Available Events
Section titled “Available Events”| Event name | Event payload | Description |
|---|---|---|
document.load | none | Fired when a document is initially loaded into the editor. This event is triggered once per document load, including when switching documents. |
content.change | none | Fired when the document content has changed due to user editing or programmatic modifications. Use this event to react to document changes, such as saving drafts or updating UI state. |
once(
event,handler):DocAuthEditor
Adds an event listener that will be called only once when the specified event is emitted. The listener is automatically removed after being called.
Parameters
Section titled “Parameters”EventName
handler
Section titled “handler”(payload: EventPayload) => void
Returns
Section titled “Returns”DocAuthEditor
The editor instance for method chaining
Example
Section titled “Example”// Wait for initial document loadeditor.once('document.load', () => { console.log('Document loaded for the first time');});
// Perform action after first changeeditor.once('content.change', () => { console.log('User made their first edit');});
const waitForLoad = (editor) => { editor.once('document.load', resolve); });};await waitForLoad(editor);console.log('Ready to use');Available Events
Section titled “Available Events”| Event name | Event payload | Description |
|---|---|---|
document.load | none | Fired when a document is initially loaded into the editor. This event is triggered once per document load, including when switching documents. |
content.change | none | Fired when the document content has changed due to user editing or programmatic modifications. Use this event to react to document changes, such as saving drafts or updating UI state. |