From 494a1a8776c213aa54752050dc7512ee4245af9c Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 18 Jan 2017 23:21:42 +0000 Subject: [PATCH] Make it possible to create a new script again. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3382 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 12 +++++++++--- core/terminal.js | 9 +++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/core/client.js b/core/client.js index 788da5ca..e23ba3a3 100644 --- a/core/client.js +++ b/core/client.js @@ -126,7 +126,7 @@ function edit() { function load() { var request = new XMLHttpRequest(); request.addEventListener("loadend", function() { - if (request.status == 200) { + if (request.status == 200 || request.status == 404) { document.getElementById("editPane").style.display = 'flex'; if (!gEditor) { gEditor = CodeMirror.fromTextArea(document.getElementById("editor"), { @@ -138,9 +138,15 @@ function load() { 'showTrailingSpace': true, }); } - gEditor.setValue(request.responseText); + var text; + if (request.status == 200) { + text = request.responseText; + } else { + text = '// New script\nterminal.print("Hello, world!");\n'; + } + gEditor.setValue(text); gEditor.focus(); - gBackup = request.responseText; + gBackup = text; } }); request.addEventListener("error", function() { diff --git a/core/terminal.js b/core/terminal.js index 576fd309..14dc7360 100644 --- a/core/terminal.js +++ b/core/terminal.js @@ -258,8 +258,13 @@ async function handler(request, response, packageOwner, packageName, uri) { var process; if (uri === "/view") { var data = File.readFile("packages/" + packageOwner + "/" + packageName + "/" + packageName + ".js"); - response.writeHead(200, {"Content-Type": "text/javascript; charset=utf-8", "Content-Length": data.length}); - response.end(data); + if (data) { + response.writeHead(200, {"Content-Type": "text/javascript; charset=utf-8", "Content-Length": data.length}); + response.end(data); + } else { + response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length}); + response.end("File not found"); + } } else if (uri == "/save") { var credentials = auth.query(request.headers); var userName = credentials && credentials.session && credentials.session.name ? credentials.session.name : "guest";