forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtext-op.ts
211 lines (190 loc) · 5.13 KB
/
text-op.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import * as Utils from "./utils";
export enum TextOptType {
Insert = "insert",
Delete = "delete",
Retain = "retain",
}
export interface ITextOpAttributes {
[attributeKey: string]: number | boolean | string | symbol;
}
/**
* Operation are essentially lists of ops. There are three types of ops:
*
* **Retain ops:** Advance the cursor position by a given number of characters.
* Represented by positive ints.
*
* **Insert ops:** Insert a given string at the current cursor position.
* Represented by strings.
*
* **Delete ops:** Delete the next n characters. Represented by negative ints.
*/
export interface ITextOp {
/**
* Number of characters Retained or Deleted. (`null` in case of Insert)
*/
chars: number | null;
/**
* String to be Inserted. (`null` in case of Retain or Delete)
*/
text: string | null;
/**
* Additional Attributes. (Defaults: `null`)
*/
attributes: ITextOpAttributes | null;
/**
* Tests if it's an Insert Operation.
*/
isInsert(): boolean;
/**
* Tests if it's a Delete Operation.
*/
isDelete(): boolean;
/**
* Tests if it's a Retain Operation.
*/
isRetain(): boolean;
/**
* Tests if two Individual Text Operation equal or not.
* @param other - Another Text Operation.
*/
equals(other: ITextOp): boolean;
/**
* Tests if two Individual Text Operation have Attributes or not.
* @param otherAttributes - Another Text Operation Attributes.
*/
attributesEqual(otherAttributes: ITextOpAttributes | null): boolean;
/**
* Tests if this Individual Text Operation has additional Attributes.
*/
hasEmptyAttributes(): boolean;
/**
* Returns String representation of an Individual Text Operation
*/
toString(): string;
/**
* Returns Primitive value of an Individual Text Operation
*/
valueOf(): string | number | null;
}
export class TextOp implements ITextOp {
protected readonly _type: TextOptType;
readonly chars: number | null;
readonly text: string | null;
readonly attributes: ITextOpAttributes | null;
/**
* Creates an individual Insert Operation
* @param type - Operation Type - Insert
* @param charsOrText - Insert string
* @param attributes - Additional Attributes of the operation
*/
constructor(
type: TextOptType.Insert,
charsOrText: string,
attributes: ITextOpAttributes | null
);
/**
* Creates an individual Delete Operation
* @param type - Operation Type - Delete
* @param charsOrText - Number of characters to delete
* @param attributes - Additional Attributes of the operation
*/
constructor(type: TextOptType.Delete, charsOrText: number, attributes: null);
/**
* Creates an individual Retain Operation
* @param type - Operation Type - Retain
* @param charsOrText - Number of characters to retain
* @param attributes - Additional Attributes of the operation
*/
constructor(
type: TextOptType.Retain,
charsOrText: number,
attributes: ITextOpAttributes | null
);
constructor(
type: TextOptType,
charsOrText: number | string,
attributes: ITextOpAttributes | null
) {
this._type = type;
this.chars = null;
this.text = null;
this.attributes = null;
switch (type) {
case TextOptType.Insert: {
this.text = charsOrText as string;
this.attributes = attributes || {};
break;
}
case TextOptType.Delete: {
this.chars = charsOrText as number;
break;
}
case TextOptType.Retain: {
this.chars = charsOrText as number;
this.attributes = attributes || {};
break;
}
default:
break;
}
}
isInsert(): boolean {
return this._type === TextOptType.Insert;
}
isDelete(): boolean {
return this._type === TextOptType.Delete;
}
isRetain(): boolean {
return this._type === TextOptType.Retain;
}
equals(other: TextOp): boolean {
return (
this._type === other._type &&
this.text === other.text &&
this.chars === other.chars &&
this.attributesEqual(other.attributes)
);
}
attributesEqual(otherAttributes: ITextOpAttributes | null): boolean {
if (otherAttributes == null || this.attributes == null) {
return this.attributes == otherAttributes;
}
for (const attr in this.attributes) {
if (this.attributes[attr] !== otherAttributes[attr]) {
return false;
}
}
for (const attr in otherAttributes) {
if (this.attributes[attr] !== otherAttributes[attr]) {
return false;
}
}
return true;
}
hasEmptyAttributes(): boolean {
if (this.attributes == null) {
return true;
}
return Object.keys(this.attributes).length === 0;
}
toString(): string {
const text = this.text ? `"${this.text}"` : this.chars;
return `${Utils.capitalizeFirstLetter(this._type)} ${text}`;
}
valueOf(): string | number | null {
switch (this._type) {
case TextOptType.Insert: {
return this.text!;
}
case TextOptType.Delete: {
return -this.chars!;
}
case TextOptType.Retain: {
return this.chars!;
}
default:
break;
}
return null;
}
}