added beautifier button in text editor

This commit is contained in:
Olivier Gagnon 2018-06-14 02:26:54 -04:00
parent ec862ec747
commit c35395e302

@ -1,4 +1,5 @@
var ace = require('brace'); var ace = require('brace');
var beautify = require('js-beautify').js_beautify;
require('brace/mode/javascript'); require('brace/mode/javascript');
require('../netscript'); require('../netscript');
require('brace/theme/chaos'); require('brace/theme/chaos');
@ -57,6 +58,15 @@ function scriptEditorInit() {
console.log("Error finding 'script-editor-buttons-wrapper'"); console.log("Error finding 'script-editor-buttons-wrapper'");
return; return;
} }
var beautifyButton = createElement("a", {
class:"a-link-button", display:"inline-block",
innerText:"beautify",
clickListener:()=>{
beautifyScript();
return false;
}
});
var closeButton = createElement("a", { var closeButton = createElement("a", {
class:"a-link-button", display:"inline-block", class:"a-link-button", display:"inline-block",
innerText:"Save & Close (Ctrl/Cmd + b)", innerText:"Save & Close (Ctrl/Cmd + b)",
@ -90,6 +100,7 @@ function scriptEditorInit() {
target:"_blank" target:"_blank"
}); });
wrapper.appendChild(beautifyButton);
wrapper.appendChild(closeButton); wrapper.appendChild(closeButton);
wrapper.appendChild(scriptEditorRamText); wrapper.appendChild(scriptEditorRamText);
wrapper.appendChild(scriptEditorRamCheck); wrapper.appendChild(scriptEditorRamCheck);
@ -243,6 +254,13 @@ $(document).keydown(function(e) {
} }
}); });
function beautifyScript() {
var editor = ace.edit('javascript-editor');
var code = editor.getValue();
code = beautify(code, { indent_size: 4 })
editor.setValue(code);
}
function saveAndCloseScriptEditor() { function saveAndCloseScriptEditor() {
var filename = document.getElementById("script-editor-filename").value; var filename = document.getElementById("script-editor-filename").value;
var editor = ace.edit('javascript-editor'); var editor = ace.edit('javascript-editor');