Shapes
The shape feature inserts and edits drawings inside a document, from basic geometric shapes and arrows to flowchart elements.
Introduction
In Univer Docs, shapes support the following operations:
- Add rectangles, circles, arrows, and other simple shapes.
- Adjust fill color, borders, and text.
- Place a shape anywhere in the document.
Plugin mode
Installation
pnpm add @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-uinpm install @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-uiyarn add @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-uibun add @univerjs-pro/engine-formula @univerjs-pro/docs-shape @univerjs-pro/docs-shape-uiShapes also require UniverProFormulaEnginePlugin from @univerjs-pro/engine-formula. In the base setup, replace the existing UniverFormulaEnginePlugin import and registration with this plugin. Do not register both formula engines, because doing so can make shape insertion fail with a missing dependency error.
Usage
import { UniverDocsShapePlugin } from '@univerjs-pro/docs-shape'import { UniverDocsShapeUIPlugin } from '@univerjs-pro/docs-shape-ui'import DocsShapeUIEnUS from '@univerjs-pro/docs-shape-ui/locale/en-US'import { UniverProFormulaEnginePlugin } from '@univerjs-pro/engine-formula'import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import '@univerjs-pro/docs-shape-ui/lib/index.css'const univer = new Univer({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( DocsShapeUIEnUS, ), },})univer.registerPlugin(UniverProFormulaEnginePlugin)univer.registerPlugin(UniverDocsShapePlugin)univer.registerPlugin(UniverDocsShapeUIPlugin)If you use a commercial license, configure it as described in Using License in Client.
Mobile support
Configure the mobile editor first. In the plugin-mode setup described on this page, replace the desktop registration below with the mobile export from the same package, and do so before opening a document. Keep the model plugins, styles, locales, and Facade imports, and do not register both variants.
| Package | Desktop registration | Mobile registration |
|---|---|---|
@univerjs-pro/docs-shape-ui | UniverDocsShapeUIPlugin | UniverDocsShapeMobileUIPlugin |
Edit shape text
Work with the shape object returned by insertShape(), either in the example below or in the earlier example. getText() returns a live FShapeText handle. Read plain text with getPlainText(), replace all of it with setText(), and apply formatting through the font, color, and alignment methods.
import '@univerjs-pro/docs-shape/facade'const document = univerAPI.getActiveDocument()const paragraph = document?.getParagraphs()[0]if (!document || !paragraph) throw new Error('No active document paragraph')const shape = document.insertShape({ shapeType: univerAPI.Enum.ShapeTypeEnum.Rect, placement: { wrappingStyle: univerAPI.Enum.TextWrappingStyle.INLINE, anchor: { paragraphId: paragraph.getId(), segmentId: paragraph.getSegmentId() }, },})if (!shape) throw new Error('Shape is not available')const shapeText = shape.getText()console.log(shapeText.getPlainText())shapeText .setText('Quarterly review') .setFontSize(18) .setColor('#2563eb') .setBold(true) .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER) .setVerticalAlign(univerAPI.Enum.VerticalAlign.MIDDLE)setText() replaces the existing text. Use setRichText() when one shape needs several styles. To change existing rich text, call getRichText()?.copy(), edit the builder, and pass it to setRichText(); edits to that detached builder do not apply on their own. More options are available in text style and text box padding and layout.
const richText = univerAPI.newRichText() .text('Quarterly ') .bold('review')shapeText.setRichText(richText)Shape format painter
Select a source shape, turn on the format painter, and then select the target shape. The painter copies fill, stroke, effects, and supported text formatting, and leaves the target's identity, geometry, text content, and formula bindings unchanged. Clearing formatting restores the defaults used at insertion. SmartArt is excluded. The shape UI plugin registers this behavior automatically.
How is this guide?