simplify manage:export code

This commit is contained in:
tophf 2018-08-02 15:52:34 +03:00
parent 71e2db773b
commit fa4dfd680b

View File

@ -275,39 +275,37 @@ function importFromString(jsonString, oldStyles) {
$('#file-all-styles').onclick = () => { $('#file-all-styles').onclick = () => {
API.getStyles().then(styles => { API.getStyles().then(styles => {
const text = JSON.stringify(styles, null, '\t');
const blob = new Blob([text], {type: 'application/json'});
const url = URL.createObjectURL(blob);
let link = $create('a', {
href: url,
type: 'application/json',
download: generateFileName(),
});
// https://crbug.com/714373 // https://crbug.com/714373
if (!FIREFOX && !(CHROME > 3310 && CHROME < Infinity)) { document.documentElement.appendChild(
link.dispatchEvent(new MouseEvent('click')); $create('iframe', {
return doTimeout() onload() {
.then(() => URL.revokeObjectURL(url)); const text = JSON.stringify(styles, null, '\t');
} const type = 'application/json';
const iframe = document.body.appendChild($create('iframe', { this.onload = null;
style: 'width: 0; height: 0; position: fixed; opacity: 0;'.replace(/;/g, '!important;'), this.contentDocument.body.appendChild(
})); $create('a', {
return doTimeout() href: URL.createObjectURL(new Blob([text], {type})),
.then(() => { download: generateFileName(),
link = iframe.contentDocument.importNode(link, true); type,
iframe.contentDocument.body.appendChild(link);
}) })
.then(() => doTimeout()) ).dispatchEvent(new MouseEvent('click'));
.then(() => link.dispatchEvent(new MouseEvent('click'))); },
// we can't use display:none as some browsers are ignoring such iframes
style: `
all: unset;
width: 0;
height: 0;
position: fixed;
opacity: 0;
border: none;
`.replace(/;/g, '!important;'),
})
);
// we don't remove the iframe or the object URL because the browser may show // we don't remove the iframe or the object URL because the browser may show
// a download dialog and we don't know how long it'll take until the user confirms it // a download dialog and we don't know how long it'll take until the user confirms it
// (some browsers like Vivaldi can't download if we revoke the URL) // (some browsers like Vivaldi can't download if we revoke the URL)
}); });
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);