-
Notifications
You must be signed in to change notification settings - Fork 978
Expand file tree
/
Copy pathindex.js
More file actions
593 lines (522 loc) · 14.8 KB
/
index.js
File metadata and controls
593 lines (522 loc) · 14.8 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import "./style.scss";
import toast from "components/toast";
import Ref from "html-tag-js/ref";
import actionStack from "lib/actionStack";
import appSettings from "lib/settings";
import auth, { loginEvents } from "lib/auth";
import constants from "lib/constants";
let $sidebar;
/**@type {Array<(el:HTMLElement)=>boolean>} */
let preventSlideTests = [];
const events = {
show: [],
hide: [],
};
/**
* @typedef {object} SideBar
* @extends HTMLElement
* @property {function():void} hide
* @property {function():void} toggle
* @property {function():void} onshow
*/
/**
* Create a sidebar
* @param {HTMLElement} [$container] - the element that will contain the sidebar
* @param {HTMLElement} [$toggler] - the element that will toggle the sidebar
* @returns {Sidebar}
*/
function create($container, $toggler) {
let { innerWidth } = window;
const START_THRESHOLD = constants.SIDEBAR_SLIDE_START_THRESHOLD_PX; //Point where to start swipe
const MIN_WIDTH = 200; //Min width of the side bar
const MAX_WIDTH = () => innerWidth - 40; //Max width of the side bar
const resizeBar = Ref();
const userAvatar = Ref();
const userContextMenu = Ref();
$container = $container || app;
let mode = innerWidth > 600 ? "tab" : "phone";
let width = Math.min(appSettings.value.sidebarWidth || MIN_WIDTH, MAX_WIDTH());
const eventOptions = { passive: false };
const $el = (
<div id="sidebar" className={mode}>
<div className="apps">
<div className="app-icons-container"></div>
<div
ref={userAvatar}
className="user-icon-container"
onclick={handleUserIconClick}
>
<span className="icon account_circle"></span>
</div>
</div>
<div className="container"></div>
<div
className="resize-bar w-resize"
onmousedown={onresize}
ontouchstart={onresize}
></div>
<div ref={userContextMenu} className="user-menu">
<div className="user-menu-header">
<div className="user-menu-name"></div>
<div className="user-menu-email"></div>
</div>
{/* <div className="user-menu-separator"></div> */}
<div className="user-menu-item" onclick={handleLogout}>
<span className="icon logout"></span>
{strings.logout}
</div>
</div>
</div>
);
const mask = <span className="mask" onclick={hide}></span>;
const touch = {
startX: 0,
totalX: 0,
endX: 0,
startY: 0,
totalY: 0,
endY: 0,
target: null,
};
let openedFolders = [];
let resizeTimeout = null;
let setWidthTimeout = null;
$toggler?.addEventListener("click", toggle);
$container.addEventListener("touchstart", ontouchstart, eventOptions);
window.addEventListener("resize", onWindowResize);
appSettings.on("update:sidebarWidth", () => {
width = Math.min(appSettings.value.sidebarWidth || MIN_WIDTH, MAX_WIDTH());
setWidth(width);
});
if (mode === "tab" && localStorage.sidebarShown === "1") {
show();
}
loginEvents.on(() => {
updateSidebarAvatar();
});
async function handleUserIconClick(e) {
try {
const isLoggedIn = await auth.isLoggedIn();
if (!isLoggedIn) {
auth.openLoginUrl();
} else {
toggleUserMenu();
}
} catch (error) {
console.error("Error checking login status:", error);
toast("Error checking login status", 3000);
}
}
function toggleUserMenu() {
const menu = userContextMenu.el;
const isActive = menu.classList.toggle("active");
if (isActive) {
// Populate user info
updateUserMenuInfo();
// Add click outside listener
setTimeout(() => {
document.addEventListener("click", handleClickOutside);
}, 10);
} else {
document.removeEventListener("click", handleClickOutside);
}
}
function handleClickOutside(e) {
if (
!userContextMenu.el.contains(e.target) &&
e.target !== userAvatar.el &&
!userAvatar.el.contains(e.target)
) {
userContextMenu.el.classList.remove("active");
document.removeEventListener("click", handleClickOutside);
}
}
async function updateUserMenuInfo() {
try {
const userInfo = await auth.getUserInfo();
if (userInfo) {
const menuName = userContextMenu.el.querySelector(".user-menu-name");
const menuEmail = userContextMenu.el.querySelector(".user-menu-email");
menuName.textContent = userInfo.name || "Anonymous";
if (userInfo.isAdmin) {
menuName.innerHTML += ' <span class="badge">Admin</span>';
}
menuEmail.textContent = userInfo.email || "";
}
} catch (error) {
console.error("Error fetching user info:", error);
}
}
async function handleLogout() {
try {
const success = await auth.logout();
if (success) {
userContextMenu.el.classList.remove("active");
document.removeEventListener("click", handleClickOutside);
toast("Logged out successfully");
updateSidebarAvatar();
} else {
toast("Failed to logout");
}
} catch (error) {
console.error("Error during logout:", error);
}
}
async function updateSidebarAvatar() {
const avatarUrl = await auth.getAvatar();
// Remove existing icon or avatar
const existingIcon = userAvatar.el.querySelector(".icon");
const existingAvatar = userAvatar.el.querySelector(".avatar");
if (existingIcon) {
existingIcon.remove();
}
if (existingAvatar) {
existingAvatar.remove();
}
if (avatarUrl?.startsWith("data:") || avatarUrl?.startsWith("http")) {
// Create and add avatar image
const avatarImg = document.createElement("img");
avatarImg.className = "avatar";
avatarImg.src = avatarUrl;
userAvatar.append(avatarImg);
} else {
// Fallback to default icon
const defaultIcon = document.createElement("span");
defaultIcon.className = "icon account_circle";
userAvatar.append(defaultIcon);
}
}
function onWindowResize() {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
const { innerWidth: currentWidth } = window;
if (innerWidth === currentWidth) return;
hide(true);
innerWidth = currentWidth;
$el.classList.remove(mode);
mode = innerWidth > 750 ? "tab" : "phone";
$el.classList.add(mode);
}, 300);
}
function toggle() {
if ($el.activated) return hide(true);
show();
}
function show() {
localStorage.sidebarShown = 1;
$el.activated = true;
$el.onclick = null;
setWidth(width);
if (mode === "phone") {
resizeBar.style.display = "none";
$el.onshow();
app.append($el, mask);
$el.classList.add("show");
document.ontouchstart = ontouchstart;
actionStack.push({
id: "sidebar",
action: hideMaster,
});
} else {
resizeBar.style.display = "block";
app.append($el);
$el.onclick = () => {
if (!$el.textContent) acode.exec("open-folder");
};
}
onshow();
}
function hide(hideIfTab = false) {
localStorage.sidebarShown = 0;
if (mode === "phone") {
actionStack.remove("sidebar");
hideMaster();
} else if (hideIfTab) {
$el.activated = false;
root.style.removeProperty("margin-left");
root.style.removeProperty("width");
$el.remove();
// TODO : Codemirror
//editorManager.editor.resize(true);
}
}
function hideMaster() {
$el.style.transform = null;
$el.classList.remove("show");
setTimeout(() => {
$el.activated = false;
mask.remove();
$el.remove();
$container.style.overflow = null;
onhide();
}, 300);
document.ontouchstart = null;
resetState();
openedFolders.map(($) => ($.onscroll = null));
openedFolders = [];
}
async function onshow() {
if ($el.onshow) $el.onshow.call($el);
events.show.forEach((fn) => fn());
// try {
// if (await auth.isLoggedIn()) {
// const avatar = await auth.getAvatar();
// if (avatar) {
// auth.updateSidebarAvatar(avatar);
// }
// }
// } catch (error) {
// console.error("Error updating avatar:", error);
// }
}
function onhide() {
if ($el.onhide) $el.onhide.call($el);
events.hide.forEach((fn) => fn());
}
/**
* Event handler for touchstart event
* @param {TouchEvent} e
*/
function ontouchstart(e) {
const { target } = e;
const { clientX, clientY } = getClientCoords(e);
if (preventSlideTests.find((test) => test(target))) return;
if (mode === "tab") return;
$el.style.transition = "none";
touch.startX = clientX;
touch.startY = clientY;
touch.target = target;
if ($el.activated && !$el.contains(target) && target !== mask) {
return;
} else if (
(!$el.activated && touch.startX > START_THRESHOLD) ||
target === $toggler
) {
return;
}
document.addEventListener("touchmove", ontouchmove, eventOptions);
document.addEventListener("touchend", ontouchend, eventOptions);
}
/**
* Event handler for resize event
* @param {MouseEvent | TouchEvent} e
* @returns
*/
function onresize(e) {
const { clientX } = getClientCoords(e);
let deltaX = 0;
const onMove = (e) => {
const { clientX: currentX } = getClientCoords(e);
deltaX = currentX - clientX;
resize(deltaX);
};
const onEnd = () => {
const newWidth = width + deltaX;
if (newWidth <= MIN_WIDTH) width = MIN_WIDTH;
else if (newWidth >= MAX_WIDTH()) width = MAX_WIDTH();
else width = newWidth;
appSettings.value.sidebarWidth = width;
appSettings.update(false);
document.removeEventListener("touchmove", onMove, eventOptions);
document.removeEventListener("mousemove", onMove, eventOptions);
document.removeEventListener("touchend", onEnd, eventOptions);
document.removeEventListener("mouseup", onEnd, eventOptions);
document.removeEventListener("mouseleave", onEnd, eventOptions);
document.removeEventListener("touchcancel", onEnd, eventOptions);
};
document.addEventListener("touchmove", onMove, eventOptions);
document.addEventListener("mousemove", onMove, eventOptions);
document.addEventListener("touchend", onEnd, eventOptions);
document.addEventListener("mouseup", onEnd, eventOptions);
document.addEventListener("mouseleave", onEnd, eventOptions);
document.addEventListener("touchcancel", onEnd, eventOptions);
return;
}
/**
* Resize the sidebar
* @param {number} deltaX
* @returns
*/
function resize(deltaX) {
const newWidth = width + deltaX;
if (newWidth >= MAX_WIDTH()) return;
if (newWidth <= MIN_WIDTH) return;
setWidth(newWidth);
}
/**
* Event handler for touchmove event
* @param {TouchEvent} e
*/
function ontouchmove(e) {
e.preventDefault();
const { clientX, clientY } = getClientCoords(e);
touch.endX = clientX;
touch.endY = clientY;
touch.totalX = touch.endX - touch.startX;
touch.totalY = touch.endY - touch.startY;
let width = $el.getWidth();
if (
!$el.activated &&
touch.totalX < width &&
touch.startX < START_THRESHOLD
) {
if (!$el.isConnected) {
app.append($el, mask);
$container.style.overflow = "hidden";
}
$el.style.transform = `translate3d(${-(width - touch.totalX)}px, 0, 0)`;
} else if (touch.totalX < 0 && $el.activated) {
$el.style.transform = `translate3d(${touch.totalX}px, 0, 0)`;
}
}
/**
* Event handler for touchend event
* @param {TouchEvent} e
*/
function ontouchend(e) {
if (e.target !== mask && touch.totalX === 0) return resetState();
else if (e.target === mask && touch.totalX === 0) return hide();
e.preventDefault();
const threshold = $el.getWidth() / 3;
if (
($el.activated && touch.totalX > -threshold) ||
(!$el.activated && touch.totalX >= threshold)
) {
lclShow();
} else if (
(!$el.activated && touch.totalX < threshold) ||
($el.activated && touch.totalX <= -threshold)
) {
hide();
}
function lclShow() {
onshow();
$el.activated = true;
$el.style.transform = `translate3d(0, 0, 0)`;
document.addEventListener("touchstart", ontouchstart, eventOptions);
actionStack.remove("sidebar");
actionStack.push({
id: "sidebar",
action: hideMaster,
});
resetState();
}
}
/**
* Reset the touch state
*/
function resetState() {
touch.totalY = 0;
touch.startY = 0;
touch.endY = 0;
touch.totalX = 0;
touch.startX = 0;
touch.endX = 0;
touch.target = null;
$el.style.transition = null;
document.removeEventListener("touchmove", ontouchmove, eventOptions);
document.removeEventListener("touchend", ontouchend, eventOptions);
}
/**
* Set the width of the sidebar
* @param {number} width
*/
function setWidth(width) {
$el.style.transition = "none";
$el.style.width = width + "px";
$el.style.maxWidth = width + "px";
if (mode === "tab") {
root.style.marginLeft = width + "px";
root.style.width = `calc(100% - ${width}px)`;
}
clearTimeout(setWidthTimeout);
setWidthTimeout = setTimeout(() => {
editorManager?.editor?.resize(true);
}, 300);
}
/**
* Get the clientX and clientY from the event
* @param {TouchEvent | MouseEvent} e
* @returns {{clientX: number, clientY: number}}
*/
function getClientCoords(e) {
const { clientX, clientY } = (e.touches ?? [])[0] ?? e;
return { clientX, clientY };
}
$el.show = show;
$el.hide = hide;
$el.toggle = toggle;
$el.onshow = () => {};
$el.getWidth = function () {
const configuredWidth = appSettings.value.sidebarWidth || MIN_WIDTH;
return mode === "phone" ? Math.min(configuredWidth, MAX_WIDTH()) : width;
};
return $el;
}
/**
* Create a sidebar or return the existing one
* @param {object} [arg0] - the element that will activate the sidebar
* @param {HTMLElement} [arg0.container] - the element that will contain the sidebar
* @param {HTMLElement} [arg0.toggler] - the element that will toggle the sidebar
* @returns {HTMLElement & SideBar}
*/
function Sidebar({ container, toggler }) {
$sidebar = $sidebar ?? create(container, toggler);
return $sidebar;
}
Sidebar.hide = () => $sidebar?.hide();
Sidebar.show = () => $sidebar?.show();
Sidebar.toggle = () => $sidebar?.toggle();
Sidebar.on = (
/**@type {'hide'|'show'} */ event,
/**@type {Function} */ callback,
) => {
if (!events[event]) return;
events[event].push(callback);
};
Sidebar.off = (
/**@type {'hide'|'show'} */ event,
/**@type {Function} */ callback,
) => {
if (!events[event]) return;
events[event] = events[event].filter((cb) => cb !== callback);
};
/**@type {HTMLElement} */
Sidebar.el = null;
Object.defineProperty(Sidebar, "el", {
get() {
return $sidebar;
},
});
preventSlideTests.push((target) => {
let lastEl;
return testScrollable(target.closest(".scroll"));
/**
* Test if the element is scrollable recursively
* @param {HTMLElement} container
* @returns
*/
function testScrollable(container) {
if (!container || container === lastEl) return false;
const { scrollHeight, offsetHeight, scrollWidth, offsetWidth } = container;
if (scrollHeight > offsetHeight) return true;
if (scrollWidth > offsetWidth) return true;
lastEl = container;
return testScrollable(container.parentElement.closest(".scroll"));
}
});
preventSlideTests.push((target) => {
return (
target instanceof HTMLInputElement ||
target instanceof HTMLTextAreaElement ||
target.contentEditable === "true"
);
});
export default Sidebar;
/**
* Prevent the sidebar from sliding when the test returns true
* @param {(target:Element)=>boolean} test
*/
export function preventSlide(test) {
preventSlideTests.push(test);
}