@plattar/plattar-qrcode 1.120.3 → 1.122.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -126,7 +126,6 @@ class BaseElement extends HTMLElement {
126
126
  type: "canvas"
127
127
  };
128
128
 
129
- this._options.data = url;
130
129
  this._options.margin = margin;
131
130
  this._options.image = image;
132
131
 
@@ -144,6 +143,45 @@ class BaseElement extends HTMLElement {
144
143
  this._options.dotsOptions.type = "rounded";
145
144
  }
146
145
 
146
+ const shortenURL = this.hasAttribute("shorten") ? this.getAttribute("shorten") : "false";
147
+
148
+ if (shortenURL && shortenURL.toLowerCase() === "true") {
149
+ this._ShortenURL(url).then((newURL) => {
150
+ this._GenerateQRCode(newURL, width, height);
151
+ }).catch((_err) => {
152
+ console.warn(_err);
153
+ // ignore error and just generate normal QR Code
154
+ this._GenerateQRCode(url, width, height);
155
+ });
156
+ }
157
+ else {
158
+ this._GenerateQRCode(url, width, height);
159
+ }
160
+ }
161
+
162
+ _UpdateCanvas(width, height) {
163
+ if (!this._qrCode) {
164
+ return;
165
+ }
166
+
167
+ const canvas = this._qrCode._canvas;
168
+
169
+ if (canvas) {
170
+ canvas.style.width = "100%";
171
+ canvas.style.height = "100%";
172
+ }
173
+
174
+ if (this._divContainer) {
175
+ const div = this._divContainer;
176
+
177
+ div.style.width = width + "px";
178
+ div.style.height = height + "px";
179
+ }
180
+ }
181
+
182
+ _GenerateQRCode(url, width, height) {
183
+ this._options.data = url;
184
+
147
185
  const shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
148
186
 
149
187
  const qrCode = this._qrCode;
@@ -166,24 +204,36 @@ class BaseElement extends HTMLElement {
166
204
  this._UpdateCanvas(width, height);
167
205
  }
168
206
 
169
- _UpdateCanvas(width, height) {
170
- if (!this._qrCode) {
171
- return;
172
- }
207
+ _IsFetchAPISupported() {
208
+ return 'fetch' in window;
209
+ }
173
210
 
174
- const canvas = this._qrCode._canvas;
211
+ _ShortenURL(url) {
212
+ return new Promise((accept, reject) => {
213
+ if (!this._IsFetchAPISupported()) {
214
+ return reject(new Error("PlattarQR._ShortenURL() - fetch api not supported, cannot proceed"));
215
+ }
175
216
 
176
- if (canvas) {
177
- canvas.style.width = "100%";
178
- canvas.style.height = "100%";
179
- }
217
+ try {
218
+ const b64 = btoa(url);
219
+ const endpoint = "https://c.plattar.space/api/v2/shorten?base64=" + b64;
180
220
 
181
- if (this._divContainer) {
182
- const div = this._divContainer;
221
+ fetch(endpoint).then((response) => {
222
+ if (!response.ok) {
223
+ throw new Error("PlattarQR._ShortenURL() - response was invalid");
224
+ }
183
225
 
184
- div.style.width = width + "px";
185
- div.style.height = height + "px";
186
- }
226
+ return response.text();
227
+ }).then((text) => {
228
+ return accept(text);
229
+ }).catch(() => {
230
+ return reject(new Error("PlattarQR._ShortenURL() - there was an unexpected issue generating short url"));
231
+ });
232
+ }
233
+ catch (err) {
234
+ return reject(err);
235
+ }
236
+ });
187
237
  }
188
238
  }
189
239
 
package/index.js CHANGED
@@ -2,9 +2,13 @@
2
2
  const QRCodeElement = require("./elements/qrcode-element.js");
3
3
  const Version = require("./version");
4
4
 
5
- console.log("using @plattar/plattar-qrcode v" + Version);
5
+ if (customElements) {
6
+ if (customElements.get("plattar-qrcode") === undefined) {
7
+ customElements.define("plattar-qrcode", QRCodeElement);
8
+ }
9
+ }
6
10
 
7
- customElements.define("plattar-qrcode", QRCodeElement);
11
+ console.log("using @plattar/plattar-qrcode v" + Version);
8
12
 
9
13
  module.exports = {
10
14
  version: Version
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@plattar/plattar-qrcode",
3
- "version": "1.120.3",
3
+ "version": "1.122.1",
4
4
  "description": "Allows embedding Plattar-Style QR Codes for existing websites.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "scripts": {
9
- "clean": "rm -rf build node_modules",
9
+ "clean": "rm -rf build node_modules package-lock.json",
10
10
  "build": "npm run clean && npm install && npm run build-es2019 && npm run build-es2015",
11
11
  "build-es2019": "rm -rf build/es2019 && mkdir -p build/es2019 && browserify --standalone PlattarQRCode index.js -o build/es2019/plattar-qrcode.js && uglifyjs build/es2019/plattar-qrcode.js --output build/es2019/plattar-qrcode.min.js",
12
12
  "build-es2015": "rm -rf build/es2015 && mkdir -p build/es2015 && babel build/es2019/plattar-qrcode.js --presets=@babel/env > build/es2015/plattar-qrcode.js && uglifyjs build/es2015/plattar-qrcode.js --output build/es2015/plattar-qrcode.min.js",
@@ -42,9 +42,9 @@
42
42
  "qr-code-styling": "^1.6.0-rc.1"
43
43
  },
44
44
  "devDependencies": {
45
- "@babel/cli": "^7.16.0",
46
- "@babel/core": "^7.16.0",
47
- "@babel/preset-env": "^7.16.4",
45
+ "@babel/cli": "^7.17.0",
46
+ "@babel/core": "^7.17.0",
47
+ "@babel/preset-env": "^7.16.11",
48
48
  "browserify": "^17.0.0",
49
49
  "uglify-es": "^3.3.9"
50
50
  },
package/version.js CHANGED
@@ -1 +1 @@
1
- module.exports = "1.120.3";
1
+ module.exports = "1.122.1";