General API
The Facade API available in Univer depends on the current unit type and the registered plugins. This page covers the common APIs used in a Univer Boards application.
Importing
import { FUniver } from '@univerjs/core/facade'import '@univerjs-pro/boards/facade'const univerAPI = FUniver.newAPI(univer)Commands
Most operations in Univer are registered with the command system. This single execution path supports undo, redo, and collaboration.
For the design rationale, see the Univer command system.
Listening to commands
Event.BeforeCommandExecute fires before a command executes, and Event.CommandExecuted fires 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)})A BeforeCommandExecute listener can prevent a command from running by setting event.cancel to true.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, (event) => { if (event.id === 'board.command.set-name') { event.cancel = true }})Each listener returns an IDisposable. Dispose it 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 Boards command renames the active Board:
const board = univerAPI.getActiveBoard()if (board) { await univerAPI.executeCommand('board.command.set-name', { unitId: board.getId(), name: 'Planning Board', })}Events API
univerAPI.Event lists event names from Univer core and from the plugins registered in the current application. Core supplies the shared command and unit events, and plugins can add their own. Confirm that the plugin providing an event is registered before using the event.
The Facade Events reference lists the event names and parameter types in the current release.
Subscribe with addEvent, and dispose the returned object to unsubscribe:
const disposable = univerAPI.addEvent(univerAPI.Event.CommandExecuted, ({ id }) => { if (id === 'board.command.set-name') { console.log('The Board name changed') }})// Remove the listener when it is no longer neededdisposable.dispose()Undo and redo
await univerAPI.undo()await univerAPI.redo()UI
To extend menus, the toolbar, and other Boards interface areas, see Boards UI components.
WebSocket
To use WebSocket APIs, install @univerjs/network, register UniverNetworkPlugin, and import its Facade extension.
import { UniverNetworkPlugin } from '@univerjs/network'import '@univerjs/network/facade'univer.registerPlugin(UniverNetworkPlugin)After registration, the socket supports subscribing to socket events, sending messages, and closing the connection:
const socket = univerAPI.createSocket('wss://example.com/boards')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 on univerAPI.Enum:
console.log(univerAPI.Enum.UniverInstanceType.UNIVER_BOARD)console.log(univerAPI.Enum.BoardElementType.Shape)Utility method API
Shared utilities are exposed on univerAPI.Util:
console.log(univerAPI.Util.tools.isString('Univer Boards')) // trueHow is this guide?