forked from interviewstreet/firepad-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firepad-monaco.ts
43 lines (39 loc) · 1.39 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
import firebase from "firebase/compat/app";
import "firebase/compat/database";
import * as monaco from "monaco-editor";
import { v4 as uuid } from "uuid";
import { FirebaseAdapter } from "./firebase-adapter";
import { Firepad, IFirepad, IFirepadConstructorOptions } from "./firepad";
import { MonacoAdapter } from "./monaco-adapter";
import * as Utils from "./utils";
/**
* 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 {
// Initialize constructor options with their default values
const userId: string | number = 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();
const databaseAdapter = new FirebaseAdapter(
databaseRef,
userId,
userColor,
userName
);
const editorAdapter = new MonacoAdapter(editor, false);
return new Firepad(databaseAdapter, editorAdapter, {
userId,
userName,
userColor,
defaultText,
});
}