REFACTORY: Dry up some composer syncing code.

This commit is contained in:
Guo Xiang Tan 2017-11-24 14:57:36 +08:00
parent e0e99d4bbd
commit 44333c5de3
2 changed files with 16 additions and 16 deletions

View File

@ -141,12 +141,7 @@ export default Ember.Component.extend({
$preview.off('scroll');
$input.on('scroll', () => {
if (this.get('shouldBuildScrollMap')) {
this.set('scrollMap', this._buildScrollMap($input, $preview));
this.set('shouldBuildScrollMap', false);
}
Ember.run.throttle(this, this._syncEditorAndPreviewScroll, $input, $preview, this.get('scrollMap'), 20);
this._syncScroll(this._syncEditorAndPreviewScroll, $input, $preview);
});
});
@ -154,16 +149,20 @@ export default Ember.Component.extend({
$input.off('scroll');
$preview.on('scroll', () => {
if (this.get('shouldBuildScrollMap')) {
this.set('scrollMap', this._buildScrollMap($input, $preview));
this.set('shouldBuildScrollMap', false);
}
Ember.run.throttle(this, this._syncPreviewAndEditorScroll, $input, $preview, this.get('scrollMap'), 20);
this._syncScroll(this._syncPreviewAndEditorScroll, $input, $preview);
});
});
},
_syncScroll($callback, $input, $preview) {
if (!this.get('scrollMap') || this.get('shouldBuildScrollMap')) {
this.set('scrollMap', this._buildScrollMap($input, $preview));
this.set('shouldBuildScrollMap', false);
}
Ember.run.throttle(this, $callback, $input, $preview, this.get('scrollMap'), 20);
},
_teardownInputPreviewSync() {
[this.$('.d-editor-input'), this.$('.d-editor-preview')].forEach($element => {
$element.off("mouseenter touchstart");

View File

@ -28,11 +28,12 @@ export function setup(helper) {
helper.registerPlugin(md => {
const injectLineNumber = (tokens, index, options, env, self) => {
let line;
const token = tokens[index];
if (tokens[index].map && tokens[index].level === 0) {
line = tokens[index].map[0];
tokens[index].attrJoin('class', 'preview-sync-line');
tokens[index].attrSet('data-line-number', String(line));
if (token.map && token.level === 0) {
line = token.map[0];
token.attrJoin('class', 'preview-sync-line');
token.attrSet('data-line-number', String(line));
}
return self.renderToken(tokens, index, options, env, self);