forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
firepad.ts
298 lines (267 loc) · 7.99 KB
/
firepad.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { Cursor } from "./cursor";
import {
DatabaseAdapterEvent,
DatabaseAdapterStateType,
IDatabaseAdapter,
UserIDType,
} from "./database-adapter";
import { EditorAdapterStateType, IEditorAdapter } from "./editor-adapter";
import {
EditorClient,
EditorClientEvent,
IEditorClient,
IEditorClientEvent,
} from "./editor-client";
import { EventEmitter, EventListenerType, IEventEmitter } from "./emitter";
import * as Utils from "./utils";
export enum FirepadEvent {
Ready = "ready",
Synced = "synced",
Undo = "undo",
Redo = "redo",
Error = "error",
}
export interface IFirepadConstructorOptions {
/** Unique Identifier for current User */
userId: UserIDType;
/** Unique Hexadecimal color code for current User */
userColor: string;
/** Name/Short Name of the current User (optional) */
userName?: string;
/** Default content of Firepad (optional) */
defaultText?: string;
}
export interface IFirepad extends Utils.IDisposable {
/**
* Add listener to Firepad.
* @param event - Event name.
* @param listener - Event handler callback.
*/
on(
event: FirepadEvent.Ready | FirepadEvent.Synced,
listener: EventListenerType<boolean>
): void;
on(
event: FirepadEvent.Undo | FirepadEvent.Redo,
listener: EventListenerType<string>
): void;
on(event: FirepadEvent.Error, listener: EventListenerType<Error>): void;
on(event: FirepadEvent, listener: EventListenerType<any>): void;
/**
* Remove listener to Firepad.
* @param event - Event name.
* @param listener - Event handler callback.
*/
off(
event: FirepadEvent.Ready | FirepadEvent.Synced,
listener: EventListenerType<boolean>
): void;
off(
event: FirepadEvent.Undo | FirepadEvent.Redo,
listener: EventListenerType<string>
): void;
off(event: FirepadEvent.Error, listener: EventListenerType<Error>): void;
off(event: FirepadEvent, listener: EventListenerType<any>): void;
/**
* Tests if any operation has been performed in Firepad.
*/
isHistoryEmpty(): boolean;
/**
* Set User Id to Firepad to distinguish user.
* @param userId - Unique Identifier for current User.
*/
setUserId(userId: UserIDType): void;
/**
* Set User Color to identify current User's cursor or selection.
* @param userColor - Hexadecimal color code.
*/
setUserColor(userColor: string): void;
/**
* Set User Name to mark current user's Cursor.
* @param userName - Name of current user.
*/
setUserName(userName: string): void;
/**
* Returns current content of Firepad.
*/
getText(): string;
/**
* Sets content into Firepad.
* @param text - Text content to set
*/
setText(text: string): void;
/**
* Clears undo redo stack of current Editor model.
*/
clearUndoRedoStack(): void;
/**
* Returns current Firepad configuration.
* @param option - Configuration option (same as constructor).
*/
getConfiguration(option: keyof IFirepadConstructorOptions): any;
}
export class Firepad implements IFirepad {
protected readonly _options: IFirepadConstructorOptions;
protected readonly _editorClient: IEditorClient;
protected readonly _editorAdapter: IEditorAdapter;
protected readonly _databaseAdapter: IDatabaseAdapter;
protected _ready: boolean;
protected _zombie: boolean;
protected _emitter: IEventEmitter | null;
/**
* Creates modern Firepad.
* @param databaseAdapter - Database interface wrapped inside Adapter.
* @param editorAdapter - Editor interface wrapped inside Adapter.
* @param options - Additional construction options.
*/
constructor(
databaseAdapter: IDatabaseAdapter,
editorAdapter: IEditorAdapter,
options: IFirepadConstructorOptions
) {
/** If not called with `new` operator */
if (!new.target) {
return new Firepad(databaseAdapter, editorAdapter, options);
}
this._ready = false;
this._zombie = false;
this._options = options;
this._databaseAdapter = databaseAdapter;
this._editorAdapter = editorAdapter;
this._editorClient = new EditorClient(databaseAdapter, editorAdapter);
this._emitter = new EventEmitter([
FirepadEvent.Ready,
FirepadEvent.Synced,
FirepadEvent.Undo,
FirepadEvent.Redo,
FirepadEvent.Error,
]);
this._init();
}
protected _init(): void {
this._databaseAdapter.on(DatabaseAdapterEvent.Ready, () => {
this._ready = true;
const { defaultText } = this._options;
if (defaultText && this.isHistoryEmpty()) {
this.setText(defaultText);
this.clearUndoRedoStack();
}
this._trigger(FirepadEvent.Ready, true);
});
this._editorClient.on(
EditorClientEvent.Synced,
(synced: boolean | IEditorClientEvent) => {
setTimeout(() => {
this._trigger(FirepadEvent.Synced, synced as boolean);
});
}
);
this._editorClient.on(
EditorClientEvent.Undo,
(undoOperation: string | IEditorClientEvent) => {
setTimeout(() => {
this._trigger(FirepadEvent.Undo, undoOperation as string);
});
}
);
this._editorClient.on(
EditorClientEvent.Redo,
(redoOperation: string | IEditorClientEvent) => {
setTimeout(() => {
this._trigger(FirepadEvent.Redo, redoOperation as string);
});
}
);
this._editorClient.on(
EditorClientEvent.Error,
(
error: Error | IEditorClientEvent,
operation: string,
state: DatabaseAdapterStateType | EditorAdapterStateType
) => {
setTimeout(() => {
this._trigger(FirepadEvent.Error, error as Error, operation, state);
});
}
);
}
getConfiguration(option: keyof IFirepadConstructorOptions): any {
return option in this._options ? this._options[option] : null;
}
on(event: FirepadEvent, listener: EventListenerType<any>): void {
return this._emitter?.on(event, listener);
}
off(event: FirepadEvent, listener: EventListenerType<any>): void {
return this._emitter?.off(event, listener);
}
protected _trigger(
event: FirepadEvent.Ready | FirepadEvent.Synced,
eventAttr: boolean
): void;
protected _trigger(
event: FirepadEvent.Undo | FirepadEvent.Redo,
eventAttr: string
): void;
protected _trigger(
event: FirepadEvent.Error,
eventAttr: Error,
...extraArgs: [string, DatabaseAdapterStateType | EditorAdapterStateType]
): void;
protected _trigger(
event: FirepadEvent,
eventAttr: any,
...extraArgs: unknown[]
): void {
return this._emitter?.trigger(event, eventAttr, ...extraArgs);
}
isHistoryEmpty(): boolean {
this._assertReady("isHistoryEmpty");
return this._databaseAdapter.isHistoryEmpty();
}
setUserId(userId: string | number): void {
this._databaseAdapter.setUserId(userId);
this._options.userId = userId;
}
setUserColor(userColor: string): void {
this._databaseAdapter.setUserColor(userColor);
this._options.userColor = userColor;
}
setUserName(userName: string): void {
this._databaseAdapter.setUserName(userName);
this._options.userName = userName;
}
getText(): string {
this._assertReady("getText");
return this._editorAdapter.getText();
}
setText(text: string = ""): void {
this._assertReady("setText");
this._editorAdapter.setText(text);
this._editorAdapter.setCursor(new Cursor(0, 0));
}
clearUndoRedoStack(): void {
this._assertReady("clearUndoRedoStack");
this._editorClient.clearUndoRedoStack();
}
dispose(): void {
this._zombie = true;
this._databaseAdapter.dispose();
this._editorAdapter.dispose();
this._editorClient.dispose();
if (this._emitter) {
this._trigger(FirepadEvent.Ready, false);
this._emitter.dispose();
this._emitter = null;
}
}
protected _assertReady(func: string): void {
Utils.validateTruth(
this._ready,
`You must wait for the "ready" event before calling ${func}.`
);
Utils.validateFalse(
this._zombie,
`You can't use a Firepad after calling dispose()! [called ${func}]`
);
}
}