From 3ec37e2c669f1417ae0d549b21f98a93d044f08b Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Thu, 11 Feb 2021 23:10:33 +0900 Subject: [PATCH] make flow download code separate utility instead of polyfill --- .../editor-client/src/js/polyfills.js | 27 ----------------- .../editor-client/src/js/ui/clipboard.js | 30 ++++++++++++++----- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/polyfills.js b/packages/node_modules/@node-red/editor-client/src/js/polyfills.js index b8f36af69..100cd4913 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/polyfills.js +++ b/packages/node_modules/@node-red/editor-client/src/js/polyfills.js @@ -52,32 +52,5 @@ Set.prototype = _Set.prototype; Set.prototype.constructor = Set; } - - if (window.navigator.msSaveBlob) { - // IE does not support data uri scheme for downloading data - window.addEventListener("click", function (ev) { - var tgt = ev.target; - if ((tgt.tagName === "A") && - tgt.hasAttribute("download") && - tgt.hasAttribute("href")) { - // partial support of data uri downloading - var filename = tgt.getAttribute("download"); - var dataUri = tgt.getAttribute("href"); - var match = /^data:([^,]+),(.*)/.exec(dataUri); - if (match) { - ev.preventDefault(); - var enc = match[1]; - var data = decodeURIComponent(match[2]); - var blob = new Blob([data], { - type: enc, - }); - navigator.msSaveBlob(blob, filename); - } - else { - console.log("download not supported:", tgt); - } - } - }); - } } })(); diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/clipboard.js b/packages/node_modules/@node-red/editor-client/src/js/ui/clipboard.js index 4ed618f6d..d39240938 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/clipboard.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/clipboard.js @@ -30,6 +30,27 @@ RED.clipboard = (function() { var pendingImportConfig; + + function downloadData(file, data) { + if (window.navigator.msSaveBlob) { + // IE11 workaround + // IE does not support data uri scheme for downloading data + var blob = new Blob([data], { + type: "data:text/plain;charset=utf-8" + }); + navigator.msSaveBlob(blob, file); + } + else { + var element = document.createElement('a'); + element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data)); + element.setAttribute('download', file); + element.style.display = 'none'; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); + } + } + function setupDialogs() { dialog = $('
') .appendTo("#red-ui-editor") @@ -56,13 +77,8 @@ RED.clipboard = (function() { class: "primary", text: RED._("clipboard.download"), click: function() { - var element = document.createElement('a'); - element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent($("#red-ui-clipboard-dialog-export-text").val())); - element.setAttribute('download', "flows.json"); - element.style.display = 'none'; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element); + var data = $("#red-ui-clipboard-dialog-export-text").val(); + downloadData("flows.json", data); $( this ).dialog( "close" ); } },