2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2014-06-06 05:18:03 +04:00
|
|
|
import setScrollClassName from 'ghost/utils/set-scroll-classname';
|
|
|
|
|
|
|
|
var EditorViewMixin = Ember.Mixin.create({
|
|
|
|
// create a hook for jQuery logic that will run after
|
|
|
|
// a view and all child views have been rendered,
|
|
|
|
// since didInsertElement runs only when the view's el
|
|
|
|
// has rendered, and not necessarily all child views.
|
|
|
|
//
|
|
|
|
// http://mavilein.github.io/javascript/2013/08/01/Ember-JS-After-Render-Event/
|
|
|
|
// http://emberjs.com/api/classes/Ember.run.html#method_next
|
|
|
|
scheduleAfterRender: function () {
|
|
|
|
Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
|
|
|
|
}.on('didInsertElement'),
|
|
|
|
|
|
|
|
// all child views will have rendered when this fires
|
|
|
|
afterRenderEvent: function () {
|
2014-09-03 07:42:03 +04:00
|
|
|
var $previewViewPort = this.$('.js-entry-preview-content');
|
2014-06-06 05:18:03 +04:00
|
|
|
|
|
|
|
// cache these elements for use in other methods
|
|
|
|
this.set('$previewViewPort', $previewViewPort);
|
2014-09-03 07:42:03 +04:00
|
|
|
this.set('$previewContent', this.$('.js-rendered-markdown'));
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
$previewViewPort.on('scroll', Ember.run.bind($previewViewPort, setScrollClassName, {
|
2014-09-03 07:42:03 +04:00
|
|
|
target: this.$('.js-entry-preview'),
|
2014-06-06 05:18:03 +04:00
|
|
|
offset: 10
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
removeScrollHandlers: function () {
|
|
|
|
this.get('$previewViewPort').off('scroll');
|
|
|
|
}.on('willDestroyElement'),
|
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
// updated when gh-ed-editor component scrolls
|
|
|
|
editorScrollInfo: null,
|
|
|
|
// updated when markdown is rendered
|
|
|
|
height: null,
|
|
|
|
|
|
|
|
// HTML Preview listens to scrollPosition and updates its scrollTop value
|
|
|
|
// This property receives scrollInfo from the textEditor, and height from the preview pane, and will update the
|
|
|
|
// scrollPosition value such that when either scrolling or typing-at-the-end of the text editor the preview pane
|
|
|
|
// stays in sync
|
|
|
|
scrollPosition: Ember.computed('editorScrollInfo', 'height', function () {
|
|
|
|
if (!this.get('editorScrollInfo')) {
|
2014-06-06 05:18:03 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
var scrollInfo = this.get('editorScrollInfo'),
|
|
|
|
previewHeight = this.get('$previewContent').height() - this.get('$previewViewPort').height(),
|
|
|
|
previewPosition,
|
2014-09-15 03:38:32 +04:00
|
|
|
ratio;
|
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
ratio = previewHeight / scrollInfo.diff;
|
|
|
|
previewPosition = scrollInfo.top * ratio;
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
return previewPosition;
|
2014-06-06 05:18:03 +04:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
export default EditorViewMixin;
|