Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
Return more promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
exupero committed Feb 10, 2019
1 parent c378d19 commit ccfd5bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "save-svg-as-png",
"version": "1.4.8",
"version": "1.4.9",
"description": "Convert a browser SVG to PNG or dataUri",
"main": "lib/saveSvgAsPng.js",
"scripts": {
Expand Down
18 changes: 12 additions & 6 deletions src/saveSvgAsPng.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
const requireDomNode = el => {
if (!isElement(el)) throw new Error(`an HTMLElement or SVGElement is required; got ${el}`);
};
const requireDomNodePromise = el =>
new Promise((resolve, reject) => {
if (isElement(el)) resolve(el)
else reject(new Error(`an HTMLElement or SVGElement is required; got ${el}`));
})
const isExternal = url => url && url.lastIndexOf('http',0) === 0 && url.lastIndexOf(window.location.host) === -1;

const getFontMimeTypeFromUrl = fontUrl => {
Expand Down Expand Up @@ -359,20 +364,21 @@
}
saveLink.click();
document.body.removeChild(saveLink);
}
else {
} else {
window.open(uri, '_temp', 'menubar=no,toolbar=no,status=no');
}
}
};

out$.saveSvg = (el, name, options) => {
requireDomNode(el);
out$.svgAsDataUri(el, options || {}, uri => out$.download(name, uri));
return requireDomNodePromise(el)
.then(out$.svgAsDataUri(el, options || {}))
.then(uri => out$.download(name, uri));
};

out$.saveSvgAsPng = (el, name, options) => {
requireDomNode(el);
out$.svgAsPngUri(el, options || {}, uri => out$.download(name, uri));
return requireDomNodePromise(el)
.then(out$.svgAsPngUri(el, options || {}))
.then(uri => out$.download(name, uri));
};
})();

0 comments on commit ccfd5bb

Please sign in to comment.