You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by wa...@apache.org on 2020/11/26 07:36:53 UTC

[incubator-echarts-examples] 01/01: fix(editor): fix no source code in downloaded file bug.

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

wangzx pushed a commit to branch fix-example-download
in repository https://gitbox.apache.org/repos/asf/incubator-echarts-examples.git

commit 48c56952205c672915492cf92a851158ba148470
Author: plainheart <yh...@all-my-life.cn>
AuthorDate: Thu Nov 26 15:36:30 2020 +0800

    fix(editor): fix no source code in downloaded file bug.
    
    - fix download issue in IE (But current build files cannot run in IE, can fix later if necessary)
---
 public/en/editor.html         |  1 -
 src/common/helper.js          | 15 +++++++++++++++
 src/common/store.js           |  2 --
 src/editor/downloadExample.js | 16 +++++++++-------
 4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/public/en/editor.html b/public/en/editor.html
index f6d5378..2a92cdc 100644
--- a/public/en/editor.html
+++ b/public/en/editor.html
@@ -18,7 +18,6 @@
     <script src="//cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js"></script>
     <script src="//cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
     <script src="//cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script>
-    <script src="//cdn.jsdelivr.net/npm/ace-builds@1.2.5/src-noconflict/"></script>
     <script src="//cdn.jsdelivr.net/npm/element-ui@2.13.2/lib/index.js"></script>
 </head>
 <body>
diff --git a/src/common/helper.js b/src/common/helper.js
index f346560..ac4397a 100644
--- a/src/common/helper.js
+++ b/src/common/helper.js
@@ -43,3 +43,18 @@ export function loadScriptsAsync(scripts) {
         return promise;
     }));
 }
+
+export function downloadBlob(blob, fileName) {
+    // for IE
+    if (typeof window.navigator.msSaveBlob === 'function') {
+        window.navigator.msSaveOrOpenBlob(blob, fileName);
+    }
+    else {
+        const a = document.createElement('a');
+        a.href = URL.createObjectURL(blob);
+        a.download = fileName;
+        a.click();
+        // should revoke the blob url after the download
+        URL.revokeObjectURL(a.href);
+    }
+}
\ No newline at end of file
diff --git a/src/common/store.js b/src/common/store.js
index 9089dfc..469928d 100644
--- a/src/common/store.js
+++ b/src/common/store.js
@@ -14,8 +14,6 @@ export const store = {
     typeCheck: URL_PARAMS.editor === 'monaco',
     useDirtyRect: 'useDirtyRect' in URL_PARAMS,
 
-    code: '',
-
     runCode: '',
     sourceCode: '',
 
diff --git a/src/editor/downloadExample.js b/src/editor/downloadExample.js
index c55b997..b9f1a48 100644
--- a/src/editor/downloadExample.js
+++ b/src/editor/downloadExample.js
@@ -1,11 +1,15 @@
 import {store} from '../common/store';
 import {URL_PARAMS, SCRIPT_URLS} from '../common/config';
+import {downloadBlob} from '../common/helper';
 
-const hasRootPath = store.code.indexOf('ROOT_PATH') >= 0;
+const hasRootPath = store.sourceCode.indexOf('ROOT_PATH') >= 0;
 const rootPathCode = hasRootPath ? `var ROOT_PATH = '${store.cdnRoot}'` : '';
 
 export function download() {
-    const code = `<!DOCTYPE html>
+    const code = `<!--
+    THIS EXAMPLE WAS DOWNLOADED FROM ${window.location.href}
+-->
+<!DOCTYPE html>
 <html style="height: 100%">
     <head>
         <meta charset="utf-8">
@@ -41,7 +45,7 @@ var option;
 
 ${rootPathCode}
 
-${store.code}
+${store.sourceCode}
 
 if (option && typeof option === 'object') {
     myChart.setOption(option);
@@ -55,8 +59,6 @@ if (option && typeof option === 'object') {
         type: 'text/html;charset=UTF-8',
         encoding: 'UTF-8'
     });
-    const a = document.createElement('a');
-    a.href = URL.createObjectURL(file);
-    a.download = URL_PARAMS.c + '.html';
-    a.click();
+    // download the blob
+    downloadBlob(file, URL_PARAMS.c + '.html');
 }
\ No newline at end of file


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