Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: saving codebase on switching language #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions hackIDE/static/hackIDE/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -791,3 +806,22 @@ $(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'];

supportedLanguages.forEach((language) => {
let previousCode = localStorage.getItem(language);
if(previousCode !== null && previousCode.length !== 0)
{
langBoilerplate[language] = previousCode;
}
})
}

// function to reset local storage
function resetLocalStorage() {
window.localStorage.clear();
}