This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch next
in repository https://gitbox.apache.org/repos/asf/incubator-echarts-doc.git


The following commit(s) were added to refs/heads/next by this push:
     new f608db8  fix code update in live example after diff algorithm changed
f608db8 is described below

commit f608db8d19fda1e32b1e09a36da3f935199f5bba
Author: pissang <bm2736...@gmail.com>
AuthorDate: Thu Dec 31 12:50:14 2020 +0800

    fix code update in live example after diff algorithm changed
---
 src/components/LiveExample.vue | 43 +++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/src/components/LiveExample.vue b/src/components/LiveExample.vue
index 72d23c1..9beba87 100644
--- a/src/components/LiveExample.vue
+++ b/src/components/LiveExample.vue
@@ -71,7 +71,7 @@ import 'codemirror/theme/dracula.css';
 // import 'codemirror/mode/javascript/javascript.js'
 import beautifier from 'js-beautify';
 import throttle from 'lodash.throttle';
-import arrayDiff from 'zrender/esm/core/arrayDiff';
+import arrayDiff from 'zrender/lib/core/arrayDiff';
 import scrollIntoView from 'scroll-into-view';
 import {ECHARTS_LIB} from '../config';
 
@@ -93,27 +93,32 @@ function fetchECharts() {
 function diffUpdateCode(oldCode, newCode, cmInstance) {
     const oldLines = oldCode.split(/\n/);
     const newLines = newCode.split(/\n/);
-    const result = arrayDiff(oldLines, newLines);
+    const diff = arrayDiff(oldLines, newLines);
 
     const changedLines = [];
-    const len = result.length;
-
-    for (let i = len - 1; i >= 0; i--) {
-        const item = result[i];
-        if (item.cmd === '-') {
-            cmInstance.replaceRange(
-                '', {line: item.idx, ch: 0}, {line: item.idx + 1, ch: 0}
-            );
+
+    // Remove lines from bottom to top so the line number won't be changed.
+    for (let i = diff.length - 1; i >= 0; i--) {
+        const item = diff[i];
+        if (item.removed) {
+            for (let k = item.count - 1; k >= 0; k--) {
+                const idx = item.indices[k];
+                cmInstance.replaceRange(
+                    '', {line: idx, ch: 0}, {line: idx + 1, ch: 0}
+                );
+            }
         }
     }
-
-    for (let i = 0; i < len; i++) {
-        const item = result[i];
-        if (item.cmd === '+') {
-            cmInstance.replaceRange(
-                newLines[item.idx] + '\n', {line: item.idx, ch: 0}
-            );
-            changedLines.push(item.idx);
+    for (let i = 0; i < diff.length; i++) {
+        const item = diff[i];
+        if (item.added) {
+            for (let k = 0; k < item.count; k++) {
+                const idx = item.indices[k];
+                cmInstance.replaceRange(
+                    newLines[idx] + '\n', {line: idx, ch: 0}
+                );
+                changedLines.push(idx);
+            }
         }
     }
 
@@ -121,7 +126,7 @@ function diffUpdateCode(oldCode, newCode, cmInstance) {
         cmInstance.addLineClass(idx, 'wrap', 'option-changed');
     });
 
-    if (len) {
+    if (diff.length) {
         setTimeout(() => {
             cmInstance.scrollIntoView({
                 line: changedLines[0],


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org
For additional commands, e-mail: commits-h...@echarts.apache.org

Reply via email to