Skip to the content

Frameworks

HTML and web components

@dorvyn/elements is the editor as web components. It works on any page and with any framework: plain HTML, Vue, Svelte, Solid, Lit, or a server-rendered app. The React, Next.js and Angular packages are thin wrappers around these same elements.

Install#

Terminal
npm install @dorvyn/elements

Import it once, anywhere in your page's JavaScript, to register the elements:

JavaScript
import "@dorvyn/elements/define";

It's an ES module, so use it through your bundler (Vite, webpack, esbuild, Rollup…) or a <script type="module">.

The full editor#

HTML
<dorvyn-editor server-url="/api/dorvyn" autosave-key="my-doc" style="display: block; height: 100vh"></dorvyn-editor>

With nothing inside it, <dorvyn-editor> is the complete editor. Give it a height: it fills its box. server-url is your server's Dorvyn routes (strict mode). For development with a publishable key, use license="dvn_pk_test_…" instead.

Your own layout#

Put the pieces you want inside it, and leave out the rest:

HTML
<dorvyn-editor id="editor" server-url="/api/dorvyn">
  <dorvyn-ribbon tabs="home,insert,layout"></dorvyn-ribbon>
  <div style="display: flex; height: 80vh">
    <dorvyn-document style="display: flex; flex: 1"></dorvyn-document>
    <dorvyn-panes style="width: 280px"></dorvyn-panes>
  </div>
  <dorvyn-dialogs></dorvyn-dialogs>
  <dorvyn-context-menu></dorvyn-context-menu>
</dorvyn-editor>

Every piece is described in The full editor and layouts.

Using the editor from your code#

JavaScript
const element = document.getElementById("editor");
const editor = await element.whenReady();

await editor.commands.run("bold");
editor.state.select((s) => s.words, (words) => (counter.textContent = `${words} words`));

whenReady() resolves with the editor once it has started. The editor's API is in Commands, state and lists and Events and license states.

Events#

The element fires DOM events that bubble, so you can listen on it or on any ancestor:

JavaScript
element.addEventListener("dorvyn-change", (e) => queueSave(e.detail.document));
element.addEventListener("dorvyn-error", (e) => console.error(e.detail));
element.addEventListener("dorvyn-notice", (e) => toast(e.detail.message));
element.addEventListener("dorvyn-open", (e) => console.log("wants", e.detail));
EventdetailWhen
dorvyn-change{ document }The document changed (at most every 0.7 s while someone types)
dorvyn-errorthe errorThe editor couldn't start, or an action failed
dorvyn-notice{ message }A message for the person ("Select a picture first")
dorvyn-openthe requestThe editor wants a dialog, pane or view
dorvyn-theme"light" or "dark"The person changed the theme

Options that attributes can't carry#

Set editorOptions on the element before it's on the page (or before it gets its license or server-url):

JavaScript
const element = document.createElement("dorvyn-editor");
element.editorOptions = {
  onSave: async (doc) => {
    await fetch("/api/documents/42", { method: "PUT", body: JSON.stringify(doc) });
  },
};
element.setAttribute("server-url", "/api/dorvyn");
element.style.cssText = "display: block; height: 100vh";
document.body.append(element);

Every option is in Options and attributes.

A key that arrives later#

The editor starts when it has a license or a server-url. If the key comes later (fetched, or from the user's settings), add the element without one and set it when you have it:

JavaScript
element.license = await getKeyFromSomewhere();   // a property: not written into the page's HTML

Vue, Svelte and others#

Tell your framework that dorvyn-* tags are custom elements, then use them as above.

JavaScript
// vite.config.js (Vue)
vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.startsWith("dorvyn-") } } });

Svelte and Solid need nothing extra. Pass options as a property (element.editorOptions = …) rather than an attribute.