From 582ab8955ab520a8bfe90fd1f8fa57de2267ea46 Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Wed, 22 Feb 2017 17:34:12 -0600 Subject: [PATCH] Fix up multiple editor/compiler problems. See #287, #289, #171, #175 --- static/compiler.js | 4 ++-- static/editor.js | 18 +++++++++++++----- static/main.js | 36 +++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/static/compiler.js b/static/compiler.js index 9500ae95d..7350a6d19 100644 --- a/static/compiler.js +++ b/static/compiler.js @@ -114,7 +114,7 @@ define(function (require) { container.on('resize', this.resize, this); container.on('shown', this.resize, this); container.on('open', function () { - self.eventHub.emit('compilerOpen', self.id); + self.eventHub.emit('compilerOpen', self.id, self.sourceEditorId); self.updateFontScale(); }); this.eventHub.on('editorChange', this.onEditorChange, this); @@ -438,8 +438,8 @@ define(function (require) { Compiler.prototype.currentState = function () { var state = { compiler: this.compiler ? this.compiler.id : "", + source: this.sourceEditorId, options: this.options, - source: this.editor, filters: this.filters.get() // NB must *not* be effective filters }; this.fontScale.addState(state); diff --git a/static/editor.js b/static/editor.js index 1e4e260af..dbb31afd3 100644 --- a/static/editor.js +++ b/static/editor.js @@ -46,6 +46,7 @@ define(function (require) { this.domRoot.html($('#codeEditor').html()); this.eventHub = hub.createEventHub(); this.settings = {}; + this.ourCompilers = {} this.widgetsByCompiler = {}; this.asmByCompiler = {}; @@ -144,6 +145,7 @@ define(function (require) { this.eventHub.on('selectLine', this.onSelectLine, this); this.eventHub.on('settingsChange', this.onSettingsChange, this); + this.eventHub.emit('requestSettings'); // NB a new compilerConfig needs to be created every time; else the state is shared // between all compilers created this way. That leads to some nasty-to-find state @@ -231,7 +233,16 @@ define(function (require) { this.eventHub.emit('colours', this.id, colours, this.settings.colourScheme); }; + Editor.prototype.onCompilerOpen = function (compilerId, editorId) { + if (editorId === this.id) { + // On any compiler open, rebroadcast our state in case they need to know it. + this.maybeEmitChange(true); + this.ourCompilers[compilerId] = true; + } + }; + Editor.prototype.onCompilerClose = function (compilerId) { + if (!this.ourCompilers[compilerId]) return; monaco.editor.setModelMarkers(this.editor.getModel(), compilerId, []); delete this.widgetsByCompiler[compilerId]; delete this.asmByCompiler[compilerId]; @@ -239,16 +250,13 @@ define(function (require) { this.numberUsedLines(); }; - Editor.prototype.onCompilerOpen = function () { - // On any compiler open, rebroadcast our state in case they need to know it. - this.maybeEmitChange(true); - }; - Editor.prototype.onCompiling = function (compilerId) { + if (!this.ourCompilers[compilerId]) return; this.busyCompilers[compilerId] = true; }; Editor.prototype.onCompileResponse = function (compilerId, compiler, result) { + if (!this.ourCompilers[compilerId]) return; this.busyCompilers[compilerId] = false; var output = (result.stdout || []).concat(result.stderr || []); var widgets = _.compact(_.map(output, function (obj) { diff --git a/static/main.js b/static/main.js index f29ed4c97..b27907dc9 100644 --- a/static/main.js +++ b/static/main.js @@ -65,6 +65,28 @@ define(function (require) { var Raven = require('raven-js'); var settings = require('./settings'); + function setupSettings(eventHub) { + var currentSettings = {}; + try { + currentSettings = JSON.parse(window.localStorage.getItem('settings')); + } catch (e) { + } + function onChange(settings) { + currentSettings = settings; + try { + window.localStorage.setItem('settings', JSON.stringify(settings)); + } catch (e) { + } + eventHub.emit('settingsChange', settings); + } + + eventHub.on('requestSettings', function () { + eventHub.emit('settingsChange', currentSettings); + }); + + settings($('#settings'), currentSettings, onChange); + } + function start() { analytics.initialise(); sharing.initialise(); @@ -152,19 +174,7 @@ define(function (require) { new clipboard('.btn.clippy'); - settings($('#settings'), function () { - try { - return JSON.parse(window.localStorage.getItem('settings')); - } catch (e) { - return {}; - } - }(), function (settings) { - try { - window.localStorage.setItem('settings', JSON.stringify(settings)); - } catch (e) { - } - layout.eventHub.emit('settingsChange', settings); - }); + setupSettings(layout.eventHub); sharing.initShareButton($('#share'), layout);