File size: 2,274 Bytes
76e638f 0bfcad5 76e638f 0bfcad5 76e638f 0bfcad5 76e638f 0bfcad5 76e638f | 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 | // File switching functionality
document.querySelectorAll('#fileList div').forEach(fileItem => {
fileItem.addEventListener('click', function() {
const fileName = this.querySelector('span').textContent;
switchFile(fileName);
});
});
let currentFileListener = null;
function switchFile(fileName) {
currentFile = fileName;
// Clean up previous listener if exists
if (currentFileListener) {
currentFileListener(); // Unsubscribes the listener
}
// Update UI - set active file
document.querySelectorAll('#fileList div').forEach(item => {
if (item.querySelector('span').textContent === fileName) {
item.classList.add('bg-[rgba(124,58,237,0.2)]');
const icon = item.querySelector('i');
icon.classList.add('glow-effect');
} else {
item.classList.remove('bg-[rgba(124,58,237,0.2)]');
const icon = item.querySelector('i');
icon.classList.remove('glow-effect');
}
});
// Change language mode based on file extension
let language;
if (fileName.endsWith('.js')) language = 'javascript';
if (fileName.endsWith('.css')) language = 'css';
if (editor && language) {
monaco.editor.setModelLanguage(editor.getModel(), language);
}
// Switch Firebase listener to new file
const { db, fb } = window;
const fileRef = fb.ref(db, `rooms/cyber-room/files/${currentFile.replace(/\./g, '_')}/code`);
// Update current file in presence data
const cursorRef = fb.ref(db, `rooms/cyber-room/cursors/${userId}`);
fb.set(cursorRef, {
name: userName,
color: userColor,
currentFile: currentFile
});
// Update editor content from new file
currentFileListener = fb.onValue(fileRef, (snapshot) => {
const data = snapshot.val();
if (data && data !== editor.getValue()) {
isRemoteUpdate = true;
editor.setValue(data || `// ${fileName}\n// Start coding...`);
isRemoteUpdate = false;
}
});
}
// Initialize with first file
document.addEventListener('DOMContentLoaded', () => {
setTimeout(() => {
document.querySelector('#fileList div:first-child').click();
}, 100);
}); |