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
refactor: transform in body (#3886)
Ran Luoa437ad0460ef
diff --git a/packages/core/src/docs/data-model/text-x/__tests__/compose.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/compose.spec.tsindex 3db21548b2..0dce649973 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/compose.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/compose.spec.ts@@ -16,6 +16,7 @@import type { TextXAction } from '../action-types';import { describe, expect, it } from 'vitest';+import { UpdateDocsAttributeType } from '../../../../shared';import { BooleanNumber } from '../../../../types/enum/text-style';import { TextXActionType } from '../action-types';import { TextX } from '../text-x';@@ -325,6 +326,7 @@ describe('compose test cases', () => {const expect_actions: TextXAction[] = [{t: TextXActionType.RETAIN,+ coverType: UpdateDocsAttributeType.COVER,body: {dataStream: '',textRuns: [{diff --git a/packages/core/src/docs/data-model/text-x/__tests__/transform-paragraph.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/transform-paragraph.spec.tsnew file mode 100644index 0000000000..7e52c09266--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/__tests__/transform-paragraph.spec.ts@@ -0,0 +1,425 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IDocumentBody } from '../../../../types/interfaces/i-document-data';+import type { TextXAction } from '../action-types';+import { describe, expect, it } from 'vitest';+import { UpdateDocsAttributeType } from '../../../../shared';+import { PresetListType } from '../../preset-list-type';+import { TextXActionType } from '../action-types';+import { TextX } from '../text-x';++function getDefaultDocWithParagraph() {+ const doc: IDocumentBody = {+ dataStream: '\r\n',+ paragraphs: [+ {+ startIndex: 0,+ },+ ],+ };++ return doc;+}++// Test Retain + Retain with different coverType in transform paragraph.+describe('transform paragraph in body', () => {+ it('should pass test when REPLACE + REPLACE', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ },+ }],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ },+ }],+ },+ },+ ];++ const doc1 = getDefaultDocWithParagraph();+ const doc2 = getDefaultDocWithParagraph();+ const doc3 = getDefaultDocWithParagraph();+ const doc4 = getDefaultDocWithParagraph();++ 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);++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ expect(resultA).toEqual(resultB);+ expect(resultC).toEqual(resultD);+ expect(resultA).toEqual(resultC);+ expect(composedAction1).toEqual(composedAction2);+ });++ it('should pass test when REPLACE + COVER', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 2,+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 2,+ spaceBelow: {+ v: 20,+ },+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ const doc1 = getDefaultDocWithParagraph();+ const doc2 = getDefaultDocWithParagraph();+ const doc3 = getDefaultDocWithParagraph();+ const doc4 = getDefaultDocWithParagraph();++ const resultA = TextX.apply(TextX.apply(doc1, actionsA), TextX.transform(actionsB, actionsA, 'right'));+ const resultB = TextX.apply(TextX.apply(doc2, actionsB), TextX.transform(actionsA, actionsB, 'left'));++ const composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'right'));+ const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'left'));++ 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));++ expect(resultA).toEqual(resultB);+ expect(resultC).toEqual(resultD);+ expect(resultA).toEqual(resultC);+ expect(composedAction1).toEqual(composedAction2);++ const doc5 = getDefaultDocWithParagraph();+ const doc6 = getDefaultDocWithParagraph();+ const doc7 = getDefaultDocWithParagraph();+ const doc8 = getDefaultDocWithParagraph();++ const resultE = TextX.apply(TextX.apply(doc5, actionsA), TextX.transform(actionsB, actionsA, 'left'));+ const resultF = TextX.apply(TextX.apply(doc6, actionsB), TextX.transform(actionsA, actionsB, 'right'));++ const composedAction3 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));+ const composedAction4 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));++ const resultG = TextX.apply(doc7, composedAction3);+ const resultH = TextX.apply(doc8, composedAction4);++ expect(resultE).toEqual(resultF);+ expect(resultG).toEqual(resultH);+ expect(resultE).toEqual(resultG);+ expect(composedAction3).toEqual(composedAction4);+ });++ it('should pass test when COVER + COVER', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 2,+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ spaceAbove: {+ v: 30,+ },+ },+ }],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ spaceAbove: {+ v: 30,+ },+ },+ }],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ spaceBelow: {+ v: 20,+ },+ spaceAbove: {+ v: 30,+ },+ },+ }],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ const doc1 = getDefaultDocWithParagraph();+ const doc2 = getDefaultDocWithParagraph();+ const doc3 = getDefaultDocWithParagraph();+ const doc4 = getDefaultDocWithParagraph();++ 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);++ expect(resultA).toEqual(resultB);+ expect(resultC).toEqual(resultD);+ expect(resultA).toEqual(resultC);+ expect(composedAction1).toEqual(composedAction2);+ });+});diff --git a/packages/core/src/docs/data-model/text-x/__tests__/transform-textrun.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/transform-textrun.spec.tsnew file mode 100644index 0000000000..dd60af8a2b--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/__tests__/transform-textrun.spec.ts@@ -0,0 +1,384 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IDocumentBody } from '../../../../types/interfaces/i-document-data';+import type { TextXAction } from '../action-types';+import { describe, expect, it } from 'vitest';+import { UpdateDocsAttributeType } from '../../../../shared';+import { BooleanNumber } from '../../../../types/enum';+import { TextXActionType } from '../action-types';+import { TextX } from '../text-x';++function getDefaultDocWithTextRuns() {+ const doc: IDocumentBody = {+ dataStream: 'w\r\n',+ textRuns: [+ {+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ },+ ],+ };++ return doc;+}++// Test Retain + Retain with different coverType in transform textRun.+describe('transform textRun in body', () => {+ it('should pass test when REPLACE + REPLACE', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ fs: 10,+ },+ }],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.FALSE,+ fs: 14,+ },+ }],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ fs: 10,+ },+ }],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.FALSE,+ fs: 14,+ },+ }],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ const doc1 = getDefaultDocWithTextRuns();+ const doc2 = getDefaultDocWithTextRuns();+ const doc3 = getDefaultDocWithTextRuns();+ const doc4 = getDefaultDocWithTextRuns();++ 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'));++ // console.log(JSON.stringify(TextX.transform(actionsB, actionsA, 'left'), null, 2));+ // console.log(JSON.stringify(composedAction1, null, 2));+ // console.log(JSON.stringify(composedAction2, null, 2));++ 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 pass test when REPLACE + COVER', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ fs: 10,+ },+ }],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ it: BooleanNumber.FALSE,+ fs: 14,+ },+ }],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ }],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ it: BooleanNumber.FALSE,+ fs: 14,+ },+ }],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ const doc1 = getDefaultDocWithTextRuns();+ const doc2 = getDefaultDocWithTextRuns();+ const doc3 = getDefaultDocWithTextRuns();+ const doc4 = getDefaultDocWithTextRuns();++ 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'));++ // console.log(JSON.stringify(TextX.transform(actionsA, actionsB, 'right'), null, 2));+ // console.log('resultA');+ // console.log(JSON.stringify(resultA, null, 2));+ // console.log('resultB');+ // console.log(JSON.stringify(resultB, null, 2));+ // console.log(JSON.stringify(composedAction1, null, 2));+ // console.log(JSON.stringify(composedAction2, null, 2));++ 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);++ const doc5 = getDefaultDocWithTextRuns();+ const doc6 = getDefaultDocWithTextRuns();+ const doc7 = getDefaultDocWithTextRuns();+ const doc8 = getDefaultDocWithTextRuns();++ const resultE = TextX.apply(TextX.apply(doc5, actionsA), TextX.transform(actionsB, actionsA, 'right'));+ const resultF = TextX.apply(TextX.apply(doc6, actionsB), TextX.transform(actionsA, actionsB, 'left'));++ const composedAction3 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'right'));+ const composedAction4 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'left'));++ const resultG = TextX.apply(doc7, composedAction3);+ const resultH = TextX.apply(doc8, composedAction4);++ expect(resultE).toEqual(resultF);+ expect(resultG).toEqual(resultH);+ expect(resultE).toEqual(resultG);+ expect(composedAction3).toEqual(composedAction4);+ });++ it('should pass test when COVER + COVER', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ fs: 10,+ ul: {+ s: BooleanNumber.TRUE,+ },+ },+ }],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ it: BooleanNumber.FALSE,+ fs: 14,+ },+ }],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ }],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ it: BooleanNumber.FALSE,+ fs: 14,+ },+ }],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ const doc1 = getDefaultDocWithTextRuns();+ const doc2 = getDefaultDocWithTextRuns();+ const doc3 = getDefaultDocWithTextRuns();+ const doc4 = getDefaultDocWithTextRuns();++ 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'));++ // console.log(JSON.stringify(TextX.transform(actionsB, actionsA, 'left'), null, 2));+ // console.log(JSON.stringify(TextX.transform(actionsA, actionsB, 'right'), null, 2));+ // console.log(JSON.stringify(composedAction1, null, 2));+ // console.log(JSON.stringify(composedAction2, null, 2));++ 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);+ });+});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 7e746b47e0..aa033e9ce9 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@@ -368,6 +368,7 @@ describe('transform()', () => {{t: TextXActionType.RETAIN,len: 1,+ coverType: UpdateDocsAttributeType.COVER,body: {dataStream: '',textRuns: [@@ -470,6 +471,7 @@ describe('transform()', () => {{t: TextXActionType.RETAIN,len: 1,+ coverType: UpdateDocsAttributeType.COVER,body: {dataStream: '',textRuns: [@@ -491,6 +493,7 @@ describe('transform()', () => {{t: TextXActionType.RETAIN,len: 1,+ coverType: UpdateDocsAttributeType.COVER,body: {dataStream: '',textRuns: [@@ -589,6 +592,7 @@ describe('transform()', () => {{t: TextXActionType.RETAIN,len: 1,+ coverType: UpdateDocsAttributeType.COVER,body: {dataStream: '',paragraphs: [@@ -607,6 +611,7 @@ describe('transform()', () => {{t: TextXActionType.RETAIN,len: 1,+ coverType: UpdateDocsAttributeType.COVER,body: {dataStream: '',paragraphs: [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 789cf5d7f7..d0a4e5fd13 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@@ -18,6 +18,7 @@ import type { IDocumentBody } from '../../../../types/interfaces/i-document-dataimport type { IRetainAction } from '../action-types';import { describe, expect, it } from 'vitest';import { BooleanNumber } from '../../../../types/enum/text-style';+import { PresetListType } from '../../preset-list-type';import { TextXActionType } from '../action-types';import { composeBody, getBodySlice, isUselessRetainAction } from '../utils';@@ -237,7 +238,7 @@ describe('test text-x utils', () => {}).toThrowError();});- it('test composeBody fn both width paragraphs', () => {+ it('test composeBody both with paragraphs', () => {const thisBody: IDocumentBody = {dataStream: 'hello\nworld',paragraphs: [{@@ -272,6 +273,62 @@ describe('test text-x utils', () => {});});+ it('test composeBody both with paragraphs and one has bullet list', () => {+ const thisBody: IDocumentBody = {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 2,+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ };++ const otherBody: IDocumentBody = {+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ },+ }],+ };++ const composedBody = composeBody(thisBody, otherBody);++ expect(composedBody).toEqual({+ dataStream: '',+ paragraphs: [{+ startIndex: 0,+ paragraphStyle: {+ lineSpacing: 1,+ spaceBelow: {+ v: 20,+ },+ },+ bullet: {+ listType: PresetListType.BULLET_LIST,+ listId: 'testBullet',+ nestingLevel: 0,+ textStyle: {+ fs: 15,+ },+ },+ }],+ });+ });+it('test composeBody fn both width paragraphs and from retain', () => {const thisBody: IDocumentBody = {dataStream: '',diff --git a/packages/core/src/docs/data-model/text-x/action-iterator.ts b/packages/core/src/docs/data-model/text-x/action-iterator.tsindex 9c0f867bee..40a40663f3 100644--- a/packages/core/src/docs/data-model/text-x/action-iterator.ts+++ b/packages/core/src/docs/data-model/text-x/action-iterator.ts@@ -42,6 +42,7 @@ export class ActionIterator {if (nextAction) {const offset = this._offset;const actionLength = nextAction.len;+if (length >= actionLength - offset) {// Return the action if the length is great than action length, and reset offset to 0.length = actionLength - offset;diff --git a/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.ts b/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.tsindex f38c90715c..3af6f6cabb 100644--- a/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.ts+++ b/packages/core/src/docs/data-model/text-x/apply-utils/update-apply.ts@@ -101,19 +101,19 @@ function updateTextRuns(// eslint-disable-next-line max-lines-per-function, complexityexport function coverTextRuns(updateDataTextRuns: ITextRun[],- removeTextRuns: ITextRun[],+ originTextRuns: ITextRun[],coverType: UpdateDocsAttributeType) {- if (removeTextRuns.length === 0) {+ if (originTextRuns.length === 0) {return updateDataTextRuns;}updateDataTextRuns = Tools.deepClone(updateDataTextRuns);- removeTextRuns = Tools.deepClone(removeTextRuns);+ originTextRuns = Tools.deepClone(originTextRuns);const newUpdateTextRuns: ITextRun[] = [];const updateLength = updateDataTextRuns.length;- const removeLength = removeTextRuns.length;+ const removeLength = originTextRuns.length;let updateIndex = 0;let removeIndex = 0;let pending: Nullable<ITextRun> = null;@@ -131,13 +131,13 @@ export function coverTextRuns(while (updateIndex < updateLength && removeIndex < removeLength) {const { st: updateSt, ed: updateEd, ts: updateStyle } = updateDataTextRuns[updateIndex];- const { st: removeSt, ed: removeEd, ts: removeStyle, sId } = removeTextRuns[removeIndex];+ const { st: removeSt, ed: removeEd, ts: originStyle, sId } = originTextRuns[removeIndex];let newTs;if (coverType === UpdateDocsAttributeType.COVER) {- newTs = { ...removeStyle, ...updateStyle };+ newTs = { ...originStyle, ...updateStyle };} else {- newTs = { ...updateStyle, ...removeStyle };+ newTs = { ...updateStyle };}if (updateEd < removeSt) {@@ -148,7 +148,7 @@ export function coverTextRuns(updateIndex++;} else if (removeEd < updateSt) {if (!pushPendingAndReturnStatus()) {- newUpdateTextRuns.push(removeTextRuns[removeIndex]);+ newUpdateTextRuns.push(originTextRuns[removeIndex]);}removeIndex++;@@ -156,7 +156,7 @@ export function coverTextRuns(const newTextRun = {st: Math.min(updateSt, removeSt),ed: Math.max(updateSt, removeSt),- ts: updateSt < removeSt ? { ...updateStyle } : { ...removeStyle },+ ts: updateSt < removeSt ? { ...updateStyle } : { ...originStyle },sId: updateSt < removeSt ? undefined : sId,};@@ -173,8 +173,8 @@ export function coverTextRuns(if (updateEd < removeEd) {updateIndex++;- removeTextRuns[removeIndex].st = updateEd;- if (removeTextRuns[removeIndex].st === removeTextRuns[removeIndex].ed) {+ originTextRuns[removeIndex].st = updateEd;+ if (originTextRuns[removeIndex].st === originTextRuns[removeIndex].ed) {removeIndex++;}} else {@@ -188,7 +188,7 @@ export function coverTextRuns(const pendingTextRun = {st: Math.min(updateEd, removeEd),ed: Math.max(updateEd, removeEd),- ts: updateEd < removeEd ? { ...removeStyle } : { ...updateStyle },+ ts: updateEd < removeEd ? { ...originStyle } : { ...updateStyle },sId: updateEd < removeEd ? sId : undefined,};@@ -201,7 +201,7 @@ export function coverTextRuns(// If the last textRun is also disjoint, then the last textRun needs to be pushed in `newUpdateTextRun`const tempTopTextRun = newUpdateTextRuns[newUpdateTextRuns.length - 1];const updateLastTextRun = updateDataTextRuns[updateLength - 1];- const removeLastTextRun = removeTextRuns[removeLength - 1];+ const removeLastTextRun = originTextRuns[removeLength - 1];if (tempTopTextRun && (tempTopTextRun.ed !== Math.max(updateLastTextRun.ed, removeLastTextRun.ed))) {if (updateLastTextRun.ed > removeLastTextRun.ed) {diff --git a/packages/core/src/docs/data-model/text-x/text-x.ts b/packages/core/src/docs/data-model/text-x/text-x.tsindex 2157a585d0..64c7828ffe 100644--- a/packages/core/src/docs/data-model/text-x/text-x.ts+++ b/packages/core/src/docs/data-model/text-x/text-x.ts@@ -40,6 +40,7 @@ export class TextX {return textXApply(doc, actions);}+ // eslint-disable-next-line complexitystatic compose(thisActions: TextXAction[], otherActions: TextXAction[]): TextXAction[] {const thisIter = new ActionIterator(thisActions);const otherIter = new ActionIterator(otherActions);@@ -69,8 +70,14 @@ export class TextX {if (thisAction.body == null && otherAction.body == null) {textX.push(thisAction.len !== Number.POSITIVE_INFINITY ? thisAction : otherAction); // or otherAction} else if (thisAction.body && otherAction.body) {+ const coverType = thisAction.coverType === UpdateDocsAttributeType.REPLACE || otherAction.coverType === UpdateDocsAttributeType.REPLACE+ ? UpdateDocsAttributeType.REPLACE+ : UpdateDocsAttributeType.COVER;+textX.push({...thisAction,+ t: TextXActionType.RETAIN,+ coverType,body: composeBody(thisAction.body, otherAction.body, otherAction.coverType),});} else {@@ -159,9 +166,12 @@ export class TextX {if (thisAction.body == null || otherAction.body == null) {textX.push(otherAction);} else {+ const { coverType, body } = transformBody(thisAction as IRetainAction, otherAction as IRetainAction, priority === 'left');textX.push({...otherAction,- body: transformBody(thisAction as IRetainAction, otherAction as IRetainAction, priority === 'left'),+ t: TextXActionType.RETAIN,+ coverType,+ body,});}}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 8344aefe8d..42ed6d716b 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@@ -30,8 +30,14 @@ enum TextXTransformType {}// eslint-disable-next-line max-lines-per-function, complexity-function transformTextRuns(originTextRuns: ITextRun[], targetTextRuns: ITextRun[], transformType: TextXTransformType) {- if (originTextRuns.length === 0) {+function transformTextRuns(+ originTextRuns: ITextRun[],+ targetTextRuns: ITextRun[],+ originCoverType: UpdateDocsAttributeType,+ targetCoverType: UpdateDocsAttributeType,+ transformType: TextXTransformType+) {+ if (originTextRuns.length === 0 || targetTextRuns.length === 0) {return targetTextRuns;}@@ -55,7 +61,6 @@ function transformTextRuns(originTextRuns: ITextRun[], targetTextRuns: ITextRun[return false;}- // TODO: @JOCS, I think no need to iterate over the text runs and compare them! because the textRuns length is always 1.while (updateIndex < updateLength && removeIndex < removeLength) {const { st: updateSt, ed: updateEd, ts: targetStyle } = targetTextRuns[updateIndex];const { st: removeSt, ed: removeEd, ts: originStyle, sId } = originTextRuns[removeIndex];@@ -63,15 +68,58 @@ function transformTextRuns(originTextRuns: ITextRun[], targetTextRuns: ITextRun[if (transformType === TextXTransformType.COVER) {newTs = { ...targetStyle };- } else {- newTs = { ...targetStyle };- if (originStyle) {+ if (originCoverType === UpdateDocsAttributeType.COVER && targetCoverType === UpdateDocsAttributeType.REPLACE && originStyle) {const keys = Object.keys(originStyle);for (const key of keys) {- if (newTs[key as keyof ITextStyle]) {- delete newTs[key as keyof ITextStyle];+ if (newTs[key as keyof ITextStyle] === undefined) {+ newTs[key as keyof ITextStyle] = originStyle[key as keyof ITextStyle] as any;+ }+ }+ }+ } else {+ newTs = { ...targetStyle };++ if (originCoverType === UpdateDocsAttributeType.REPLACE) {+ if (targetCoverType === UpdateDocsAttributeType.REPLACE) {+ // If they are all REPLACE types, the highest priority is retained when transforming.+ newTs = { ...originStyle };+ } else {+ // If the target action is COVER and has a low priority, then the origin field should not be cover.+ if (targetStyle && originStyle) {+ const keys = Object.keys(targetStyle);++ for (const key of keys) {+ if (originStyle[key as keyof ITextStyle]) {+ delete newTs[key as keyof ITextStyle];+ }+ }+ }+ }+ } else {+ // If the origin is of type Cover and the target action is of type REPLACE+ // and has a low priority, then all fields in origin should be copied to the target+ if (targetCoverType === UpdateDocsAttributeType.REPLACE) {+ if (originStyle) {+ const keys = Object.keys(originStyle);++ for (const key of keys) {+ if (originStyle[key as keyof ITextStyle] !== undefined) {+ newTs[key as keyof ITextStyle] = originStyle[key as keyof ITextStyle] as any;+ }+ }+ }+ } else {+ // If they are all of the Cover type, then the fields in the Origin need to be deleted.+ if (originStyle) {+ const keys = Object.keys(originStyle);++ for (const key of keys) {+ if (newTs[key as keyof ITextStyle] !== undefined) {+ delete newTs[key as keyof ITextStyle];+ }+ }}}}@@ -172,37 +220,76 @@ function transformCustomRanges(// At present, only the two properties of paragraphStyle and bullet are handled,// paragraphStyle is treated separately, while bullet is treated as a whole because// the properties in bullet are related++// eslint-disable-next-line complexity, max-lines-per-functionfunction transformParagraph(originParagraph: IParagraph,targetParagraph: IParagraph,- transformType: TextXTransformType,- coverType: UpdateDocsAttributeType = UpdateDocsAttributeType.COVER+ originCoverType: UpdateDocsAttributeType,+ targetCoverType: UpdateDocsAttributeType,+ transformType: TextXTransformType): IParagraph {const paragraph: IParagraph = {startIndex: targetParagraph.startIndex,};if (targetParagraph.paragraphStyle) {- if (originParagraph.paragraphStyle == null) {- paragraph.paragraphStyle = {- ...targetParagraph.paragraphStyle,- };- } else {- if (coverType === UpdateDocsAttributeType.REPLACE) {- paragraph.paragraphStyle = transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED- ? { ...originParagraph.paragraphStyle }- : { ...targetParagraph.paragraphStyle };- } else {- paragraph.paragraphStyle = {- ...targetParagraph.paragraphStyle,- };+ paragraph.paragraphStyle = Tools.deepClone(targetParagraph.paragraphStyle);++ if (originParagraph.paragraphStyle) {+ if (originCoverType === UpdateDocsAttributeType.REPLACE) {+ if (targetCoverType === UpdateDocsAttributeType.REPLACE) {+ if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {+ paragraph.paragraphStyle = {+ ...originParagraph.paragraphStyle,+ };+ }+ } else {+ if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {+ const keys = Object.keys(originParagraph.paragraphStyle);++ for (const key of keys) {+ if (originParagraph.paragraphStyle[key as keyof IParagraphStyle] !== undefined) {+ paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;+ }+ }+ } else {+ const keys = Object.keys(originParagraph.paragraphStyle);- if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {- const keys = Object.keys(originParagraph.paragraphStyle);+ for (const key of keys) {+ if (paragraph.paragraphStyle[key as keyof IParagraphStyle] === undefined) {+ paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;+ }+ }+ }+ }+ } else {+ if (targetCoverType === UpdateDocsAttributeType.REPLACE) {+ if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {+ const keys = Object.keys(originParagraph.paragraphStyle);++ for (const key of keys) {+ if (originParagraph.paragraphStyle[key as keyof IParagraphStyle] !== undefined) {+ paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;+ }+ }+ } else {+ const keys = Object.keys(originParagraph.paragraphStyle);- for (const key of keys) {- if (paragraph.paragraphStyle[key as keyof IParagraphStyle]) {- delete paragraph.paragraphStyle[key as keyof IParagraphStyle];+ for (const key of keys) {+ if (paragraph.paragraphStyle[key as keyof IParagraphStyle] === undefined) {+ paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;+ }+ }+ }+ } else {+ if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {+ const keys = Object.keys(originParagraph.paragraphStyle);++ for (const key of keys) {+ if (paragraph.paragraphStyle[key as keyof IParagraphStyle]) {+ delete paragraph.paragraphStyle[key as keyof IParagraphStyle];+ }}}}@@ -210,18 +297,22 @@ function transformParagraph(}}- if (targetParagraph.bullet) {- if (originParagraph.bullet == null) {- paragraph.bullet = {- ...targetParagraph.bullet,- };+ if (originCoverType === UpdateDocsAttributeType.REPLACE && targetCoverType === UpdateDocsAttributeType.REPLACE) {+ paragraph.bullet = transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED+ ? Tools.deepClone(originParagraph.bullet)+ : Tools.deepClone(targetParagraph.bullet);+ } else {+ if (originParagraph.bullet === undefined) {+ paragraph.bullet = Tools.deepClone(targetParagraph.bullet);} else {- if (coverType === UpdateDocsAttributeType.REPLACE) {- paragraph.bullet = transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED- ? { ...originParagraph.bullet }- : { ...targetParagraph.bullet };+ if (originCoverType === UpdateDocsAttributeType.REPLACE || targetCoverType === UpdateDocsAttributeType.REPLACE) {+ paragraph.bullet = transformType === TextXTransformType.COVER && targetParagraph.bullet+ ? Tools.deepClone(targetParagraph.bullet)+ : Tools.deepClone(originParagraph.bullet);} else {- throw new Error('Bullet is only supported in replace mode.');+ if (transformType === TextXTransformType.COVER && targetParagraph.bullet !== undefined) {+ paragraph.bullet = Tools.deepClone(targetParagraph.bullet);+ }}}}@@ -229,35 +320,38 @@ function transformParagraph(return paragraph;}+interface ITransformBodyResult {+ body: IDocumentBody;+ coverType: UpdateDocsAttributeType;+}+// eslint-disable-next-line max-lines-per-function, complexityexport function transformBody(thisAction: IRetainAction,otherAction: IRetainAction,priority: boolean = false-): IDocumentBody {- const { body: thisBody, coverType: thisCoverType } = thisAction;- const { body: otherBody, coverType: otherCoverType } = otherAction;+): ITransformBodyResult {+ const { body: thisBody, coverType: thisCoverType = UpdateDocsAttributeType.COVER } = thisAction;+ const { body: otherBody, coverType: otherCoverType = UpdateDocsAttributeType.COVER } = otherAction;if (thisBody == null || thisBody.dataStream !== '' || otherBody == null || otherBody.dataStream !== '') {throw new Error('Data stream is not supported in retain transform.');}- if (thisCoverType !== otherCoverType) {- throw new Error('Cover type is not consistent.');- }-const retBody: IDocumentBody = {dataStream: '',};+ const coverType = otherCoverType;+const { textRuns: thisTextRuns = [], paragraphs: thisParagraphs = [], customRanges: thisCustomRanges = [] } = thisBody;const { textRuns: otherTextRuns = [], paragraphs: otherParagraphs = [], customRanges: otherCustomRanges = [] } = otherBody;let textRuns: ITextRun[] = [];if (priority) {- textRuns = transformTextRuns(thisTextRuns, otherTextRuns, TextXTransformType.COVER_ONLY_NOT_EXISTED);+ textRuns = transformTextRuns(thisTextRuns, otherTextRuns, thisCoverType, otherCoverType, TextXTransformType.COVER_ONLY_NOT_EXISTED);} else {- textRuns = transformTextRuns(thisTextRuns, otherTextRuns, TextXTransformType.COVER);+ textRuns = transformTextRuns(thisTextRuns, otherTextRuns, thisCoverType, otherCoverType, TextXTransformType.COVER);}if (textRuns.length) {retBody.textRuns = textRuns;@@ -295,11 +389,18 @@ export function transformBody(paragraph = transformParagraph(thisParagraph,otherParagraph,- TextXTransformType.COVER_ONLY_NOT_EXISTED,- thisCoverType+ thisCoverType,+ otherCoverType,+ TextXTransformType.COVER_ONLY_NOT_EXISTED);} else {- paragraph = transformParagraph(thisParagraph, otherParagraph, TextXTransformType.COVER, thisCoverType);+ paragraph = transformParagraph(+ thisParagraph,+ otherParagraph,+ thisCoverType,+ otherCoverType,+ TextXTransformType.COVER+ );}paragraphs.push(paragraph);@@ -322,5 +423,8 @@ export function transformBody(retBody.paragraphs = paragraphs;}- return retBody;+ return {+ coverType,+ body: retBody,+ };}diff --git a/packages/core/src/docs/data-model/text-x/utils.ts b/packages/core/src/docs/data-model/text-x/utils.tsindex d63a8a52b4..60c72232ba 100644--- a/packages/core/src/docs/data-model/text-x/utils.ts+++ b/packages/core/src/docs/data-model/text-x/utils.ts@@ -346,6 +346,7 @@ export function composeBody(if (paragraphs.length) {retBody.paragraphs = paragraphs;}+return retBody;}diff --git a/packages/core/src/shared/command-enum.ts b/packages/core/src/shared/command-enum.tsindex 5579caec6a..4e091ddacd 100644--- a/packages/core/src/shared/command-enum.ts+++ b/packages/core/src/shared/command-enum.ts@@ -17,5 +17,6 @@export enum UpdateDocsAttributeType {COVER, // Default, if not present, add; if present, overwrite, while retaining the original properties.REPLACE, // Replace the original properties entirely.- INTERSECTION, // Retain only the intersecting properties.+ // Retain only the intersecting properties.+ // INTERSECTION,}