From 9e3398d59355a72d3b74827134d7c3dfb4953d0e Mon Sep 17 00:00:00 2001 From: Denys Kozak Date: Fri, 21 Aug 2020 16:06:29 +0300 Subject: [PATCH] Add posibility to change downloaded file name --- README.md | 15 +++++++++++---- package-lock.json | 2 +- package.json | 2 +- src/core/QRCodeStyling.ts | 29 ++++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 123dd06d..6c3f4b87 100644 --- a/README.md +++ b/README.md @@ -118,11 +118,18 @@ Param |Type |Description -------|------|-------------------------------------- options|object|The same options as for initialization -`QRCodeStyling.download(extension) => void` +`QRCodeStyling.download(downloadOptions) => void` -Param |Type |Default Value ----------|------------------------------|------------- -extension|string (`'png' 'jpeg' 'webp'`)|`'png'` +Param |Type |Description +---------------|------|------------ +downloadOptions|object|Options with extension and name of file (not required) + +`downloadOptions` structure + +Property |Type |Default Value|Description +---------|------------------------------|-------------|----------------------------------------------------- +name |string |`'qr'` |Name of the downloaded file +extension|string (`'png' 'jpeg' 'webp'`)|`'png'` |File extension ### License diff --git a/package-lock.json b/package-lock.json index 846f435a..7b0289ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "qr-code-styling", - "version": "1.1.3", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 32b6dd64..61e58fc5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qr-code-styling", - "version": "1.1.3", + "version": "1.2.0", "description": "Add a style and an image to your QR code", "main": "lib/qr-code-styling.js", "files": [ diff --git a/src/core/QRCodeStyling.ts b/src/core/QRCodeStyling.ts index 1d90373c..fac0cfa4 100644 --- a/src/core/QRCodeStyling.ts +++ b/src/core/QRCodeStyling.ts @@ -5,6 +5,11 @@ import QRCanvas from "./QRCanvas"; import defaultOptions, { Options } from "./QROptions"; import qrcode from "qrcode-generator"; +type DownloadOptions = { + name?: string; + extension?: Extension; +}; + export default class QRCodeStyling { _options: Options; _container?: HTMLElement; @@ -55,14 +60,32 @@ export default class QRCodeStyling { this._container = container; } - download(extension?: Extension): void { + download(downloadOptions?: Partial | string): void { if (!this._drawingPromise) return; this._drawingPromise.then(() => { if (!this._canvas) return; - const data = this._canvas.getCanvas().toDataURL(extension ? `image/${extension}` : undefined); - downloadURI(data, `qr.${extension || "png"}`); + let extension = "png"; + let name = "qr"; + + //TODO remove deprecated code in the v2 + if (typeof downloadOptions === "string") { + extension = downloadOptions; + console.warn( + "Extension is deprecated as argument for 'download' method, please pass object { name: '...', extension: '...' } as argument" + ); + } else if (typeof downloadOptions === "object" && downloadOptions !== null) { + if (downloadOptions.name) { + name = downloadOptions.name; + } + if (downloadOptions.extension) { + extension = downloadOptions.extension; + } + } + + const data = this._canvas.getCanvas().toDataURL(`image/${extension}`); + downloadURI(data, `${name}.${extension}`); }); } }