import { api } from '../api.js'; class CodeEditorView extends HTMLElement { constructor() { super(); this.editor = null; this.file = null; this.previousView = null; this.boundHandleClick = this.handleClick.bind(this); this.boundHandleEscape = this.handleEscape.bind(this); } connectedCallback() { this.addEventListener('click', this.boundHandleClick); document.addEventListener('keydown', this.boundHandleEscape); } disconnectedCallback() { this.removeEventListener('click', this.boundHandleClick); document.removeEventListener('keydown', this.boundHandleEscape); if (this.editor) { this.editor.toTextArea(); this.editor = null; } } handleEscape(e) { if (e.key === 'Escape') { this.goBack(); } } async setFile(file, previousView = 'files') { this.file = file; this.previousView = previousView; await this.loadAndRender(); } async loadAndRender() { try { const blob = await api.downloadFile(this.file.id); const content = await blob.text(); this.render(content); this.initializeEditor(content); } catch (error) { console.error('Failed to load file:', error); document.dispatchEvent(new CustomEvent('show-toast', { detail: { message: 'Failed to load file: ' + error.message, type: 'error' } })); this.render(''); window.history.back(); } } getMimeType(filename) { const extension = filename.split('.').pop().toLowerCase(); const mimeMap = { 'js': 'text/javascript', 'json': 'application/json', 'py': 'text/x-python', 'md': 'text/x-markdown', 'html': 'text/html', 'xml': 'application/xml', 'css': 'text/css', 'txt': 'text/plain', 'log': 'text/plain', 'sh': 'text/x-sh', 'yaml': 'text/x-yaml', 'yml': 'text/x-yaml' }; return mimeMap[extension] || 'text/plain'; } render(content) { this.innerHTML = `