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#
npm install @dorvyn/elementsImport it once, anywhere in your page's JavaScript, to register the elements:
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#
<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:
<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#
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:
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));| Event | detail | When |
|---|---|---|
dorvyn-change | { document } | The document changed (at most every 0.7 s while someone types) |
dorvyn-error | the error | The editor couldn't start, or an action failed |
dorvyn-notice | { message } | A message for the person ("Select a picture first") |
dorvyn-open | the request | The 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):
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:
element.license = await getKeyFromSomewhere(); // a property: not written into the page's HTMLVue, Svelte and others#
Tell your framework that dorvyn-* tags are custom elements, then use them as above.
// 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.