2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-06-13 20:53:43 +03:00
|
|
|
import setScrollClassName from 'ghost/utils/set-scroll-classname';
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Component, computed, run} = Ember;
|
|
|
|
const {equal} = computed;
|
|
|
|
|
|
|
|
export default Component.extend({
|
2014-05-31 22:32:22 +04:00
|
|
|
tagName: 'section',
|
2015-06-13 20:53:43 +03:00
|
|
|
classNames: ['gh-view'],
|
2015-06-13 17:34:09 +03:00
|
|
|
|
2015-06-13 20:53:43 +03:00
|
|
|
// updated when gh-ed-editor component scrolls
|
|
|
|
editorScrollInfo: null,
|
|
|
|
// updated when markdown is rendered
|
|
|
|
height: null,
|
2015-10-28 14:36:45 +03:00
|
|
|
activeTab: 'markdown',
|
|
|
|
|
|
|
|
markdownActive: equal('activeTab', 'markdown'),
|
|
|
|
previewActive: equal('activeTab', 'preview'),
|
2015-06-13 20:53:43 +03:00
|
|
|
|
|
|
|
// 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
|
2015-10-28 14:36:45 +03:00
|
|
|
scrollPosition: computed('editorScrollInfo', 'height', function () {
|
|
|
|
let scrollInfo = this.get('editorScrollInfo');
|
|
|
|
let $previewContent = this.get('$previewContent');
|
|
|
|
let $previewViewPort = this.get('$previewViewPort');
|
|
|
|
|
|
|
|
if (!scrollInfo || !$previewContent || !$previewViewPort) {
|
2015-06-13 20:53:43 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
let previewHeight = $previewContent.height() - $previewViewPort.height();
|
|
|
|
let previewPosition, ratio;
|
2015-06-13 20:53:43 +03:00
|
|
|
|
|
|
|
ratio = previewHeight / scrollInfo.diff;
|
|
|
|
previewPosition = scrollInfo.top * ratio;
|
|
|
|
|
|
|
|
return previewPosition;
|
2015-09-01 14:22:10 +03:00
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
scheduleAfterRender() {
|
|
|
|
run.scheduleOnce('afterRender', this, this.afterRenderEvent);
|
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
2015-10-28 14:36:45 +03:00
|
|
|
this.scheduleAfterRender();
|
|
|
|
},
|
|
|
|
|
|
|
|
afterRenderEvent() {
|
|
|
|
let $previewViewPort = this.$('.js-entry-preview-content');
|
|
|
|
|
|
|
|
// cache these elements for use in other methods
|
|
|
|
this.set('$previewViewPort', $previewViewPort);
|
|
|
|
this.set('$previewContent', this.$('.js-rendered-markdown'));
|
|
|
|
|
|
|
|
$previewViewPort.on('scroll', run.bind($previewViewPort, setScrollClassName, {
|
|
|
|
target: this.$('.js-entry-preview'),
|
|
|
|
offset: 10
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
2015-10-28 14:36:45 +03:00
|
|
|
// removes scroll handlers from the view
|
|
|
|
this.get('$previewViewPort').off('scroll');
|
|
|
|
},
|
2015-09-01 14:22:10 +03:00
|
|
|
|
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
selectTab(tab) {
|
2015-09-01 14:22:10 +03:00
|
|
|
this.set('activeTab', tab);
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 10:02:21 +04:00
|
|
|
});
|