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,12 +306,7 @@ 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;
// for long URLs; https://github.com/schomery/stylus/issues/13#issuecomment-284582600
}).then(fetch)
.then(res => res.blob())
.then(blob => {
const objectURL = URL.createObjectURL(blob); const objectURL = URL.createObjectURL(blob);
let link = $element({ let link = $element({
tag:'a', tag:'a',
@ -328,13 +323,14 @@ $('#file-all-styles').onclick = () => {
tag: 'iframe', tag: 'iframe',
style: 'width: 0; height: 0; position: fixed; opacity: 0;'.replace(/;/g, '!important;'), style: 'width: 0; height: 0; position: fixed; opacity: 0;'.replace(/;/g, '!important;'),
})); }));
doTimeout().then(() => { 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();
@ -342,6 +338,10 @@ $('#file-all-styles').onclick = () => {
} }
}); });
function doTimeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function generateFileName() { function generateFileName() {
const today = new Date(); const today = new Date();
const dd = ('0' + today.getDate()).substr(-2); const dd = ('0' + today.getDate()).substr(-2);