blob: 710d1d1d86d7a0a491eecd119d1ff81ad68525f9 [file] [log] [blame] [raw]
define(function (require) {
"use strict";
var CodeMirror = require('codemirror');
require('codemirror/mode/clike/clike');
require('codemirror/mode/d/d');
require('codemirror/mode/go/go');
require('codemirror/mode/rust/rust');
function Editor(hub, id, container, lang) {
var self = this;
this.id = id;
var domRoot = container.getElement();
domRoot.html($('#codeEditor').html());
var cmMode;
switch (lang.toLowerCase()) {
default:
cmMode = "text/x-c++src";
break;
case "c":
cmMode = "text/x-c";
break;
case "rust":
cmMode = "text/x-rustsrc";
break;
case "d":
cmMode = "text/x-d";
break;
case "go":
cmMode = "text/x-go";
break;
}
this.editor = CodeMirror.fromTextArea(domRoot.find("textarea")[0], {
lineNumbers: true,
matchBrackets: true,
useCPP: true,
mode: cmMode
});
// With reference to "fix typing '#' in emacs mode"
// https://github.com/mattgodbolt/gcc-explorer/pull/131
this.editor.setOption("extraKeys", {
"Alt-F": false
});
this.editor.on("change", function () {
hub.onEditorChange(self);
});
function resize() {
self.editor.setSize(domRoot.width(), domRoot.height());
self.editor.refresh();
}
container.on('resize', resize);
container.on('open', resize);
container.on('close', function () {
hub.removeEditor(self);
});
container.setTitle(lang + " source");
}
Editor.prototype.getSource = function () {
return this.editor.getValue();
};
Editor.prototype.getId = function () {
return this.id;
};
return Editor;
});