From fdfafc951917273c90381bb7b1d77e13641e4467 Mon Sep 17 00:00:00 2001 From: benweet Date: Fri, 17 Nov 2017 08:20:12 +0000 Subject: [PATCH] Fixed text to preview diffs --- src/components/common/PreviewClassApplier.js | 1 - src/services/editorSvc.js | 39 +++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/components/common/PreviewClassApplier.js b/src/components/common/PreviewClassApplier.js index 12019f1d..ea2acd9b 100644 --- a/src/components/common/PreviewClassApplier.js +++ b/src/components/common/PreviewClassApplier.js @@ -31,7 +31,6 @@ export default class PreviewClassApplier { } }; - editorSvc.$on('previewHtml', this.restoreClass); editorSvc.$on('sectionDescWithDiffsList', this.restoreClass); nextTick(() => this.applyClass()); } diff --git a/src/services/editorSvc.js b/src/services/editorSvc.js index 896b1276..350bdea0 100644 --- a/src/services/editorSvc.js +++ b/src/services/editorSvc.js @@ -233,23 +233,36 @@ const editorSvc = Object.assign(new Vue(), editorSvcDiscussions, editorSvcUtils, /** * Make the diff between editor's markdown and preview's html. */ - makeTextToPreviewDiffs: allowDebounce(() => { + makeTextToPreviewDiffs() { if (editorSvc.sectionDescList && editorSvc.sectionDescList !== editorSvc.sectionDescWithDiffsList ) { - editorSvc.sectionDescList - .forEach((sectionDesc) => { - if (!sectionDesc.textToPreviewDiffs) { - sectionDesc.previewText = sectionDesc.previewElt.textContent; - sectionDesc.textToPreviewDiffs = diffMatchPatch.diff_main( - sectionDesc.section.text, sectionDesc.previewText); - } - }); - editorSvc.previewTextWithDiffsList = editorSvc.previewText; - editorSvc.sectionDescWithDiffsList = editorSvc.sectionDescList; - editorSvc.$emit('sectionDescWithDiffsList', editorSvc.sectionDescWithDiffsList); + const makeOne = () => { + let hasOne = false; + const hasMore = editorSvc.sectionDescList + .some((sectionDesc) => { + if (!sectionDesc.textToPreviewDiffs) { + if (hasOne) { + return true; + } + sectionDesc.previewText = sectionDesc.previewElt.textContent; + sectionDesc.textToPreviewDiffs = diffMatchPatch.diff_main( + sectionDesc.section.text, sectionDesc.previewText); + hasOne = true; + } + return false; + }); + if (hasMore) { + setTimeout(() => makeOne(), 10); + } else { + editorSvc.previewTextWithDiffsList = editorSvc.previewText; + editorSvc.sectionDescWithDiffsList = editorSvc.sectionDescList; + editorSvc.$emit('sectionDescWithDiffsList', editorSvc.sectionDescWithDiffsList); + } + }; + makeOne(); } - }, 50), + }, /** * Save editor selection/scroll state into the current file content.