2015-04-04 00:50:08 +03:00
|
|
|
/* global CodeMirror */
|
2016-06-30 21:14:25 +03:00
|
|
|
import Component from 'ember-component';
|
2016-07-05 19:30:14 +03:00
|
|
|
import run, {bind, scheduleOnce} from 'ember-runloop';
|
|
|
|
import injectService from 'ember-service/inject';
|
2017-03-03 19:13:22 +03:00
|
|
|
import RSVP from 'rsvp';
|
2016-07-05 19:30:14 +03:00
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
import boundOneWay from 'ghost-admin/utils/bound-one-way';
|
|
|
|
import {InvokeActionMixin} from 'ember-invoke-action';
|
2015-04-04 00:50:08 +03:00
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
const CmEditorComponent = Component.extend(InvokeActionMixin, {
|
2015-04-04 00:50:08 +03:00
|
|
|
classNameBindings: ['isFocused:focused'],
|
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
_value: boundOneWay('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
|
|
|
|
|
2016-07-05 19:30:14 +03:00
|
|
|
lazyLoader: injectService(),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didInsertElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2017-03-03 19:13:22 +03:00
|
|
|
let loader = this.get('lazyLoader');
|
2016-07-05 19:30:14 +03:00
|
|
|
|
2017-03-03 19:13:22 +03:00
|
|
|
RSVP.all([
|
|
|
|
loader.loadStyle('codemirror', 'assets/codemirror/codemirror.css'),
|
|
|
|
loader.loadScript('codemirror', 'assets/codemirror/codemirror.js')
|
|
|
|
]).then(() => {
|
2016-07-05 19:30:14 +03:00
|
|
|
scheduleOnce('afterRender', this, function () {
|
|
|
|
this._initCodeMirror();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_initCodeMirror() {
|
2015-10-28 14:36:45 +03:00
|
|
|
let options = this.getProperties('lineNumbers', 'indentUnit', 'mode', 'theme');
|
2016-06-18 14:44:23 +03:00
|
|
|
let editor = new CodeMirror(this.element, options);
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
editor.getDoc().setValue(this.get('_value'));
|
2015-04-04 00:50:08 +03:00
|
|
|
|
|
|
|
// events
|
2016-06-18 14:44:23 +03:00
|
|
|
editor.on('focus', bind(this, 'set', 'isFocused', true));
|
|
|
|
editor.on('blur', 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 () {
|
2016-06-18 14:44:23 +03:00
|
|
|
this.invokeAction('update', editor.getDoc().getValue());
|
2015-10-15 15:03:26 +03:00
|
|
|
});
|
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);
|
|
|
|
|
2017-02-17 13:15:28 +03:00
|
|
|
// Ensure the editor exists before trying to destroy it. This fixes
|
|
|
|
// an error that occurs if codemirror hasn't finished loading before
|
|
|
|
// the component is destroyed.
|
|
|
|
if (this._editor) {
|
|
|
|
let editor = this._editor.getWrapperElement();
|
|
|
|
editor.parentNode.removeChild(editor);
|
|
|
|
this._editor = null;
|
|
|
|
}
|
2015-04-04 00:50:08 +03:00
|
|
|
}
|
|
|
|
});
|
2016-06-18 14:44:23 +03:00
|
|
|
|
|
|
|
CmEditorComponent.reopenClass({
|
|
|
|
positionalParams: ['value']
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CmEditorComponent;
|