Foli

Document engine for the web · MIT

Edit rich text
documents
in the browser.

Foli is an open-source editing engine with real pages, Word's document model and lossless .docx round-trips. Its model, transactions, view and layout are its own, so page breaks are computed by the engine as you type.

npm i @folijs/react @folijs/core
Get started GitHub

MIT · v0.5.0 · React or plain DOM · Chromium, Firefox, WebKit

  • PagesA4, Letter or any size, per section; widows, orphans, keep with next
  • ModelWord's: paragraph styles, numbering, tables, pictures
  • .docxIn and out; parts Foli does not edit come back byte for byte
  • ReviewTracked changes and comments that round-trip with Word

Live The real editor, running in this page.

FoliPagination

The page is the unit of layout.

Most web editors draw page breaks over one long scroll. Foli lays text into pages: it measures every line box, breaks paragraphs where they fit, and re-flows only what an edit moved. In the editor above, the paragraph that starts "Every page here is A6" breaks this way from page 1 to page 2.

Split at a line
A paragraph continues on the next page from the line that did not fit.
Widows and orphans
No single line left alone at the top or bottom of a page.
Keep with next
A heading never ends a page without its paragraph.
Keep lines together
A paragraph moves whole instead of splitting.
Sections
Each section has its own page size and margins.
Print
One sheet per page, at the section's size.
1 1
2 3 2
  1. 1The paragraph breaks after the last line that fits.
  2. 2At least two lines carry over: no widow.
  3. 3The heading stays with its paragraph.
1
FoliGetting started

One component, real pages.

FoliEditor brings the pages, a toolbar and a comments pane. Load a saved document with parseDocument, which validates it and migrates older versions. Save from onChange.

Prefer your own toolbar, or no React at all? The getting started guide covers both, plus Next.js and read-only mode.

Editor.tsx
import { createDocument, parseDocument } from "@folijs/core";
import { type FoliChange, FoliEditor } from "@folijs/react";
import "@folijs/react/styles.css";

const saved = localStorage.getItem("doc");
const initial = saved
  ? parseDocument(JSON.parse(saved))
  : createDocument();

const save = (c: FoliChange) =>
  localStorage.setItem("doc", JSON.stringify(c.document));

export function Editor() {
  return (
    <FoliEditor initialContent={initial} onChange={save} />
  );
}
2
Foli.docx

Open a .docx. Save it back. Lose nothing.

@folijs/docx reads Word files into a model shaped like Word's own: paragraph styles, numbering, runs with their properties, tables, pictures, sections, comments and tracked changes. On export, the parts Foli does not edit come back byte for byte, and unchanged elements verbatim.

What the editor does not model yet stays in the document as a block it keeps and writes back. The import report tells you what those are.

open-and-save.ts
import { importDocx, exportDocx } from "@folijs/docx";

const { document, package: pkg, report } =
  await importDocx(bytes);
report.opaque; // { "w:drawing": 1 }: kept as is

// …edit…
const doc = editor.getDocument();
const out = await exportDocx(doc, pkg);
word/document.xmledited kept verbatim
<w:p w14:paraId="1E2F0A3C">  <w:pPr><w:pStyle w:val="Heading1"/></w:pPr>  <w:r><w:t>Terms of service</w:t></w:r></w:p><w:p>  <w:r><w:t xml:space="preserve">Payment is due in   </w:t></w:r><w:ins w:author="You">    <w:r><w:t>30</w:t></w:r></w:ins>  <w:r><w:t xml:space="preserve"> days.</w:t></w:r></w:p><w:p>  <w:r><w:drawing>    <wp:inline>…<c:chart r:id="rId7"/>…</wp:inline>  </w:drawing></w:r></w:p>
A chart Foli does not edit yet, written back as it came in.
3
FoliReview

Track changes and comments, the way Word does them.

Turn on tracking and every insertion, deletion and formatting change is recorded in the author's name as they type. Show all markup, simple markup or none. Accept or reject one change, a selection, one author's work, or everything.

Comments sit in a pane beside the pages, with replies and resolved state. Both round-trip with Word: open a reviewed file, keep reviewing, send it back.

4. Payment

The Client pays each invoice within fourteen (14) thirty (30) days of receiving it. Late payments carry interest at the statutory rate.

Invoices are sent by email at the end of each calendar month.

MMarta 2 h

Which statute? Name it, or legal will ask.

YYou now

The Late Payment Directive. Naming it now.

Drawn for this page. The editor above records the real thing: turn on Track changes and edit.
4
FoliEngine

An engine of its own, from model to layout.

No ProseMirror, Tiptap, Lexical or Slate underneath. Foli has its own immutable document model, steps and transactions with undo, a view that maps the screen back to document positions, and the layout that cuts it into pages. Pagination is part of the engine, so the cursor, selection and undo all know where pages begin.

The model has no DOM: it runs in Node, workers and tests. The view works without React. Documents are plain JSON you can store anywhere.

Under a frame. In Chromium, a keystroke painted in at most 13.2 ms (p95) in documents up to 100 pages, with Enter moving every later page. Method and numbers, measured in Phase 5, before schema v3.

@folijs/reactEditor, toolbar, comments pane, SSR88.6 kB
@folijs/viewDOM view, input, IME, layout into pages64.2 kB
@folijs/coreModel, steps, history, commands. No DOM.52.7 kB
@folijs/docxImport and lossless export. No DOM.51.9 kB
The view and React packages build on core; docx needs only core. Sizes: minified and gzipped with the Foli packages each pulls in, React excluded, measured on main after 0.5.0.
5
FoliStatus

Where it stands.

Foli is young and says so. The API may change in any 0.x release, and some checks have not been done by hand yet: IME on real devices, screen readers, Safari on a Mac. The known limitations list them all. Read it before you ship.

  1. Engine, model, transactions, view0.1
  2. Native pagination0.1
  3. Lists, links, colors, paste0.2
  4. Word's model, .docx import and export0.3
  5. Tracked changes and comments0.4
  6. Tables and pictures0.5
  7. Sections, headers and footers, fields, templates, PDFin progress
  8. Ecosystem and 1.0next