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): preserve custom range metadata when splitting (#7559)

Univerea427576768a
diff --git a/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts
index 3808477839..270bdb103c 100644
--- a/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts
+++ b/packages/core/src/docs/data-model/text-x/__tests__/apply.spec.ts
@@ -157,6 +157,48 @@ describe('apply method', () => {
expect(doc.paragraphs?.[0]).not.toBe(actions[0].body?.paragraphs?.[0]);
});
+ it('preserves whole-entity metadata when inserting inside a custom range', () => {
+ const doc: IDocumentBody = {
+ dataStream: 'formula\r\n',
+ customRanges: [{
+ startIndex: 0,
+ endIndex: 6,
+ rangeId: 'formula-range',
+ rangeType: CustomRangeType.CUSTOM,
+ wholeEntity: true,
+ properties: { kind: 'formula' },
+ }],
+ };
+
+ TextX.apply(doc, [
+ { t: TextXActionType.RETAIN, len: 3 },
+ {
+ t: TextXActionType.INSERT,
+ len: 1,
+ body: {
+ dataStream: 'X',
+ customRanges: [{
+ startIndex: 0,
+ endIndex: 0,
+ rangeId: 'formula-range',
+ rangeType: CustomRangeType.CUSTOM,
+ wholeEntity: true,
+ properties: { kind: 'formula' },
+ }],
+ },
+ },
+ ]);
+
+ expect(doc.customRanges).toEqual([{
+ startIndex: 0,
+ endIndex: 7,
+ rangeId: 'formula-range',
+ rangeType: CustomRangeType.CUSTOM,
+ wholeEntity: true,
+ properties: { kind: 'formula' },
+ }]);
+ });
+
it('should get the same result when apply two actions by order OR composed first case 1', () => {
const actionsA: TextXAction[] = [
{
diff --git a/packages/core/src/docs/data-model/text-x/apply-utils/common.ts b/packages/core/src/docs/data-model/text-x/apply-utils/common.ts
index bdb5a12308..7bda20cd55 100644
--- a/packages/core/src/docs/data-model/text-x/apply-utils/common.ts
+++ b/packages/core/src/docs/data-model/text-x/apply-utils/common.ts
@@ -646,14 +646,12 @@ export function splitCustomRangesByIndex(customRanges: ICustomRange[], currentIn
if (matchedCustomRange) {
customRanges.splice(matchedCustomRangeIndex, 1, {
- rangeId: matchedCustomRange.rangeId,
- rangeType: matchedCustomRange.rangeType,
+ ...matchedCustomRange,
startIndex: matchedCustomRange.startIndex,
endIndex: currentIndex - 1,
properties: { ...matchedCustomRange.properties },
}, {
- rangeId: matchedCustomRange.rangeId,
- rangeType: matchedCustomRange.rangeType,
+ ...matchedCustomRange,
startIndex: currentIndex,
endIndex: matchedCustomRange.endIndex,
properties: { ...matchedCustomRange.properties },