From 770d3860199f6966cec97a91a731a1247ef11021 Mon Sep 17 00:00:00 2001 From: Ananya Date: Sun, 22 Feb 2026 18:43:37 +0530 Subject: [PATCH 1/3] Fixes code editor where only Python was working, now all 4 languages are functional. --- .../static/virtual_lab/js/code_editor.js | 46 ++++++++++++++++++- .../virtual_lab/code_editor/code_editor.html | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/web/virtual_lab/static/virtual_lab/js/code_editor.js b/web/virtual_lab/static/virtual_lab/js/code_editor.js index f5318170f..8435de164 100644 --- a/web/virtual_lab/static/virtual_lab/js/code_editor.js +++ b/web/virtual_lab/static/virtual_lab/js/code_editor.js @@ -6,17 +6,61 @@ function getCookie(name) { return match ? decodeURIComponent(match[2]) : null; } +// Language configuration for Ace editor +const languageConfig = { + python: { + mode: "ace/mode/python", + sample: 'print("Hello, World!")' + }, + javascript: { + mode: "ace/mode/javascript", + sample: 'console.log("Hello, World!");' + }, + c: { + mode: "ace/mode/c_cpp", + sample: '#include \n\nint main() {\n printf("Hello, World!\\n");\n return 0;\n}' + }, + cpp: { + mode: "ace/mode/c_cpp", + sample: '#include \nusing namespace std;\n\nint main() {\n cout << "Hello, World!" << endl;\n return 0;\n}' + } +}; + // Bootstrap Ace const editor = ace.edit("editor"); editor.setTheme("ace/theme/github"); editor.session.setMode("ace/mode/python"); -editor.setOptions({ fontSize: "14px", showPrintMargin: false }); +editor.setOptions({ + fontSize: "14px", + showPrintMargin: false, + wrap: true, + enableBasicAutocompletion: true, + enableLiveAutocompletion: true +}); const runBtn = document.getElementById("run-btn"); const outputEl = document.getElementById("output"); const stdinEl = document.getElementById("stdin-input"); const langSel = document.getElementById("language-select"); +// Function to update editor mode and sample code based on selected language +function updateEditorLanguage(language) { + const config = languageConfig[language]; + if (config) { + editor.session.setMode(config.mode); + editor.setValue(config.sample, -1); // -1 moves cursor to end + } +} + +// Language selector change handler +langSel.addEventListener("change", (e) => { + const selectedLanguage = e.target.value; + updateEditorLanguage(selectedLanguage); +}); + +// Initialize with Python sample code +updateEditorLanguage("python"); + runBtn.addEventListener("click", () => { const code = editor.getValue(); const stdin = stdinEl.value; diff --git a/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html b/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html index 882355c54..e3b288936 100644 --- a/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html +++ b/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html @@ -38,7 +38,7 @@

Interactive Code Editor

-
print("Hello, world!")
+
From 25762c6da2f25a6ec52f0f73d9d5dbd8fdae1946 Mon Sep 17 00:00:00 2001 From: Ananya Date: Sun, 22 Feb 2026 19:13:08 +0530 Subject: [PATCH 2/3] fixes --- .../static/virtual_lab/js/code_editor.js | 47 +++++++++++++++++-- .../virtual_lab/code_editor/code_editor.html | 2 +- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/web/virtual_lab/static/virtual_lab/js/code_editor.js b/web/virtual_lab/static/virtual_lab/js/code_editor.js index 8435de164..0377655f5 100644 --- a/web/virtual_lab/static/virtual_lab/js/code_editor.js +++ b/web/virtual_lab/static/virtual_lab/js/code_editor.js @@ -26,16 +26,16 @@ const languageConfig = { } }; +// Wait for DOM to be ready +document.addEventListener('DOMContentLoaded', function() { + // Bootstrap Ace const editor = ace.edit("editor"); editor.setTheme("ace/theme/github"); -editor.session.setMode("ace/mode/python"); editor.setOptions({ fontSize: "14px", showPrintMargin: false, - wrap: true, - enableBasicAutocompletion: true, - enableLiveAutocompletion: true + wrap: true }); const runBtn = document.getElementById("run-btn"); @@ -43,12 +43,47 @@ const outputEl = document.getElementById("output"); const stdinEl = document.getElementById("stdin-input"); const langSel = document.getElementById("language-select"); +// Helper to detect if the editor contains unsaved (non-sample) code +function hasUnsavedChanges() { + const currentCode = editor.getValue(); + const normalizedCurrent = currentCode.trimEnd(); + + // Empty editor is treated as having no unsaved changes + if (!normalizedCurrent) { + return false; + } + + // If the current code matches any sample (ignoring trailing whitespace), we treat it as not modified + const matchesAnySample = Object.values(languageConfig).some((config) => { + return config.sample.trimEnd() === normalizedCurrent; + }); + + return !matchesAnySample; +} + // Function to update editor mode and sample code based on selected language function updateEditorLanguage(language) { const config = languageConfig[language]; if (config) { + // Always update syntax highlighting mode editor.session.setMode(config.mode); - editor.setValue(config.sample, -1); // -1 moves cursor to end + + // Only overwrite the editor contents if there are no unsaved changes, + // or if the user explicitly confirms discarding their current code. + if (hasUnsavedChanges()) { + const confirmDiscard = window.confirm( + "You have code in the editor that differs from the default samples. " + + "Switching languages will replace it with example code for the selected language. " + + "Do you want to discard your current code?" + ); + + if (!confirmDiscard) { + // Preserve the current code while still keeping the updated syntax mode + return; + } + } + + editor.setValue(config.sample, -1); // -1 moves cursor to start } } @@ -95,3 +130,5 @@ runBtn.addEventListener("click", () => { runBtn.disabled = false; }); }); + +}); // End DOMContentLoaded diff --git a/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html b/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html index e3b288936..3136f8da4 100644 --- a/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html +++ b/web/virtual_lab/templates/virtual_lab/code_editor/code_editor.html @@ -1,5 +1,5 @@ {# templates/virtual_lab/code_editor/code_editor.html #} -{% extends 'virtual_lab/layout.html' %} +{% extends "virtual_lab/layout.html" %} {% load static %} From bb78a4bcccc98201da4b589d539ceb39b383deca Mon Sep 17 00:00:00 2001 From: Ananya Date: Sun, 22 Feb 2026 19:26:46 +0530 Subject: [PATCH 3/3] fix --- web/virtual_lab/static/virtual_lab/js/code_editor.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/web/virtual_lab/static/virtual_lab/js/code_editor.js b/web/virtual_lab/static/virtual_lab/js/code_editor.js index 0377655f5..5316d0ce5 100644 --- a/web/virtual_lab/static/virtual_lab/js/code_editor.js +++ b/web/virtual_lab/static/virtual_lab/js/code_editor.js @@ -43,6 +43,9 @@ const outputEl = document.getElementById("output"); const stdinEl = document.getElementById("stdin-input"); const langSel = document.getElementById("language-select"); +// Track the currently active language +let currentLanguage = "python"; + // Helper to detect if the editor contains unsaved (non-sample) code function hasUnsavedChanges() { const currentCode = editor.getValue(); @@ -65,9 +68,6 @@ function hasUnsavedChanges() { function updateEditorLanguage(language) { const config = languageConfig[language]; if (config) { - // Always update syntax highlighting mode - editor.session.setMode(config.mode); - // Only overwrite the editor contents if there are no unsaved changes, // or if the user explicitly confirms discarding their current code. if (hasUnsavedChanges()) { @@ -78,11 +78,14 @@ function updateEditorLanguage(language) { ); if (!confirmDiscard) { - // Preserve the current code while still keeping the updated syntax mode + // Revert the dropdown to the previously active language + langSel.value = currentLanguage; return; } } + currentLanguage = language; + editor.session.setMode(config.mode); editor.setValue(config.sample, -1); // -1 moves cursor to start } }