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
|
|
|
|
2017-05-31 15:31:24 -04:00
|
|
|
const LOAD_ASYNC = !Ember.Test;
|
|
|
|
|
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() {
|
2017-05-31 15:31:24 -04:00
|
|
|
if (LOAD_ASYNC && this._editor && !this._skipContentChangeEvent) {
|
2017-04-12 10:52:52 -04:00
|
|
|
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);
|
|
|
|
}
|
2017-05-15 16:10:07 -04:00
|
|
|
|
|
|
|
$(window).off('ace: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
|
|
|
|
2017-05-31 15:31:24 -04:00
|
|
|
if (LOAD_ASYNC) {
|
|
|
|
editor.setTheme("ace/theme/chrome");
|
|
|
|
}
|
2015-05-13 16:24:49 -04:00
|
|
|
editor.setShowPrintMargin(false);
|
2017-04-12 10:52:52 -04:00
|
|
|
editor.setOptions({fontSize: "14px"});
|
2017-05-31 15:31:24 -04:00
|
|
|
if (LOAD_ASYNC) {
|
|
|
|
editor.getSession().setMode("ace/mode/" + this.get('mode'));
|
|
|
|
}
|
2016-11-28 11:29:56 -05:00
|
|
|
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;
|
2017-05-15 16:10:07 -04:00
|
|
|
|
|
|
|
$(window).off('ace:resize').on('ace:resize', ()=>{
|
|
|
|
this.appEvents.trigger('ace:resize');
|
|
|
|
});
|
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
if (this.appEvents) {
|
2015-07-15 13:15:05 -04:00
|
|
|
// xxx: don't run during qunit tests
|
2017-05-15 16:10:07 -04:00
|
|
|
this.appEvents.on('ace:resize', ()=>this.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
|
|
|
}
|
|
|
|
});
|