From 3f78902157a44bd4e6540439de0f77c77da2be37 Mon Sep 17 00:00:00 2001 From: Ran Luo Date: Sat, 16 Nov 2024 10:43:36 +0800 Subject: [PATCH] fix(docs): invert action issue (#4083) --- .../__tests__/apply-consistency.spec.ts | 63 +++++++++++++++++++ .../data-model/text-x/apply-utils/common.ts | 14 ----- .../text-x/apply-utils/delete-apply.ts | 21 ------- .../core/src/docs/data-model/text-x/text-x.ts | 2 +- 4 files changed, 64 insertions(+), 36 deletions(-) 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 index f136d15c709..df821064b97 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.ts index 67efd46bd6c..20491124425 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, currentI const 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.ts index 0b05f262781..868ef20db94 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.ts index e9f4afc8aac..67454f549c0 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;