Skip to content

Commit 2647a7e

Browse files
committed
imp(UndoRedoManager): stacks transformation logic improved
1 parent a8c2de8 commit 2647a7e

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

packages/collaboration-manager/src/OperationsTransformer.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,12 @@ export class OperationsTransformer {
120120
* Cover case 2
121121
*/
122122
case OperationType.Delete:
123-
if (operation.index.blockIndex !== undefined) {
124-
if (againstOp.index.blockIndex! < operation.index.blockIndex!) {
125-
newIndexBuilder.addBlockIndex(operation.index.blockIndex! - 1);
126-
} else {
127-
return new Operation(OperationType.Neutral, newIndexBuilder.build(), { payload: [] }, operation.userId, operation.rev);
128-
}
123+
if (againstOp.index.blockIndex! >= operation.index.blockIndex!) {
124+
return new Operation(OperationType.Neutral, newIndexBuilder.build(), { payload: [] }, operation.userId, operation.rev);
129125
}
130126

127+
newIndexBuilder.addBlockIndex(operation.index.blockIndex! - 1);
128+
131129
break;
132130

133131
/**
@@ -301,9 +299,7 @@ export class OperationsTransformer {
301299
* Cover case 2.2
302300
*/
303301
case (RangeIntersectionType.Right):
304-
const overlapLength = index.textRange![1] - againstIndex.textRange![0];
305-
306-
newIndexBuilder.addTextRange([index.textRange![0], index.textRange![1] - overlapLength]);
302+
newIndexBuilder.addTextRange([index.textRange![0], againstIndex.textRange![0]]);
307303
break;
308304

309305
/**

packages/collaboration-manager/src/UndoRedoManager.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ export class UndoRedoManager {
1414
*/
1515
#redoStack: Operation[] = [];
1616

17+
/**
18+
* Transforms passed operations stack against the operation
19+
*
20+
* @param operation - operation to transform against
21+
* @param stack - stack to transform
22+
* @returns - new transformed list of operations
23+
*/
24+
private transformStack(operation: Operation, stack: Operation[]): Operation[] {
25+
return stack.map((op: Operation) => op.transform(operation));
26+
}
27+
1728
/**
1829
* Returns operation to undo (if any)
1930
*/
@@ -65,28 +76,7 @@ export class UndoRedoManager {
6576
* @param operation - operation to transform against
6677
*/
6778
public transformStacks(operation: Operation): void {
68-
this.transformStack(operation, this.#undoStack);
69-
this.transformStack(operation, this.#redoStack);
70-
}
71-
72-
/**
73-
* Transforms passed operations stack against the operation
74-
*
75-
* @param operation - operation to transform against
76-
* @param stack - stack to transform
77-
*/
78-
public transformStack(operation: Operation, stack: Operation[]): void {
79-
const transformed = stack.flatMap((op) => {
80-
const transformedOp = op.transform(operation);
81-
82-
if (transformedOp === null) {
83-
return [];
84-
}
85-
86-
return [ transformedOp ];
87-
});
88-
89-
stack.length = 0;
90-
stack.push(...transformed);
79+
this.#undoStack = this.transformStack(operation, this.#undoStack);
80+
this.#redoStack = this.transformStack(operation, this.#redoStack);
9181
}
9282
}

0 commit comments

Comments
 (0)