diff --git a/core/client.js b/core/client.js index 2146a9ef..3b6bb918 100644 --- a/core/client.js +++ b/core/client.js @@ -5,6 +5,7 @@ var gCredentials; var gCurrentFile; var gFiles = {}; +var gApp = {files: {}}; var gEditor; var kErrorColor = "#dc322f"; @@ -160,6 +161,10 @@ function load() { gFiles[name] = {}; loadFile(name, json['files'][name]); }); + if (Object.keys(json['files']).length == 0) { + document.getElementById("editPane").style.display = 'flex'; + } + gApp = JSON.parse(text); } } catch { } @@ -212,20 +217,23 @@ function save() { var appFinished = function(success) { document.getElementById("save").disabled = false; Object.values(gFiles).forEach(function(file) { - file.doc.markClean(); + file.generation = file.doc.changeGeneration(); }); updateFiles(); } var always = function() { var anyUnfinished = Object.values(gFiles).some(x => x.request); - var anyUnsaved = Object.values(gFiles).some(x => !x.id); + var anyUnsaved = Object.values(gFiles).some(x => !x.doc.isClean(x.generation) && !x.id); + if (!anyUnfinished && !anyUnsaved) { var app = { type: "tildefriends-app", - files: Object.fromEntries(Object.keys(gFiles).map(x => [x, gFiles[x].id])), + files: Object.fromEntries(Object.keys(gFiles).map(x => [x, gFiles[x].id || gApp.files[x]])), }; + console.log(app); Object.values(gFiles).forEach(function(file) { delete file.id; }); + gApp = JSON.parse(JSON.stringify(app)); var request = new XMLHttpRequest(); request.addEventListener("error", function() { @@ -274,7 +282,14 @@ function save() { } }; + var anySkipped = false; Object.values(gFiles).forEach(function(file) { + if (file.doc.isClean(file.generation)) { + console.log("Not saving clean file."); + anySkipped = true; + return; + } + delete file.id; file.request = new XMLHttpRequest(); file.request.addEventListener("error", function() { @@ -305,10 +320,15 @@ function save() { always(); }); + console.log("Saving file"); file.request.open("POST", "/save", true); file.request.setRequestHeader("Content-Type", "text/plain"); file.request.send(file.doc.getValue()); }); + + if (anySkipped) { + always(); + } } function url() { @@ -591,7 +611,7 @@ function updateFiles() { if (file == gCurrentFile) { li.classList.add("current"); } - if (!gFiles[file].doc.isClean()) { + if (!gFiles[file].doc.isClean(gFiles[file].generation)) { li.classList.add("dirty"); } node.appendChild(li); @@ -602,7 +622,8 @@ function updateFiles() { function makeNewFile(name) { gFiles[name] = { - doc: new CodeMirror.Doc("", guessMode(name)) + doc: new CodeMirror.Doc("", guessMode(name)), + generation: -1, }; openFile(name); }