|
134 | 134 | }); |
135 | 135 | }); |
136 | 136 | }); |
| 137 | + |
137 | 138 | var QueuedRequester = function(){ this.q=[]; this.r=false; this.x=null; } |
138 | 139 | QueuedRequester.prototype = { |
139 | 140 | _request: function(req){ |
|
432 | 433 | // Check filename from text field or current file |
433 | 434 | var pathField = gId("filepath"); |
434 | 435 | var filename = (pathField && pathField.value) ? pathField.value : currentFile; |
435 | | - aceEditor.session.setMode(filename && filename.toLowerCase().endsWith('.json') ? "ace/mode/json" : "ace/mode/text"); |
| 436 | + aceEditor.session.setMode(filename && (/\.json$/i.test(filename)) ? "ace/mode/json" : "ace/mode/text"); // same as filename.toLowerCase().endsWith('.json') |
436 | 437 | } |
437 | 438 |
|
438 | 439 | // Try to initialize Ace editor if available |
|
488 | 489 | var filename = pathField ? pathField.value : currentFile; |
489 | 490 | var border = "2px solid #333"; |
490 | 491 |
|
491 | | - if (filename && filename.toLowerCase().endsWith('.json')) { |
| 492 | + if (filename && (/\.json$/i.test(filename))) { // same as filename.toLowerCase().endsWith('.json') |
492 | 493 | try { |
493 | 494 | JSON.parse(ta.value); |
494 | 495 | } catch(e) { |
|
499 | 500 | }; |
500 | 501 |
|
501 | 502 | function saveFile(filename,data){ |
502 | | - var finalData = data; |
503 | | - // Minify JSON files before upload |
504 | | - if (filename.toLowerCase().endsWith('.json')) { |
| 503 | + var outdata = data; |
| 504 | + if (/\.json$/i.test(filename)) { // same as filename.toLowerCase().endsWith('.json') |
505 | 505 | try { |
506 | | - finalData = JSON.stringify(JSON.parse(data)); |
| 506 | + outdata = JSON.stringify(JSON.parse(data)); // validate and minify |
507 | 507 | } catch(e) { |
508 | | - alert("Invalid JSON! Please fix syntax."); |
| 508 | + alert("Invalid JSON! Please fix."); |
509 | 509 | return; |
510 | 510 | } |
511 | 511 | } |
512 | | - var fd=new FormData(); |
513 | | - fd.append("file",new Blob([finalData],{type:"text/plain"}),filename); |
514 | | - req.add("POST","/upload",fd,function(st,resp){ |
515 | | - if (st!=200) alert("ERROR "+st+": "+resp); |
516 | | - else { |
517 | | - showToast("File saved"); |
| 512 | + uploadFile({files: [new Blob([outdata], {type:"text/plain"})]}, filename, function(s) { |
| 513 | + if(s) { |
518 | 514 | refreshTree(); |
| 515 | + loadFile(filename); // (re)load if saved successfully to update formating or show file content |
519 | 516 | } |
520 | 517 | }); |
521 | 518 | } |
|
526 | 523 | gId("preview").style.display="none"; |
527 | 524 | gId("editor").style.display="flex"; |
528 | 525 | if (st==200) { |
529 | | - if (filename.toLowerCase().endsWith('.json')) { |
| 526 | + if ((/\.json$/i.test(filename))) { // same as filename.toLowerCase().endsWith('.json') |
530 | 527 | try { |
531 | | - setContent(filename.toLowerCase().includes('ledmap') ? prettyLedmap(resp) : JSON.stringify(JSON.parse(resp), null, 2)); |
| 528 | + setContent(/ledmap/i.test(filename) ? prettyLedmap(resp) : JSON.stringify(JSON.parse(resp), null, 2)); // pretty-print ledmap files (i.e. if file name includes "ledmap" case-insensitive) |
532 | 529 | } catch(e) { |
533 | 530 | setContent(resp); |
534 | 531 | } |
|
555 | 552 | } |
556 | 553 | if (!fn.startsWith("/")) fn = "/" + fn; |
557 | 554 | currentFile = fn; // Update current file |
558 | | - saveFile(fn, getContent()); |
559 | | - loadFile(fn); |
| 555 | + saveFile(fn, getContent()) |
560 | 556 | }, |
561 | 557 | loadText:function(fn){ |
562 | 558 | currentFile=fn; |
|
0 commit comments