Embedding Surface
How to embed the editor surface in your application: iframe and WebView, mounting and unmounting, subscribing to events, reconnection, and a production setup.
The surface starts locally and opens at the address from bootstrap. This page is about embedding it in your application.
How to start the surface
Section titled “How to start the surface”# $CLI — path to the processor, see "Installation and verification""$CLI" surface --port 0 \ --doc /data/input.docx --save /data/output.docx
# first line of stdout: fastdoc.surface-host-bootstrap.v1# open the address from bootstrap.url in WKWebView, WebView2, or a browserMount the surface and remove it
Section titled “Mount the surface and remove it”const handle = window.fastdocSurface.mount(container, { theme: "light", locale: "ru", readOnly: false, cssVariables: { "--fastdoc-accent": "#2f5bff" }});
// when the tab or document closeshandle.unmount();How to receive events
Section titled “How to receive events”window.addEventListener("fastdoc-host-event", (event) => { const message = event.detail; if (message.schema !== "fastdoc.surface-host-event.v1") return;
switch (message.type) { case "ready": console.log(message.documentPath); break; case "dirty": setUnsavedBadge(message.dirty); break; case "saved": console.log(message.path, message.byteCount); break; case "error": showError(message.operation, message.message); break; case "unmounted": releasePartnerResources(); break; }});| Environment | Where it occurs | Event channel |
|---|---|---|
| WKWebView, WebKitGTK | macOS, iOS, Linux | a message handler named fastdocHost |
| WebView2 | Windows | window.chrome.webview, the WebMessageReceived event |
| A plain browser, no wrapper | everywhere | the DOM event fastdoc-host-event |
You don’t need to choose a channel: the surface sends every event to all three at once. The host listens on whichever one it has and knows nothing about the rest.
Embedding
Section titled “Embedding”<iframe src="http://127.0.0.1:9272/?token=a4-local-demo&locale=ru-RU&theme=light" title="SumDoc document editor" style="width:100%;height:720px;border:0"></iframe>The surface reports its states — ready, dirty, saved, error, and unmounted — through the fastdoc-host-event window event. That’s enough to know what’s happening with the document, but not enough to control it: for full access to the model and the whole set of operations, work in parallel through JSON-line exchange or WebSocket.
What to plan for in a production setup
Section titled “What to plan for in a production setup”The surface starts on a random free port, not a predetermined one: a busy port and connections from elsewhere to it are one more way to disrupt operation.
The pass is issued anew for every session and must not end up in web server logs together with the page address.
After a drop, the connection recovers, but an edit is never blindly retried: first confirm it wasn’t applied before the drop, or the document will get it twice.
Saving first writes a new copy, and only by the user’s explicit decision does it replace the original file.
Exact names
Section titled “Exact names”Event names, schemas, the global object, and startup parameters are collected in the browser surface interface reference. Take them from there: on this page they’re given as examples and may lag behind.