diff --git a/src/app.js b/src/app.js index 7f4850614d..dd63f92233 100644 --- a/src/app.js +++ b/src/app.js @@ -277,6 +277,8 @@ var run = function () { return false }) + editor.event.register('sessionSwitched', updateFiles) + function switchToFile (file) { editor.setCacheFile(file) updateFiles() @@ -573,7 +575,9 @@ var run = function () { compileTimeout = window.setTimeout(runCompiler, 300) } - editor.onChangeSetup(editorOnChange) + editor.event.register('contentChanged', editorOnChange) + // in order to save the file when switching + editor.event.register('sessionSwitched', editorOnChange) $('#compile').click(function () { runCompiler() diff --git a/src/app/debugger.js b/src/app/debugger.js index 6a604b9e9b..923facd599 100644 --- a/src/app/debugger.js +++ b/src/app/debugger.js @@ -26,10 +26,9 @@ function Debugger (id, editor, compiler, executionContextEvent, switchToFile, of self.removeCurrentMarker() }) - this.editor.onChangeSetup(function () { - if (arguments.length > 0) { // if arguments.length === 0 this is a session change, we don't want to stop debugging in that case - self.debugger.unLoad() - } + // unload if a file has changed (but not if tabs were switched) + editor.event.register('contentChanged', function () { + self.debugger.unLoad() }) // register selected code item, highlight the corresponding source location diff --git a/src/app/editor.js b/src/app/editor.js index ae1f6afea2..7db3a0fb51 100644 --- a/src/app/editor.js +++ b/src/app/editor.js @@ -1,6 +1,7 @@ /* global FileReader */ 'use strict' +var EventManager = require('../lib/eventManager') var examples = require('./example-contracts') var ace = require('brace') @@ -11,6 +12,8 @@ function Editor (doNotLoadStorage, storage) { var editor = ace.edit('input') document.getElementById('input').editor = editor // required to access the editor during tests + var event = new EventManager() + this.event = event var sessions = {} var sourceAnnotations = [] @@ -146,14 +149,6 @@ function Editor (doNotLoadStorage, storage) { editor.getSession().setAnnotations(sourceAnnotations) } - this.onChangeSetup = function (onChange) { - editor.getSession().on('change', onChange) - editor.on('changeSession', function () { - editor.getSession().on('change', onChange) - onChange() - }) - } - this.handleErrorClick = function (errLine, errCol) { editor.focus() editor.gotoLine(errLine + 1, errCol - 1, true) @@ -169,6 +164,13 @@ function Editor (doNotLoadStorage, storage) { } // Do setup on initialisation here + editor.on('changeSession', function () { + event.trigger('sessionSwitched', []) + + editor.getSession().on('change', function () { + event.trigger('contentChanged', []) + }) + }) // Unmap ctrl-t & ctrl-f editor.commands.bindKeys({ 'ctrl-t': null })