|
| 1 | +// State management |
| 2 | +let currentConcept = null; |
| 3 | +let originalCode = ''; |
| 4 | + |
| 5 | +// DOM elements |
| 6 | +const conceptsList = document.getElementById('conceptsList'); |
| 7 | +const conceptTitle = document.getElementById('conceptTitle'); |
| 8 | +const conceptDescription = document.getElementById('conceptDescription'); |
| 9 | +const codeEditor = document.getElementById('codeEditor'); |
| 10 | +const output = document.getElementById('output'); |
| 11 | +const searchInput = document.getElementById('searchInput'); |
| 12 | +const runBtn = document.getElementById('runBtn'); |
| 13 | +const clearBtn = document.getElementById('clearBtn'); |
| 14 | +const resetBtn = document.getElementById('resetBtn'); |
| 15 | +const clearOutputBtn = document.getElementById('clearOutputBtn'); |
| 16 | + |
| 17 | +// Initialize the application |
| 18 | +function init() { |
| 19 | + renderConceptsList(); |
| 20 | + setupEventListeners(); |
| 21 | + |
| 22 | + // Load first concept by default |
| 23 | + if (conceptsData.length > 0) { |
| 24 | + selectConcept(conceptsData[0].id); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Render the concepts list |
| 29 | +function renderConceptsList(filter = '') { |
| 30 | + conceptsList.innerHTML = ''; |
| 31 | + |
| 32 | + const filteredConcepts = conceptsData.filter(concept => |
| 33 | + concept.title.toLowerCase().includes(filter.toLowerCase()) |
| 34 | + ); |
| 35 | + |
| 36 | + filteredConcepts.forEach(concept => { |
| 37 | + const li = document.createElement('li'); |
| 38 | + li.innerHTML = ` |
| 39 | + <span class="concept-number">${concept.id}</span> |
| 40 | + ${concept.title} |
| 41 | + `; |
| 42 | + li.addEventListener('click', () => selectConcept(concept.id)); |
| 43 | + |
| 44 | + if (currentConcept && currentConcept.id === concept.id) { |
| 45 | + li.classList.add('active'); |
| 46 | + } |
| 47 | + |
| 48 | + conceptsList.appendChild(li); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +// Select and load a concept |
| 53 | +function selectConcept(conceptId) { |
| 54 | + const concept = conceptsData.find(c => c.id === conceptId); |
| 55 | + if (!concept) return; |
| 56 | + |
| 57 | + currentConcept = concept; |
| 58 | + originalCode = concept.example; |
| 59 | + |
| 60 | + // Update UI |
| 61 | + conceptTitle.textContent = `${concept.id}. ${concept.title}`; |
| 62 | + conceptDescription.textContent = concept.description; |
| 63 | + codeEditor.value = concept.example; |
| 64 | + |
| 65 | + // Clear output |
| 66 | + clearOutput(); |
| 67 | + |
| 68 | + // Update active state in list |
| 69 | + renderConceptsList(searchInput.value); |
| 70 | + |
| 71 | + // Scroll to top of content |
| 72 | + window.scrollTo({ top: 0, behavior: 'smooth' }); |
| 73 | +} |
| 74 | + |
| 75 | +// Setup event listeners |
| 76 | +function setupEventListeners() { |
| 77 | + // Search functionality |
| 78 | + searchInput.addEventListener('input', (e) => { |
| 79 | + renderConceptsList(e.target.value); |
| 80 | + }); |
| 81 | + |
| 82 | + // Run code button |
| 83 | + runBtn.addEventListener('click', runCode); |
| 84 | + |
| 85 | + // Clear editor button |
| 86 | + clearBtn.addEventListener('click', () => { |
| 87 | + codeEditor.value = ''; |
| 88 | + clearOutput(); |
| 89 | + }); |
| 90 | + |
| 91 | + // Reset to original code |
| 92 | + resetBtn.addEventListener('click', () => { |
| 93 | + if (originalCode) { |
| 94 | + codeEditor.value = originalCode; |
| 95 | + clearOutput(); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // Clear output button |
| 100 | + clearOutputBtn.addEventListener('click', clearOutput); |
| 101 | + |
| 102 | + // Keyboard shortcuts |
| 103 | + document.addEventListener('keydown', (e) => { |
| 104 | + // Ctrl/Cmd + Enter to run code |
| 105 | + if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { |
| 106 | + e.preventDefault(); |
| 107 | + runCode(); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + // Allow Tab key in textarea |
| 112 | + codeEditor.addEventListener('keydown', (e) => { |
| 113 | + if (e.key === 'Tab') { |
| 114 | + e.preventDefault(); |
| 115 | + const start = codeEditor.selectionStart; |
| 116 | + const end = codeEditor.selectionEnd; |
| 117 | + |
| 118 | + codeEditor.value = codeEditor.value.substring(0, start) + |
| 119 | + ' ' + |
| 120 | + codeEditor.value.substring(end); |
| 121 | + |
| 122 | + codeEditor.selectionStart = codeEditor.selectionEnd = start + 4; |
| 123 | + } |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +// Run the code in the editor |
| 128 | +function runCode() { |
| 129 | + const code = codeEditor.value.trim(); |
| 130 | + |
| 131 | + if (!code) { |
| 132 | + addOutput('No code to execute', 'error'); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + clearOutput(); |
| 137 | + |
| 138 | + // Capture console output |
| 139 | + const originalConsole = { |
| 140 | + log: console.log, |
| 141 | + error: console.error, |
| 142 | + warn: console.warn, |
| 143 | + info: console.info |
| 144 | + }; |
| 145 | + |
| 146 | + const logs = []; |
| 147 | + |
| 148 | + // Override console methods |
| 149 | + console.log = (...args) => { |
| 150 | + const message = args.map(arg => formatValue(arg)).join(' '); |
| 151 | + logs.push({ type: 'log', message }); |
| 152 | + originalConsole.log(...args); |
| 153 | + }; |
| 154 | + |
| 155 | + console.error = (...args) => { |
| 156 | + const message = args.map(arg => formatValue(arg)).join(' '); |
| 157 | + logs.push({ type: 'error', message }); |
| 158 | + originalConsole.error(...args); |
| 159 | + }; |
| 160 | + |
| 161 | + console.warn = (...args) => { |
| 162 | + const message = args.map(arg => formatValue(arg)).join(' '); |
| 163 | + logs.push({ type: 'warn', message }); |
| 164 | + originalConsole.warn(...args); |
| 165 | + }; |
| 166 | + |
| 167 | + console.info = (...args) => { |
| 168 | + const message = args.map(arg => formatValue(arg)).join(' '); |
| 169 | + logs.push({ type: 'info', message }); |
| 170 | + originalConsole.info(...args); |
| 171 | + }; |
| 172 | + |
| 173 | + try { |
| 174 | + // Execute the code |
| 175 | + const result = eval(code); |
| 176 | + |
| 177 | + // Display logs |
| 178 | + logs.forEach(log => { |
| 179 | + addOutput(log.message, log.type); |
| 180 | + }); |
| 181 | + |
| 182 | + // If there's a return value and no logs, display it |
| 183 | + if (result !== undefined && logs.length === 0) { |
| 184 | + addOutput(`=> ${formatValue(result)}`, 'success'); |
| 185 | + } |
| 186 | + |
| 187 | + // If no output at all |
| 188 | + if (logs.length === 0 && result === undefined) { |
| 189 | + addOutput('Code executed successfully (no output)', 'success'); |
| 190 | + } |
| 191 | + |
| 192 | + } catch (error) { |
| 193 | + addOutput(`Error: ${error.message}`, 'error'); |
| 194 | + console.error(error); |
| 195 | + } finally { |
| 196 | + // Restore console methods |
| 197 | + console.log = originalConsole.log; |
| 198 | + console.error = originalConsole.error; |
| 199 | + console.warn = originalConsole.warn; |
| 200 | + console.info = originalConsole.info; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +// Format values for display |
| 205 | +function formatValue(value) { |
| 206 | + if (value === null) return 'null'; |
| 207 | + if (value === undefined) return 'undefined'; |
| 208 | + if (typeof value === 'string') return value; |
| 209 | + if (typeof value === 'function') return value.toString(); |
| 210 | + if (typeof value === 'symbol') return value.toString(); |
| 211 | + |
| 212 | + try { |
| 213 | + return JSON.stringify(value, null, 2); |
| 214 | + } catch (e) { |
| 215 | + return String(value); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +// Add output to the console |
| 220 | +function addOutput(message, type = 'log') { |
| 221 | + const outputDiv = document.getElementById('output'); |
| 222 | + |
| 223 | + // Remove empty state |
| 224 | + outputDiv.classList.remove('empty'); |
| 225 | + |
| 226 | + const logEntry = document.createElement('div'); |
| 227 | + logEntry.className = `log ${type}`; |
| 228 | + logEntry.textContent = message; |
| 229 | + |
| 230 | + outputDiv.appendChild(logEntry); |
| 231 | + |
| 232 | + // Auto-scroll to bottom |
| 233 | + outputDiv.scrollTop = outputDiv.scrollHeight; |
| 234 | +} |
| 235 | + |
| 236 | +// Clear the output console |
| 237 | +function clearOutput() { |
| 238 | + output.innerHTML = ''; |
| 239 | + output.classList.add('empty'); |
| 240 | + output.textContent = 'Run your code to see output here...'; |
| 241 | +} |
| 242 | + |
| 243 | +// Initialize the application when DOM is ready |
| 244 | +if (document.readyState === 'loading') { |
| 245 | + document.addEventListener('DOMContentLoaded', init); |
| 246 | +} else { |
| 247 | + init(); |
| 248 | +} |
0 commit comments