Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(docs): invert action issue (#4083)
Ran Luo3f78902157a4
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.tsindex f136d15c70..df821064b9 100644--- 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@@ -41,12 +41,75 @@ function getDefaultDocWithCustomRange() {startIndex: 9,},],+ customDecorations: [],};return doc;}describe('apply consistency', () => {+ it('should get the same result when compose delete action with body', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.DELETE,+ len: 2,+ },+ ];+ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 4,+ },+ {+ t: TextXActionType.DELETE,+ len: 1,+ body: {+ dataStream: '\r',+ paragraphs: [{+ startIndex: 0,+ }],+ customRanges: [],+ },+ },+ {+ t: TextXActionType.DELETE,+ len: 1,+ },+ {+ t: TextXActionType.DELETE,+ len: 1,+ },+ ];++ 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);++ const undoActions = TextX.invert(TextX.makeInvertible(composedAction1, getDefaultDocWithCustomRange()));+ const undoDoc = TextX.apply(resultA, undoActions);++ // console.log(JSON.stringify(composedAction1, null, 2));+ // console.log(JSON.stringify(undoActions, null, 2));+ // console.log(JSON.stringify(undoDoc, null, 2));++ expect(undoDoc).toEqual(getDefaultDocWithCustomRange());+ });+it('should get the same result when apply delete and cancel link', () => {const actionsA: TextXAction[] = [{diff --git a/packages/core/src/docs/data-model/text-x/apply-utils/common.ts b/packages/core/src/docs/data-model/text-x/apply-utils/common.tsindex 67efd46bd6..2049112442 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@@ -588,20 +588,6 @@ export function deleteTextRuns(body: IDocumentBody, textLength: number, currentIconst endIndex = currentIndex + textLength;const removeTextRuns: ITextRun[] = [];- // Handles special case where repeated set inline format style by cursor.- // if (startIndex === endIndex && textRuns?.find((t) => t.st === currentIndex && t.ed === currentIndex)) {- // const textRun = textRuns.find((t) => t.st === currentIndex && t.ed === currentIndex)!;- // removeTextRuns.push({- // ...textRun,- // st: textRun.st - currentIndex,- // ed: textRun.ed - currentIndex,- // });-- // body.textRuns = body.textRuns?.filter((t) => t !== textRun);-- // return removeTextRuns;- // }-if (textRuns) {const newTextRuns = [];diff --git a/packages/core/src/docs/data-model/text-x/apply-utils/delete-apply.ts b/packages/core/src/docs/data-model/text-x/apply-utils/delete-apply.tsindex 0b05f26278..868ef20db9 100644--- a/packages/core/src/docs/data-model/text-x/apply-utils/delete-apply.ts+++ b/packages/core/src/docs/data-model/text-x/apply-utils/delete-apply.ts@@ -30,7 +30,6 @@ export function updateAttributeByDelete(body: IDocumentBody, textLength: number,const { dataStream } = body;const startIndex = currentIndex;-const endIndex = currentIndex + textLength;const removeTextRuns = deleteTextRuns(body, textLength, currentIndex);@@ -64,23 +63,3 @@ export function updateAttributeByDelete(body: IDocumentBody, textLength: number,customDecorations: removeCustomDecorations,};}--// export function recoveryBody(bodyModel: DocumentViewModel, body: IDocumentBody, deleBody: IDocumentBody) {-// if (bodyModel.children[0].children.length === 0) {-// bodyModel.reset({-// dataStream: DEFAULT_EMPTY_DOCUMENT_VALUE,-// });-// }--// if (body.dataStream === '\n') {-// body.dataStream = DEFAULT_EMPTY_DOCUMENT_VALUE;--// const firstParagraph = deleBody.paragraphs?.[0];--// if (firstParagraph != null) {-// const newParagraph = Tools.deepClone(firstParagraph) as IParagraph;-// newParagraph.startIndex = 0;-// body.paragraphs = [newParagraph];-// }-// }-// }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 e9f4afc8aa..67454f549c 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@@ -262,7 +262,7 @@ export class TextX {let index = 0;for (const action of actions) {- if (action.t === TextXActionType.DELETE && action.body == null) {+ if (action.t === TextXActionType.DELETE && (action.body == null || (action.body && action.body.dataStream.length !== action.len))) {const body = getBodySlice(doc, index, index + action.len, false);action.len = body.dataStream.length;action.body = body;