Skip to main content

Document lifecycle

open

Open a document from a file, URL, or buffer.
source
string | File | Blob | Buffer
Document source. Omit for a blank document.
options
OpenOptions
await editor.open(docxFile);
await editor.open(null, { mode: 'html', html: '<p>Hello</p>' });
This is the instance method. For one-liner creation, use the static Editor.open() factory — see Configuration.

close

Close the current document. The editor instance stays alive for reuse.
editor.close();
await editor.open(anotherFile);

save

Save back to the original source path (Node.js only).
options
SaveOptions
await editor.save();
await editor.save({ isFinalDoc: true });
Throws if the editor was opened from a Blob/Buffer instead of a file path. Use saveTo() or exportDocument() instead.

saveTo

Save to a specific path (Node.js only).
path
string
required
File path
await editor.saveTo('/path/to/output.docx');

exportDocument

Export as a Blob (browser) or Buffer (Node.js).
const blob = await editor.exportDocument();
const blob = await editor.exportDocument({ isFinalDoc: true });

exportDocx

Lower-level export with additional control.
options
Object
const blob = await editor.exportDocx({
  isFinalDoc: true,
  commentsType: 'clean'
});

replaceFile

Replace the current DOCX with a new file.
await editor.replaceFile(newDocxFile);

Content

getHTML

const html = editor.getHTML();
const html = editor.getHTML({ unflattenLists: true });

getJSON

const json = editor.getJSON();

getMarkdown

const md = await editor.getMarkdown();

replaceContent

Replace the entire document content.
editor.replaceContent(proseMirrorJson);

replaceNodeWithHTML

Replace a specific node with HTML.
const table = editor.getNodesOfType('table')[0];
editor.replaceNodeWithHTML(table, '<table>...</table>');

Editor control

mount / unmount

editor.mount(document.querySelector('#editor'));
editor.unmount(); // Keeps instance alive

destroy

Permanently destroy the editor.
Irreversible. The instance cannot be used after this.
editor.destroy();

focus / blur

editor.focus();
editor.blur();

setEditable

editor.setEditable(false); // Read-only
editor.setEditable(true);  // Editable

setDocumentMode

editor.setDocumentMode('editing');    // Full editing
editor.setDocumentMode('suggesting'); // Track changes
editor.setDocumentMode('viewing');    // Read-only

Commands

All commands are accessed via editor.commands:
// Formatting
editor.commands.toggleBold();
editor.commands.toggleItalic();
editor.commands.toggleUnderline();

// Tables
editor.commands.insertTable({ rows: 3, cols: 3 });

// Selection
editor.commands.setTextSelection({ from: 10, to: 20 });
editor.commands.selectAll();

insertContent

Insert content with automatic format detection.
content
string | Object
required
Content to insert
options
Object
editor.commands.insertContent(htmlContent, { contentType: 'html' });
editor.commands.insertContent(markdownText, { contentType: 'markdown' });
editor.commands.insertContent('Plain text', { contentType: 'text' });
HTML and Markdown inline styles are stripped on import to ensure Word compatibility.

Document metadata

getMetadata

const { documentGuid, isModified, version } = editor.getMetadata();

getDocumentIdentifier

Get a stable identifier (GUID or content hash).
const id = await editor.getDocumentIdentifier();

isDocumentModified

if (editor.isDocumentModified()) {
  // Prompt user to save
}

Schema

getSchemaSummaryJSON

Generate a summary of the document schema. Useful for AI agents that need to understand the document structure.
const summary = await editor.getSchemaSummaryJSON();

Position & coordinates

getElementAtPos

Get the DOM element at a document position.
pos
number
required
Document position
const element = editor.getElementAtPos(42);

getNodesOfType

Get all nodes of a specific type.
const tables = editor.getNodesOfType('table');
const paragraphs = editor.getNodesOfType('paragraph');

isActive

Check if a node or mark is active.
editor.isActive('bold');
editor.isActive('heading', { level: 2 });

getAttributes

Get attributes of the active node or mark.
const attrs = editor.getAttributes('link');
console.log(attrs.href);

Page & layout

getPageStyles

const styles = editor.getPageStyles();

updatePageStyle

editor.updatePageStyle({
  pageMargins: { top: '1in', bottom: '1in', left: '1in', right: '1in' }
});
const results = editor.commands.search('hello');
const results = editor.commands.search(/\d{3}-\d{4}/gi);

editor.commands.goToSearchResult(results[0]);

Properties

PropertyTypeDescription
lifecycleStatestring'initialized', 'documentLoading', 'ready', 'saving', 'closed', 'destroyed'
isEditablebooleanWhether editor accepts input
isDestroyedbooleanWhether editor has been destroyed
isFocusedbooleanWhether editor has focus
docChangedbooleanWhether any edits have been made
sourcePathstring | nullSource file path (null if opened from Blob)