Skip to content

Commit

Permalink
Merge pull request #21 from kozakdenys/feature/possibility-to-change-…
Browse files Browse the repository at this point in the history
…filename

Add posibility to change downloaded file name
  • Loading branch information
kozakdenys authored Aug 21, 2020
2 parents f2c46a8 + 9e3398d commit d01b74d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
29 changes: 26 additions & 3 deletions src/core/QRCodeStyling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,14 +60,32 @@ export default class QRCodeStyling {
this._container = container;
}

download(extension?: Extension): void {
download(downloadOptions?: Partial<DownloadOptions> | 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}`);
});
}
}

0 comments on commit d01b74d

Please sign in to comment.