2015-04-04 00:50:08 +03:00
|
|
|
/* global CodeMirror */
|
|
|
|
import Ember from 'ember';
|
|
|
|
|
2016-06-11 19:52:36 +03:00
|
|
|
const {Component, run} = Ember;
|
2015-04-04 00:50:08 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Component.extend({
|
2015-04-04 00:50:08 +03:00
|
|
|
classNameBindings: ['isFocused:focused'],
|
|
|
|
|
|
|
|
value: '', // make sure a value exists
|
2015-10-28 14:36:45 +03:00
|
|
|
isFocused: false,
|
2015-04-04 00:50:08 +03:00
|
|
|
|
|
|
|
// options for the editor
|
|
|
|
lineNumbers: true,
|
|
|
|
indentUnit: 4,
|
|
|
|
mode: 'htmlmixed',
|
|
|
|
theme: 'xq-light',
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_editor: null, // reference to CodeMirror editor
|
|
|
|
|
|
|
|
didInsertElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
let options = this.getProperties('lineNumbers', 'indentUnit', 'mode', 'theme');
|
|
|
|
let editor = new CodeMirror(this.get('element'), options);
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2015-04-04 00:50:08 +03:00
|
|
|
editor.getDoc().setValue(this.get('value'));
|
|
|
|
|
|
|
|
// events
|
2016-06-11 19:52:36 +03:00
|
|
|
editor.on('focus', run.bind(this, 'set', 'isFocused', true));
|
|
|
|
editor.on('blur', run.bind(this, 'set', 'isFocused', false));
|
2015-10-15 15:03:26 +03:00
|
|
|
editor.on('change', () => {
|
2016-06-11 19:52:36 +03:00
|
|
|
run(this, function () {
|
2015-10-15 15:03:26 +03:00
|
|
|
this.set('value', editor.getDoc().getValue());
|
|
|
|
});
|
2015-04-04 00:50:08 +03:00
|
|
|
});
|
|
|
|
|
2015-11-20 19:40:41 +03:00
|
|
|
this._editor = editor;
|
2015-04-04 00:50:08 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
willDestroyElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
let editor = this._editor.getWrapperElement();
|
2015-04-04 00:50:08 +03:00
|
|
|
editor.parentNode.removeChild(editor);
|
2015-11-20 19:40:41 +03:00
|
|
|
this._editor = null;
|
2015-04-04 00:50:08 +03:00
|
|
|
}
|
|
|
|
});
|