forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathoperation-meta.ts
65 lines (57 loc) · 1.89 KB
/
operation-meta.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ICursor } from "./cursor";
import { ITextOperation } from "./text-operation";
export interface IOperationMeta {
/**
* Return shallow clone of Operation Metadata
*/
clone(): IOperationMeta;
/**
* Return updated Operation Metadata on Inversion of Operation
*/
invert(): IOperationMeta;
/**
* Return updated Operation Metadata on Composition of Operation
* @param other - Operation Metadata from other operation
*/
compose(other: IOperationMeta): IOperationMeta;
/**
* Return updated Operation Metadata on Transformation of Operation
* @param operation - Text Operation.
*/
transform(operation: ITextOperation): IOperationMeta;
/**
* Returns final state of Cursor
*/
getCursor(): ICursor | null;
}
export class OperationMeta implements IOperationMeta {
protected readonly _cursorBefore: ICursor | null;
protected readonly _cursorAfter: ICursor | null;
/**
* Creates additional metadata for a Text Operation
* @param cursorBefore - Cursor position before Text Operation is applied
* @param cursorAfter - Cursor position after Text Operation is applied
*/
constructor(cursorBefore: ICursor | null, cursorAfter: ICursor | null) {
this._cursorBefore = cursorBefore;
this._cursorAfter = cursorAfter;
}
clone(): IOperationMeta {
return new OperationMeta(this._cursorBefore, this._cursorAfter);
}
invert(): IOperationMeta {
return new OperationMeta(this._cursorAfter, this._cursorBefore);
}
compose(other: OperationMeta): IOperationMeta {
return new OperationMeta(this._cursorBefore, other._cursorAfter);
}
transform(operation: ITextOperation): IOperationMeta {
return new OperationMeta(
this._cursorBefore ? this._cursorBefore.transform(operation) : null,
this._cursorAfter ? this._cursorAfter.transform(operation) : null
);
}
getCursor(): ICursor | null {
return this._cursorAfter;
}
}