<commit_before>import React from 'react';
import AceEditor from 'react-ace';
import brace from 'brace';
import EditorActionCreators from '../actions/EditorActionCreators';
import EditorStore from '../stores/EditorStore';
import AlertActions from '../actions/AlertActions';
import EditorToolbar from './EditorToolbar';
import Mousetrap from 'mousetrap';
import smalltalk from 'smalltalk';
import _ from 'lodash';
import ConsoleOutput from './ConsoleOutput';
import RobotActions from '../actions/RobotActions';
import Ansible from '../utils/Ansible';
import {Panel} from 'react-bootstrap';
import { EditorButton } from './EditorClasses';
import ace from 'brace';
import 'brace/ext/language_tools';
import 'brace/ext/searchbox';
import 'brace/mode/python';
// React-ace themes
import 'brace/theme/monokai';
import 'brace/theme/github';
import 'brace/theme/tomorrow';
import 'brace/theme/kuroir';
import 'brace/theme/twilight';
import 'brace/theme/xcode';
import 'brace/theme/textmate';
import 'brace/theme/solarized_dark';
import 'brace/theme/solarized_light';
import 'brace/theme/terminal';
import {remote} from 'electron';
let langtools = ace.acequire('ace/ext/language_tools');
let storage = remote.require('electron-json-storage');
let dialog = remote.dialog;
let currentWindow = remote.getCurrentWindow();

export default React.createClass({
  getInitialState() {
    return {
      showConsole: false,
      filepath: null,
      latestSaveCode: '',
      editorCode: '',
      editorTheme: 'github'
    };
  },
  componentDidMount() {

    // If there are unsaved changes and the user tries to close Dawn,
    // check if they want to save their changes first.
    window.onbeforeunload = (e) => {
      if (this.hasUnsavedChanges()) {
        e.returnValue = false;
        dialog.showMessageBox({
          type: 'warning',
          buttons: ['Save and exit', 'Quit without saving', 'Cancel'],
          title: 'You have unsaved changes!',
          message: 'What do you want to do with your unsaved changes?'
        }, (res)=>{
          // 'res' is an integer corrseponding to index in button list above.
          if (res === 0) {
            this.saveFile(()=>{
              window.onbeforeunload = null;
              currentWindow.close();
            });
          } else if (res === 1) {
            window.onbeforeunload = null;
            currentWindow.close();
          } else {
            console.log('Exit canceled.');
          }
        });
      }
    };

    this.refs.CodeEditor.editor.setOption('enableBasicAutocompletion', true);

    Mousetrap.prototype.stopCallback = function(e, element, combo) {
      return false; // Always respond to keyboard combos
    };

    Mousetrap.bind(['mod+s'], (e)=>{
      if (e.preventDefault) {
        e.preventDefault();
      }
      this.saveFile();
    });

    // If possible, reopen the last opened file.
    let lastFile = localStorage.getItem('lastFile');
    if (lastFile !== null) {
      EditorActionCreators.readFilepath(lastFile);
    }

    storage.has('editorTheme').then((hasKey)=>{
      if (hasKey) {
        storage.get('editorTheme').then((data)=>{
          this.setState({
            editorTheme: data.theme
          });
        });
      } else {
        storage.set('editorTheme', {
          theme: 'github'
        }, (err)=>{
          if (err) throw err;
        });
      }
    });

    EditorStore.on('change', this.updateEditorData);
  },
  componentWillUnmount() {
    Mousetrap.unbind(['mod+s']);
    EditorStore.removeListener('change', this.updateEditorData);
  },
  updateEditorData() {
    this.setState({
      filepath: EditorStore.getFilepath(),
      latestSaveCode: EditorStore.getLatestSaveCode(),
      editorCode: EditorStore.getEditorCode()
    });
  },
  openFile() {
    if (this.hasUnsavedChanges()) {
      smalltalk.confirm(
        'Are you sure?',
        'You have unsaved changes, opening a new file will discard them!'
      ).then(()=>{
        EditorActionCreators.openFile();
      }, ()=>{
        console.log('Canceled');
      });
    } else {
      EditorActionCreators.openFile();
    }
  },
  saveFile(callback) {
    EditorActionCreators.saveFile(
      this.state.filepath, this.state.editorCode, callback);
  },
  saveAsFile() {
    EditorActionCreators.saveFile(null, this.state.editorCode);
  },
  createNewFile() {
    if (this.hasUnsavedChanges()) {
      smalltalk.confirm(
        'Are you sure?',
        'You have unsaved changes, creating a new file will discard them!'
      ).then(()=>{
        EditorActionCreators.createNewFile();
      }, ()=>{
        console.log('Canceled');
      });
    } else {
      EditorActionCreators.createNewFile();
    }
  },
  editorUpdate(newVal) {
    EditorActionCreators.editorUpdate(newVal);
  },
  correctText(text) {
    // Removes non-ASCII characters from text.<commit_msg>Use electron dialogs for unsaved changes check when opening/creating new file<commit_after>import React from 'react';
import AceEditor from 'react-ace';
import brace from 'brace';
import EditorActionCreators from '../actions/EditorActionCreators';
import EditorStore from '../stores/EditorStore';
import AlertActions from '../actions/AlertActions';
import EditorToolbar from './EditorToolbar';
import Mousetrap from 'mousetrap';
import _ from 'lodash';
import ConsoleOutput from './ConsoleOutput';
import RobotActions from '../actions/RobotActions';
import Ansible from '../utils/Ansible';
import {Panel} from 'react-bootstrap';
import { EditorButton } from './EditorClasses';
import ace from 'brace';
import 'brace/ext/language_tools';
import 'brace/ext/searchbox';
import 'brace/mode/python';
// React-ace themes
import 'brace/theme/monokai';
import 'brace/theme/github';
import 'brace/theme/tomorrow';
import 'brace/theme/kuroir';
import 'brace/theme/twilight';
import 'brace/theme/xcode';
import 'brace/theme/textmate';
import 'brace/theme/solarized_dark';
import 'brace/theme/solarized_light';
import 'brace/theme/terminal';
import {remote} from 'electron';
let langtools = ace.acequire('ace/ext/language_tools');
let storage = remote.require('electron-json-storage');
let dialog = remote.dialog;
let currentWindow = remote.getCurrentWindow();

export default React.createClass({
  getInitialState() {
    return {
      showConsole: false,
      filepath: null,
      latestSaveCode: '',
      editorCode: '',
      editorTheme: 'github'
    };
  },
  componentDidMount() {

    // If there are unsaved changes and the user tries to close Dawn,
    // check if they want to save their changes first.
    window.onbeforeunload = (e) => {
      if (this.hasUnsavedChanges()) {
        e.returnValue = false;
        dialog.showMessageBox({
          type: 'warning',
          buttons: ['Save and exit', 'Quit without saving', 'Cancel exit'],
          title: 'You have unsaved changes!',
          message: 'You are trying to exit Dawn, but you have unsaved changes. What do you want to do with your unsaved changes?'
        }, (res)=>{
          // 'res' is an integer corrseponding to index in button list above.
          if (res === 0) {
            this.saveFile(()=>{
              window.onbeforeunload = null;
              currentWindow.close();
            });
          } else if (res === 1) {
            window.onbeforeunload = null;
            currentWindow.close();
          } else {
            console.log('Exit canceled.');
          }
        });
      }
    };

    this.refs.CodeEditor.editor.setOption('enableBasicAutocompletion', true);

    Mousetrap.prototype.stopCallback = function(e, element, combo) {
      return false; // Always respond to keyboard combos
    };

    Mousetrap.bind(['mod+s'], (e)=>{
      if (e.preventDefault) {
        e.preventDefault();
      }
      this.saveFile();
    });

    // If possible, reopen the last opened file.
    let lastFile = localStorage.getItem('lastFile');
    if (lastFile !== null) {
      EditorActionCreators.readFilepath(lastFile);
    }

    storage.has('editorTheme').then((hasKey)=>{
      if (hasKey) {
        storage.get('editorTheme').then((data)=>{
          this.setState({
            editorTheme: data.theme
          });
        });
      } else {
        storage.set('editorTheme', {
          theme: 'github'
        }, (err)=>{
          if (err) throw err;
        });
      }
    });

    EditorStore.on('change', this.updateEditorData);
  },
  componentWillUnmount() {
    Mousetrap.unbind(['mod+s']);
    EditorStore.removeListener('change', this.updateEditorData);
  },
  updateEditorData() {
    this.setState({
      filepath: EditorStore.getFilepath(),
      latestSaveCode: EditorStore.getLatestSaveCode(),
      editorCode: EditorStore.getEditorCode()
    });
  },
  openFile() {
    if (this.hasUnsavedChanges()) {
      dialog.showMessageBox({
        type: 'warning',
        buttons: ['Save and open', 'Discard and open', 'Cancel action'],
        title: 'You have unsaved changes!',
        message: 'You are trying to open a new file, but you have unsaved changes to your current one. What do you want to do?'
      }, (res)=>{
        // 'res' is an integer corrseponding to index in button list above.
        if (res === 0) {
          this.saveFile(()=>{
            process.nextTick(()=>{
              EditorActionCreators.openFile();
            });
          });
        } else if (res === 1) {
          EditorActionCreators.openFile();
        } else {
          console.log('File open canceled.');
        }
      });
    } else {
      EditorActionCreators.openFile();
    }
  },
  saveFile(callback) {
    EditorActionCreators.saveFile(
      this.state.filepath, this.state.editorCode, callback);
  },
  saveAsFile() {
    EditorActionCreators.saveFile(null, this.state.editorCode);
  },
  createNewFile() {
    if (this.hasUnsavedChanges()) {
      dialog.showMessageBox({
        type: 'warning',
        buttons: ['Save and create', 'Discard and create', 'Cancel action'],
        title: 'You have unsaved changes!',
        message: 'You are trying to create a new file, but you have unsaved changes to your current one. What do you want to do?'
      }, (res)=>{
        // 'res' is an integer corrseponding to index in button list above.
        if (res === 0) {
          this.saveFile(()=>{
            process.nextTick(()=>{
              EditorActionCreators.createNewFile();
            });
          });
        } else if (res === 1) {
          EditorActionCreators.createNewFile();
        } else {
          console.log('New file creation canceled.');
        }
      });
    } else {
      EditorActionCreators.createNewFile();
    }
  },
  editorUpdate(newVal) {
    EditorActionCreators.editorUpdate(newVal);
  },
  correctText(text) {
    // Removes non-ASCII characters from text.
{'fraction_non_alphanumeric': 0.13628367743727432, 'fraction_numerical': 0.0007406721599851866, 'mean_word_length': 2.661694915254237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '332607', 'n_tokens_mistral': 3126, 'n_tokens_neox': 3080, 'n_words': 776}