export: create blob directly

fixes #211
This commit is contained in:
tophf 2017-10-14 04:25:46 +03:00
parent 0b17c669de
commit 721dfc4787
3 changed files with 27 additions and 35 deletions

View File

@ -37,7 +37,6 @@ globals:
saveStyleSafe: false saveStyleSafe: false
sessionStorageHash: false sessionStorageHash: false
download: false download: false
doTimeout: false
invokeOrPostpone: false invokeOrPostpone: false
# localization.js # localization.js
template: false template: false

View File

@ -383,13 +383,6 @@ function download(url) {
} }
function doTimeout(ms = 0, ...args) {
return ms > 0
? () => new Promise(resolve => setTimeout(resolve, ms, ...args))
: new Promise(resolve => setTimeout(resolve, 0, ...args));
}
function invokeOrPostpone(isInvoke, fn, ...args) { function invokeOrPostpone(isInvoke, fn, ...args) {
return isInvoke return isInvoke
? fn(...args) ? fn(...args)

View File

@ -306,41 +306,41 @@ function importFromString(jsonString) {
$('#file-all-styles').onclick = () => { $('#file-all-styles').onclick = () => {
getStylesSafe().then(styles => { getStylesSafe().then(styles => {
const text = JSON.stringify(styles, null, '\t'); const text = JSON.stringify(styles, null, '\t');
const url = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text); const blob = new Blob([text], {type : 'application/json'});
return url; const objectURL = URL.createObjectURL(blob);
// for long URLs; https://github.com/schomery/stylus/issues/13#issuecomment-284582600 let link = $element({
}).then(fetch) tag:'a',
.then(res => res.blob()) href: objectURL,
.then(blob => { type: 'application/json',
const objectURL = URL.createObjectURL(blob); download: generateFileName(),
let link = $element({ });
tag:'a', // TODO: remove the fallback when FF multi-process bug is fixed
href: objectURL, if (!FIREFOX) {
type: 'application/json', link.dispatchEvent(new MouseEvent('click'));
download: generateFileName(), setTimeout(() => URL.revokeObjectURL(objectURL));
}); } else {
// TODO: remove the fallback when FF multi-process bug is fixed const iframe = document.body.appendChild($element({
if (!FIREFOX) { tag: 'iframe',
link.dispatchEvent(new MouseEvent('click')); style: 'width: 0; height: 0; position: fixed; opacity: 0;'.replace(/;/g, '!important;'),
setTimeout(() => URL.revokeObjectURL(objectURL)); }));
} else { doTimeout()
const iframe = document.body.appendChild($element({ .then(() => {
tag: 'iframe',
style: 'width: 0; height: 0; position: fixed; opacity: 0;'.replace(/;/g, '!important;'),
}));
doTimeout().then(() => {
link = iframe.contentDocument.importNode(link, true); link = iframe.contentDocument.importNode(link, true);
iframe.contentDocument.body.appendChild(link); iframe.contentDocument.body.appendChild(link);
}) })
.then(doTimeout) .then(() => doTimeout())
.then(() => link.dispatchEvent(new MouseEvent('click'))) .then(() => link.dispatchEvent(new MouseEvent('click')))
.then(doTimeout(1000)) .then(() => doTimeout(1000))
.then(() => { .then(() => {
URL.revokeObjectURL(objectURL); URL.revokeObjectURL(objectURL);
iframe.remove(); iframe.remove();
}); });
} }
}); });
function doTimeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function generateFileName() { function generateFileName() {
const today = new Date(); const today = new Date();