Skip to the content

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 wantUse
People open and save .docx files themselvesNothing: 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 filesexportDocument("docx") / importDocument(blob)
Work survives a closed tabautosave-key (drafts)

Keeping documents in your own storage#

The editor gives you the document as plain JSON (EditorDocument), and opens it again later:

JavaScript
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());
  • setDocument waits 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.
  • EditorDocument has a format ("dorvyn.document") and a version. Keep them with it. Newer SDKs keep opening older versions. setDocument refuses anything that isn't an EditorDocument.
  • 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:

TSX
<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
  }}
/>
JavaScript
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.saveTo is "page" with onSave, and "download" without it, so your own Save button can say what it does.

Saving as they type#

JavaScript
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:

JavaScript
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)
  • importDocument waits for the license, like setDocument. Exporting needs the license too.
  • .docx is 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 .docx to your server as a file: new FormData() with the Blob.

Commands for the same#

CommandWhat it does
file.newA new document: "blank", "report" or "letter"
file.openOpen a .docx, web page, text or Markdown file (a Blob or ArrayBuffer; the file picker without one)
file.saveSave: to onSave, or as .docx
file.saveDocx, file.saveHtml, file.saveTextDownload as .docx, web page or text
file.renameRename the document (the saved file is named after it)
printPrint, 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:

HTML
<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. Without autosave-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.saveStatus is "saved", "saving" or "unsaved", or "toolarge" if the browser has no room left for it.
  • file.discardDraft forgets 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:

TypeScript
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:

  1. 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.
  2. Edit: change fires as they type. Save a few seconds after the last change, and on Save and Ctrl+S (onSave).
  3. Export: file.saveDocx (or exportDocument("docx")) makes the Word file from what's on screen: the saved document plus anything changed since.
  4. 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. setDocument doesn't fire change, 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 .docx on its own (for example in a nightly job). To keep Word files on your server, upload the Blob from exportDocument("docx") while the person has the document open.
  • Drafts: don't combine autosave-key with your own saving for the same document. Your server's copy is the one that counts, and setDocument replaces 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.