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(core): synchronize normalized rich-text builder bodies (#7940)

Univer19475abbed02
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 4798f2c17e..f0d0071fb7 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
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { IBullet } from '../../../types/interfaces';
import { describe, expect, it } from 'vitest';
+import type { IBullet, IDocumentData } from '../../../types/interfaces';
import {
BaselineOffset,
BooleanNumber,
@@ -359,6 +359,98 @@ describe('RichTextValue', () => {
});
describe('RichTextBuilder', () => {
+ describe('normalized imported documents', () => {
+ it.each([
+ {
+ name: 'missing section ids',
+ dataStream: 'Data\rData\r\n',
+ secondStart: 5,
+ sectionBreaks: [{ startIndex: 10 }],
+ },
+ {
+ name: 'duplicate section ids',
+ dataStream: 'Data\r\nData\r\n',
+ secondStart: 6,
+ sectionBreaks: [
+ { startIndex: 5, sectionId: 'duplicate' },
+ { startIndex: 11, sectionId: 'duplicate' },
+ ],
+ },
+ ])('publishes copied style edits after normalizing $name', ({ dataStream, secondStart, sectionBreaks }) => {
+ const style = { ff: '宋体', fs: 18, bl: BooleanNumber.TRUE, cl: { rgb: '#FF0000' } };
+ // Legacy import payloads can omit ids required by the current document schema.
+ const data = {
+ id: 'imported-rich-text',
+ documentStyle: { textStyle: { ff: '宋体' } },
+ body: {
+ dataStream,
+ textRuns: [
+ { st: 0, ed: 4, ts: style },
+ { st: secondStart, ed: secondStart + 4, ts: style },
+ ],
+ paragraphs: [{ startIndex: 4 }, { startIndex: secondStart + 4 }],
+ sectionBreaks,
+ customRanges: [{
+ startIndex: secondStart,
+ endIndex: secondStart + 3,
+ rangeId: 'source-link',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: { url: 'https://example.com/report' },
+ }],
+ },
+ } as unknown as IDocumentData;
+ const original = RichTextValue.create(data);
+ const before = JSON.parse(JSON.stringify(original.getData()));
+ const edited = original.copy();
+ const untouched = original.copy();
+
+ edited.setStyle(0, 4, { ff: 'Courier New', fs: 22, bl: BooleanNumber.FALSE });
+
+ const result = edited.getData();
+ expect(result.body?.textRuns).toEqual([
+ { st: 0, ed: 4, ts: { ...style, ff: 'Courier New', fs: 22, bl: BooleanNumber.FALSE } },
+ { st: secondStart, ed: secondStart + 4, ts: style },
+ ]);
+ expect(result.body?.dataStream).toBe(dataStream);
+ expect(result.body?.customRanges).toEqual(before.body.customRanges);
+ expect(result.documentStyle).toEqual(before.documentStyle);
+ const ids = result.body!.sectionBreaks!.map((section) => section.sectionId);
+ expect(ids.every(Boolean)).toBe(true);
+ expect(new Set(ids).size).toBe(ids.length);
+ expect(original.getData()).toEqual(before);
+ expect(untouched.getData().body?.textRuns).toEqual(before.body.textRuns);
+ expect(edited.copy().getData().body?.textRuns).toEqual(result.body?.textRuns);
+ });
+
+ it('keeps text-run handles and later edits synchronized after section normalization', () => {
+ const original = RichTextValue.create({
+ id: 'imported-text-runs',
+ documentStyle: {},
+ body: {
+ dataStream: 'Data\rData\r\n',
+ textRuns: [
+ { st: 0, ed: 4, ts: { ff: '宋体', fs: 18 } },
+ { st: 5, ed: 9, ts: { ff: '等线', fs: 11 } },
+ ],
+ paragraphs: [{ startIndex: 4 }, { startIndex: 9 }],
+ sectionBreaks: [{ startIndex: 10 }],
+ },
+ } as unknown as IDocumentData);
+ const before = JSON.parse(JSON.stringify(original.getData()));
+ const edited = original.copy();
+ edited.getParagraphs()[0].getTextRuns()[0].setText('Ready');
+ edited.setStyle(0, 5, { bl: BooleanNumber.TRUE });
+ edited.insertText(5, '!');
+
+ expect(edited.toPlainText()).toBe('Ready!\nData');
+ expect(edited.getParagraphs().map((paragraph) => paragraph.getText())).toEqual(['Ready!', 'Data']);
+ expect(edited.getData().body?.textRuns?.[0].ts).toMatchObject({ ff: '宋体', fs: 18, bl: BooleanNumber.TRUE });
+ expect(edited.getData().body?.textRuns?.at(-1)).toEqual({ st: 7, ed: 11, ts: { ff: '等线', fs: 11 } });
+ expect(edited.getData().body?.sectionBreaks?.[0].startIndex).toBe(12);
+ expect(original.getData()).toEqual(before);
+ });
+ });
+
it('should create empty rich text builder', () => {
const builder = RichTextBuilder.create();
const emptyData = RichTextBuilder.newEmptyData();
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 67edfbfa76..80cdfc016f 100644
--- a/packages/core/src/docs/data-model/rich-text-builder.ts
+++ b/packages/core/src/docs/data-model/rich-text-builder.ts
@@ -2000,6 +2000,8 @@ export class RichTextBuilder extends RichTextValue {
constructor(data: IDocumentData) {
super(data);
this._doc = new DocumentDataModel(data);
+ // Section normalization may replace the body; all builder reads must use the edited body.
+ this._data.body = this._doc.getBody();
}
/**
diff --git a/packages/sheets/src/facade/__tests__/f-range.spec.ts b/packages/sheets/src/facade/__tests__/f-range.spec.ts
index 00f5d9bfd9..2cd78c7c2b 100644
--- a/packages/sheets/src/facade/__tests__/f-range.spec.ts
+++ b/packages/sheets/src/facade/__tests__/f-range.spec.ts
@@ -24,7 +24,6 @@ import type {
Workbook,
} from '@univerjs/core';
import type { FUniver } from '@univerjs/core/facade';
-import type { FWorksheet } from '../f-worksheet';
import {
AbsoluteRefType,
Dimension,
@@ -77,6 +76,7 @@ import {
UnregisterWorksheetRangeThemeStyleMutation,
} from '@univerjs/sheets';
import { beforeEach, describe, expect, it, vi } from 'vitest';
+import type { FWorksheet } from '../f-worksheet';
import { SHEETS_CUSTOM_FIELD_WARNING_MESSAGE } from '../const';
import { createFacadeTestBed } from './create-test-bed';
@@ -775,6 +775,39 @@ describe('Test FRange', () => {
expect(activeSheet.getRange('I3').getValue(true)).toBeUndefined();
});
+ it('persists copied rich-text edits without changing the source cell', () => {
+ const workbook = univerAPI.getActiveWorkbook()!;
+ const sheet = workbook.getActiveSheet()!;
+ const style = { ff: '宋体', fs: 18, bl: 1, cl: { rgb: '#FF0000' } };
+ const data = {
+ id: 'imported-rich-text',
+ documentStyle: {},
+ body: {
+ dataStream: 'Data\rData\r\n',
+ textRuns: [
+ { st: 0, ed: 4, ts: { ...style, bl: 1 } },
+ { st: 5, ed: 9, ts: { ...style, bl: 1 } },
+ ],
+ paragraphs: [{ startIndex: 4 }, { startIndex: 9 }],
+ sectionBreaks: [{ startIndex: 10 }],
+ },
+ } as unknown as IDocumentData;
+ sheet.getRange('H4').setRichTextValueForCell(data);
+ const original = sheet.getRange('H4').getValue(true) as RichTextValue;
+ const before = JSON.parse(JSON.stringify(original.getData()));
+ const edited = original.copy().setStyle(0, 4, { ff: 'Courier New', fs: 22, bl: 0 });
+ sheet.getRange('I4').setRichTextValueForCell(edited);
+
+ const saved = workbook.save().sheets[sheet.getSheetId()].cellData!;
+ expect(saved[3][8].p?.body?.textRuns).toEqual([
+ { st: 0, ed: 4, ts: { ...style, ff: 'Courier New', fs: 22, bl: 0 } },
+ { st: 5, ed: 9, ts: style },
+ ]);
+ expect(saved[3][8].p?.body?.dataStream).toBe('Data\rData\r\n');
+ expect(saved[3][7].p).toEqual(before);
+ expect(original.getData()).toEqual(before);
+ });
+
it('Range inserts and deletes cells along both dimensions', async () => {
const activeSheet = univerAPI.getActiveWorkbook()!.getActiveSheet()!;
activeSheet.getRange('A20:D23').setValues([