Installation & Basic Usage

GitHubEdit on GitHub

Univer adopts a plugin-based design philosophy, aiming to provide developers with a flexible and extensible framework for electronic document applications. Through this plugin-based design, Univer can easily integrate various functional modules to meet the needs of different users. However, the plugin-based design increases the complexity of the application, especially for developers who are new to Univer.

For this reason, Univer provides two ways to help you quickly integrate and use Univer: plugin mode and preset mode. The so-called preset is actually a combination of pre-configured plugins, so the capabilities provided by both modes are the same without complex extensions.

Caution

  • Consistent Dependency Versions: Whether you use preset mode or plugin mode, you must ensure that all dependencies have consistent version numbers.
  • Cautious Mixing of Plugins and Presets: If a plugin is already included in a preset, you do not need to import that plugin separately when using the preset. Otherwise, it may lead to plugin conflicts or functional anomalies.

Differences between preset mode and plugin mode:

Plugin ModePreset Mode
Requires manual import of corresponding facade packagesNo need to manually import any facade packages
Requires attention to the registration order of plugins with the same functionalityNo need to pay attention to the registration order of functionalities included in presets
Supports on-demand lazy loadingOnly supports preset-level lazy loading, plugins within the preset can only be loaded synchronously

Setting Up via Preset Mode

Here we will briefly introduce how to quickly build a Univer Docs application with less than twenty lines of code.

Using Package Manager

If your project already uses modern front-end development tools, integrating Univer will be very simple. We recommend using build tools like Vite, esbuild, or Webpack 5 that have good support for ES Modules to build Univer applications. If you are using other build tools (such as Webpack 4), you may need some additional configuration.

Installation

Choose your package manager to install @unvierjs/presets:

npm install @univerjs/presets @univerjs/preset-docs-core

Usage

If your build tool does not support the exports field in package.json (common in Webpack 4), you need to manually change the mapped paths to the actual paths.

With the following code, you can start an Univer Docs application:

import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'
import UniverPresetDocsCoreEnUS from '@univerjs/preset-docs-core/locales/en-US'
import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'

import '@univerjs/preset-docs-core/lib/index.css'

const { univer, univerAPI } = createUniver({
  locale: LocaleType.EN_US,
  locales: {
    [LocaleType.EN_US]: mergeLocales(
      UniverPresetDocsCoreEnUS,
    ),
  },
  presets: [
    UniverDocsCorePreset({
      container: 'app',
    }),
  ],
})

univerAPI.createUniverDoc({})

The univerAPI object returned here is called the Univer Facade API, through which you can call many features provided by Univer.

You can also import UniverDocsCorePreset from @univerjs/presets/preset-docs-core, but you must ensure that your build tool supports the exports field in package.json.

createUniver Method

The createUniver method accepts a configuration object that contains the configuration information for Univer, such as language, theme, and plugins. This method returns an object containing the Univer instance and the Univer Facade API instance.

Some properties of the configuration object are as follows:

  • locale: Language environment, can be an LocaleType enumeration value.
  • locales: Locales, an object, the key is the language environment, and the value is the language pack object.
  • theme: A theme object, which is optional.
  • presets: An array of presets that contains the presets to be registered, such as UniverDocsCorePreset.
  • plugins: An array of plugins that contains the plugins that need to be registered additionally.

When you use a plugin that is not included in any preset package or you implement your own plugin, you can register these plugins through the plugins property. You can also choose to register plugins using the univer.registerPlugin method after obtaining the Univer instance.

You can find more detailed information about the createUniver method in the API Reference / createUniver.

Import from CDN

If you do not want to use a package manager or just want to quickly try out Univer's features, you can import the relevant resources of Univer via CDN. For details, please refer to Using Univer via CDN.

Setting Up via Plugin Mode

Univer provides a series of features in the form of plugins. In addition to some core plugins that are essential for the product, you can selectively introduce other plugins as needed. Here, we will take the most basic Univer Docs application as an example to introduce how to manually combine and install plugins.

Different from preset mode, plugin mode does not include Facade API by default. The parts related to Facade API in the following code examples are optional, and you can decide whether to remove them based on your needs.

Using Package Manager

Installation

We use React to develop the view (this does not affect your use of Univer in Vue or Angular), and Rxjs to handle data streams. Since these two libraries are widely used in modern front-end development, we treat them as peerDependencies. However, different package management tools have different behaviors regarding peerDependencies, so you may need to pay attention to some details.

  • If you are using npm, make sure you are using npm@7 or higher. This is because npm@3 ~ npm@6 will not correctly install peerDependencies1.
  • If you are using pnpm, make sure you are using pnpm@8 or higher. If you are using pnpm@6 ~ pnpm@7, you can try configuring auto-install-peers=true 2 to resolve dependency installation issues.
  • If you are using yarn, you need to manually install the missing peerDependencies3, but don't worry, the installation commands below already include these dependencies.
npm install @univerjs/core @univerjs/design @univerjs/docs @univerjs/docs-ui @univerjs/engine-formula @univerjs/engine-render @univerjs/ui
pnpm add @univerjs/core @univerjs/design @univerjs/docs @univerjs/docs-ui @univerjs/engine-formula @univerjs/engine-render @univerjs/ui
yarn add @univerjs/core @univerjs/design @univerjs/docs @univerjs/docs-ui @univerjs/engine-formula @univerjs/engine-render @univerjs/ui react react-dom rxjs

Usage

Caution

  • Not all plugins include facade packages, language packs, and style files. We will specify this in the documentation for each feature.
  • The order of importing style files is important. Make sure to import the CSS styles of @univerjs/design and @univerjs/ui before importing the styles of other plugins.

You need to import the Univer style files, language packs, and some necessary plugins in your project:

import { LocaleType, mergeLocales, Univer, UniverInstanceType } from '@univerjs/core'
import { FUniver } from '@univerjs/core/facade'
import DesignEnUS from '@univerjs/design/locale/en-US'
import { UniverDocsPlugin } from '@univerjs/docs'
import { UniverDocsUIPlugin } from '@univerjs/docs-ui'
import DocsUIEnUS from '@univerjs/docs-ui/locale/en-US'
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula'
import { UniverRenderEnginePlugin } from '@univerjs/engine-render'
import { UniverUIPlugin } from '@univerjs/ui'
import UIEnUS from '@univerjs/ui/locale/en-US'

import '@univerjs/design/lib/index.css'
import '@univerjs/ui/lib/index.css'
import '@univerjs/docs-ui/lib/index.css'

If you need to use the Facade API, you can import the relevant facade packages here. This is optional (I believe you will need them 😀):

import '@univerjs/engine-formula/facade'
import '@univerjs/ui/facade'
import '@univerjs/docs-ui/facade'

Then create a Univer instance and register these plugins:

const univer = new Univer({
  locale: LocaleType.EN_US,
  locales: {
    [LocaleType.EN_US]: mergeLocales(
      DesignEnUS,
      UIEnUS,
      DocsUIEnUS,
    ),
  },
})

univer.registerPlugin(UniverRenderEnginePlugin)
univer.registerPlugin(UniverFormulaEnginePlugin)

univer.registerPlugin(UniverUIPlugin, {
  container: 'app',
})

univer.registerPlugin(UniverDocsPlugin)
univer.registerPlugin(UniverDocsUIPlugin)

univer.createUnit(UniverInstanceType.UNIVER_DOC, {})

const univerAPI = FUniver.newAPI(univer)

univer.registerPlugin Method

``univer.registerPlugin` method is used to register a plugin to the Univer instance. You can call this method after creating the Univer instance to register plugins.

You can register a plugin using the univer.registerPlugin(Plugin, options) method, where Plugin is the plugin to be registered, and options are optional configuration items, as each plugin may have different configuration items.

Of course, you can also pass an array to register multiple plugins at once. This is very useful for scenarios where you need to manage plugins centrally.

univer.registerPlugin([
  UniverRenderEnginePlugin,
  UniverFormulaEnginePlugin,
  [UniverUIPlugin, {
    container: 'app',
  }],
  UniverDocsPlugin,
  UniverDocsUIPlugin,
])

Lazy Loading Plugins

A common advantage of plugin mode is that you can control the loading timing of plugins more flexibly. A common approach is to load only the essential plugins during application initialization, while deferring the loading of some plugins until after the first render is complete.

// ...

univer.createUnit(UniverInstanceType.UNIVER_DOC, {})

import('@univerjs/watermark').then(({ UniverWatermarkPlugin }) => {
  univer.registerPlugin(UniverWatermarkPlugin, {
    textWatermarkSettings: {
      content: 'Hello, Univer!',
      fontSize: 36,
    },
  })
})

Our official demo uses this optimization technique, which you can refer to.

Import from CDN

If you do not want to use a package manager or just want to quickly try out Univer's features, you can import the relevant resources of Univer via CDN. For details, please refer to Using Univer via CDN.

Other Releases

In addition to stable releases, Univer offers alpha / beta and nightly channels. These versions allow you to get early access to the newest features that have not been officially released yet. However, keep in mind that more features also mean more risks. These versions may contain bugs, incomplete functionality, or unstable features. Please avoid using these versions in production environments whenever possible.

Alpha / Beta Release

When the Univer development team completes a new feature or significant change, they may pre-release it to the alpha or beta channel. You can install the alpha / beta version of a Univer package using the following command:

npm install @univerjs/<package-name>@alpha # Alpha version
npm install @univerjs/<package-name>@beta # Beta version

If you encounter any issues or have any feedback while using alpha / beta versions, please report them to the Univer development team through appropriate channels, such as Github Issues or the Discord channels.

Happy early access experimentation!

Next Steps

  • Basic Concepts - Learn about the basic concepts of Univer.
  • Features - Learn about the features of Univer and how to manipulate data.

Footnotes

  1. https://blog.npmjs.org/post/110924823920/npm-weekly-5

  2. https://pnpm.io/npmrc#auto-install-peers

  3. https://github.com/yarnpkg/yarn/issues/1503

How is this guide?