-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.html
401 lines (325 loc) · 12 KB
/
embed.html
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<meta content="" name="description">
<meta content="Jeffrey Meng" name="author">
<title>Techedit</title>
<!-- Favicons -->
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="/favicon/apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/favicon/apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/favicon/apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/favicon/apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="/favicon/apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="/favicon/apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="/favicon/apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="/favicon/apple-touch-icon-152x152.png" />
<link rel="icon" type="image/png" href="/favicon/favicon-196x196.png" sizes="196x196" />
<link rel="icon" type="image/png" href="/favicon/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/png" href="/favicon/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="/favicon/favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="/favicon/favicon-128.png" sizes="128x128" />
<meta name="application-name" content=" " />
<meta name="msapplication-TileColor" content="#FFFFFF" />
<meta name="msapplication-TileImage" content="/favicon/mstile-144x144.png" />
<meta name="msapplication-square70x70logo" content="/favicon/mstile-70x70.png" />
<meta name="msapplication-square150x150logo" content="/favicon/mstile-150x150.png" />
<meta name="msapplication-wide310x150logo" content="/favicon/mstile-310x150.png" />
<meta name="msapplication-square310x310logo" content="/favicon/mstile-310x310.png" />
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom fonts -->
<link href="/css/simple-line-icons.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"><!-- Custom styles for this template -->
<style>
/* loader: http://tobiasahlin.com/spinkit/ */
.spinner {
margin: 100px auto 0;
width: 70px;
text-align: center;
}
.spinner > div {
width: 18px;
height: 18px;
background-color: #333;
border-radius: 100%;
display: inline-block;
-webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;
animation: sk-bouncedelay 1.4s infinite ease-in-out both;
}
.spinner .bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.spinner .bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
@-webkit-keyframes sk-bouncedelay {
0%, 80%, 100% { -webkit-transform: scale(0) }
40% { -webkit-transform: scale(1.0) }
}
@keyframes sk-bouncedelay {
0%, 80%, 100% {
-webkit-transform: scale(0);
transform: scale(0);
} 40% {
-webkit-transform: scale(1.0);
transform: scale(1.0);
}
}
#editor {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.hidden {
display:none !important;
}
</style>
</head>
<body>
<div id="loader" class="text-center">
<div class="spinner">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
<div id="notfounderror-wrapper" class="container hidden">
<div class="col-lg-12 pt-3">
<h1>404 Error: File not found</h1>
<p>No public resource was found at this location. Either the page no longer exists, or you do not have permission to view it.</p>
</div>
</div>
<div id="editor-box">
<div id="editor"></div>
</div>
<!-- Bootstrap core JavaScript -->
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.bundle.min.js"></script>
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/cookie.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/firepad.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.5/firebase-database.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyABAa5dEDjLd46i01STVlapvKrFFMinTqo",
authDomain: "techedit-io.firebaseapp.com",
databaseURL: "https://techedit-io.firebaseio.com",
projectId: "techedit-io",
storageBucket: "techedit-io.appspot.com",
messagingSenderId: "84859966897"
};
firebase.initializeApp(config);
</script>
<script src="/js/techedit.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/edit-helpers.js" type="text/javascript" charset="utf-8"></script>
<script>
function hideLoadingText() {
$("#loader").addClass("hidden");
$("#split-wrapper").removeClass("hidden");
}
function showLoadingText() {
$("#loader").removeClass("hidden");
$("#split-wrapper").addClass("hidden");
}
function onLanguageChange() {
var oldMode = editor.getSession().getMode().$id.substring("ace/mode/".length);
editor.getSession().setMode("ace/mode/" + $("#lang-select").val());
firebase.database().ref("/f/" + fileId + "/data/language/").set($("#lang-select").val());
firebase.database().ref("/f/" + fileId + "/chat/").push({
text:"Changed the language from " + oldMode + " to " + $("#lang-select").val(),
actionMsg:true,
timestamp:new Date().getTime(),
author:userId,
displayName:userDisplayName
});
}
function setUserData(user, path, value) {//path should be relative to /users/$UID/
if (user != null) {
firebase.database().ref("/users/" + user.uid + "/data/" + (path.replaceAll(".", "/") || "")).set(value);
} else {
var data = Cookies.get("techedit-anonymous-user-data");
if (data == undefined) {
data = {}
} else {
data = JSON.parse(data);
}
if (path == null || path == "") {
data = value;
} else {
setObjectValueByString(path, data, value);
}
Cookies.set("techedit-anonymous-user-data", data, {expires:1});
}
}
function setDisplayName(userId, userDisplayName, oldDisplayName) {//path should be relative to /users/$UID/
firebase.database().ref("/f/" +fileId + "/users/" + userId + "/displayName/").set(userDisplayName);
if (oldDisplayName !== false) {
firebase.database().ref("/f/" + fileId + "/chat/").push({
text: "Changed username from " + oldDisplayName + " to " + userDisplayName,
actionMsg: true,
timestamp: new Date().getTime(),
author: userId,
displayName: userDisplayName
});
}
}
function stripUsername(username) {
return username.substring(0, 64).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function reverseStripUsername(username) {
return username.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function stripText(text) {
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function reverseText(text) {
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function stripId(id) {
return id.replace(new RegExp("[\\[\\]\\\\\\/\\$#\\.]", 'g'), "");
}
function safeStripUsername(username) {
//this can be used to strip a username that should already be stripped
//if the username has all quotes then it each quote could potentially end up being 6 characters.
return username.substring(0, 64 * 6).replace(/</g, '<').replace(/"/g, '"');
}
</script>
<script>
var editor;
var path = window.location.pathname.substring(1).split("/");
fileId = path[1];
// check if document exists
// strip id will remove characters that cause firebase to throw an error, and also slashes.
// if the id is valid strip id will not change the id, if the id is invalid then no file can
// possible exist with that id and firebase will return null, so we shoe 404 as expected.
firebase.database().ref("/f/" + stripId(fileId) + "/createdOn/").once("value").then(function(snapshot) {
if (snapshot.val() != null) {
load();
} else {
//show 404
$("#loader").addClass("hidden");
$("#notfounderror-wrapper").removeClass("hidden");
}
});
function load() {
$("#notfounderror-wrapper").addClass("hidden");
showLoadingText();
$("#editor").html("");
let editorExists = editor != null && editor.destroy;
if (editorExists) {
editor.destroy();
}
editor = ace.edit("editor");
(function () {
//user is null
var cookie = Cookies.get("techedit-anonymous-user-data");
if (cookie) {
cookie = JSON.parse(cookie);
}
var anonData = cookie;
var tempuid = firebase.database().ref("/f/" + fileId + "/users/").push().key;
var defaultData = {
uid: tempuid,
displayName: tempuid,
color: colorFromUserId(tempuid),
editor: {}
};
var anonData = merge(defaultData, anonData);
setUserData(null, null, anonData);
main(null, anonData);
})();
}
var theme;
var userDisplayName;
var userId;
function main(user, userdata) {
userId = userdata.uid;
init(user, userdata);
firebase.database().ref("/f/" + fileId + "/users/").on("value", function(snapshot) {
var users = snapshot.val();
//https://stackoverflow.com/a/684692/5511561 (slightly adapted)
for (var key in users) {
if (users.hasOwnProperty(key)) {
if (key !== userdata.uid) {
console.log(users[key].color)
if (users[key].color == undefined) {
//user has been deleted
firebase.database().ref("/f/" + fileId + "/users/" + key).set(null);
continue;
}
// add the initial displayname to firebase
setDisplayName(userdata.uid, userDisplayName, false);
}
}
}
});
firebase.database().ref("/f/" + fileId + "/data/language/").on("value", function(snapshot) {
$("#lang-select").off("change", onLanguageChange);
var lang = snapshot.val();
if (lang == null) {
lang = "text";
firebase.database().ref("/f/" + fileId + "/data/language/").set("text");
}
editor.getSession().setMode("ace/mode/" + lang);
});
}
function init(user, userdata) {
if (userdata.editor) {
//if userdata.editor.theme is undefined it will be caught at the if statement
theme = userdata.editor.theme;
}
if (theme == undefined) {
theme = "monokai";
setUserData(user, "editor.theme", theme);
}
userDisplayName = userdata.displayName;
editor.setTheme("ace/theme/" + theme);
if (userdata.editor.printMargin > 0) {
editor.setShowPrintMargin(true);
editor.setPrintMarginColumn(parseInt(userdata.editor.printMargin, 10));
$("#print-margin-length").val(userdata.editor.printMargin);
$("#print-margin-checkbox").attr("checked", "checked");
$("#print-margin-collapse").collapse("show");
} else {
editor.setShowPrintMargin(false);
}
if (userdata.editor.keymap != null && userdata.editor.keymap !== "default") {
editor.setKeyboardHandler("ace/keyboard/" + userdata.editor.keymap);
}
// Get Firebase Database reference.
var firepadRef = firebase.database().ref("/f/" + fileId);
firebase.database().ref("/f/" + fileId + "/data/").once("value").then(function(snapshot) {
var data = snapshot.val();
if (!data) {
data = {};
}
if (!data.language) {
data.language = "text";
firebase.database().ref("/f/" + fileId + "/data/language/").set(data.language);
}
var initialText = null;
if (data.initialization != undefined) {
initialText = atob(data.initialization.text);
firebase.database().ref("/f/" + fileId + "/data/initialization/").set(null);
}
var firepad = Firepad.fromACE(firepadRef, editor, {
userId:"Embedded User",
color:"#84A3DD",
defaultText:initialText
});
firepad.on("ready", function() {
hideLoadingText();
});
});
}
</script>
</body>
</html>