From ccfd5bbf5f5dff9548047aa6d7c7651d0960d897 Mon Sep 17 00:00:00 2001 From: Eric Shull Date: Sun, 10 Feb 2019 08:56:58 -0500 Subject: [PATCH] Return more promises. --- package.json | 2 +- src/saveSvgAsPng.js | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index a7ed945..409059b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/saveSvgAsPng.js b/src/saveSvgAsPng.js index 620af07..c184418 100644 --- a/src/saveSvgAsPng.js +++ b/src/saveSvgAsPng.js @@ -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 => { @@ -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)); }; })();