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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(docs): reserve empty textRuns when transform (#3949)
Ran Luo39d0d46b2cc4
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.tsindex a12209a39f..9c8c7c8e90 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@@ -14,11 +14,11 @@* limitations under the License.*/-import type { IDocumentBody } from '../../../../types/interfaces';import type { TextXAction } from '../action-types';import { describe, expect, it } from 'vitest';import { UpdateDocsAttributeType } from '../../../../shared';import { BooleanNumber, HorizontalAlign } from '../../../../types/enum';+import { CustomRangeType, type IDocumentBody } from '../../../../types/interfaces';import { PresetListType } from '../../preset-list-type';import { TextXActionType } from '../action-types';import { TextX } from '../text-x';@@ -79,6 +79,37 @@ function getDefaultDocWithParagraph() {return doc;}+function getDefaultDocWithCustomRange() {+ const doc: IDocumentBody = {+ dataStream: 'helloworld\r\n',+ textRuns: [],+ customRanges: [{+ startIndex: 0,+ endIndex: 4,+ rangeId: 'rangeId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'http://www.baidu.com',+ },+ }, {+ startIndex: 5,+ endIndex: 9,+ rangeId: 'rangeId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'http://www.yahoo.com',+ },+ }],+ paragraphs: [+ {+ startIndex: 10,+ },+ ],+ };++ return doc;+}+describe('apply method', () => {it('should get the same result when apply two actions by order OR composed first case 1', () => {const actionsA: TextXAction[] = [@@ -613,6 +644,55 @@ describe('apply method', () => {const resultC = TextX.apply(doc3, composedAction1);const resultD = TextX.apply(doc4, composedAction2);+ expect(resultA).toEqual(resultB);+ expect(resultC).toEqual(resultD);+ expect(resultA).toEqual(resultC);+ expect(composedAction1).toEqual(composedAction2);+ });++ it('should get the same result when one is undo actions and the other is cancel link', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 5,+ body: {+ dataStream: '',+ customRanges: [],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 5,+ },+ {+ t: TextXActionType.RETAIN,+ len: 5,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customDecorations: [],+ customRanges: [],+ },+ },+ ];++ const doc1 = getDefaultDocWithCustomRange();+ const doc2 = getDefaultDocWithCustomRange();+ const doc3 = getDefaultDocWithCustomRange();+ const doc4 = getDefaultDocWithCustomRange();++ const resultA = TextX.apply(TextX.apply(doc1, actionsA), TextX.transform(actionsB, actionsA, 'left'));+ const resultB = TextX.apply(TextX.apply(doc2, actionsB), TextX.transform(actionsA, actionsB, 'right'));++ const composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));+ const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));++ const resultC = TextX.apply(doc3, composedAction1);+ const resultD = TextX.apply(doc4, composedAction2);+// console.log(JSON.stringify(resultA, null, 2));// console.log(JSON.stringify(resultB, null, 2));@@ -620,6 +700,7 @@ describe('apply method', () => {// console.log('composedAction2', JSON.stringify(composedAction2, null, 2));// console.log(JSON.stringify(resultC, null, 2));+ // console.log(JSON.stringify(resultD, null, 2));expect(resultA).toEqual(resultB);expect(resultC).toEqual(resultD);diff --git a/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.tsindex 06d6a22df9..fb0560f94b 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.ts@@ -371,7 +371,23 @@ describe('transform()', () => {},];- const expectedActions2: TextXAction[] = [];+ const expectedActions2: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 1,+ ts: {},+ },+ ],+ },+ },+ ];expect(TextX._transform(actionsA1, actionsB1, 'left')).toEqual(expectedActions1);expect(TextX._transform(actionsB2, actionsA2, 'left')).toEqual(expectedActions2);diff --git a/packages/core/src/docs/data-model/text-x/__tests__/utils.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/utils.spec.tsindex 36617544af..d38a07395e 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/utils.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/utils.spec.ts@@ -430,7 +430,7 @@ describe('test text-x utils', () => {};expect(isUselessRetainAction(action1)).toBe(true);- expect(isUselessRetainAction(action2)).toBe(true);+ expect(isUselessRetainAction(action2)).toBe(false);expect(isUselessRetainAction(action3)).toBe(false);});});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.tsindex bd1d605d0a..a22522c45d 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@@ -30,7 +30,7 @@ import { horizontalLineSegmentsSubtraction, sortRulesFactory, Tools } from '../.import { isSameStyleTextRun } from '../../../../shared/compare';import { getBodySlice } from '../utils';-export function normalizeTextRuns(textRuns: ITextRun[]) {+export function normalizeTextRuns(textRuns: ITextRun[], reserveEmptyTextRun = false): ITextRun[] {const results: ITextRun[] = [];for (const textRun of textRuns) {@@ -45,7 +45,7 @@ export function normalizeTextRuns(textRuns: ITextRun[]) {}// Delete textRun if it has no style(ts is empty or has no sId)- if (Tools.isEmptyObject(ts) && textRun.sId == null) {+ if (!reserveEmptyTextRun && Tools.isEmptyObject(ts) && textRun.sId == null) {continue;}diff --git a/packages/core/src/docs/data-model/text-x/transform-utils.ts b/packages/core/src/docs/data-model/text-x/transform-utils.tsindex 343218cfb7..75034b0f81 100644--- a/packages/core/src/docs/data-model/text-x/transform-utils.ts+++ b/packages/core/src/docs/data-model/text-x/transform-utils.ts@@ -193,7 +193,7 @@ function transformTextRuns(}}- return normalizeTextRuns(newUpdateTextRuns);+ return normalizeTextRuns(newUpdateTextRuns, true);}function transformCustomRanges(diff --git a/packages/core/src/docs/data-model/text-x/utils.ts b/packages/core/src/docs/data-model/text-x/utils.tsindex b64d6c21e2..201f6f11dc 100644--- a/packages/core/src/docs/data-model/text-x/utils.ts+++ b/packages/core/src/docs/data-model/text-x/utils.ts@@ -426,9 +426,9 @@ export function isUselessRetainAction(action: IRetainAction): boolean {return true;}- const { textRuns = [], paragraphs = [], customRanges = [], customBlocks = [], customDecorations = [] } = body;+ const { textRuns, paragraphs, customRanges, customBlocks, customDecorations, tables } = body;- if (textRuns.length === 0 && paragraphs.length === 0 && customRanges.length === 0 && customBlocks.length === 0 && customDecorations.length === 0) {+ if (textRuns == null && paragraphs == null && customRanges == null && customBlocks == null && customDecorations == null && tables == null) {return true;}