forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
firepad-classic.ts
233 lines (199 loc) · 6.52 KB
/
firepad-classic.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
import firebase from "firebase";
import * as monaco from "monaco-editor";
import { Cursor } from "./cursor";
import {
DatabaseAdapterEvent,
IDatabaseAdapter,
IDatabaseAdapterEvent,
UserIDType,
} from "./database-adapter";
import { IEditorAdapter, IEditorAdapterEvent } from "./editor-adapter";
import {
EditorClient,
EditorClientEvent,
IEditorClient,
} from "./editor-client";
import { EventEmitter, EventListenerType, IEventEmitter } from "./emitter";
import { FirebaseAdapter } from "./firebase-adapter";
import { FirepadEvent as FirepadClassicEvent, IFirepad } from "./firepad";
import { MonacoAdapter } from "./monaco-adapter";
import * as Utils from "./utils";
interface IFirepadClassicConstructorOptions {
/** Unique Identifier for current User */
userId?: UserIDType;
/** Unique Hexadecimal color code for current User */
userColor?: string;
/** Name/Short Name of the current User */
userName?: string;
/** Default content of Firepad */
defaultText?: string;
}
export default class FirepadClassic implements IFirepad {
protected readonly _options: IFirepadClassicConstructorOptions;
protected readonly _editorClient: IEditorClient;
protected readonly _editorAdapter: IEditorAdapter;
protected readonly _databaseAdapter: IDatabaseAdapter;
protected _ready: boolean;
protected _zombie: boolean;
protected _emitter: IEventEmitter | null;
/**
* Creates a classic Firepad
* @deprecated
* @param databaseRef - Firebase database Reference object.
* @param editor - Editor instance (expected Monaco).
* @param options - Firepad constructor options (optional).
*/
constructor(
databaseRef: firebase.database.Reference,
editor: monaco.editor.IStandaloneCodeEditor,
options: IFirepadClassicConstructorOptions = {}
) {
/** If not called with `new` operator */
if (!new.target) {
return new FirepadClassic(databaseRef, editor, options);
}
this._ready = false;
this._zombie = false;
this._options = options;
options.userId = this._getOptions("userId", () => databaseRef.push().key);
options.userColor = this._getOptions("userColor", () =>
Utils.colorFromUserId(options.userId!.toString())
);
options.userName = this._getOptions("userName", () => options.userId);
options.defaultText = this._getOptions("defaultText", () =>
editor.getValue()
);
editor.setValue(""); // Ensure editor is empty before wraping with adapter
this._databaseAdapter = new FirebaseAdapter(
databaseRef,
options.userId!,
options.userColor!,
options.userName!
);
this._editorAdapter = new MonacoAdapter(editor, false);
this._editorClient = new EditorClient(
this._databaseAdapter,
this._editorAdapter
);
this._emitter = new EventEmitter([
FirepadClassicEvent.Ready,
FirepadClassicEvent.Synced,
FirepadClassicEvent.Undo,
FirepadClassicEvent.Redo,
]);
this.init();
}
protected init(): void {
this._databaseAdapter.on(DatabaseAdapterEvent.Ready, () => {
this._ready = true;
const defaultText = this._getOptions("defaultText", () => null);
if (defaultText && this.isHistoryEmpty()) {
this.setText(defaultText);
this.clearUndoRedoStack();
}
this._trigger(FirepadClassicEvent.Ready, true);
});
this._editorClient.on(
EditorClientEvent.Synced,
(synced: boolean | IDatabaseAdapterEvent) => {
this._trigger(FirepadClassicEvent.Synced, synced);
}
);
this._editorClient.on(
EditorClientEvent.Undo,
(undoOperation: string | IEditorAdapterEvent) => {
this._trigger(FirepadClassicEvent.Undo, undoOperation);
}
);
this._editorClient.on(
EditorClientEvent.Redo,
(redoOperation: string | IEditorAdapterEvent) => {
this._trigger(FirepadClassicEvent.Redo, redoOperation);
}
);
}
dispose(): void {
this._databaseAdapter.dispose();
this._editorAdapter.dispose();
this._editorClient.dispose();
if (this._emitter) {
this._trigger(FirepadClassicEvent.Ready, false);
this._emitter.dispose();
this._emitter = null;
}
}
on(event: FirepadClassicEvent, listener: EventListenerType<any>): void {
return this._emitter?.on(event, listener);
}
off(event: FirepadClassicEvent, listener: EventListenerType<any>): void {
return this._emitter?.off(event, listener);
}
protected _trigger(
event: FirepadClassicEvent,
eventAttr: any,
...extraArgs: any[]
): 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();
}
getConfiguration(option: keyof IFirepadClassicConstructorOptions): any {
return this._getOptions(option, () => null);
}
protected _getOptions(
option: keyof IFirepadClassicConstructorOptions,
getDefault: () => any
) {
return option in this._options ? this._options[option] : getDefault();
}
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}]`
);
}
/**
* Creates a classic Firepad from Monaco editor.
* @param databaseRef - Firebase database Reference object.
* @param editor - Monaco Editor instance.
* @param options - Firepad constructor options (optional).
*/
static fromMonaco(
databaseRef: firebase.database.Reference,
editor: monaco.editor.IStandaloneCodeEditor,
options?: IFirepadClassicConstructorOptions
) {
return new FirepadClassic(databaseRef, editor, options);
}
}