From 44333c5de324c3896363a2ed1be2b673529f1da6 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 24 Nov 2017 14:57:36 +0800 Subject: [PATCH] REFACTORY: Dry up some composer syncing code. --- .../components/composer-editor.js.es6 | 23 +++++++++---------- .../inject-line-number.js.es6 | 9 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6 index d2cda54ab69..951e2140eb4 100644 --- a/app/assets/javascripts/discourse/components/composer-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6 @@ -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"); diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/inject-line-number.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/inject-line-number.js.es6 index 30e4fa84329..eaad9f61b6a 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/inject-line-number.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/inject-line-number.js.es6 @@ -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);