Docs / Guides
Office Suite
The Office studio is a suite: a launcher plus one app per document kind. The word processor is the first app
(kind: "document", user guide: office-studio.md); Spreadsheet (sheet), Presentation
(slides) and Whiteboard (board) plug in through the same contract. This page is for engineers who add or change
an Office app. Decision record: ADR 0016.
Architecture#
ui/shell.js ──import()──► office/host.js ──► office/launcher.js (no Office document active, or Home)
│
└──► office/apps.js registry ──load()──► <app impl>.mount(el, doc, app) → instance
▲ = app.officeUI
office/index.js registers manifests ─────┘
commands/file.js ─► ui/office/open.js ─► registry.appForFile / engine/office detectOfficeFormat ─► impl.open()
- Manifest (plain data, registered when the suite first loads): kind, label, icon, formats, templates, AI note,
load(). Cheap; the launcher, file routing and Vibe use only this. - Implementation (loaded with
import()the first time a document of that kind is opened, created or exported): create / open / save / mount and optional adapters. - Document storage:
doc.officeis acore/office.js ▸ OfficeProject { kind, model, view }. The model is your app's plain JSON. In.ofiniit isdocument.json → "office"plusoffice/<kind>/media/*entries (file-format.md).kindis additive (absent ="document"); writers stamp format version 2 only for non-document kinds, so older OpenFini versions warn instead of opening a spreadsheet as an empty text document. - Nothing of the suite is in the start-up bundle (
tests/architecture/guardrails.test.jslistssrc/office/as lazy). Start-up keeps only the studio definition and menus (studios/office.js).
Where things live#
| Part | File |
|---|---|
| Registry (pure) | app/src/office/apps.js |
| Built-in manifests (one line per app) | app/src/office/index.js |
| Suite host (bar, kind switcher, mounting) | app/src/office/host.js |
| Launcher (new, templates, recent, open, Google import) | app/src/office/launcher.js |
| Export service | app/src/office/services/export.js |
| Vibe prompt summary | app/src/office/ai.js |
Document app (adapter around ui/office/, engine/office/) | app/src/office/document/{manifest,app}.js |
| Presentation app (office-slides.md) | app/src/office/slides/ |
| Office Toolkit | app/src/office/toolkit/ (ui.js DOM; selection, virtual, find, clipboard, numfmt, rough, export pure or engine-only) |
| Styles (suite + toolkit) | app/styles/office-suite.css (your app: its own stylesheet) |
| Storage, undo | app/src/core/office.js (OfficeProject, commitOffice) |
| File opening / creating | app/src/ui/office/open.js (openOfficeFile, createOfficeDocument, openOfficeModel) |
| Content sniffing + guidance texts | app/src/engine/office/index.js (FORMATS, detectOfficeFormat, officeGuidance) |
| Save in the original format | app/src/core/source-format.js, app/src/commands/save-source.js |
| Live editing | app/src/collab/office-records.js, office-sync.js, doc-records.js (word processor) + ui/collab/live.js ▸ OfficeBinding |
| Comment anchors | app/src/collab/anchors.js ▸ officeItemAnchor, protocol.js ANCHOR kind "item" |
| Vibe tools | app/src/vibe/tools/office-suite.js (all kinds), office.js (text documents), office-board.js (whiteboards) |
| Whiteboard app | app/src/office/board/ (office-board.md) |
| Tests | tests/office/suite.test.js, tests/office/toolkit.test.js, tests/collab/office-records.test.js, tests/collab/office-sync.test.js |
| Spreadsheet app | app/src/office/sheet/ (office-sheet.md) |
The contract#
Manifest: registerOfficeApp(manifest)#
registerOfficeApp({
kind: "sheet", // [a-z][a-z0-9-]{1,23}; stored in files, never rename
label: "Spreadsheet", // English; the UI translates with t()
icon: "table", // Bootstrap icon in app/vendor/shoelace/assets/icons (or the "of" set)
blurb: "Budgets, lists and calculations: Excel, OpenDocument, CSV",
order: 20, // launcher/kind switcher order: document 10, sheet 20, slides 30, board 40
formats: [ // read = this app opens it; write = save()/export; template = opens as new
{ id: "xlsx", label: "Excel workbook", ext: ["xlsx", "xlsm"], mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", read: true, write: true },
{ id: "xltx", label: "Excel template", ext: ["xltx"], read: true, template: true },
{ id: "csv", label: "CSV", ext: ["csv", "tsv"], mime: "text/csv", read: true, write: true, lossy: true },
{ id: "pdf", label: "PDF", ext: ["pdf"], mime: "application/pdf", write: true },
],
templates: [{ id: "blank", label: "Blank spreadsheet" }, { id: "budget", label: "Budget", description: "…", icon: "currency-dollar", category: "Business" }],
aiNote: "The active Office document is a spreadsheet. Read it with sheet_read … before editing.",
load: () => import("./app.js"), // the implementation module (default export)
});
Registration throws for an invalid/duplicate kind, a missing label/load, or an extension another app already
reads. Manifests are frozen. Other registry functions: officeApps(), officeApp(kind), appForFile(name),
officeOpenExtensions(), loadOfficeApp(kind) (cached promise, validates the impl with checkImpl),
loadedOfficeApp(kind) (sync, null until loaded), officeKindOf(doc).
Implementation (default export of load())#
| Member | Required | Signature and meaning |
|---|---|---|
create(opts) | yes | ({ template, title, ...}) → model | Promise<model>: a new model (templates of the manifest). |
mount(host, doc, app) | yes | Build your UI inside host (an element the suite owns, one per document; keep your state on the instance). Return the instance, which becomes app.officeUI while the document is active. |
open(bytes, name, { report, app }) | for read formats | → { model, asNew? }. Fill the ImportReport (report.kept/simplified/dropped, see core/import-report.js). asNew (or a template format) opens as an untitled copy. |
save(model, formatId, { title, doc }) | for write formats | → { bytes, mime?, ext?, notes?: string[] }. Used by Export, File ▸ Save in the original format and office_export. notes say what the format cannot keep. |
describe(model) | recommended | Small JSON summary (no DOM) for Vibe office_describe and the prompt. |
aiAdapter | optional | { describe(model) } (overrides describe for Vibe). App tools are static (see Vibe below). |
collabAdapter | optional | { records(model) → { key: json }, applyRecords(model, { key: json | null }) → model, inlineMedia?, prepare?(prj) } or a stateful { bind() → { records, applyRecords, rebase? } } (see Collaboration). |
Instance (returned by mount, all optional except as noted):
| Member | Used by |
|---|---|
kind, doc | set by the host if you do not |
activate() / deactivate() | the host when the document becomes active / inactive (focus, relayout, listeners) |
destroy() | the host when the document closes (disconnect observers, el.destroy?.() of toolkit parts) |
statusText() | status bar (emit app.emit("officestatus") when it changes) |
exportDialog() | office.export (else the generic export dialog of services/export.js) |
commentAnchor() → anchor | Collaboration ▸ Add comment: name the selection with officeItemAnchor({ item, label }) |
revealAnchor(anchor) | clicking a comment: select / scroll to anchor.item |
Rules every app follows#
- Undo: change the model only with
commitOffice(doc, label, nextModel, { coalesce })(core/office.js). Models are immutable values: build a new object per change. Listen todoc.office.on("change", info);info.originis"undo","redo"or"collab"(remote, not in history). - Commands, menus, shortcuts: register commands in a new
app/src/commands/office-<kind>.js(ids<kind>.<action>,studios: ["office"],enabled: (a) => a.officeUI?.kind === "<kind>"), and add top-level menus toOFFICE_MENUSinstudios/office.jswithofficeKinds: ["<kind>"]so they show only while your app is active. Never wire behavior directly into a button. - Context menus:
officeContextMenu(app, event, "office-<kind>", data)from the toolkit plusregisterContextProvider({ matches: (ctx) => ctx.kind === "office-<kind>" … })(context-menus.md). - Find / replace: flatten text items to
{ id, text }, usetoolkit/find.js, commit replacements as one step. - Clipboard:
toolkit/clipboard.js(writeClipboard({ text, html, json, png }), TSV viatoDelimited). - Print / PDF / PNG: render pages as display lists →
toolkit/export.js ▸ displayListsToPdf; canvases →canvasToPng; SVG →svgToPng. - i18n / a11y / theme:
t("English"), German inapp/src/i18n/locales/de.json(npm run i18n:extract); every control labelled and keyboard reachable; colors only from--of-*tokens; respectprefers-reduced-motion. - Size: the impl and everything it imports must be lazy; heavy parsers go behind another
import()inopen().
Worked example: a minimal app (about 60 lines)#
// app/src/office/notes/manifest.js
export const NOTES_APP = {
kind: "notes", label: "Notes", icon: "sticky", order: 50,
formats: [{ id: "txt", label: "Plain text", ext: ["note"], mime: "text/plain", read: true, write: true }],
templates: [{ id: "blank", label: "Blank notes" }, { id: "todo", label: "To-do list", icon: "list-check" }],
aiNote: "The active document is a notes list: read it with office_describe.",
load: () => import("./app.js"),
};
// app/src/office/notes/app.js
import { h } from "../../core/util.js";
import { commitOffice } from "../../core/office.js";
import { ribbon, button, officeContextMenu } from "../toolkit/ui.js";
const lines = (m) => m.items.map((i) => i.text);
export function describe(model) {
return { items: model.items.length, first: model.items[0]?.text || "" };
}
export default {
create: ({ template }) => ({ items: template === "todo" ? [{ id: "i1", text: "First task" }] : [] }),
open: async (bytes) => ({ model: { items: new TextDecoder().decode(bytes).split(/\r?\n/).filter(Boolean).map((text, i) => ({ id: `i${i + 1}`, text })) } }),
save: async (model) => ({ bytes: new TextEncoder().encode(lines(model).join("\n")), mime: "text/plain", ext: "note" }),
describe,
aiAdapter: { describe },
collabAdapter: {
records: (m) => Object.fromEntries(m.items.map((i, k) => [`item:${i.id}`, { text: i.text, pos: k }])),
applyRecords: (m, ch) => {
const map = new Map(m.items.map((i, k) => [`item:${i.id}`, { text: i.text, pos: k }]));
for (const [k, v] of Object.entries(ch)) v === null ? map.delete(k) : map.set(k, v);
const items = [...map].sort((a, b) => a[1].pos - b[1].pos).map(([k, v]) => ({ id: k.slice(5), text: v.text }));
return { ...m, items };
},
},
mount(host, doc, app) {
const list = h("ul", { role: "list" });
const render = () => list.replaceChildren(...doc.office.model.items.map((i) => h("li", i.text)));
const add = () => commitOffice(doc, "Add Note", { ...doc.office.model, items: [...doc.office.model.items, { id: `i${Date.now()}`, text: "New note" }] });
const bar = ribbon([{ id: "edit", label: "Edit", items: [button({ icon: "plus-lg", label: "Add note", onClick: add })] }]);
list.addEventListener("contextmenu", (e) => officeContextMenu(app, e, "office-notes", { doc }));
const off = doc.office.on("change", render);
host.append(bar, list);
render();
return {
statusText: () => `${doc.office.model.items.length} notes`,
commentAnchor: () => null,
destroy: () => (off?.(), bar.destroy()),
};
},
};
Then add import { NOTES_APP } from "./notes/manifest.js"; and NOTES_APP, to office/index.js. The launcher shows
"Blank notes" and the "To-do list" template, File ▸ Open routes .note (also add the extension to OFFICE_FILE in
commands/file.js and OFFICE_ONLY_EXTS in engine/office/index.js; a test keeps them equal), Export and Vibe's
office_describe / office_new / office_export work without further code.
File routing#
commands/file.jssends every extension inOFFICE_FILE(=engine/office/index.js ▸ OFFICE_ONLY_EXTS) toui/office/open.js ▸ openOfficeFile. In the Office studio,.html .md .txt .csv .tsvgo there too; elsewhere they keep their studios (Code studio).detectOfficeFormat(name, bytes)sniffs content first (a renamed.xlsxis still a workbook; OLE binaries, ODF mimetypes, flat ODF XML, RTF) and maps it to a kind (document,spreadsheet→sheet,presentation→slides,whiteboard→board).- If a registered app reads that format, its
open()runs, the import report is attached, the file becomes a recent file, and for non-document kindsdoc.sourcerecords{ officeKind, officeId }so File ▸ Save writes the original format again throughsave()(core/source-format.js, commands/save-source.js). - Otherwise
officeGuidance()explains the state honestly: Google.gdoc/.gsheet/.gslides(cloud links; download a copy, button opens the document in the browser), Apple iWork, legacy binaries (.doc,.xls,.ppt: convert first), and kinds whose app is not in this version. - OS integration:
platform/file-types.jsis the single table (rowskind: "office"for docx, odt, rtf, xlsx, ods, pptx, odp, excalidraw, tldraw). Adding a row means updatingsrc-tauri/tauri.conf.json,tauri.windows.conf.json(owned types only),src-tauri/windows/open-with.nsh,src-tauri/linux/openfini.desktopandapp/manifest.webmanifest;tests/packaging/os-integration.test.jsfails on drift. OpenFini is only an "Open with" candidate for these types, never the default.
Vibe#
app/src/vibe/tools/office-suite.js(group Office suite):office_formats(apps, formats, templates),office_list_documents,office_describe(kind-aware viaaiAdapter.describe),office_new(kind + template).office_export(inoffice.js) exports any kind throughsave(). Files arrive throughopen_attachment/ File ▸ Open, which use the same routing.- The system prompt has an
officestudio note and the active document's summary (office/ai.js ▸ officeSummary: label, kind,describe()when the app is loaded, and the manifest'saiNote). - App tools are static: put them in
app/src/vibe/tools/office-<kind>.jsexporting(app) => Tool[], register them invibe/tools/index.jsandTOOL_GROUPS(tests/ai/tool-contract.test.js). Insiderun(), checkapp.doc?.office?.kind === "<kind>",await loadOfficeApp(kind)if you need the impl, and commit withcommitOffice(doc, "Vibe: …", next)so the user can undo the step. The text-document tools refuse other kinds.
Collaboration#
- Live editing ("Edit together"): give the impl a
collabAdapter.records(model)flattens the model into stable keys ("cell:<sheet>!B3","obj:<slide>/<id>","item:<id>","meta:title");applyRecords(model, changes)returns the new model with{ key: value | null }applied (null = deleted).OfficeBindingdiffs your records after every model change (60 ms batches), stamps ops{ k, v, c, s }(Lamport clock + site) and merges remote ops: the same record goes to the higher(clock, site), different records merge, remote changes bypass the undo history (info.origin === "collab", withinfo.prev, the model before, andinfo.remote: true). Keep orders as fractional keys (collab/fractional.js) inside records, never as arrays, so concurrent moves do not fight. Limits: key ≤ 256 chars, value ≤ 64 KiB JSON (larger values are kept back and counted; Resync brings them). The engine (collab/office-sync.js) guarantees what used to go wrong:- local edits are the records that changed since the binding last looked at the model, never "the view minus the
model", so
applyRecordsmay refuse or normalize a record (an element whose page has not arrived) without that turning into a delete for everybody; refused keys are retried with the next remote message; - Undo/Redo: while a document is live,
prj.rebase(cur, from, to)is set andcommitOfficeapplies only the records of the step on top of the current model, so other people's edits made in between stay; - media:
inlineMedia(bytes) says up to which size your records carry media bytes themselves; largermodel.media[id].bytestravel as chunk recordsblob:<id>/<i>(45 KiB each) and are put back intomodel.media[id].byteswhen complete. YourapplyRecordsmust keep existingbytesand ignoreblob:keys; prepare(prj)normalizes a model that was loaded but not opened yet (_raw) before the first records are taken;- a stateful adapter (
bind()) gets one instance per shared document: the word processor (collab/doc-records.js) keeps block ids in aWeakMapby block object and implements its ownrebase. Every app has an adapter: Document (one record per block: different paragraphs merge, the same paragraph goes to the later writer), Spreadsheet (per cell), Presentation (per slide and object), Whiteboard (per element). An Office kind an extension registers without an adapter is shared as a copy.
- local edits are the records that changed since the binding last looked at the model, never "the view minus the
model", so
- Comments: anchor kind
item(collab/anchors.js ▸ officeItemAnchor({ item, label })) uses the same record keys; the instance'scommentAnchor()andrevealAnchor(anchor)connect it to your selection. Text documents keep thetextanchor.
Office Toolkit#
All in app/src/office/toolkit/; module headers document the full API.
| Module | What |
|---|---|
ui.js | button, ribbon(groups) (groups with overflow into "More", arrow-key focus), formulaBar, colorPicker, fontControls, numberFormatMenu, zoomControl, splitPanels, ruler, virtualGrid({ rows, cols, renderCell }), dragGesture, officeContextMenu, re-exported openDialog/confirmDialog/promptDialog |
selection.js | A1 refs and ranges (colName, parseRef, parseRange, formatRange, moveCell, extendRange), createItemSelection |
virtual.js | createAxis({ count, size }): offsets, custom sizes, indexAt, visible() in O(log n) (million-row sheets) |
find.js | findMatches, replaceMatches, compileQuery (case, whole word, regex; never throws) |
clipboard.js | parseDelimited / toDelimited (RFC 4180, TSV), sniffSeparator, rowsToHtml, writeClipboard, readClipboard |
numfmt.js | NUMBER_FORMATS, formatValue(value, fmt, locale) (number, percent, currency, accounting, scientific, date/time from serial days) |
rough.js | hand-drawn strokes: roughLine/Rect/Ellipse/Polygon, hachure, strokesToPath, drawStrokes; seeded, deterministic |
export.js | displayListsToPdf, canvasToPng, svgToPng |
Testing#
- Pure logic (models, codecs, adapters'
records/applyRecords, toolkit) gets unit tests undertests/office/. Register a fake app in a test withregisterOfficeAppand remove it withunregisterOfficeApp(seetests/office/suite.test.js). - Round trip your model through
OfficeProject.manifest()/entries()/fromManifest()andDoc.toJSON(). - Codecs get hostile-input tests (zip limits from
engine/office/zip.js, no entity expansion). - Browser check:
node scripts/serve.mjs --port <n>,openfini.setStudio("office"), create from the launcher, open a file with(await import("/src/ui/office/open.js")).openOfficeFile(openfini, { name, bytes }). npm run build && node scripts/check-size.mjsmust pass; the suite adds nothing to start-up.
Roadmap: Office depth#
Done: semantic section tools, cross-app tools (tables → sheet, sheet range → table, outline → slides, board → document, sheet range → slide chart), the accessibility check, Office skills and spreadsheet data bars (vibe-studio.md); presentation slide master and layout editing, footers and slide numbers, mixed formatting in a text box, charts from sheet data, designed templates and a themes gallery (office-slides.md). Document: themes with a custom theme, style sets, update style from selection, sections with visible columns, floating text boxes and wrapped images, the table style gallery with banding, table sort, total row, distribute and fit, tracked changes with accept / reject (kept in DOCX), a generated table of contents (a TOC field in DOCX), designed templates (newsletter, proposal, academic paper, event flyer) and themed starter templates, with the design Vibe tools (vibe-studio.md, office-studio.md). Still open, roughly by payoff:
- Document: first/even-page headers, page borders and backgrounds, drawing shapes, rich text inside text boxes, text boxes and columns in ODT, multi-column sections that split across pages in the editor, column resize by drag, table formulas, multilevel numbering styles, tracking paragraph splits and formatting changes, smart typography while typing.
- Spreadsheet: quick analysis; table styles, named ranges, data validation, paste special, row/column groups, page setup and print, dynamic arrays with spilling, pivot-lite, sparklines and sheet chart data for other apps are done (office-sheet.md); still open there: XLSX/ODS round trip of these features, an outline gutter, refreshable pivots.
- Presentation: master and layout editing, slide numbers and footers, mixed formatting, charts from sheet data, designed templates and a themes gallery are done (office-slides.md); still open: bullet levels in one text box, font and size runs, native PowerPoint charts, more shapes, smart guides and distribute.
- Vibe: board → document is done (
office_board_to_document); still open: sheet → chart report tool, per-tool Watch out notes on the older tools.
Edit on GitHub → docs/office-suite.md