Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: support rich text columns (#7262)

Univerdb425b290e88
diff --git a/packages/core/src/docs/data-model/__tests__/rich-text-builder.test.ts b/packages/core/src/docs/data-model/__tests__/rich-text-builder.test.ts
index 5160bc7189..d707ee4e73 100644
--- a/packages/core/src/docs/data-model/__tests__/rich-text-builder.test.ts
+++ b/packages/core/src/docs/data-model/__tests__/rich-text-builder.test.ts
@@ -594,6 +594,21 @@ describe('RichTextBuilder', () => {
expect(data.body?.paragraphs?.[0]?.paragraphStyle?.horizontalAlign).toBe(HorizontalAlign.CENTER);
});
+ it('should set and clear rich text columns', () => {
+ const builder = RichTextBuilder.create()
+ .columns({ count: 2, spacing: 12 })
+ .text('Columns');
+
+ expect(builder.getData().body?.sectionBreaks?.[0]?.columnProperties).toEqual([
+ { width: 0, paddingEnd: 12 },
+ { width: 0, paddingEnd: 0 },
+ ]);
+
+ builder.columns({ count: 1 });
+
+ expect(builder.getData().body?.sectionBreaks?.[0]?.columnProperties).toBeUndefined();
+ });
+
it('should start a paragraph chain without a leading blank paragraph', () => {
const builder = RichTextBuilder.create()
.paragraph()
diff --git a/packages/core/src/docs/data-model/index.ts b/packages/core/src/docs/data-model/index.ts
index 8c9014b957..009227adfc 100644
--- a/packages/core/src/docs/data-model/index.ts
+++ b/packages/core/src/docs/data-model/index.ts
@@ -31,6 +31,7 @@ export {
TextStyleBuilder,
TextStyleValue,
} from './rich-text-builder';
+export type { IRichTextColumnsOptions } from './rich-text-builder';
export { DEFAULT_DOCUMENT_SUB_COMPONENT_ID } from './subdocument';
export { ActionIterator } from './text-x/action-iterator';
export { PRESERVE_INSERTED_PARAGRAPH_IDS, TextXActionType } from './text-x/action-types';
diff --git a/packages/core/src/docs/data-model/rich-text-builder.ts b/packages/core/src/docs/data-model/rich-text-builder.ts
index d4df5f5edb..14cfd86192 100644
--- a/packages/core/src/docs/data-model/rich-text-builder.ts
+++ b/packages/core/src/docs/data-model/rich-text-builder.ts
@@ -24,6 +24,7 @@ import type {
INumberUnit,
IParagraphBorder,
IParagraphStyle,
+ ISectionColumnProperties,
IShading,
ITabStop,
ITextDecoration,
@@ -210,6 +211,13 @@ export interface IRichTextAlignment {
vertical?: VerticalAlign;
}
+export interface IRichTextColumnsOptions {
+ /** Number of text-flow columns. Use 1 to clear multi-column layout. */
+ count: number;
+ /** Spacing between columns in px. Defaults to 0. */
+ spacing?: number;
+}
+
/**
* Agent-friendly options for one paragraph list item.
*
@@ -2032,6 +2040,69 @@ export class RichTextBuilder extends RichTextValue {
return this;
}
+ /**
+ * Sets the column layout for this rich-text document.
+ *
+ * Columns are stored in the rich-text document section layout. This creates text-flow columns, not table columns.
+ * The host renderer must support rich-text columns for the layout to be visible.
+ *
+ * @param options The column layout options.
+ * @param options.count The number of text columns. Must be a positive integer. Use `1` to clear multi-column layout.
+ * @param options.spacing The spacing between columns, in px. Defaults to `0`.
+ * @returns The current builder for chaining.
+ *
+ * @example
+ * ```ts
+ * const presentation = univerAPI.getActivePresentation();
+ * if (!presentation) throw new Error('No active presentation');
+ *
+ * const slide = presentation.getSlideByIndex(0);
+ * const richText = univerAPI.newRichText()
+ * .columns({ count: 2, spacing: 12 })
+ * .text('Column text');
+ *
+ * const shapeInfo = slide.newShape()
+ * .setRichText(richText)
+ * .setAbsolutePosition(80, 80)
+ * .setSize(360, 160)
+ * .build();
+ *
+ * slide.insertShape(shapeInfo);
+ * ```
+ */
+ columns(options: IRichTextColumnsOptions): RichTextBuilder {
+ if (!Number.isInteger(options.count) || options.count < 1) {
+ throw new RangeError('Rich text column count must be a positive integer.');
+ }
+
+ const body = this._data.body ??= { dataStream: '\r\n' };
+ const sectionBreaks = body.sectionBreaks ??= [];
+ if (!sectionBreaks.length) {
+ sectionBreaks.push({
+ sectionId: createSectionId(new Set()),
+ startIndex: Math.max(0, (body.dataStream?.length ?? 0) - 1),
+ });
+ }
+
+ const spacing = Math.max(0, options.spacing ?? 0);
+ const columns: ISectionColumnProperties[] | undefined = options.count === 1
+ ? undefined
+ : Array.from({ length: options.count }, (_, index) => ({
+ width: 0,
+ paddingEnd: index === options.count - 1 ? 0 : spacing,
+ }));
+
+ for (const sectionBreak of sectionBreaks) {
+ if (columns) {
+ sectionBreak.columnProperties = columns.map((column) => ({ ...column }));
+ } else {
+ delete sectionBreak.columnProperties;
+ }
+ }
+
+ return this;
+ }
+
/**
* Appends one text span with explicit style.
*