forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfirepad-monaco.ts
92 lines (84 loc) · 3.06 KB
/
firepad-monaco.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
import * as monaco from "monaco-editor";
import { v4 as uuid } from "uuid";
import firebase from "firebase/app";
import { IDatabaseAdapter, UserIDType } from "./database-adapter";
import { FirebaseAdapter } from "./firebase-adapter";
import { Firepad, IFirepad, IFirepadConstructorOptions } from "./firepad";
import { MonacoAdapter } from "./monaco-adapter";
import * as Utils from "./utils";
import { FirestoreAdapter } from "./firestore-adapter";
/**
* Creates a modern Firepad from Monaco editor.
* @param databaseRef - Firebase database Reference path.
* @param editor - Monaco Editor instance.
* @param options - Firepad constructor options (optional).
*/
export function fromMonacoWithFirebase(
databaseRef: string | firebase.database.Reference,
editor: monaco.editor.IStandaloneCodeEditor,
options: Partial<IFirepadConstructorOptions> = {}
): IFirepad {
// Initialize constructor options with their default values
const userId: UserIDType = options.userId || uuid();
const userColor: string =
options.userColor || Utils.colorFromUserId(userId.toString());
const userName: string = options.userName || userId.toString();
const defaultText: string = options.defaultText || editor.getValue();
let databaseAdapter: IDatabaseAdapter = new FirebaseAdapter(
databaseRef,
userId,
userColor,
userName
);
const editorAdapter = new MonacoAdapter(editor, false);
return new Firepad(databaseAdapter, editorAdapter, {
userId,
userName,
userColor,
defaultText,
});
}
/**
* Creates a modern Firepad from Monaco editor.
* @param databaseRef - Firestore database document Reference.
* @param editor - Monaco Editor instance.
* @param options - Firepad constructor options (optional).
*/
export function fromMonacoWithFirestore(
databaseRef: firebase.firestore.DocumentReference, //TODO should we support path : string
editor: monaco.editor.IStandaloneCodeEditor,
options: Partial<IFirepadConstructorOptions> = {}
): IFirepad {
// Initialize constructor options with their default values
const userId: UserIDType = options.userId || uuid();
const userColor: string =
options.userColor || Utils.colorFromUserId(userId.toString());
const userName: string = options.userName || userId.toString();
const defaultText: string = options.defaultText || editor.getValue();
let databaseAdapter: IDatabaseAdapter = new FirestoreAdapter(
databaseRef,
userId,
userColor,
userName
);
const editorAdapter = new MonacoAdapter(editor, false);
return new Firepad(databaseAdapter, editorAdapter, {
userId,
userName,
userColor,
defaultText,
});
}
/**
* Creates a modern Firepad from Monaco editor.
* @param databaseRef - Firebase database Reference path.
* @param editor - Monaco Editor instance.
* @param options - Firepad constructor options (optional).
*/
export function fromMonaco(
databaseRef: string | firebase.database.Reference,
editor: monaco.editor.IStandaloneCodeEditor,
options: Partial<IFirepadConstructorOptions> = {}
): IFirepad {
return fromMonacoWithFirebase(databaseRef, editor, options);
}