2015-05-14 04:24:49 +08:00
|
|
|
/* global ace:true */
|
2015-03-10 00:49:11 +08:00
|
|
|
import loadScript from 'discourse/lib/load-script';
|
2016-07-05 23:03:10 +08:00
|
|
|
import { escapeExpression } from 'discourse/lib/utilities';
|
2015-03-10 00:49:11 +08:00
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
|
|
|
mode: 'css',
|
|
|
|
classNames: ['ace-wrapper'],
|
|
|
|
_editor: null,
|
|
|
|
_skipContentChangeEvent: null,
|
|
|
|
|
|
|
|
contentChanged: function() {
|
|
|
|
if (this._editor && !this._skipContentChangeEvent) {
|
|
|
|
this._editor.getSession().setValue(this.get('content'));
|
|
|
|
}
|
|
|
|
}.observes('content'),
|
|
|
|
|
|
|
|
render(buffer) {
|
|
|
|
buffer.push("<div class='ace'>");
|
|
|
|
if (this.get('content')) {
|
2016-06-15 02:31:51 +08:00
|
|
|
buffer.push(escapeExpression(this.get('content')));
|
2015-03-10 00:49:11 +08:00
|
|
|
}
|
|
|
|
buffer.push("</div>");
|
|
|
|
},
|
|
|
|
|
|
|
|
_destroyEditor: function() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.destroy();
|
|
|
|
this._editor = null;
|
|
|
|
}
|
2015-07-16 01:15:05 +08:00
|
|
|
if (this.appEvents) {
|
|
|
|
// xxx: don't run during qunit tests
|
|
|
|
this.appEvents.off('ace:resize', this, this.resize);
|
|
|
|
}
|
2015-03-10 00:49:11 +08:00
|
|
|
}.on('willDestroyElement'),
|
|
|
|
|
2015-07-10 04:25:33 +08:00
|
|
|
resize() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.resize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-10 00:49:11 +08:00
|
|
|
_initEditor: function() {
|
|
|
|
const self = this;
|
|
|
|
|
2015-03-13 01:09:17 +08:00
|
|
|
loadScript("/javascripts/ace/ace.js", { scriptTag: true }).then(function() {
|
2015-05-14 04:24:49 +08:00
|
|
|
ace.require(['ace/ace'], function(loadedAce) {
|
|
|
|
const editor = loadedAce.edit(self.$('.ace')[0]);
|
|
|
|
|
|
|
|
editor.setTheme("ace/theme/chrome");
|
|
|
|
editor.setShowPrintMargin(false);
|
2015-05-15 00:46:32 +08:00
|
|
|
editor.getSession().setMode("ace/mode/" + self.get('mode'));
|
2015-05-14 04:24:49 +08:00
|
|
|
editor.on('change', function() {
|
|
|
|
self._skipContentChangeEvent = true;
|
|
|
|
self.set('content', editor.getSession().getValue());
|
|
|
|
self._skipContentChangeEvent = false;
|
|
|
|
});
|
2015-07-10 04:25:33 +08:00
|
|
|
editor.$blockScrolling = Infinity;
|
2015-05-14 04:24:49 +08:00
|
|
|
|
|
|
|
self.$().data('editor', editor);
|
|
|
|
self._editor = editor;
|
2015-07-16 01:15:05 +08:00
|
|
|
if (self.appEvents) {
|
|
|
|
// xxx: don't run during qunit tests
|
|
|
|
self.appEvents.on('ace:resize', self, self.resize);
|
|
|
|
}
|
2015-03-10 00:49:11 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}.on('didInsertElement')
|
|
|
|
});
|