The editor
Opening and saving documents
Where documents come from and where they go: files the person opens and saves, documents you keep in your own storage, and drafts kept in the browser.
The choices#
| You want | Use |
|---|---|
People open and save .docx files themselves | Nothing: the full editor's File screen does it |
| Your app keeps documents (a database, S3, your API) | getDocument / setDocument, and onSave for the Save button (the whole flow) |
Your app keeps .docx files | exportDocument("docx") / importDocument(blob) |
| Work survives a closed tab | autosave-key (drafts) |
Keeping documents in your own storage#
The editor gives you the document as plain JSON (EditorDocument), and opens it again later:
const editor = await element.whenReady();
// Out: the document as data.
const doc = editor.getDocument();
await fetch(`/api/documents/${id}`, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(doc) });
// In: what you kept.
await editor.setDocument(await (await fetch(`/api/documents/${id}`)).json());setDocumentwaits for the license, then opens the document. Its HTML is cleaned the same way as anything opened, so a document from storage can't run scripts in your page.EditorDocumenthas aformat("dorvyn.document") and aversion. Keep them with it. Newer SDKs keep opening older versions.setDocumentrefuses anything that isn't anEditorDocument.- It holds the body, header and footer (as HTML), page setup, page numbers, comments, the title and the author. Pictures are inside it, so it can be large: store it as a JSON column or object, not in a size-limited text field.
The Save button#
With onSave, Save and Ctrl+S hand you the document instead of downloading a .docx:
<DorvynEditor
serverUrl="/api/dorvyn"
onSave={async (doc) => {
const res = await fetch(`/api/documents/${id}`, { method: "PUT", body: JSON.stringify(doc) });
if (!res.ok) throw new Error("Not saved"); // the person is told saving failed
}}
/>element.editorOptions = { onSave: async (doc) => { /* … */ } }; // HTML: before the editor starts- Throw (or reject) when saving fails: the person sees "Saving failed: …" with your error's message, and the document stays as it is.
- Save a Copy on the File screen still downloads a
.docx. state.saveTois"page"withonSave, and"download"without it, so your own Save button can say what it does.
Saving as they type#
editor.on("change", ({ document }) => queueSave(document));change fires at most every 0.7 s while someone types. Debounce your own saves on top (every few seconds), and save once more when the page is hidden (visibilitychange).
Files#
Without downloading anything:
const docx = await editor.exportDocument("docx"); // Blob: a Word file (through Dorvyn's export service)
const html = await editor.exportDocument("html"); // Blob: a web page
const text = await editor.exportDocument("text"); // Blob: plain text
const json = await editor.exportDocument("json"); // Blob: the EditorDocument
await editor.importDocument(blob); // a .docx, a web page or text (from the Blob's type or name)
await editor.importDocument(arrayBuffer); // a .docx
await editor.importDocument("<h1>Hello</h1>"); // a web page
await editor.importDocument("# Notes", "text"); // text (Markdown opens as text)importDocumentwaits for the license, likesetDocument. Exporting needs the license too..docxis made by Dorvyn's export service: the document is sent to it, made into a file, and not kept. Web pages, text and JSON are made in the browser, so they work even while the export service can't be reached.- Upload a
.docxto your server as a file:new FormData()with theBlob.
Commands for the same#
| Command | What it does |
|---|---|
file.new | A new document: "blank", "report" or "letter" |
file.open | Open a .docx, web page, text or Markdown file (a Blob or ArrayBuffer; the file picker without one) |
file.save | Save: to onSave, or as .docx |
file.saveDocx, file.saveHtml, file.saveText | Download as .docx, web page or text |
file.rename | Rename the document (the saved file is named after it) |
print | Print, or save as PDF from the browser's print dialog |
Drafts#
With autosave-key, the editor keeps a draft in the person's browser as they edit, and opens it again on their next visit:
<dorvyn-editor server-url="/api/dorvyn" autosave-key="doc-42"></dorvyn-editor>- Use one key per document (
doc-${id}), so drafts of different documents never mix. Withoutautosave-key, there are no drafts. - Drafts are compressed and kept in IndexedDB (localStorage where IndexedDB isn't available, as in some private windows). They never leave the browser: Dorvyn never sees them.
state.saveStatusis"saved","saving"or"unsaved", or"toolarge"if the browser has no room left for it.file.discardDraftforgets it.- Drafts belong to the browser, not the person: on a shared computer, the next person on the same site sees it. Turn drafts off for shared machines, or use a key that includes your user's id.
Keeping drafts somewhere else#
Give the drafts option an object with three methods:
const drafts = {
load: async (key: string) => sessionStorage.getItem(key), // string or null
save: async (key: string, value: string) => sessionStorage.setItem(key, value),
remove: async (key: string) => sessionStorage.removeItem(key),
};
element.editorOptions = { drafts };Drafts are only a safety net. Save documents for real with onSave or getDocument.
The whole flow in your app#
A document kept on your server goes round like this:
- Open: your page fetches the saved JSON (or your server reads it while rendering) and opens it with
setDocument. The person sees it exactly as they left it. - Edit:
changefires as they type. Save a few seconds after the last change, and on Save and Ctrl+S (onSave). - Export:
file.saveDocx(orexportDocument("docx")) makes the Word file from what's on screen: the saved document plus anything changed since. - Close and come back: step 1 again.
Complete, tested code for each framework, with your server's routes, autosave, a save status and a download button:
- React (Vite and Express)
- Next.js (App Router, Server Components and a route handler; the Pages Router too)
Good to know:
- Opening isn't an edit.
setDocumentdoesn't firechange, so opening never triggers your autosave. - What comes back: the text and its formatting, styles, lists, tables, pictures, headers and footers, page setup, page numbers, comments and the title. What doesn't: the cursor position, scroll position, zoom and undo history. The document opens at the top, with nothing to undo.
- Exporting needs the editor. A Word file is made by an editor open in a browser, for the person using it. Your server can't turn stored JSON into
.docxon its own (for example in a nightly job). To keep Word files on your server, upload theBlobfromexportDocument("docx")while the person has the document open. - Drafts: don't combine
autosave-keywith your own saving for the same document. Your server's copy is the one that counts, andsetDocumentreplaces any draft.
What Word features come through#
Saving keeps what the editor can show and edit: text and its formatting, styles, lists, tables, pictures and their wrapping, shapes and text boxes, headers and footers, page numbers, page size and margins, footnotes, comments, links and bookmarks, tables of contents and figures, captions and cross-references, and equations. Opening a .docx brings in the same. Word features the editor has no tools for, such as macros, form controls and embedded objects, may not survive a round trip: test your own documents if you rely on them.