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(docs): transform custom range and decoration (#3930)
Ran Luoae5832e64d44
diff --git a/packages/core/src/docs/data-model/text-x/__tests__/action-iterator.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/action-iterator.spec.tsindex cf43fe1b3c..09d3398709 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/action-iterator.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/action-iterator.spec.ts@@ -75,8 +75,6 @@ describe('Test action iterator', () => {bl: BooleanNumber.TRUE,},}],- customRanges: [],- customDecorations: [],},len: 2,});@@ -89,8 +87,6 @@ describe('Test action iterator', () => {t: TextXActionType.INSERT,body: {dataStream: 'llo',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,ed: 3,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 0dce649973..8e8b1c2603 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@@ -143,8 +143,6 @@ describe('compose test cases', () => {t: TextXActionType.INSERT,body: {dataStream: 'h',- customRanges: [],- customDecorations: [],},len: 1,}, {@@ -190,8 +188,6 @@ describe('compose test cases', () => {len: 1,body: {dataStream: 'h',- customRanges: [],- customDecorations: [],},}];@@ -235,8 +231,6 @@ describe('compose test cases', () => {t: TextXActionType.INSERT,body: {dataStream: 'h',- customRanges: [],- customDecorations: [],},len: 1,}];@@ -418,8 +412,6 @@ describe('compose test cases', () => {it: BooleanNumber.TRUE,},}],- customRanges: [],- customDecorations: [],},len: 1,}];@@ -460,8 +452,6 @@ describe('compose test cases', () => {t: TextXActionType.INSERT,body: {dataStream: 'ab',- customRanges: [],- customDecorations: [],},len: 2,}];@@ -489,8 +479,6 @@ describe('compose test cases', () => {t: TextXActionType.INSERT,body: {dataStream: 'c',- customRanges: [],- customDecorations: [],},len: 1,}];diff --git a/packages/core/src/docs/data-model/text-x/__tests__/invert.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/invert.spec.tsindex 6c0c5652e1..2b7246e708 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/invert.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/invert.spec.ts@@ -223,8 +223,6 @@ describe('test TextX static methods invert and makeInvertible', () => {len: 2,body: {dataStream: 'lo',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,ed: 2,diff --git a/packages/core/src/docs/data-model/text-x/__tests__/transform-custom-decorations.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/transform-custom-decorations.spec.tsnew file mode 100644index 0000000000..2c457850be--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/__tests__/transform-custom-decorations.spec.ts@@ -0,0 +1,252 @@+/**+ * 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 { TextXAction } from '../action-types';+import { describe, expect, it } from 'vitest';+import { UpdateDocsAttributeType } from '../../../../shared';+import { BooleanNumber } from '../../../../types/enum';+import { CustomDecorationType, type IDocumentBody } from '../../../../types/interfaces/i-document-data';+import { TextXActionType } from '../action-types';+import { TextX } from '../text-x';++function getDefaultDocWithCustomDecorations() {+ const doc: IDocumentBody = {+ dataStream: 'Google\r\n',+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.COMMENT,+ },+ ],+ };++ return doc;+}++// Test Retain + Retain with different coverType in transform textRun.+describe('transform custom range in body', () => {+ it('should pass test when normal transform', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.DELETED,+ },+ ],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.COMMENT,+ },+ ],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.DELETED,+ },+ ],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.DELETED,+ },+ ],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ const doc1 = getDefaultDocWithCustomDecorations();+ const doc2 = getDefaultDocWithCustomDecorations();+ const doc3 = getDefaultDocWithCustomDecorations();+ const doc4 = getDefaultDocWithCustomDecorations();++ 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);+ });++ it('should pass test when any is empty', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ it: BooleanNumber.TRUE,+ },+ },+ ],+ customDecorations: [],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ],+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.DELETED,+ },+ ],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ],+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.DELETED,+ },+ ],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ],+ customDecorations: [+ {+ startIndex: 0,+ endIndex: 5,+ id: 'commentId',+ type: CustomDecorationType.DELETED,+ },+ ],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);+ });+});diff --git a/packages/core/src/docs/data-model/text-x/__tests__/transform-custom-range.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/transform-custom-range.spec.tsnew file mode 100644index 0000000000..9000a641c4--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/__tests__/transform-custom-range.spec.ts@@ -0,0 +1,508 @@+/**+ * 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 { TextXAction } from '../action-types';+import { describe, expect, it } from 'vitest';+import { UpdateDocsAttributeType } from '../../../../shared';+import { BooleanNumber } from '../../../../types/enum';+import { CustomRangeType, type IDocumentBody } from '../../../../types/interfaces/i-document-data';+import { TextXActionType } from '../action-types';+import { TextX } from '../text-x';++function getDefaultDocWithCustomRange() {+ const doc: IDocumentBody = {+ dataStream: 'Google\r\n',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'jocsId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.google.com',+ },+ },+ ],+ };++ return doc;+}++// Test Retain + Retain with different coverType in transform textRun.+describe('transform custom range in body', () => {+ it('should pass test when REPLACE + REPLACE', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'xxxId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'yyyId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.yahoo.com',+ },+ },+ ],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'xxxId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'yyyId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.yahoo.com',+ },+ },+ ],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ 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);++ 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: 6,+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'xxxId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'jocsId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.yahoo.com',+ },+ },+ ],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'xxxId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'jocsId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.yahoo.com',+ },+ },+ ],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ 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);++ expect(resultA).toEqual(resultB);+ expect(resultC).toEqual(resultD);+ expect(resultA).toEqual(resultC);+ expect(composedAction1).toEqual(composedAction2);++ const doc5 = getDefaultDocWithCustomRange();+ const doc6 = getDefaultDocWithCustomRange();+ const doc7 = getDefaultDocWithCustomRange();+ const doc8 = getDefaultDocWithCustomRange();++ 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: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'jocsId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'jocsId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.yahoo.com',+ },+ },+ ],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'jocsId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: 0,+ endIndex: 5,+ rangeId: 'jocsId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.yahoo.com',+ },+ },+ ],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ 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);++ expect(resultA).toEqual(resultB);+ expect(resultC).toEqual(resultD);+ expect(resultA).toEqual(resultC);+ expect(composedAction1).toEqual(composedAction2);+ });++ it('should pass test when any is empty', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ it: BooleanNumber.TRUE,+ },+ },+ ],+ customRanges: [],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ],+ customRanges: [],+ },+ },+ ];++ const expectedTransformedActionFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ],+ customRanges: [],+ },+ },+ ];++ const expectedTransformedActionTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 6,+ coverType: UpdateDocsAttributeType.COVER,+ body: {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 6,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ],+ customRanges: [],+ },+ },+ ];++ expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);+ expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);++ 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);++ 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 e0d45742c8..06d6a22df9 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@@ -30,8 +30,6 @@ describe('transform()', () => {len: 1,body: {dataStream: 'h',- customRanges: [],- customDecorations: [],},},];@@ -55,8 +53,6 @@ describe('transform()', () => {len: 1,body: {dataStream: 'e',- customRanges: [],- customDecorations: [],},},];@@ -67,8 +63,6 @@ describe('transform()', () => {len: 1,body: {dataStream: 'e',- customRanges: [],- customDecorations: [],},},];@@ -115,8 +109,6 @@ describe('transform()', () => {len: 1,body: {dataStream: '',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,@@ -140,8 +132,6 @@ describe('transform()', () => {len: 1,body: {dataStream: '',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,@@ -211,8 +201,6 @@ describe('transform()', () => {len: 1,body: {dataStream: 'h',- customRanges: [],- customDecorations: [],},},];@@ -277,8 +265,6 @@ describe('transform()', () => {t: TextXActionType.INSERT,body: {dataStream: 'h',- customRanges: [],- customDecorations: [],},len: 1,},@@ -351,6 +337,7 @@ describe('transform()', () => {len: 1,body: {dataStream: '',+ customRanges: [],textRuns: [{st: 0,@@ -722,6 +709,7 @@ describe('transform()', () => {coverType: UpdateDocsAttributeType.REPLACE,body: {dataStream: '',+ textRuns: [],customRanges: [{startIndex: 0,@@ -846,8 +834,6 @@ describe('transform()', () => {len: 1,body: {dataStream: 'e',- customRanges: [],- customDecorations: [],},}];@@ -856,8 +842,6 @@ describe('transform()', () => {len: 1,body: {dataStream: '',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,@@ -905,8 +889,6 @@ describe('transform()', () => {len: 1,body: {dataStream: 'e',- customRanges: [],- customDecorations: [],},}];@@ -918,8 +900,6 @@ describe('transform()', () => {len: 1,body: {dataStream: '',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,@@ -973,8 +953,6 @@ describe('transform()', () => {len: 1,body: {dataStream: 'e',- customRanges: [],- customDecorations: [],},}];@@ -983,8 +961,6 @@ describe('transform()', () => {len: 1,body: {dataStream: '',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,@@ -1003,8 +979,6 @@ describe('transform()', () => {len: 1,body: {dataStream: '',- customRanges: [],- customDecorations: [],textRuns: [{st: 0,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 d0a4e5fd13..36617544af 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@@ -48,7 +48,7 @@ describe('test text-x utils', () => {}],};- const sliceBody = getBodySlice(body, 3, 8);+ const sliceBody = getBodySlice(body, 3, 8, false);expect(sliceBody).toEqual({dataStream: 'lo\nwo',textRuns: [@@ -71,8 +71,6 @@ describe('test text-x utils', () => {paragraphs: [{startIndex: 2,}],- customDecorations: [],- customRanges: [],} as IDocumentBody);});@@ -108,10 +106,8 @@ describe('test text-x utils', () => {}],};- const sliceBody = getBodySlice(body, 2, 8);+ const sliceBody = getBodySlice(body, 2, 8, false);expect(sliceBody).toEqual({- customDecorations: [],- customRanges: [],dataStream: 'llo\nwo',textRuns: [{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 40a40663f3..ad920e098f 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@@ -73,7 +73,7 @@ export class ActionIterator {return Tools.deepClone({...nextAction,len: length,- body: getBodySlice(nextAction.body!, offset, offset + length),+ body: getBodySlice(nextAction.body!, offset, offset + length, false),});}} else {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 cb7e8cba0e..bd1d605d0a 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@@ -437,13 +437,14 @@ export function mergeContinuousDecorations(ranges: ICustomDecoration[]): ICustom}// Push the last rangemergedRanges.push(currentRange);+return mergedRanges;}-export function splitCustomDecroatesByIndex(customDecorations: ICustomDecoration[], currentIndex: number) {- const matcheds = customDecorations.filter((c) => c.startIndex < currentIndex && c.endIndex >= currentIndex);+export function splitCustomDecoratesByIndex(customDecorations: ICustomDecoration[], currentIndex: number) {+ const matches = customDecorations.filter((c) => c.startIndex < currentIndex && c.endIndex >= currentIndex);- matcheds.forEach((matched) => {+ matches.forEach((matched) => {const index = customDecorations.indexOf(matched);customDecorations.splice(index, 1, {id: matched.id,@@ -552,7 +553,7 @@ export function insertCustomDecorations(}const { customDecorations } = body;- splitCustomDecroatesByIndex(customDecorations, currentIndex);+ splitCustomDecoratesByIndex(customDecorations, currentIndex);for (let i = 0, len = customDecorations.length; i < len; i++) {const customDecoration = customDecorations[i];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 9917ec44fb..a40a6fbc7d 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@@ -42,7 +42,7 @@ import {mergeContinuousDecorations,mergeContinuousRanges,normalizeTextRuns,- splitCustomDecroatesByIndex,+ splitCustomDecoratesByIndex,splitCustomRangesByIndex,} from './common';@@ -109,7 +109,9 @@ export function coverTextRuns(return updateDataTextRuns;}+ // eslint-disable-next-line no-param-reassignupdateDataTextRuns = Tools.deepClone(updateDataTextRuns);+ // eslint-disable-next-line no-param-reassignoriginTextRuns = Tools.deepClone(originTextRuns);const newUpdateTextRuns: ITextRun[] = [];@@ -439,7 +441,6 @@ function updateTables(}// retain-function updateCustomRanges(body: IDocumentBody,updateBody: IDocumentBody,@@ -453,11 +454,13 @@ function updateCustomRanges(splitCustomRangesByIndex(body.customRanges, currentIndex);splitCustomRangesByIndex(body.customRanges, currentIndex + textLength);+const start = currentIndex;const end = currentIndex + textLength - 1;const { customRanges: updateDataCustomRanges } = updateBody;const newCustomRanges: ICustomRange[] = [];const relativeCustomRanges = new Map<string, ICustomRange>();+body.customRanges.forEach((customRange) => {const { startIndex, endIndex } = customRange;if (startIndex >= start && endIndex <= end) {@@ -472,7 +475,8 @@ function updateCustomRanges(if (!updateDataCustomRanges) {return [];}- updateBody.customRanges?.forEach((customRange) => {++ updateDataCustomRanges.forEach((customRange) => {const { startIndex, endIndex } = customRange;newCustomRanges.push({...customRange,@@ -482,6 +486,7 @@ function updateCustomRanges(});body.customRanges = mergeContinuousRanges(newCustomRanges);+return removeCustomRanges;}@@ -497,8 +502,9 @@ function updateCustomDecorations(body.customDecorations = [];}- splitCustomDecroatesByIndex(body.customDecorations, currentIndex);- splitCustomDecroatesByIndex(body.customDecorations, currentIndex + textLength);+ splitCustomDecoratesByIndex(body.customDecorations, currentIndex);+ splitCustomDecoratesByIndex(body.customDecorations, currentIndex + textLength);+const removeCustomDecorations: ICustomDecoration[] = [];const { customDecorations } = body;const { customDecorations: updateDataCustomDecorations = [] } = updateBody;@@ -507,12 +513,13 @@ function updateCustomDecorations(for (let index = 0; index < customDecorations.length; index++) {const customDecoration = customDecorations[index];const { startIndex, endIndex } = customDecoration;+if (startIndex >= currentIndex && endIndex <= currentIndex + textLength - 1) {removeCustomDecorations.push(customDecoration);}}- updateDataCustomDecorations?.forEach((customDecoration) => {+ updateDataCustomDecorations.forEach((customDecoration) => {const { startIndex, endIndex } = customDecoration;customDecorations.push({...customDecoration,@@ -523,7 +530,8 @@ function updateCustomDecorations(} else {for (const updateCustomDecoration of updateDataCustomDecorations) {const { id } = updateCustomDecoration;- if (updateCustomDecoration.type === CustomDecorationType.DELTED) {++ if (updateCustomDecoration.type === CustomDecorationType.DELETED) {const oldCustomDecorations = customDecorations.filter((d) => d.id === id);if (oldCustomDecorations.length) {removeCustomDecorations.push(...oldCustomDecorations);@@ -547,5 +555,6 @@ function updateCustomDecorations(}body.customDecorations = mergeContinuousDecorations(customDecorations);+return removeCustomDecorations;}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 42ed6d716b..343218cfb7 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@@ -17,9 +17,9 @@/* eslint-disable no-param-reassign */import type { Nullable } from '../../../shared';-import type { ICustomRange, IDocumentBody, IParagraph, IParagraphStyle, ITextRun, ITextStyle } from '../../../types/interfaces';import type { IRetainAction } from './action-types';import { Tools, UpdateDocsAttributeType } from '../../../shared';+import { CustomDecorationType, type ICustomDecoration, type ICustomRange, type IDocumentBody, type IParagraph, type IParagraphStyle, type ITextRun, type ITextStyle } from '../../../types/interfaces';import { normalizeTextRuns } from './apply-utils/common';enum TextXTransformType {@@ -31,16 +31,20 @@ enum TextXTransformType {// eslint-disable-next-line max-lines-per-function, complexityfunction transformTextRuns(- originTextRuns: ITextRun[],- targetTextRuns: ITextRun[],+ originTextRuns: ITextRun[] | undefined,+ targetTextRuns: ITextRun[] | undefined,originCoverType: UpdateDocsAttributeType,targetCoverType: UpdateDocsAttributeType,transformType: TextXTransformType-) {- if (originTextRuns.length === 0 || targetTextRuns.length === 0) {+): ITextRun[] | undefined {+ if (originTextRuns == null || targetTextRuns == null) {return targetTextRuns;}+ if (originTextRuns.length === 0 || targetTextRuns.length === 0) {+ return [];+ }+targetTextRuns = Tools.deepClone(targetTextRuns);originTextRuns = Tools.deepClone(originTextRuns);@@ -70,13 +74,7 @@ function transformTextRuns(newTs = { ...targetStyle };if (originCoverType === UpdateDocsAttributeType.COVER && targetCoverType === UpdateDocsAttributeType.REPLACE && originStyle) {- const keys = Object.keys(originStyle);-- for (const key of keys) {- if (newTs[key as keyof ITextStyle] === undefined) {- newTs[key as keyof ITextStyle] = originStyle[key as keyof ITextStyle] as any;- }- }+ newTs = Object.assign({}, originStyle, newTs);}} else {newTs = { ...targetStyle };@@ -199,21 +197,57 @@ function transformTextRuns(}function transformCustomRanges(- originCustomRanges: ICustomRange[],- targetCustomRanges: ICustomRange[],- transformType: TextXTransformType,- coverType: UpdateDocsAttributeType = UpdateDocsAttributeType.COVER-): ICustomRange[] {- if (originCustomRanges.length === 0) {- return Tools.deepClone(targetCustomRanges);+ originCustomRanges: ICustomRange[] | undefined,+ targetCustomRanges: ICustomRange[] | undefined,+ originCoverType: UpdateDocsAttributeType,+ targetCoverType: UpdateDocsAttributeType,+ transformType: TextXTransformType+): ICustomRange[] | undefined {+ if (originCustomRanges == null || targetCustomRanges == null) {+ return targetCustomRanges;+ }++ if (originCustomRanges.length === 0 || targetCustomRanges.length === 0) {+ return [];+ }++ if (originCustomRanges.length > 1 || targetCustomRanges.length > 1) {+ throw new Error('CustomRanges is only supported transform for length one now.');}- if (coverType === UpdateDocsAttributeType.REPLACE) {+ const originCustomRange = originCustomRanges[0];+ const targetCustomRange = targetCustomRanges[0];++ if (originCoverType === UpdateDocsAttributeType.REPLACE) {return transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED- ? Tools.deepClone(originCustomRanges)- : Tools.deepClone(targetCustomRanges);+ ? [Tools.deepClone(originCustomRange)]+ : [Tools.deepClone(targetCustomRange)];} else {- throw new Error('CustomRanges is only supported in replace mode.');+ if (targetCoverType === UpdateDocsAttributeType.REPLACE) {+ const customRange: ICustomRange = Tools.deepClone(targetCustomRange);+ if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {+ Object.assign(customRange, Tools.deepClone(originCustomRange));+ }++ return [customRange];+ } else {+ const customRange: ICustomRange = Tools.deepClone(targetCustomRange);+ if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {+ Object.assign(customRange, Tools.deepClone(originCustomRange));++ // Because customRange behavior like REPLACE.+ // const keys = Object.keys(originCustomRange);++ // for (const key of keys) {+ // // Should not delete `startIndex` and `endIndex`.+ // if (customRange[key as keyof ICustomRange] !== undefined) {+ // delete customRange[key as keyof ICustomRange];+ // }+ // }+ }++ return [customRange];+ }}}@@ -320,6 +354,46 @@ function transformParagraph(return paragraph;}+function transformCustomDecorations(+ originCustomDecorations: ICustomDecoration[] | undefined,+ targetCustomDecorations: ICustomDecoration[] | undefined+) {+ if (originCustomDecorations == null || targetCustomDecorations == null) {+ return targetCustomDecorations;+ }++ if (originCustomDecorations.length === 0 || targetCustomDecorations.length === 0) {+ return Tools.deepClone(targetCustomDecorations);+ }++ const customDecorations: ICustomDecoration[] = [];++ for (const decoration of targetCustomDecorations) {+ const { id, type } = decoration;++ let pushed = false;++ for (const originDecoration of originCustomDecorations) {+ if (originDecoration.id === id) {+ if (originDecoration.type === CustomDecorationType.DELETED || type === CustomDecorationType.DELETED) {+ pushed = true;+ customDecorations.push({+ ...decoration,+ type: CustomDecorationType.DELETED,+ });+ }+ break;+ }+ }++ if (!pushed) {+ customDecorations.push(decoration);+ }+ }++ return customDecorations;+}+interface ITransformBodyResult {body: IDocumentBody;coverType: UpdateDocsAttributeType;@@ -344,30 +418,53 @@ export function transformBody(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, thisCoverType, otherCoverType, TextXTransformType.COVER_ONLY_NOT_EXISTED);- } else {- textRuns = transformTextRuns(thisTextRuns, otherTextRuns, thisCoverType, otherCoverType, TextXTransformType.COVER);- }- if (textRuns.length) {+ const {+ textRuns: thisTextRuns,+ paragraphs: thisParagraphs = [],+ customRanges: thisCustomRanges,+ customDecorations: thisCustomDecorations,+ } = thisBody;+ const {+ textRuns: otherTextRuns,+ paragraphs: otherParagraphs = [],+ customRanges: otherCustomRanges,+ customDecorations: otherCustomDecorations,+ } = otherBody;++ const textRuns = transformTextRuns(+ thisTextRuns,+ otherTextRuns,+ thisCoverType,+ otherCoverType,+ priority ? TextXTransformType.COVER_ONLY_NOT_EXISTED : TextXTransformType.COVER+ );++ if (textRuns) {retBody.textRuns = textRuns;}- let customRanges: ICustomRange[] = [];- if (priority) {- customRanges = transformCustomRanges(thisCustomRanges, otherCustomRanges, TextXTransformType.COVER_ONLY_NOT_EXISTED, thisCoverType);- } else {- customRanges = transformCustomRanges(thisCustomRanges, otherCustomRanges, TextXTransformType.COVER, thisCoverType);- }+ const customRanges = transformCustomRanges(+ thisCustomRanges,+ otherCustomRanges,+ thisCoverType,+ otherCoverType,+ priority ? TextXTransformType.COVER_ONLY_NOT_EXISTED : TextXTransformType.COVER+ );- if (customRanges.length) {+ if (customRanges) {retBody.customRanges = customRanges;}+ // Transform custom decorations.+ const customDecorations = transformCustomDecorations(+ thisCustomDecorations,+ otherCustomDecorations+ );++ if (customDecorations) {+ retBody.customDecorations = customDecorations;+ }+const paragraphs: IParagraph[] = [];let thisIndex = 0;diff --git a/packages/core/src/docs/data-model/text-x/utils.ts b/packages/core/src/docs/data-model/text-x/utils.tsindex 594c079562..b64d6c21e2 100644--- a/packages/core/src/docs/data-model/text-x/utils.ts+++ b/packages/core/src/docs/data-model/text-x/utils.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { ICustomBlock, ICustomDecoration, IDocumentBody, IParagraph, ISectionBreak, ITextRun } from '../../../types/interfaces/i-document-data';+import type { ICustomBlock, ICustomDecoration, ICustomRange, IDocumentBody, IParagraph, ISectionBreak, ITextRun } from '../../../types/interfaces/i-document-data';import type { IRetainAction } from './action-types';import { UpdateDocsAttributeType } from '../../../shared/command-enum';import { Tools } from '../../../shared/tools';@@ -25,47 +25,46 @@ export enum SliceBodyType {cut,}-// TODO: Support other properties like custom ranges, tables, etc.// eslint-disable-next-line max-lines-per-function, complexityexport function getBodySlice(body: IDocumentBody,startOffset: number,endOffset: number,- returnEmptyTextRun = false,+ returnEmptyArray = true,type = SliceBodyType.cut): IDocumentBody {- const { dataStream, textRuns = [], paragraphs = [], customBlocks = [], tables = [], sectionBreaks = [] } = body;+ const { dataStream, textRuns, paragraphs = [], customBlocks = [], tables = [], sectionBreaks = [] } = body;const docBody: IDocumentBody = {dataStream: dataStream.slice(startOffset, endOffset),};- const newTextRuns: ITextRun[] = [];-- for (const textRun of textRuns) {- const clonedTextRun = Tools.deepClone(textRun);- const { st, ed } = clonedTextRun;-- if (Tools.hasIntersectionBetweenTwoRanges(st, ed, startOffset, endOffset)) {- if (startOffset >= st && startOffset <= ed) {- newTextRuns.push({- ...clonedTextRun,- st: startOffset,- ed: Math.min(endOffset, ed),- });- } else if (endOffset >= st && endOffset <= ed) {- newTextRuns.push({- ...clonedTextRun,- st: Math.max(startOffset, st),- ed: endOffset,- });- } else {- newTextRuns.push(clonedTextRun);+ if (textRuns) {+ const newTextRuns: ITextRun[] = [];++ for (const textRun of textRuns) {+ const clonedTextRun = Tools.deepClone(textRun);+ const { st, ed } = clonedTextRun;++ if (Tools.hasIntersectionBetweenTwoRanges(st, ed, startOffset, endOffset)) {+ if (startOffset >= st && startOffset <= ed) {+ newTextRuns.push({+ ...clonedTextRun,+ st: startOffset,+ ed: Math.min(endOffset, ed),+ });+ } else if (endOffset >= st && endOffset <= ed) {+ newTextRuns.push({+ ...clonedTextRun,+ st: Math.max(startOffset, st),+ ed: endOffset,+ });+ } else {+ newTextRuns.push(clonedTextRun);+ }}}- }- if (newTextRuns.length) {docBody.textRuns = newTextRuns.map((tr) => {const { st, ed } = tr;return {@@ -74,7 +73,7 @@ export function getBodySlice(ed: ed - startOffset,};});- } else if (returnEmptyTextRun) {+ } else if (returnEmptyArray) {// In the case of no style before, add the style, removeTextRuns will be empty,// in this case, you need to add an empty textRun for undo.docBody.textRuns = [{@@ -136,10 +135,19 @@ export function getBodySlice(}if (type === SliceBodyType.cut) {- docBody.customDecorations = getCustomDecorationSlice(body, startOffset, endOffset);+ const customDecorations = getCustomDecorationSlice(body, startOffset, endOffset);+ if (customDecorations) {+ docBody.customDecorations = customDecorations;+ } else if (returnEmptyArray) {+ docBody.customDecorations = [];+ }}const { customRanges } = getCustomRangeSlice(body, startOffset, endOffset);- docBody.customRanges = customRanges;+ if (customRanges) {+ docBody.customRanges = customRanges;+ } else if (returnEmptyArray) {+ docBody.customRanges = [];+ }const newCustomBlocks: ICustomBlock[] = [];@@ -223,7 +231,11 @@ export function normalizeBody(body: IDocumentBody): IDocumentBody {}export function getCustomRangeSlice(body: IDocumentBody, startOffset: number, endOffset: number) {- const { customRanges = [] } = body;+ if (body.customRanges == null) {+ return {};+ }++ const { customRanges } = body;const leftOffset = 0;const rightOffset = 0;const relativeCustomRanges = customRanges@@ -246,8 +258,11 @@ export function getCustomRangeSlice(body: IDocumentBody, startOffset: number, en}export function getCustomDecorationSlice(body: IDocumentBody, startOffset: number, endOffset: number) {- const { customDecorations = [] } = body;+ if (body.customDecorations == null) {+ return;+ }+ const { customDecorations = [] } = body;const customDecorationSlice: ICustomDecoration[] = [];customDecorations.forEach((range) => {// 34 35@@ -265,6 +280,70 @@ export function getCustomDecorationSlice(body: IDocumentBody, startOffset: numbereturn customDecorationSlice;}+function composeTextRuns(+ updateDataTextRuns: ITextRun[] | undefined,+ originTextRuns: ITextRun[] | undefined,+ coverType: UpdateDocsAttributeType+): ITextRun[] | undefined {+ if (updateDataTextRuns == null || originTextRuns == null) {+ return updateDataTextRuns ?? originTextRuns;+ }++ return coverTextRuns(updateDataTextRuns, originTextRuns, coverType);+}++function composeCustomRanges(+ updateDataCustomRanges: ICustomRange[] | undefined,+ originCustomRanges: ICustomRange[] | undefined,+ coverType: UpdateDocsAttributeType+): ICustomRange[] | undefined {+ if (updateDataCustomRanges == null || originCustomRanges == null) {+ return updateDataCustomRanges ?? originCustomRanges;+ }++ if (originCustomRanges.length === 0 || updateDataCustomRanges.length === 0) {+ return updateDataCustomRanges;+ }++ if (originCustomRanges.length > 1 || updateDataCustomRanges.length > 1) {+ throw new Error('Cannot cover multiple customRanges');+ }++ if (coverType === UpdateDocsAttributeType.REPLACE) {+ return [{+ ...updateDataCustomRanges[0],+ }];+ } else {+ return [{+ ...originCustomRanges[0],+ ...updateDataCustomRanges[0],+ }];+ }+}++function composeCustomDecorations(+ updateDataCustomDecorations: ICustomDecoration[],+ originCustomDecorations: ICustomDecoration[],+ coverType: UpdateDocsAttributeType+) {+ if (originCustomDecorations.length === 0 || updateDataCustomDecorations.length === 0) {+ return updateDataCustomDecorations;+ }++ if (coverType === UpdateDocsAttributeType.REPLACE) {+ return updateDataCustomDecorations;+ } else {+ return [+ ...updateDataCustomDecorations,+ ...originCustomDecorations.filter((originCustomDecoration) => {+ return !updateDataCustomDecorations.some((updateDataCustomDecoration) => {+ return originCustomDecoration.id === updateDataCustomDecoration.id;+ });+ }),+ ];+ }+}+export function composeBody(thisBody: IDocumentBody,otherBody: IDocumentBody,@@ -278,12 +357,26 @@ export function composeBody(dataStream: thisBody.dataStream,};- const { textRuns: thisTextRuns = [], paragraphs: thisParagraphs = [] } = thisBody;- const { textRuns: otherTextRuns = [], paragraphs: otherParagraphs = [] } = otherBody;-- const textRuns = coverTextRuns(otherTextRuns, thisTextRuns, coverType);- if (textRuns.length) {- retBody.textRuns = textRuns;+ const {+ textRuns: thisTextRuns,+ paragraphs: thisParagraphs = [],+ customRanges: thisCustomRanges,+ customDecorations: thisCustomDecorations = [],+ } = thisBody;+ const {+ textRuns: otherTextRuns,+ paragraphs: otherParagraphs = [],+ customRanges: otherCustomRanges,+ customDecorations: otherCustomDecorations = [],+ } = otherBody;++ retBody.textRuns = composeTextRuns(otherTextRuns, thisTextRuns, coverType);++ retBody.customRanges = composeCustomRanges(otherCustomRanges, thisCustomRanges, coverType);++ const customDecorations = composeCustomDecorations(otherCustomDecorations, thisCustomDecorations, coverType);+ if (customDecorations.length) {+ retBody.customDecorations = customDecorations;}const paragraphs: IParagraph[] = [];diff --git a/packages/core/src/types/interfaces/i-document-data.ts b/packages/core/src/types/interfaces/i-document-data.tsindex 77aa4d53ad..d15d5d5903 100644--- a/packages/core/src/types/interfaces/i-document-data.ts+++ b/packages/core/src/types/interfaces/i-document-data.ts@@ -385,8 +385,7 @@ export interface ICustomBlock {export enum CustomDecorationType {COMMENT,-- DELTED = 9999,+ DELETED = 9999,}export interface ICustomDecoration {diff --git a/packages/docs-ui/src/basics/custom-decoration-factory.ts b/packages/docs-ui/src/basics/custom-decoration-factory.tsindex 39c4e9a879..bd6fb66458 100644--- a/packages/docs-ui/src/basics/custom-decoration-factory.ts+++ b/packages/docs-ui/src/basics/custom-decoration-factory.ts@@ -164,7 +164,7 @@ export function deleteCustomDecorationFactory(accessor: IAccessor, params: IDelestartIndex: 0,endIndex: len - 1,id,- type: CustomDecorationType.DELTED,+ type: CustomDecorationType.DELETED,},],},diff --git a/packages/docs-ui/src/commands/commands/doc-delete.command.ts b/packages/docs-ui/src/commands/commands/doc-delete.command.tsindex cae5540889..8528ce837a 100644--- a/packages/docs-ui/src/commands/commands/doc-delete.command.ts+++ b/packages/docs-ui/src/commands/commands/doc-delete.command.ts@@ -14,15 +14,12 @@* limitations under the License.*/-import type { DocumentDataModel, IAccessor, ICommand, ICustomBlock, IDocumentBody, IMutationInfo, IParagraph, ITextRange, ITextRun, JSONXActions, Nullable } from '@univerjs/core';+import type { DocumentDataModel, ICommand, IMutationInfo, IParagraph, ITextRange, JSONXActions, Nullable } from '@univerjs/core';import type { IRichTextEditingMutationParams } from '@univerjs/docs';import type { IRectRangeWithStyle, ITextRangeWithStyle } from '@univerjs/engine-render';import {CommandType,DataStreamTreeTokenType,- generateRandomId,- getCustomDecorationSlice,- getCustomRangeSlice,ICommandService,IUniverInstanceService,JSONX,@@ -668,76 +665,6 @@ export const DeleteRightCommand: ICommand = {},};-function getParagraphBody(- accessor: IAccessor,- unitId: string,- body: IDocumentBody,- start: number,- end: number-): IDocumentBody {- const { textRuns: originTextRuns = [], customBlocks: originCustomBlocks = [] } = body;- const dataStream = body.dataStream.substring(start, end);-- const bodySlice: IDocumentBody = {- dataStream,- customRanges: getCustomRangeSlice(body, start, end).customRanges.map((range) => ({- ...Tools.deepClone(range),- rangeId: generateRandomId(),- })),- customDecorations: getCustomDecorationSlice(body, start, end),- };-- const textRuns: ITextRun[] = [];-- for (const textRun of originTextRuns) {- const { st, ed } = textRun;- if (ed <= start || st >= end) {- continue;- }-- if (st < start) {- textRuns.push({- ...textRun,- st: 0,- ed: ed - start,- });- } else if (ed > end) {- textRuns.push({- ...textRun,- st: st - start,- ed: end - start,- });- } else {- textRuns.push({- ...textRun,- st: st - start,- ed: ed - start,- });- }- }-- if (textRuns.length > 0) {- bodySlice.textRuns = textRuns;- }-- const customBlocks: ICustomBlock[] = [];- for (const block of originCustomBlocks) {- const { startIndex } = block;- if (startIndex >= start && startIndex <= end) {- customBlocks.push({- ...block,- startIndex: startIndex - start,- });- }- }-- if (customBlocks.length > 0) {- bodySlice.customBlocks = customBlocks;- }-- return bodySlice;-}-// get cursor position when BACKSPACE/DELETE excuse the CutContentCommand.function getTextRangesWhenDelete(activeRange: ITextRangeWithStyle, ranges: readonly ITextRange[]) {let cursor = activeRange.endOffset;