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.

fix(docs): normalize embedded rich text layout (#7396)

Univerc7f6cd4b4a1d
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 305d0d906d..df6a715cc5 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
@@ -355,6 +355,44 @@ describe('RichTextBuilder', () => {
expect(data.textRuns![0].ed).toBe(5);
});
+ it('should offset inserted styles exactly once', () => {
+ const data = RichTextBuilder.create()
+ .text('Hello World')
+ .insertText(5, ' Bold', { bl: BooleanNumber.TRUE })
+ .getData();
+
+ expect(data.body?.dataStream).toBe('Hello Bold World\r\n');
+ expect(data.body?.textRuns).toEqual([{
+ st: 5,
+ ed: 10,
+ ts: { bl: BooleanNumber.TRUE },
+ }]);
+ });
+
+ it('should convert platform line endings into document paragraphs', () => {
+ const data = RichTextBuilder.create()
+ .text('First\r\nSecond\rThird\nFourth')
+ .getData();
+
+ expect(data.body?.dataStream).toBe('First\rSecond\rThird\rFourth\r\n');
+ expect(data.body?.paragraphs?.map((paragraph) => paragraph.startIndex)).toEqual([5, 12, 18, 25]);
+ });
+
+ it('should keep multiline span styles aligned with normalized paragraphs', () => {
+ const data = RichTextBuilder.create()
+ .text('A')
+ .span('B\r\nC', { bold: true })
+ .getData();
+
+ expect(data.body?.dataStream).toBe('AB\rC\r\n');
+ expect(data.body?.paragraphs?.map((paragraph) => paragraph.startIndex)).toEqual([2, 4]);
+ expect(data.body?.textRuns).toEqual([{
+ st: 1,
+ ed: 4,
+ ts: { bl: BooleanNumber.TRUE },
+ }]);
+ });
+
it('should build agent-friendly styled spans', () => {
const builder = RichTextBuilder.create()
.text('Status: ')
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 7b912fb3dc..e4ed71c72c 100644
--- a/packages/core/src/docs/data-model/rich-text-builder.ts
+++ b/packages/core/src/docs/data-model/rich-text-builder.ts
@@ -2363,18 +2363,7 @@ export class RichTextBuilder extends RichTextValue {
return this;
}
- return this.insertRichText(RichTextValue.create({
- id: 'd',
- documentStyle: {},
- body: {
- dataStream: text,
- textRuns: [{
- st: 0,
- ed: text.length,
- ts: normalizeRichTextSpanStyle(style),
- }],
- },
- }));
+ return this.insertText(text, normalizeRichTextSpanStyle(style));
}
/**
@@ -2571,7 +2560,7 @@ export class RichTextBuilder extends RichTextValue {
insertText = start;
} else {
startIndex = Math.min(start, startIndex);
- insertText = text as string;
+ insertText = typeof text === 'string' ? text : '';
}
if (typeof text === 'object') {
@@ -2581,14 +2570,16 @@ export class RichTextBuilder extends RichTextValue {
}
if (!insertText) return this;
+ const plainTextBody = BuildTextUtils.transform.fromPlainText(insertText);
const newBody: IDocumentBody = {
- dataStream: insertText,
+ dataStream: plainTextBody.dataStream,
+ paragraphs: plainTextBody.paragraphs,
textRuns: insertStyle
? [
{
ts: insertStyle,
- st: startIndex,
- ed: startIndex + insertText.length,
+ st: 0,
+ ed: plainTextBody.dataStream.length,
},
]
: [],
diff --git a/packages/core/src/docs/data-model/text-x/build-utils/__tests__/parse.spec.ts b/packages/core/src/docs/data-model/text-x/build-utils/__tests__/parse.spec.ts
index 4fdf2c5eb3..1c3cb53c28 100644
--- a/packages/core/src/docs/data-model/text-x/build-utils/__tests__/parse.spec.ts
+++ b/packages/core/src/docs/data-model/text-x/build-utils/__tests__/parse.spec.ts
@@ -52,4 +52,11 @@ describe('parse build utils', () => {
customRanges: [],
});
});
+
+ it('should normalize platform line endings without creating duplicate paragraphs', () => {
+ const body = fromPlainText('First\r\nSecond\rThird\nFourth');
+
+ expect(body.dataStream).toBe('First\rSecond\rThird\rFourth');
+ expect(body.paragraphs?.map((paragraph) => paragraph.startIndex)).toEqual([5, 12, 18]);
+ });
});
diff --git a/packages/core/src/docs/data-model/text-x/build-utils/parse.ts b/packages/core/src/docs/data-model/text-x/build-utils/parse.ts
index 0129a0840e..eb0b5be467 100644
--- a/packages/core/src/docs/data-model/text-x/build-utils/parse.ts
+++ b/packages/core/src/docs/data-model/text-x/build-utils/parse.ts
@@ -53,7 +53,7 @@ export const isEmptyDocument = (dataStream?: string) => {
};
export const fromPlainText = (text: string): IDocumentBody => {
- const dataStream = text.replace(/\n/g, '\r');
+ const dataStream = text.replace(/\r\n?|\n/g, '\r');
const paragraphs: IParagraph[] = [];
const existingParagraphIds = new Set<string>();
const customRanges: ICustomRange[] = [];