From 890215c75b1965999d7fa10638c805dc2b815075 Mon Sep 17 00:00:00 2001 From: Deepak Mehrotra Date: Fri, 27 Aug 2021 22:41:18 +0530 Subject: [PATCH] feature: saving codebase on switching language --- hackIDE/static/hackIDE/js/custom.js | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/hackIDE/static/hackIDE/js/custom.js b/hackIDE/static/hackIDE/js/custom.js index 22f6a35..d6e755a 100644 --- a/hackIDE/static/hackIDE/js/custom.js +++ b/hackIDE/static/hackIDE/js/custom.js @@ -8,6 +8,8 @@ $(document).ready(function(){ + // to reset all the previous code base if present in local storage on reload + resetLocalStorage() // contents of the editor at any step var editorContent; // language selected @@ -40,6 +42,9 @@ $(document).ready(function(){ langBoilerplate['RUST'] = "fn main() {\n // The statements here will be executed when the compiled binary is called\n\n // Print text to the console\n println!(\"Hello World!\");\n}\n"; langBoilerplate['SCALA'] = "object Main extends App {\n // your code goes here\n}\n"; + // changing boiler plate of languages according to the once present in local storage + updateBoilerPlate(langBoilerplate) + // flag to block requests when a request is running var request_ongoing = false; @@ -72,6 +77,14 @@ $(document).ready(function(){ checkForInitialData(); + // function to auto-save current code in local storage + $("#editor").keyup(() => { + var editor = ace.edit("editor"); + var languageCode = editor.getValue(); + var lang = $("#lang").val(); + localStorage.setItem(lang, languageCode); + }) + function showResultBox() { $(".output-response-box").show(); $(".run-status").show(); @@ -646,6 +659,8 @@ $(document).ready(function(){ // when lang is changed $("#lang").change(function(){ + updateBoilerPlate(langBoilerplate) + languageSelected = $("#lang").val(); // update the language (mode) for the editor @@ -791,3 +806,21 @@ $(document).ready(function(){ }); + +// function to update the language boiler plates +function updateBoilerPlate(langBoilerplate) +{ + var supportedLanguages = ['C', 'CPP', 'CSHARP', 'CSS', 'CLOJURE', 'HASKELL', 'JAVA', 'JAVASCRIPT', 'OBJECTIVEC', 'PERL', 'PHP', 'PYTHON', 'R', 'RUBY', 'RUST', 'SCALA']; + + for(let i = 0; i < supportedLanguages.length; i++) { + let previousCode = localStorage.getItem(supportedLanguages[i]); + if(previousCode !== null && previousCode.length !== 0) { + langBoilerplate[supportedLanguages[i]] = previousCode; + } + } +} + +// function to reset local storage +function resetLocalStorage() { + window.localStorage.clear(); +} \ No newline at end of file