HotS Data Editor

by MrPolywhirl

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-beautify.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.8/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/css/tooltipster.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/js/jquery.tooltipster.min.js"></script>
<div class="title">Hero Data Editor</div>
<textarea name="editor">{ "fname" : "foo", "lname": "bar" }</textarea>
<div id="editor"></div>
<div class="commands">
<button class="btn-import fa fa-upload tooltip" title="Import"></button>
  <button class="btn-export fa fa-download tooltip" title="Export"></button>
  <button class="btn-preview fa fa-eye tooltip" title="Preview"></button>
  <button class="btn-format fa fa-sitemap tooltip" title="Format"></button>
</div>
<input type="file" id="file-explorer" />
<div id="dialog-export" title="Save As?">
  <p><span class="ui-icon ui-icon-info"></span> Please choose an export type</p>
</div>

CSS

body {
  background: #DDD;
}
.title {
  font-size: 2em;
  font-weight: bolder;
  margin: 0.75em;
  text-align: center;
  color: #007;
}
#editor {
  width: 90%;
  height: 75vh;
  margin: 0 auto;
}
.commands {
  text-align: center;
  margin: 0.5em auto;
}
.commands button {
  display: inline-block;
  padding: 3px;
  border: thin solid #CCC; 
  width: 3em;
  margin: 4px;
  font-size: 1.33em;
  color: #007;
  background: #EEE;
  font-family: FontAwesome;
}
.commands button:hover {
  color: #44A;
  background: #FFF;
  cursor: pointer;
}
#file-explorer {
  display: none;
}
.btn-preview {
  display: none !important;
}
#dialog-export {
  display: none;
}

JavaScript

var $exportDialog = null;
// Setup
var beautify = ace.require('ace/ext/beautify');
var editor = ace.edit('editor');
var $textarea = $('textarea[name="editor"]').hide();
var formatOpts = {
	indent_size : 2
};
var exportDialogConfig = {
  resizable: false,
  height: 'auto',
  width: 400,
  modal: true,
  buttons: {
    'JSON' : saveToFileAsJson,
    'Wiki' : saveToFileAsWiki
  }
};
editor.setTheme('ace/theme/monokai');
editor.getSession().setMode('ace/mode/json');
editor.getSession().setTabSize(4);
editor.getSession().setUseSoftTabs(true);
editor.$blockScrolling = Infinity;

// Init
$('.tooltip').tooltipster(); // Enable tooltips
syncEditor();
setTimeout(formatCode, 1000); // Format sample after 1 second.

// Listeners
editor.getSession().on('change', commitChanges);
$('.btn-import').on('click', handleImport);
$('.btn-export').on('click', handleExport);
$('.btn-preview').on('click', handlePreview);
$('.btn-format').on('click', handleFormat);
$('#file-explorer').on('change', doOpenFile);

// Event Handlers
function handleImport(e) {
	$('#file-explorer').trigger('click');
  return false;
}
function handleExport(e) {
	commitChanges();
  if ($exportDialog == null) {
     $exportDialog = $('#dialog-export').dialog(exportDialogConfig);
  } else {
  	$exportDialog.dialog('open');
  }
}
function handlePreview(e) {
	showPreview();
}
function handleFormat(e) {
	formatCode();
}

// Dialog Callbacks
function saveToFileAsJson() {
	saveToFile($textarea.val(), 'data.json', { type:'text/json' });
  $exportDialog.dialog('close');
}
function saveToFileAsWiki() {
	var wikiData = toWikiData(JSON.parse($textarea.val()));
	saveToFile(wikiData, 'data.wiki', { type:'text/plain' });
  $exportDialog.dialog('close');
}

// Controller Methods
function syncEditor() {
	editor.getSession().setValue($textarea.val());
}
function commitChanges() {
	$textarea.val(editor.getSession().getValue());
}
function formatCode() {
  //beautify.beautify(editor.getSession());
  var session =...