2015-03-09 12:49:11 -04:00
|
|
|
import loadScript from 'discourse/lib/load-script';
|
2016-11-28 11:29:56 -05:00
|
|
|
import { observes } from 'ember-addons/ember-computed-decorators';
|
2015-03-09 12:49:11 -04:00
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
export default Ember.Component.extend({
|
2015-03-09 12:49:11 -04:00
|
|
|
mode: 'css',
|
|
|
|
classNames: ['ace-wrapper'],
|
|
|
|
_editor: null,
|
|
|
|
_skipContentChangeEvent: null,
|
|
|
|
|
2017-04-13 16:21:46 -04:00
|
|
|
@observes('editorId')
|
|
|
|
editorIdChanged() {
|
|
|
|
if (this.get('autofocus')) {
|
|
|
|
this.send('focus');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
@observes('content')
|
|
|
|
contentChanged() {
|
2015-03-09 12:49:11 -04:00
|
|
|
if (this._editor && !this._skipContentChangeEvent) {
|
|
|
|
this._editor.getSession().setValue(this.get('content'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
@observes('mode')
|
|
|
|
modeChanged() {
|
|
|
|
if (this._editor && !this._skipContentChangeEvent) {
|
|
|
|
this._editor.getSession().setMode("ace/mode/" + this.get('mode'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-09 12:49:11 -04:00
|
|
|
_destroyEditor: function() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.destroy();
|
|
|
|
this._editor = null;
|
|
|
|
}
|
2015-07-15 13:15:05 -04:00
|
|
|
if (this.appEvents) {
|
|
|
|
// xxx: don't run during qunit tests
|
|
|
|
this.appEvents.off('ace:resize', this, this.resize);
|
|
|
|
}
|
2015-03-09 12:49:11 -04:00
|
|
|
}.on('willDestroyElement'),
|
|
|
|
|
2015-07-09 16:25:33 -04:00
|
|
|
resize() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.resize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
didInsertElement() {
|
|
|
|
this._super();
|
2015-03-09 12:49:11 -04:00
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
loadScript("/javascripts/ace/ace.js", { scriptTag: true }).then(() => {
|
|
|
|
window.ace.require(['ace/ace'], loadedAce => {
|
2016-12-19 11:19:10 -05:00
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) { return; }
|
2016-11-28 11:29:56 -05:00
|
|
|
const editor = loadedAce.edit(this.$('.ace')[0]);
|
2015-05-13 16:24:49 -04:00
|
|
|
|
|
|
|
editor.setTheme("ace/theme/chrome");
|
|
|
|
editor.setShowPrintMargin(false);
|
2017-04-12 10:52:52 -04:00
|
|
|
editor.setOptions({fontSize: "14px"});
|
2016-11-28 11:29:56 -05:00
|
|
|
editor.getSession().setMode("ace/mode/" + this.get('mode'));
|
|
|
|
editor.on('change', () => {
|
|
|
|
this._skipContentChangeEvent = true;
|
|
|
|
this.set('content', editor.getSession().getValue());
|
|
|
|
this._skipContentChangeEvent = false;
|
2015-05-13 16:24:49 -04:00
|
|
|
});
|
2015-07-09 16:25:33 -04:00
|
|
|
editor.$blockScrolling = Infinity;
|
2017-04-18 15:43:22 -04:00
|
|
|
editor.renderer.setScrollMargin(10,10);
|
2015-05-13 16:24:49 -04:00
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
this.$().data('editor', editor);
|
|
|
|
this._editor = editor;
|
|
|
|
if (this.appEvents) {
|
2015-07-15 13:15:05 -04:00
|
|
|
// xxx: don't run during qunit tests
|
2016-11-28 11:29:56 -05:00
|
|
|
this.appEvents.on('ace:resize', self, self.resize);
|
2015-07-15 13:15:05 -04:00
|
|
|
}
|
2017-04-13 16:21:46 -04:00
|
|
|
|
|
|
|
if (this.get("autofocus")) {
|
|
|
|
this.send("focus");
|
|
|
|
}
|
2015-03-09 12:49:11 -04:00
|
|
|
});
|
|
|
|
});
|
2017-04-13 16:21:46 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
focus() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.focus();
|
|
|
|
this._editor.navigateFileEnd();
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 11:29:56 -05:00
|
|
|
}
|
|
|
|
});
|