@taybart/corvid 0.1.4 → 0.1.6

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.
package/README.md CHANGED
@@ -116,3 +116,14 @@ style.onDarkMode((isDark) => {
116
116
  console.log(`is dark mode: ${isDark} and background is ${style.cssVar('--color-bg')`)
117
117
  })
118
118
  ```
119
+
120
+ ### QR
121
+
122
+ ```js
123
+ import { QR } '@taybart/corvid/qr'
124
+
125
+ QR.render({
126
+ text: 'https://example.com',
127
+ size: 200,
128
+ }, document.getElementById('qr'))
129
+ ```
package/dist/dom.d.ts CHANGED
@@ -7,12 +7,13 @@ import { logger } from './utils';
7
7
  * onDomContentLoaded callback
8
8
  */
9
9
  export declare function ready(cb: () => void): void;
10
+ export declare function on(event: string, cb: (ev: Event) => void): () => void;
10
11
  export declare function onKey(key: string, cb: (ev: {
11
12
  ctrl: boolean;
12
13
  alt: boolean;
13
14
  meta: boolean;
14
15
  shift: boolean;
15
- }) => void): void;
16
+ }) => void, verbose?: boolean): () => void;
16
17
  export declare function els(query: string, verbose?: boolean): el[];
17
18
  /*** element ***/
18
19
  type elOpts = {
@@ -36,6 +37,7 @@ export declare class el {
36
37
  parent(parent: HTMLElement | el): this;
37
38
  appendChild(ch: HTMLElement | el): this;
38
39
  child(ch: HTMLElement | el): this;
40
+ prependChild(ch: HTMLElement | el): this;
39
41
  empty(): this;
40
42
  content(content: any, { text }?: {
41
43
  text?: boolean;
package/dist/index.js CHANGED
@@ -43,6 +43,7 @@ __webpack_require__.d(dom_namespaceObject, {
43
43
  el: ()=>dom_el,
44
44
  els: ()=>els,
45
45
  interpolate: ()=>interpolate,
46
+ on: ()=>on,
46
47
  onKey: ()=>onKey,
47
48
  ready: ()=>ready
48
49
  });
@@ -158,16 +159,23 @@ function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg
158
159
  ].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
159
160
  }
160
161
  const clipboard = {
162
+ check () {
163
+ if (!navigator.clipboard) throw new Error('Clipboard API not supported, or context is not https');
164
+ },
161
165
  async copy (text) {
166
+ this.check();
162
167
  await navigator.clipboard.writeText(text);
163
168
  },
164
169
  async copyArbitrary (data) {
170
+ this.check();
165
171
  await navigator.clipboard.write(data);
166
172
  },
167
173
  async read () {
174
+ this.check();
168
175
  return await navigator.clipboard.readText();
169
176
  },
170
177
  async readArbitrary () {
178
+ this.check();
171
179
  return await navigator.clipboard.read();
172
180
  },
173
181
  listen (query, cb) {
@@ -242,15 +250,28 @@ function dom_define_property(obj, key, value) {
242
250
  function ready(cb) {
243
251
  window.addEventListener('DOMContentLoaded', cb);
244
252
  }
245
- function onKey(key, cb) {
246
- window.addEventListener('keydown', (ev)=>{
253
+ function on(event, cb) {
254
+ document.addEventListener(event, cb);
255
+ return ()=>{
256
+ document.removeEventListener(event, cb);
257
+ };
258
+ }
259
+ function onKey(key, cb, verbose = false) {
260
+ const log = new logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'onKey');
261
+ log.debug(`adding ${key} keydown listener`);
262
+ const handler = (ev)=>{
247
263
  if (ev.key === key) cb({
248
264
  ctrl: ev.ctrlKey,
249
265
  alt: ev.altKey,
250
266
  meta: ev.metaKey,
251
267
  shift: ev.shiftKey
252
268
  });
253
- });
269
+ };
270
+ window.addEventListener('keydown', handler);
271
+ return ()=>{
272
+ log.debug(`removing ${key} listener`);
273
+ window.removeEventListener('keydown', handler);
274
+ };
254
275
  }
255
276
  function els(query, verbose = false) {
256
277
  return Array.from(document.querySelectorAll(query)).map((n)=>new dom_el(n, verbose));
@@ -283,6 +304,12 @@ class dom_el {
283
304
  else this.el.appendChild(ch);
284
305
  return this;
285
306
  }
307
+ prependChild(ch) {
308
+ if (!this.el) throw new Error(`no element from query: ${this.query}`);
309
+ if (ch instanceof dom_el) this.el.prepend(ch.el);
310
+ else this.el.prepend(ch);
311
+ return this;
312
+ }
286
313
  empty() {
287
314
  if (this.el) this.el.innerHTML = '';
288
315
  return this;
@@ -0,0 +1,41 @@
1
+ export interface QRCodeConfig {
2
+ text: string;
3
+ size?: number;
4
+ fill?: string | GradientFill;
5
+ background?: string | null;
6
+ ecLevel?: 'L' | 'M' | 'Q' | 'H';
7
+ minVersion?: number;
8
+ maxVersion?: number;
9
+ radius?: number;
10
+ quiet?: number;
11
+ left?: number;
12
+ top?: number;
13
+ }
14
+ export interface GradientFill {
15
+ type: 'linear-gradient' | 'radial-gradient';
16
+ position: number[];
17
+ colorStops: [number, string][];
18
+ }
19
+ export interface QRCode {
20
+ text: string;
21
+ level: string;
22
+ version: number;
23
+ moduleCount: number;
24
+ isDark: (row: number, col: number) => boolean;
25
+ }
26
+ export declare class QR {
27
+ static render(config: QRCodeConfig, element: HTMLElement | HTMLCanvasElement): void;
28
+ private static getDefaultSettings;
29
+ private static createQRCode;
30
+ private static createMinQRCode;
31
+ private static drawBackground;
32
+ private static drawModuleRoundedDark;
33
+ private static drawModuleRoundedLight;
34
+ private static drawModuleRounded;
35
+ private static drawModules;
36
+ private static setFill;
37
+ private static drawOnCanvas;
38
+ private static createCanvas;
39
+ private static renderToCanvas;
40
+ }
41
+ export default QR;
@@ -0,0 +1,71 @@
1
+ export declare class QRCodeGenerator {
2
+ private typeNumber;
3
+ private errorCorrectLevel;
4
+ private modules;
5
+ private moduleCount;
6
+ private dataCache;
7
+ private dataList;
8
+ constructor(typeNumber: number, errorCorrectLevel: string);
9
+ addData(data: string): void;
10
+ isDark(row: number, col: number): boolean;
11
+ getModuleCount(): number;
12
+ make(): void;
13
+ private makeImpl;
14
+ private createModules;
15
+ private setupPositionProbePattern;
16
+ private getBestMaskPattern;
17
+ private setupTimingPattern;
18
+ private setupPositionAdjustPattern;
19
+ private setupTypeNumber;
20
+ private setupTypeInfo;
21
+ private mapData;
22
+ private createBytes;
23
+ private getErrorCorrectPolynomial;
24
+ private createData;
25
+ static stringToBytes(s: string): number[];
26
+ }
27
+ export declare class QR8BitByte {
28
+ private mode;
29
+ private data;
30
+ private bytes;
31
+ constructor(data: string);
32
+ getMode(): number;
33
+ getLength(): number;
34
+ write(buffer: QRBitBuffer): void;
35
+ }
36
+ export declare class QRBitBuffer {
37
+ private buffer;
38
+ private length;
39
+ getBuffer(): number[];
40
+ getAt(index: number): boolean;
41
+ put(num: number, length: number): void;
42
+ getLengthInBits(): number;
43
+ putBit(bit: boolean): void;
44
+ }
45
+ export declare class QRPolynomial {
46
+ private num;
47
+ constructor(num: number[], shift: number);
48
+ getAt(index: number): number;
49
+ getLength(): number;
50
+ multiply(e: QRPolynomial): QRPolynomial;
51
+ mod(e: QRPolynomial): QRPolynomial;
52
+ }
53
+ export declare const QRMode: {
54
+ readonly MODE_8BIT_BYTE: number;
55
+ };
56
+ export declare const QRErrorCorrectLevel: {
57
+ readonly L: 1;
58
+ readonly M: 0;
59
+ readonly Q: 3;
60
+ readonly H: 2;
61
+ };
62
+ export declare const QRMaskPattern: {
63
+ readonly PATTERN000: 0;
64
+ readonly PATTERN001: 1;
65
+ readonly PATTERN010: 2;
66
+ readonly PATTERN011: 3;
67
+ readonly PATTERN100: 4;
68
+ readonly PATTERN101: 5;
69
+ readonly PATTERN110: 6;
70
+ readonly PATTERN111: 7;
71
+ };
@@ -0,0 +1,28 @@
1
+ export declare class QRUtil {
2
+ private static PATTERN_POSITION_TABLE;
3
+ private static G15;
4
+ private static G18;
5
+ private static G15_MASK;
6
+ private static getBCHDigit;
7
+ static getBCHTypeInfo(data: number): number;
8
+ static getBCHTypeNumber(data: number): number;
9
+ static getPatternPosition(typeNumber: number): number[];
10
+ static getMaskFunction(maskPattern: number): (i: number, j: number) => boolean;
11
+ static getErrorCorrectPolynomial(errorCorrectLength: number): any;
12
+ static getLengthInBits(mode: number, type: number): number;
13
+ static getLostPoint(qrcode: any): number;
14
+ }
15
+ export declare class QRMath {
16
+ private static EXP_TABLE;
17
+ private static LOG_TABLE;
18
+ static glog(n: number): number;
19
+ static gexp(n: number): number;
20
+ }
21
+ export declare class QRRSBlock {
22
+ private static RS_BLOCK_TABLE;
23
+ totalCount: number;
24
+ dataCount: number;
25
+ constructor(totalCount: number, dataCount: number);
26
+ private static getRsBlockTable;
27
+ static getRSBlocks(typeNumber: number, errorCorrectLevel: number): QRRSBlock[];
28
+ }