Skip to content

Commit

Permalink
fix(docs): invert action issue (#4083)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs authored Nov 16, 2024
1 parent 629db60 commit 3f78902
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
{
Expand Down
14 changes: 0 additions & 14 deletions packages/core/src/docs/data-model/text-x/apply-utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
// }
// }
// }
2 changes: 1 addition & 1 deletion packages/core/src/docs/data-model/text-x/text-x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3f78902

Please sign in to comment.