2017-05-08 13:35:42 +03:00
|
|
|
/* global SimpleMDE */
|
2017-08-22 10:53:26 +03:00
|
|
|
import TextArea from '@ember/component/text-area';
|
2018-05-03 19:52:39 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {assign} from '@ember/polyfills';
|
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
2019-01-21 19:06:25 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency';
|
2017-05-08 13:35:42 +03:00
|
|
|
|
|
|
|
export default TextArea.extend({
|
2019-01-21 19:06:25 +03:00
|
|
|
lazyLoader: service(),
|
2017-05-08 13:35:42 +03:00
|
|
|
|
|
|
|
// Public attributes
|
|
|
|
autofocus: false,
|
|
|
|
options: null,
|
|
|
|
value: null,
|
|
|
|
placeholder: '',
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
// Private
|
|
|
|
_editor: null,
|
|
|
|
|
2017-05-08 13:35:42 +03:00
|
|
|
// Closure actions
|
|
|
|
onChange() {},
|
|
|
|
onEditorInit() {},
|
|
|
|
onEditorDestroy() {},
|
|
|
|
|
|
|
|
// default SimpleMDE options, see docs for available config:
|
2018-01-11 01:57:43 +03:00
|
|
|
// https://github.com/sparksuite/simplemde-markdown-editor#configuration
|
2017-05-08 13:35:42 +03:00
|
|
|
defaultOptions: computed(function () {
|
|
|
|
return {
|
2019-03-06 16:53:54 +03:00
|
|
|
autofocus: this.autofocus,
|
2017-05-08 13:35:42 +03:00
|
|
|
indentWithTabs: false,
|
2019-03-06 16:53:54 +03:00
|
|
|
placeholder: this.placeholder,
|
2017-05-08 13:35:42 +03:00
|
|
|
tabSize: 4
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (isEmpty(this.options)) {
|
2017-05-08 13:35:42 +03:00
|
|
|
this.set('options', {});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
// update the editor when the value property changes from the outside
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (isEmpty(this._editor)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare values before forcing a content reset to avoid clobbering
|
|
|
|
// the undo behaviour
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.value !== this._editor.value()) {
|
2018-01-11 20:43:23 +03:00
|
|
|
let cursor = this._editor.codemirror.getDoc().getCursor();
|
2019-03-06 16:53:54 +03:00
|
|
|
this._editor.value(this.value);
|
2018-01-11 20:43:23 +03:00
|
|
|
this._editor.codemirror.getDoc().setCursor(cursor);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-08 13:35:42 +03:00
|
|
|
// instantiate the editor with the contents of value
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2019-01-21 19:06:25 +03:00
|
|
|
this.initSimpleMDE.perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._editor.toTextArea();
|
|
|
|
delete this._editor;
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
initSimpleMDE: task(function* () {
|
|
|
|
yield this.lazyLoader.loadScript('simplemde', 'assets/simplemde/simplemde.js');
|
2017-05-08 13:35:42 +03:00
|
|
|
|
|
|
|
let editorOptions = assign(
|
|
|
|
{element: document.getElementById(this.elementId)},
|
2019-03-06 16:53:54 +03:00
|
|
|
this.defaultOptions,
|
|
|
|
this.options
|
2017-05-08 13:35:42 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// disable spellchecker when testing so that the exterally loaded plugin
|
|
|
|
// doesn't fail
|
2018-05-03 19:52:39 +03:00
|
|
|
if (config.environment === 'test') {
|
2017-05-08 13:35:42 +03:00
|
|
|
editorOptions.spellChecker = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._editor = new SimpleMDE(editorOptions);
|
2019-03-06 16:53:54 +03:00
|
|
|
this._editor.value(this.value || '');
|
2017-05-08 13:35:42 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
this._editor.codemirror.on('change', (instance, changeObj) => {
|
|
|
|
// avoid a "modified x twice in a single render" error that occurs
|
|
|
|
// when the underlying value is completely swapped out
|
|
|
|
if (changeObj.origin !== 'setValue') {
|
|
|
|
this.onChange(this._editor.value());
|
|
|
|
}
|
2017-05-08 13:35:42 +03:00
|
|
|
});
|
|
|
|
|
2017-08-02 10:05:59 +03:00
|
|
|
this._editor.codemirror.on('focus', () => {
|
|
|
|
this.onFocus();
|
|
|
|
});
|
|
|
|
|
|
|
|
this._editor.codemirror.on('blur', () => {
|
|
|
|
this.onBlur();
|
|
|
|
});
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.autofocus) {
|
2017-05-08 13:35:42 +03:00
|
|
|
this._editor.codemirror.execCommand('goDocEnd');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onEditorInit(this._editor);
|
2019-01-21 19:06:25 +03:00
|
|
|
})
|
2017-05-08 13:35:42 +03:00
|
|
|
});
|