Skip to the content

The editor

Building your own ribbon

You can replace Dorvyn's ribbon with your own: your markup, your CSS, your framework. The editor stays the same; your controls drive it through commands, state and lists, the same API Dorvyn's ribbon is built on.

This page builds a complete Home ribbon (every option Dorvyn's Home tab has) in about 100 lines of plain JavaScript, step by step. The same approach works for any tab, or a toolbar of your own design.

1. The page: the editor without our ribbon#

Keep <dorvyn-editor> and <dorvyn-document>; leave out <dorvyn-ribbon>. Your toolbar can be anywhere on the page.

HTML
<div id="my-ribbon" role="toolbar" aria-label="Home">
  <button data-cmd="bold"><b>B</b></button>
  <button data-cmd="italic"><i>I</i></button>
  <button data-cmd="align.center">Center</button>
  <select id="font" aria-label="Font"></select>
  <input id="size" list="sizes" aria-label="Font size"><datalist id="sizes"></datalist>
  <!-- … -->
</div>

<dorvyn-editor id="editor" license="dvn_pk_live_…">
  <dorvyn-document></dorvyn-document>
</dorvyn-editor>

<script type="module" src="my-ribbon.js"></script>

A convention that keeps the script short: each simple button names its command in data-cmd, and its value, if it has one, in data-arg (<button data-cmd="direction" data-arg="rtl">).

2. Get the editor#

JavaScript
import "@dorvyn/elements/define";

const editor = await document.getElementById("editor").whenReady();
const { commands, state, lists } = editor;

3. Fill your menus from the editor's lists#

So your menus offer exactly what the editor supports:

JavaScript
const $ = (id) => document.getElementById(id);
$("font").innerHTML = lists.fonts.map((f) => `<option>${f}</option>`).join("");
$("sizes").innerHTML = lists.sizes.map((s) => `<option>${s}</option>`).join("");
$("style").innerHTML = lists.styles.map((s) => `<option value="${s.key}">${s.label}</option>`).join("");
$("line-spacing").innerHTML = lists.lineSpacings.map((v) => `<option>${v}</option>`).join("");

lists.themeColors and lists.highlights are the colour palettes Word offers.

4. Wire the controls to commands#

Buttons:

JavaScript
const ribbon = $("my-ribbon");
ribbon.querySelectorAll("button[data-cmd]").forEach((b) => {
  b.onclick = () => commands.run(b.dataset.cmd, b.dataset.arg);
});

Boxes and menus that take a value:

JavaScript
$("font").onchange = (e) => commands.run("fontFamily", e.target.value);
$("size").onchange = (e) => commands.run("fontSize", Number(e.target.value));
$("style").onchange = (e) => commands.run("style", e.target.value);
$("line-spacing").onchange = (e) => commands.run("lineSpacing", e.target.value);
$("font-color").oninput = (e) => commands.run("color", e.target.value);
$("bullet-style").onchange = (e) => commands.run("list.bullets", e.target.value);       // "square"…
$("number-style").onchange = (e) => commands.run("list.numbers", e.target.value);      // "lower-roman"…
$("case").onchange = (e) => commands.run("changeCase", e.target.value);                // "upper"…
$("indent-left").onchange = (e) => commands.run("paragraph.indent", { left: Number(e.target.value) });
$("space-after").onchange = (e) => commands.run("paragraph.spacing", { side: "after", pt: Number(e.target.value) });

Colour split buttons (apply the colour chosen beside them):

JavaScript
$("highlight-apply").onclick = () => {
  const c = $("highlight-color").value;
  commands.run("highlight", c === "none" ? null : c);
};

5. Show the state at the cursor#

One callback keeps the whole toolbar current as the person moves the cursor and edits:

JavaScript
state.select((s) => s, (s) => {
  ribbon.querySelectorAll("button[data-cmd]").forEach((b) => {
    b.disabled = !commands.can(b.dataset.cmd);                 // read-only or no license
    b.classList.toggle("on", commands.active(b.dataset.cmd));  // bold, centred, in a list…
  });
  if (s.format) {
    $("font").value = s.format.font;
    $("style").value = s.format.styleKey;
    if (document.activeElement !== $("size")) $("size").value = s.format.size;
  }
  $("indent-left").value = s.paragraph.indentLeft;
});

Don't overwrite a box while the person is typing in it (the activeElement check above).

The rules that keep it working#

  1. Keep the cursor in the document. A click on a button normally moves the focus to the button, and the person has to click back into the page to keep typing. Prevent that on your toolbar (but not on its boxes and menus, which need the focus):

    JavaScript
    ribbon.addEventListener("mousedown", (e) => {
      if (!e.target.closest("input, select")) e.preventDefault();
    });

    Commands still apply to the last selection either way; this is about typing on afterwards.

  2. Run clipboard commands from a click. cut, copy, paste and pasteText work only inside a person's click or key press: browsers allow the clipboard nowhere else.

  3. Dialogs are yours. Commands that need values (link, caption, equation, crossReference, find, replace) take them as arguments. Collect them in your own dialog, then run the command:

    JavaScript
    commands.run("link", { url: form.url.value, text: form.text.value });

    The editor asks for dialogs itself too (Ctrl+K, Ctrl+F, Alt+=, clicking an equation or a comment), and tells you what they should start with: answer editor.on("open", …).

    JavaScript
    editor.on("open", (req) => {
      if (req.dialog === "link") myLinkDialog.show({ text: req.text, url: req.url, editing: req.editing });
      if (req.pane === "find") myFindBar.show(req.query);
    });
    editor.on("notice", ({ message }) => myToast.show(message));

    Run the command before you close your dialog: the editor applies it to the selection it kept while your dialog had the focus.

  4. Disable with can, not your own rules. commands.can(id) already knows about read-only, Viewing and the license.

In React and Angular#

The same pattern, with the wrappers keeping it current:

TSX
// React
function BoldButton() {
  const bold = useCommand("bold");
  return <button {...bold.buttonProps}>B</button>;   // disabled, aria-pressed, and keeps the cursor in the document
}

function FontBox() {
  const editor = useEditor();
  const font = useEditorState((s) => s.format?.font ?? "", "");
  return (
    <select value={font} onChange={(e) => editor?.commands.run("fontFamily", e.target.value)}>
      {editor?.lists.fonts.map((f) => <option key={f}>{f}</option>)}
    </select>
  );
}
HTML
<!-- Angular, inside <dorvyn-editor … #ed="dorvynEditor"> -->
<button dorvynCommand="bold" #b="dorvynCommand" [class.on]="b.active()" [disabled]="!b.enabled()">B</button>
<select (change)="ed.editor()?.commands.run('fontFamily', $any($event.target).value)">
  @for (f of ed.editor()?.lists?.fonts ?? []; track f) {
    <option [selected]="ed.state()?.format?.font === f">{{ f }}</option>
  }
</select>

React's useCommand(…).buttonProps, Angular's dorvynCommand and <dorvyn-command-button> keep the cursor in the document (rule 1).

Mixing ours and yours#

You don't have to choose all or nothing. Our pieces can sit inside your toolbar:

HTML
<div id="my-ribbon">
  <dorvyn-group name="font" for="editor"></dorvyn-group>   <!-- our Font group -->
  <button data-cmd="myapp.insertClause">Insert clause</button>
</div>

See Styling for restyling our pieces instead of replacing them.