<commit_before>$(function () {
  var compiler = require('squiffy/compiler.js');
  var shell = require('shell');
  var path = require('path');
  var remote = require('remote');
  var dialog = remote.require('dialog');
  var fs = require('fs');

  window.menuClick = window.menuClick || {};

  var filename = null;

  var compile = function (input) {
    if (input.zip) {
      // TODO: Generate zip files using local compiler.js

      var url = 'http://squiffy.textadventures.co.uk/zip';
      // Using XMLHttpRequest here as jQuery doesn't support blob downloads
      var xhr = new XMLHttpRequest();
      xhr.open('POST', url, true);
      xhr.responseType = 'blob';
       
      xhr.onload = function(e) {
        if (this.status == 200) {
          input.success(this.response);
        }
        else {
          input.fail(this.response);
        }
      };
       
      xhr.send(input.data);
      return;
    }

    var js = compiler.getJs(input.data);
    if (js.indexOf('Failed') === 0) {
        input.fail(js);
        return;
    }
    input.success(js);
  };

  var updateTitle = function () {
    if (!filename) {
      document.title = 'New file';
    }
    else{
      document.title = path.basename(filename);
    }
  };

  window.menuClick.openFile = function () {
    var result = dialog.showOpenDialog({
      filters: [
        { name: 'Squiffy scripts', extensions: ['squiffy'] }
      ]
    });
    if (!result) return;
    filename = result[0];
    var data = fs.readFileSync(filename).toString();
    updateTitle();
    $('#squiffy-editor').squiffyEditor('load', data);
  };

  var init = function (data) {
    $('#squiffy-editor').squiffyEditor({
      data: data,
      compile: compile,
      open: function () {
        window.menuClick.openFile();
      },
      save: function (title) {
        bootbox.alert('Save not implemented in demo');
        $('#squiffy-editor').squiffyEditor('setInfo', 'Not saved');
      },
      autoSave: function (title) {
        console.log('Auto save');
      },
      preview: function () {
        bootbox.alert('Preview not implemented in demo');
      },
      publish: function () {
        bootbox.alert('Publish not implemented in demo');
      },
      storageKey: 'squiffy',
      updateTitle: function () {},
    });
  };

  var saved = localStorage['squiffy'];
  if (saved) {
    init(localStorage['squiffy']);
  }
  else {
    $.get('example.squiffy', init);
  }

  $('#squiffy-editor').on('click', 'a.external-link, #output-container a[href]', function (e) {
    shell.openExternal($(this).attr('href'));
    e.preventDefault();
  });
});<commit_msg>Load previous file when starting<commit_after>$(function () {
  var compiler = require('squiffy/compiler.js');
  var shell = require('shell');
  var path = require('path');
  var remote = require('remote');
  var dialog = remote.require('dialog');
  var fs = require('fs');

  window.menuClick = window.menuClick || {};

  var filename = null;

  var compile = function (input) {
    if (input.zip) {
      // TODO: Generate zip files using local compiler.js

      var url = 'http://squiffy.textadventures.co.uk/zip';
      // Using XMLHttpRequest here as jQuery doesn't support blob downloads
      var xhr = new XMLHttpRequest();
      xhr.open('POST', url, true);
      xhr.responseType = 'blob';
       
      xhr.onload = function(e) {
        if (this.status == 200) {
          input.success(this.response);
        }
        else {
          input.fail(this.response);
        }
      };
       
      xhr.send(input.data);
      return;
    }

    var js = compiler.getJs(input.data);
    if (js.indexOf('Failed') === 0) {
        input.fail(js);
        return;
    }
    input.success(js);
  };

  var setFilename = function (newFilename, noStore) {
    filename = newFilename;
    if (!noStore) localStorage['filename'] = filename;
    if (!filename) {
      document.title = 'New file';
    }
    else{
      document.title = path.basename(filename);
    }
  };

  setFilename(null, true);

  var loadFile = function (file) {
    var data;
    try {
      data = fs.readFileSync(file).toString();
    }
    catch (e) {
      return null;
    }
    setFilename(file);
    return data; 
  }

  window.menuClick.openFile = function () {
    var result = dialog.showOpenDialog({
      filters: [
        { name: 'Squiffy scripts', extensions: ['squiffy'] }
      ]
    });
    if (!result) return;
    var data = loadFile(result[0]);
    if (data === null) {
      dialog.showMessageBox({
        type: 'warning',
        message: 'Failed to load file',
        buttons: ['OK']
      });
    }
    $('#squiffy-editor').squiffyEditor('load', data);
  };

  var init = function (data) {
    $('#squiffy-editor').squiffyEditor({
      data: data,
      compile: compile,
      open: function () {
        window.menuClick.openFile();
      },
      save: function (title) {
        bootbox.alert('Save not implemented in demo');
        $('#squiffy-editor').squiffyEditor('setInfo', 'Not saved');
      },
      autoSave: function () {},
      preview: function () {
        bootbox.alert('Preview not implemented in demo');
      },
      publish: function () {
        bootbox.alert('Publish not implemented in demo');
      },
      updateTitle: function () {},
    });
  };

  var previousFilename = localStorage['filename'];
  if (previousFilename) {
    var data = loadFile(previousFilename);
    if (data) {
      init(data);
    }
    else {
      previousFilename = null;
      localStorage['filename'] = null;
    }
  }
  
  if (!previousFilename) {
    $.get('example.squiffy', init);
  }

  $('#squiffy-editor').on('click', 'a.external-link, #output-container a[href]', function (e) {
    shell.openExternal($(this).attr('href'));
    e.preventDefault();
  });
});
{'fraction_non_alphanumeric': 0.1594153052450559, 'fraction_numerical': 0.0017196904557179708, 'mean_word_length': 2.3813953488372093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '440783', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1775, 'n_words': 392}