2014-03-02 18:30:35 +04:00
|
|
|
/* global CodeMirror*/
|
2014-06-06 05:18:03 +04:00
|
|
|
import MarkerManager from 'ghost/mixins/marker-manager';
|
|
|
|
import setScrollClassName from 'ghost/utils/set-scroll-classname';
|
|
|
|
|
|
|
|
var onChangeHandler = function (cm, changeObj) {
|
|
|
|
var line,
|
|
|
|
component = cm.component,
|
|
|
|
checkLine = component.checkLine.bind(component),
|
|
|
|
checkMarkers = component.checkMarkers.bind(component);
|
|
|
|
|
|
|
|
// fill array with a range of numbers
|
|
|
|
for (line = changeObj.from.line; line < changeObj.from.line + changeObj.text.length; line += 1) {
|
|
|
|
checkLine(line, changeObj.origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a line which may have had a marker on it?
|
|
|
|
checkMarkers();
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
cm.component.set('value', cm.getDoc().getValue());
|
|
|
|
};
|
|
|
|
|
|
|
|
var onScrollHandler = function (cm) {
|
|
|
|
var scrollInfo = cm.getScrollInfo(),
|
|
|
|
component = cm.component;
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
scrollInfo.codemirror = cm;
|
|
|
|
|
2014-03-02 18:30:35 +04:00
|
|
|
// throttle scroll updates
|
|
|
|
component.throttle = Ember.run.throttle(component, function () {
|
2014-06-06 05:18:03 +04:00
|
|
|
this.set('scrollInfo', scrollInfo);
|
|
|
|
}, 10);
|
2014-03-02 18:30:35 +04:00
|
|
|
};
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
var Codemirror = Ember.TextArea.extend(MarkerManager, {
|
|
|
|
didInsertElement: function () {
|
|
|
|
Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
|
|
|
|
},
|
|
|
|
|
|
|
|
afterRenderEvent: function () {
|
|
|
|
var initMarkers = this.initMarkers.bind(this);
|
|
|
|
|
|
|
|
this.initCodemirror();
|
|
|
|
this.codemirror.eachLine(initMarkers);
|
|
|
|
this.sendAction('action', this);
|
|
|
|
},
|
|
|
|
|
|
|
|
// this needs to be placed on the 'afterRender' queue otherwise CodeMirror gets wonky
|
2014-03-02 18:30:35 +04:00
|
|
|
initCodemirror: function () {
|
|
|
|
// create codemirror
|
2014-06-06 05:18:03 +04:00
|
|
|
var codemirror = CodeMirror.fromTextArea(this.get('element'), {
|
|
|
|
mode: 'gfm',
|
|
|
|
tabMode: 'indent',
|
|
|
|
tabindex: '2',
|
|
|
|
cursorScrollMargin: 10,
|
|
|
|
lineWrapping: true,
|
|
|
|
dragDrop: false,
|
|
|
|
extraKeys: {
|
|
|
|
Home: 'goLineLeft',
|
|
|
|
End: 'goLineRight'
|
|
|
|
}
|
2014-03-02 18:30:35 +04:00
|
|
|
});
|
2014-06-06 05:18:03 +04:00
|
|
|
|
|
|
|
codemirror.component = this; // save reference to this
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
// propagate changes to value property
|
2014-06-06 05:18:03 +04:00
|
|
|
codemirror.on('change', onChangeHandler);
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
// on scroll update scrollPosition property
|
2014-06-06 05:18:03 +04:00
|
|
|
codemirror.on('scroll', onScrollHandler);
|
|
|
|
|
|
|
|
codemirror.on('scroll', Ember.run.bind(Ember.$('.CodeMirror-scroll'), setScrollClassName, {
|
|
|
|
target: Ember.$('.entry-markdown'),
|
|
|
|
offset: 10
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.set('codemirror', codemirror);
|
|
|
|
},
|
|
|
|
|
|
|
|
disableCodeMirror: function () {
|
|
|
|
var codemirror = this.get('codemirror');
|
|
|
|
|
|
|
|
codemirror.setOption('readOnly', 'nocursor');
|
|
|
|
codemirror.off('change', onChangeHandler);
|
|
|
|
},
|
|
|
|
|
|
|
|
enableCodeMirror: function () {
|
|
|
|
var codemirror = this.get('codemirror');
|
|
|
|
|
|
|
|
codemirror.setOption('readOnly', false);
|
|
|
|
|
|
|
|
// clicking the trash button on an image dropzone causes this function to fire.
|
|
|
|
// this line is a hack to prevent multiple event handlers from being attached.
|
|
|
|
codemirror.off('change', onChangeHandler);
|
|
|
|
|
|
|
|
codemirror.on('change', onChangeHandler);
|
|
|
|
},
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
removeThrottle: function () {
|
|
|
|
Ember.run.cancel(this.throttle);
|
2014-03-04 00:18:10 +04:00
|
|
|
}.on('willDestroyElement'),
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
removeCodemirrorHandlers: function () {
|
|
|
|
// not sure if this is needed.
|
2014-06-06 05:18:03 +04:00
|
|
|
var codemirror = this.get('codemirror');
|
|
|
|
codemirror.off('change', onChangeHandler);
|
|
|
|
codemirror.off('scroll');
|
|
|
|
}.on('willDestroyElement'),
|
|
|
|
|
|
|
|
clearMarkerManagerMarkers: function () {
|
|
|
|
this.clearMarkers();
|
2014-03-04 00:18:10 +04:00
|
|
|
}.on('willDestroyElement')
|
|
|
|
});
|
2014-03-02 18:30:35 +04:00
|
|
|
|
2014-05-28 23:26:05 +04:00
|
|
|
export default Codemirror;
|