Text Runs and Paragraphs
Index-based model
Univer Docs does not store rich text as a nested tree. Plain text is stored in body.dataStream1, and styles and structure are attached to the text through index ranges.
The most common structures are:
ITextRun: inline text style range.IParagraph: paragraph metadata and paragraph style.ISectionBreak: section break metadata.
ITextRun
ITextRun describes the style of one continuous text range.
| Property | Type | Description |
|---|---|---|
| st | number | Start index, inclusive. |
| ed | number | End index, exclusive. |
| sId? | string | Referenced style ID. |
| ts? | ITextStyle | Inline text style object. |
const body: IDocumentBody = { dataStream: 'Hello Univer\r\n', textRuns: [ { st: 0, ed: 5, ts: { bl: 1 }, }, { st: 6, ed: 12, ts: { cl: { rgb: '#2563eb' } }, }, ],}In this example, Hello is bold and Univer is blue. The paragraph break \r\n is not included in the styled ranges.
IParagraph
IParagraph holds paragraph-level metadata. startIndex points at the paragraph break in dataStream.
| Property | Type | Description |
|---|---|---|
| startIndex | number | Index of the paragraph break. |
| paragraphId | string | Stable paragraph ID. |
| styleId? | string | Named style the paragraph references. |
| paragraphStyle? | IParagraphStyle | Paragraph style such as alignment, indentation, line spacing, and heading type. |
| bullet? | Partial<IBullet> | null | Numbering override. Omit it to inherit numbering from the named style, or set null to turn numbering off. |
const body: IDocumentBody = { dataStream: 'Hello Univer\r\nSecond line\r\n', paragraphs: [ { startIndex: 12, paragraphId: 'p-1', paragraphStyle: { namedStyleType: NamedStyleType.HEADING_1, }, }, { startIndex: 25, paragraphId: 'p-2', }, ],}List numbering
When bullet is omitted, a paragraph keeps the numbering defined by its named style. Set bullet to null to remove numbering from that paragraph, or set individual fields such as nestingLevel to override part of the inherited bullet.
Read the effective list with resolveDocumentParagraphBullet(document, paragraph) from @univerjs/core. It returns undefined for an unnumbered paragraph and does not write inherited values back into the snapshot.
import { resolveDocumentParagraphBullet } from '@univerjs/core'const bullet = resolveDocumentParagraphBullet(document, paragraph)if (bullet) { console.log(bullet.listType, bullet.nestingLevel)}Word wrapping within a paragraph
paragraphStyle.wordWrap is controlled by the paragraph setting Allow Latin text to wrap in the middle of a word. BooleanNumber.TRUE allows a word to split across lines, and BooleanNumber.FALSE keeps each word whole. When the value is missing, it inherits from the referenced paragraph style first, then from documentStyle.defaultParagraphStyle; if neither sets it, words are not split. resolveDocumentParagraphWordWrap(document, paragraph) from @univerjs/core returns the effective value and does not write inherited defaults into the snapshot. Sheets cell wrapping is unaffected. DOCX w:wordWrap stores the inverse, so do not copy its XML value directly.
ISectionBreak
ISectionBreak also uses an index to attach section-level settings to the text stream. It can override page size, margins, headers, footers, columns, and text direction for a section.
const body: IDocumentBody = { dataStream: 'First section\nSecond section\r\n', sectionBreaks: [ { startIndex: 13, pageSize: { width: 595, height: 842 }, marginTop: 72, marginBottom: 72, }, ],}Notes
- Keep indexes aligned with
dataStream. Wrong indexes can leave style ranges, paragraphs, selections, or rendering out of sync. - Prefer Facade APIs and commands when editing an active document.
- When you build snapshots by hand, build
dataStreamfirst, then compute ranges and paragraph positions from that string.
Footnotes
-
Text offsets use JavaScript UTF-16 code units, not visible character counts. For example,
"😀".lengthis 2. A text run covers[st, ed):stis included andedis excluded; do not reuse the inclusive end indices of sheet ranges. ↩
How is this guide?