-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
118 lines (99 loc) · 2.87 KB
/
main.js
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
function init()
{
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const config = {
apiKey: "AIzaSyBihDOYXAqvOayAPXCbqIudZZCogzikx08",
authDomain: "g-notepad.firebaseapp.com",
databaseURL: "https://g-notepad-default-rtdb.firebaseio.com",
projectId: "g-notepad",
storageBucket: "g-notepad.appspot.com",
messagingSenderId: "451807484392",
appId: "1:451807484392:web:a273a3ea52f013f2ad6c5d"
};
$('#InputModal').modal({
backdrop: 'static',
keyboard: false
});
// Initialize Firebase
firebase.initializeApp(config);
var hash = window.location.hash.replace(/#/g,'');
if(hash)
{
init_pad();
}
else
{
$("#InputModal").modal("show");
}
};
function init_pad()
{
//// Get Firebase Database reference.
var firepadRef = getRef();
//// Create CodeMirror (with lineWrapping on).
var codeMirror = CodeMirror(document.getElementById('firepad-container'), { lineWrapping: true });
// Create a random ID to use as our user ID (we must give this to firepad and FirepadUserList).
var userId = Math.floor(Math.random() * 9999999999).toString();
//// Create Firepad (with rich text toolbar and shortcuts enabled).
var firepad = Firepad.fromCodeMirror(firepadRef, codeMirror, {
richTextToolbar: true,
richTextShortcuts: true,
userId: userId
});
//// Create FirepadUserList (with our desired userId).
var firepadUserList = FirepadUserList.fromDiv(
firepadRef.child('users'),
document.getElementById('userlist'), userId
);
//// Initialize contents.
firepad.on('ready', function()
{
if (firepad.isHistoryEmpty())
{
firepad.setHtml('<span style="font-size: 20px;">Hello World!');
}
});
}
// Helper to get hash from end of URL or generate a random one.
function getRef()
{
var ref = firebase.database().ref() ;
var hash = window.location.hash.replace(/#/g,''); //returns the anchor part (#) of a URL remove all after # in url
if(hash)
{
ref = ref.child(hash);
}
else
{
$("#InputModal").modal("hide");
hash = $('#noteName').val();
if(!hash)
hash = "default";
ref = ref.child(hash);
//ref = ref.push(); // generate unique location.
window.location = window.location + '#' + hash ; // add it as a hash to the URL.
}
return ref;
}
/*function auth()
{
var email = "gg@gg.in";
var password = "123456";
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
console.log(error.code);
console.log(error.message);
});
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
cur_user = user;
init_firepad()
// ...
} else {
// User is signed out
// ...
console.log("log out");
}
});
};*/