let codeMirrorEditor = null; let currentEditingFilePath = null; function getFileExtension(filename) { const lastDot = filename.lastIndexOf('.'); if (lastDot === -1) return ''; return filename.slice(lastDot + 1); } function getCodeMirrorMode(filename) { const ext = getFileExtension(filename).toLowerCase(); switch (ext) { case 'js': return 'javascript'; case 'json': return { name: "javascript", json: true }; case 'html': case 'htm': return 'htmlmixed'; case 'css': return 'css'; case 'xml': return 'xml'; case 'yml': case 'yaml': return 'yaml'; case 'properties': return 'properties'; case 'log': case 'txt': return 'text/plain'; default: return null; } } async function openFileEditor(filePath) { const fileExtension = getFileExtension(filePath); const textFileExtensions = ['txt', 'log', 'json', 'yml', 'yaml', 'properties', 'html', 'css', 'js', 'xml', 'py']; if (!textFileExtensions.includes(fileExtension.toLowerCase())) { alert('Ten typ pliku nie może być edytowany bezpośrednio. Możesz go pobrać.'); return; } try { const response = await fetch(`/api/files/content?username=${username}&path=${encodeURIComponent(filePath)}`); if (!response.ok) { const errorText = await response.text(); throw new Error(`Server responded with status ${response.status}: ${errorText}`); } const data = await response.json(); if (data.success) { document.getElementById('file-list').style.display = 'none'; document.getElementById('drop-zone').style.display = 'none'; document.getElementById('file-editor-container').style.display = 'block'; document.getElementById('editing-filename').textContent = filePath.split('/').pop(); currentEditingFilePath = filePath; if (!codeMirrorEditor) { codeMirrorEditor = CodeMirror.fromTextArea(document.getElementById('file-editor'), { lineNumbers: true, theme: 'material-darker', mode: getCodeMirrorMode(filePath), indentUnit: 4, tabSize: 4, indentWithTabs: false, lineWrapping: true }); } else { codeMirrorEditor.setValue(data.content); codeMirrorEditor.setOption('mode', getCodeMirrorMode(filePath)); } codeMirrorEditor.refresh(); } else { alert('Błąd podczas ładowania pliku: ' + (data.error || 'Nieznany błąd.')); } } catch (error) { console.error('Błąd:', error); alert('Wystąpił błąd podczas ładowania pliku: ' + error.message); } } async function saveFileContent() { if (!codeMirrorEditor || !currentEditingFilePath) { alert('Brak pliku do zapisania.'); return; } const content = codeMirrorEditor.getValue(); try { const response = await fetch('/api/files/content', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: username, path: currentEditingFilePath, content: content }) }); if (!response.ok) { const errorText = await response.text(); throw new Error(`Server responded with status ${response.status}: ${errorText}`); } const data = await response.json(); if (data.success) { alert('Plik został pomyślnie zapisany!'); } else { alert('Błąd podczas zapisywania pliku: ' + (data.error || 'Nieznany błąd.')); } } catch (error) { console.error('Błąd:', error); alert('Wystąpił błąd podczas zapisywania pliku: ' + error.message); } } function closeFileEditor() { document.getElementById('file-editor-container').style.display = 'none'; document.getElementById('file-list').style.display = 'block'; document.getElementById('drop-zone').style.display = 'block'; currentEditingFilePath = null; if (codeMirrorEditor) { codeMirrorEditor.setValue(''); } }