Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: delete apply (#4062)

Ran Luo820653c34158
diff --git a/packages/core/src/docs/data-model/text-x/__tests__/apply-consistency.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/apply-consistency.spec.ts
new file mode 100644
index 0000000000..f136d15c70
--- /dev/null
+++ b/packages/core/src/docs/data-model/text-x/__tests__/apply-consistency.spec.ts
@@ -0,0 +1,91 @@
+/**
+ * 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 { CustomRangeType, type IDocumentBody } from '../../../../types/interfaces';
+import { TextXActionType } from '../action-types';
+import { TextX } from '../text-x';
+
+function getDefaultDocWithCustomRange() {
+ const doc: IDocumentBody = {
+ dataStream: '人之初,\r性本善。\r\n',
+ textRuns: [],
+ customRanges: [{
+ startIndex: 1,
+ endIndex: 3,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }],
+ paragraphs: [
+ {
+ startIndex: 4,
+ },
+ {
+ startIndex: 9,
+ },
+ ],
+ };
+
+ return doc;
+}
+
+describe('apply consistency', () => {
+ it('should get the same result when apply delete and cancel link', () => {
+ const actionsA: TextXAction[] = [
+ {
+ t: TextXActionType.DELETE,
+ len: 2,
+ },
+ ];
+ const actionsB: TextXAction[] = [
+ {
+ t: TextXActionType.RETAIN,
+ len: 1,
+ },
+ {
+ t: TextXActionType.RETAIN,
+ len: 3,
+ body: {
+ dataStream: '',
+ 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, '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);
+
+ 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__/delete-apply.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/delete-apply.spec.ts
new file mode 100644
index 0000000000..53361597ff
--- /dev/null
+++ b/packages/core/src/docs/data-model/text-x/__tests__/delete-apply.spec.ts
@@ -0,0 +1,131 @@
+/**
+ * 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 { describe, expect, it } from 'vitest';
+import { CustomRangeType, type IDocumentBody } from '../../../../types/interfaces';
+import { deleteCustomRanges } from '../apply-utils/common';
+
+function getBodyWithCustomRanges() {
+ const body: IDocumentBody = {
+ dataStream: '人之初\r\n',
+ customRanges: [{
+ startIndex: 1,
+ endIndex: 2,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }],
+ };
+
+ return body;
+}
+
+describe('delete apply consistency', () => {
+ it('should pass the test case when delete start part custom range', () => {
+ const body = getBodyWithCustomRanges();
+
+ deleteCustomRanges(body, 2, 0);
+
+ expect(body.customRanges).toEqual([{
+ startIndex: 0,
+ endIndex: 0,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }]);
+ });
+
+ it('should pass the test case when delete end part custom range', () => {
+ const body = getBodyWithCustomRanges();
+ deleteCustomRanges(body, 2, 2);
+ expect(body.customRanges).toEqual([{
+ startIndex: 1,
+ endIndex: 1,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }]);
+ });
+
+ it('should pass the test case when delete all custom range', () => {
+ const body = getBodyWithCustomRanges();
+ deleteCustomRanges(body, 2, 1);
+ expect(body.customRanges).toEqual([]);
+ });
+
+ it('should pass the test case when delete middle part custom range', () => {
+ const body: IDocumentBody = {
+ dataStream: '人之初,性本善\r\n',
+ customRanges: [{
+ startIndex: 1,
+ endIndex: 6,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }],
+ };
+ deleteCustomRanges(body, 3, 2);
+ expect(body.customRanges).toEqual([{
+ startIndex: 1,
+ endIndex: 3,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }]);
+ });
+
+ it('should pass the test case when delete before custom range', () => {
+ const body = getBodyWithCustomRanges();
+
+ deleteCustomRanges(body, 1, 0);
+
+ expect(body.customRanges).toEqual([{
+ startIndex: 0,
+ endIndex: 1,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }]);
+ });
+
+ it('should pass the test case when delete after custom range', () => {
+ const body = getBodyWithCustomRanges();
+
+ deleteCustomRanges(body, 1, 3);
+
+ expect(body.customRanges).toEqual([{
+ startIndex: 1,
+ endIndex: 2,
+ rangeId: 'rangeId',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: {
+ url: 'http://www.baidu.com',
+ },
+ }]);
+ });
+});
diff --git a/packages/core/src/docs/data-model/text-x/apply-utils/common.ts b/packages/core/src/docs/data-model/text-x/apply-utils/common.ts
index a22522c45d..67efd46bd6 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
@@ -824,6 +824,10 @@ export function deleteTables(body: IDocumentBody, textLength: number, currentInd
} else if (st <= startIndex && ed >= endIndex) {
const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
+ if (segments.length === 0) {
+ continue;
+ }
+
table.startIndex = segments[0];
table.endIndex = segments[1];
@@ -848,6 +852,7 @@ export function deleteCustomRanges(body: IDocumentBody, textLength: number, curr
const startIndex = currentIndex;
const endIndex = currentIndex + textLength - 1;
+ // TODO: @JOCS, removeCustomRanges is not used, should we remove it?
const removeCustomRanges: ICustomRange[] = [];
if (customRanges) {
@@ -861,6 +866,11 @@ export function deleteCustomRanges(body: IDocumentBody, textLength: number, curr
continue;
} else if (Math.max(startIndex, st) <= Math.min(endIndex, ed)) {
const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
+
+ if (segments.length === 0) {
+ continue;
+ }
+
customRange.startIndex = segments[0];
customRange.endIndex = segments[1];
} else if (endIndex < st) {
@@ -872,6 +882,7 @@ export function deleteCustomRanges(body: IDocumentBody, textLength: number, curr
body.customRanges = mergeContinuousRanges(newCustomRanges);
}
+
return removeCustomRanges;
}
@@ -894,6 +905,11 @@ export function deleteCustomDecorations(body: IDocumentBody, textLength: number,
// substr decoration
} else if (Math.max(startIndex, st) <= Math.min(endIndex, ed)) {
const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
+
+ if (segments.length === 0) {
+ continue;
+ }
+
customDecoration.startIndex = segments[0];
customDecoration.endIndex = segments[1];
} else if (endIndex < st) {
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.ts
index a40a6fbc7d..d6e2de4b90 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
@@ -446,7 +446,7 @@ function updateCustomRanges(
updateBody: IDocumentBody,
textLength: number,
currentIndex: number,
- coverType: UpdateDocsAttributeType
+ _coverType: UpdateDocsAttributeType
) {
if (!body.customRanges) {
body.customRanges = [];
diff --git a/packages/core/src/shared/__tests__/doc-tool.spec.ts b/packages/core/src/shared/__tests__/doc-tool.spec.ts
new file mode 100644
index 0000000000..0f44861341
--- /dev/null
+++ b/packages/core/src/shared/__tests__/doc-tool.spec.ts
@@ -0,0 +1,29 @@
+/**
+ * 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 { describe, expect, it } from 'vitest';
+import { horizontalLineSegmentsSubtraction } from '../doc-tool';
+
+describe('test cases in doc-tool', () => {
+ it('should pass test case when call horizontalLineSegmentsSubtraction', () => {
+ expect(horizontalLineSegmentsSubtraction(1, 2, 3, 4)).toEqual([1, 2]);
+ expect(horizontalLineSegmentsSubtraction(1, 3, 2, 4)).toEqual([1, 1]);
+ expect(horizontalLineSegmentsSubtraction(1, 2, 1, 3)).toEqual([]);
+ expect(horizontalLineSegmentsSubtraction(1, 2, 1, 2)).toEqual([]);
+ expect(horizontalLineSegmentsSubtraction(1, 2, 1, 1)).toEqual([1, 1]);
+ expect(horizontalLineSegmentsSubtraction(3, 5, 2, 3)).toEqual([2, 3]);
+ });
+});
diff --git a/packages/core/src/shared/doc-tool.ts b/packages/core/src/shared/doc-tool.ts
index 05d5b0a56e..3dc2fb3acc 100644
--- a/packages/core/src/shared/doc-tool.ts
+++ b/packages/core/src/shared/doc-tool.ts
@@ -16,42 +16,42 @@
import type { IParagraph, IParagraphStyle } from '../types/interfaces/i-document-data';
-export function horizontalLineSegmentsSubtraction(A1: number, A2: number, B1: number, B2: number) {
+export function horizontalLineSegmentsSubtraction(aStart: number, aEnd: number, bStart: number, bEnd: number) {
// 确保A1 < A2, B1 < B2
- if (A1 > A2) {
- [A1, A2] = [A2, A1];
+ if (aStart > aEnd) {
+ throw new Error('a1 should be less than a2');
}
- if (B1 > B2) {
- [B1, B2] = [B2, B1];
+ if (bStart > bEnd) {
+ throw new Error('b1 should be less than b2');
}
- if (A2 < B1 || B2 < A1) {
- return [A1, A2]; // 无重叠,返回原线段A
+ if (aEnd < bStart || bEnd < aStart) {
+ return [aStart, aEnd]; // 无重叠,返回原线段A
}
- if (B1 < A1) {
- B1 = A1;
+ // 如果 b 包含 a,则结果为空区间
+ if (bStart <= aStart && bEnd >= aEnd) {
+ return [];
}
- if (B2 > A2) {
- B2 = A2;
+ const subLength = bEnd - bStart + 1;
+
+ // 如果 a 包含 b,需要返回两个剩余的区间
+ if (aStart < bStart && aEnd > bEnd) {
+ return [aStart, aEnd - subLength];
+ }
+
+ // 如果 b 覆盖了 a 的开始部分
+ if (bStart <= aStart && bEnd < aEnd) {
+ return [bEnd + 1 - subLength, aEnd - subLength];
}
- const subLength = B2 - B1 + 1;
- let result: number[] = [];
-
- if (A1 === B1) {
- // Subtract the start segment
- result = [B2 + 1 - subLength, A2 - subLength];
- } else if (A2 === B2) {
- // Subtract the end segment
- result = [A1, B1 - 1];
- } else {
- // Subtract the middle segment
- result = [A1, A2 - subLength];
+ // 如果 b 覆盖了 a 的结束部分
+ if (bStart > aStart && bEnd >= aEnd) {
+ return [aStart, bStart - 1];
}
- return result;
+ return [aStart, aEnd];
}
export function checkParagraphHasBullet(paragraph: IParagraph) {