General API
The Facade APIs available in Univer depend on the current unit type and the plugins registered in the application. This page describes the APIs most frequently used in a Docs application.
Importing
import { FUniver } from '@univerjs/core/facade'const univerAPI = FUniver.newAPI(univer)Commands
Most operations in Univer are executed through the command system. This unified execution path supports undo, redo, and collaboration.
For the design of the command system, see Univer command system.
Listening to commands
Subscribe to Event.BeforeCommandExecute to run code before a command executes, and to Event.CommandExecuted to run code after it finishes.
const beforeDisposable = univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, ({ id, params }) => { console.log('Before command:', id, params)})const afterDisposable = univerAPI.addEvent(univerAPI.Event.CommandExecuted, ({ id, params }) => { console.log('Command executed:', id, params)})To prevent a command from executing, set event.cancel to true in a BeforeCommandExecute listener.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, (event) => { if (event.id === 'doc.command.set-name') { event.cancel = true }})Each listener returns an IDisposable. Dispose the returned object when the listener is no longer needed.
beforeDisposable.dispose()afterDisposable.dispose()disposable.dispose()Executing commands
When the command ID and its parameters are known, execute the command with FUniver.executeCommand. The following Docs command renames the active document:
const document = univerAPI.getActiveDocument()if (document) { await univerAPI.executeCommand('doc.command.set-name', { unitId: document.getId(), name: 'Project Notes', })}Events API
The events on univerAPI.Event come from Univer core and from the plugins registered in the application. Core provides command events and document lifecycle events, and Docs plugins add their own events. Confirm that the plugin providing an event is registered before using the event.
The Facade Events reference lists the event names and parameter types available in the current release.
Call addEvent to subscribe, then dispose the returned object to unsubscribe:
const disposable = univerAPI.addEvent(univerAPI.Event.CommandExecuted, ({ id }) => { if (id === 'doc.command.set-name') { console.log('The document name changed') }})// Remove the listener when it is no longer neededdisposable.dispose()Undo and redo
await univerAPI.undo()await univerAPI.redo()System clipboard
After the Docs UI plugin is registered, copy and paste operate on the current selection and caret position in the document.
await univerAPI.copy()await univerAPI.paste()Copy and paste rely on the browser's native Clipboard API. They can fail when the page does not have focus, when the call is not initiated by a user action, or when clipboard permission is unavailable. For details, see the MDN clipboard documentation.
UI
The Docs UI components page describes how to extend menus, the toolbar, and other parts of the Docs interface.
WebSocket
A preset installation already exposes univerAPI.createSocket(url). In plugin mode, install @univerjs/network, register UniverNetworkPlugin, and import its Facade extension before use.
import { UniverNetworkPlugin } from '@univerjs/network'import '@univerjs/network/facade'univer.registerPlugin(UniverNetworkPlugin)Afterward, you can subscribe to socket events, send messages, and close the connection:
const socket = univerAPI.createSocket('wss://example.com/docs')socket.open$.subscribe(() => { socket.send('hello')})socket.message$.subscribe((message) => { console.log('WebSocket message:', message.data)})socket.error$.subscribe((error) => { console.error('WebSocket error:', error)})socket.close()Enum API
Common enum types are exposed through univerAPI.Enum:
console.log(univerAPI.Enum.UniverInstanceType.UNIVER_DOC)console.log(univerAPI.Enum.LifecycleStages.Rendered)Utility method API
Shared utility methods are exposed through univerAPI.Util:
console.log(univerAPI.Util.tools.isString('Univer Docs')) // trueHow is this guide?