@alegendstale/holly-components 0.2.8 → 0.2.10

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.
Files changed (53) hide show
  1. package/dist/components/absolute-container/absolute-container.js +113 -91
  2. package/dist/components/absolute-container/absolute-container.stories.js +36 -0
  3. package/dist/components/absolute-container/index.js +1 -1
  4. package/dist/components/bottom-sheet/bottom-sheet.js +415 -308
  5. package/dist/components/bottom-sheet/bottom-sheet.stories.js +43 -0
  6. package/dist/components/bottom-sheet/bottom-sheet.test.js +15 -0
  7. package/dist/components/bottom-sheet/index.js +1 -1
  8. package/dist/components/canvas/canvas-base.d.ts +2 -1
  9. package/dist/components/canvas/canvas-base.d.ts.map +1 -1
  10. package/dist/components/canvas/canvas-base.js +56 -44
  11. package/dist/components/canvas/canvas-gradient.d.ts.map +1 -1
  12. package/dist/components/canvas/canvas-gradient.js +61 -45
  13. package/dist/components/canvas/canvas-image.js +158 -111
  14. package/dist/components/canvas/index.js +2 -2
  15. package/dist/components/carousel-scroller/carousel-scroller.js +133 -121
  16. package/dist/components/carousel-scroller/carousel-scroller.stories.js +40 -0
  17. package/dist/components/carousel-scroller/index.js +1 -1
  18. package/dist/components/color-palette/color-palette-utils.d.ts.map +1 -1
  19. package/dist/components/color-palette/color-palette-utils.js +105 -40
  20. package/dist/components/color-palette/color-palette.js +429 -337
  21. package/dist/components/color-palette/component/color-palette-component.js +184 -142
  22. package/dist/components/color-palette/component/color-palette-component.stories.js +74 -0
  23. package/dist/components/color-palette/component/index.js +1 -0
  24. package/dist/components/color-palette/editor/color-palette-editor.js +702 -591
  25. package/dist/components/color-palette/editor/color-palette-editor.stories.js +39 -0
  26. package/dist/components/color-palette/editor/index.js +1 -0
  27. package/dist/components/color-palette/index.js +5 -7
  28. package/dist/components/color-palette/item/color-palette-item-edit.js +114 -87
  29. package/dist/components/color-palette/item/color-palette-item.js +155 -140
  30. package/dist/components/color-palette/item/index.js +2 -0
  31. package/dist/components/color-palette/menu/color-palette-menu.js +241 -217
  32. package/dist/components/color-palette/menu/color-palette-reorder.js +117 -103
  33. package/dist/components/color-palette/menu/index.js +2 -0
  34. package/dist/components/color-palette/storybook/color-palette-invalid.stories.js +90 -0
  35. package/dist/components/color-palette/storybook/color-palette-valid.stories.js +295 -0
  36. package/dist/components/color-palette/storybook/color-palette.stories.js +76 -0
  37. package/dist/components/tool-tip/Tooltip2.js +103 -0
  38. package/dist/components/tool-tip/index.js +1 -1
  39. package/dist/components/tool-tip/tool-tip.d.ts +1 -0
  40. package/dist/components/tool-tip/tool-tip.d.ts.map +1 -1
  41. package/dist/components/tool-tip/tool-tip.js +125 -102
  42. package/dist/components/tool-tip/tool-tip.stories.js +30 -0
  43. package/dist/index.js +6 -14
  44. package/dist/utils/EventEmitter.js +23 -23
  45. package/dist/utils/basicUtils.d.ts +2 -0
  46. package/dist/utils/basicUtils.d.ts.map +1 -1
  47. package/dist/utils/basicUtils.js +149 -23
  48. package/dist/utils/dragDropUtils.js +121 -0
  49. package/dist/utils/generateUtils.js +73 -39
  50. package/dist/utils/types.d.ts +1 -1
  51. package/dist/utils/types.d.ts.map +1 -1
  52. package/dist/utils/types.js +1 -0
  53. package/package.json +2 -3
@@ -0,0 +1,43 @@
1
+ import { html } from 'lit';
2
+ const meta = {
3
+ title: 'Bottom Sheet',
4
+ tags: ['autodocs'],
5
+ component: 'bottom-sheet',
6
+ };
7
+ export default meta;
8
+ const Template = {
9
+ render: ({ open, nonmodal, snapPoints }) => {
10
+ return html `
11
+ <bottom-sheet ?open=${open} .snapPoints=${snapPoints} ?nonmodal=${nonmodal}>
12
+ <p>
13
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eget dui eros. Nunc vel magna pellentesque, euismod lacus eu, euismod ligula. Nulla porttitor ultrices enim, ac fermentum ligula bibendum nec. Maecenas et volutpat nulla. Donec sit amet dapibus est, ultricies accumsan nunc. Praesent vitae nulla metus. Aenean non posuere lorem, bibendum scelerisque mauris. Praesent arcu dolor, efficitur vel malesuada nec, rhoncus eget eros. Nullam facilisis, diam eu tristique euismod, neque dolor pellentesque purus, id tristique ex quam eget lacus. Pellentesque lacinia consequat ligula. Duis egestas ornare porttitor. Morbi tempus fermentum nulla. Sed eu turpis quis nunc aliquam cursus vitae nec arcu. Nulla nec leo et tellus condimentum commodo mollis in dolor. Vivamus tortor eros, dictum in nulla ac, ultricies posuere ligula. Maecenas elit sem, efficitur et risus sed, placerat imperdiet nisl. Donec nunc ipsum, molestie et elementum a, hendrerit in diam. Vestibulum
14
+ ullamcorper eu dolor id maximus. Donec in nunc vel dolor vehicula mollis. Mauris elementum, massa vitae condimentum efficitur, nisi massa condimentum dolor, at ultricies nulla ante et tortor. Praesent vitae vestibulum urna. Nunc aliquam aliquam elementum. Nullam facilisis ut ligula in ultrices. Vestibulum et risus ut eros gravida malesuada varius euismod dolor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aenean dui nisl, auctor quis laoreet fringilla, aliquam id risus. Pellentesque sit amet neque finibus, pharetra dolor eu, eleifend ante. Vestibulum condimentum urna urna, ut dignissim nibh posuere in. Proin nec tincidunt enim, vel vehicula nunc. In quis pellentesque urna. Curabitur egestas arcu non arcu ullamcorper, at pellentesque lacus varius. Maecenas ac molestie erat. Integer ornare dui vitae turpis porttitor, nec semper mauris ultrices. Quisque eu arcu ipsum. Morbi sed tristique urna. Pellentesque gravida, nisi elementum ultrices condimentum,
15
+ ipsum nisl faucibus ex, eget imperdiet dolor sem sit amet dui. Aenean dictum nibh nulla, non lacinia arcu molestie tempus. Donec a ex sodales, posuere felis eget, vestibulum odio. Sed lectus sapien, lacinia in orci eu, molestie finibus ex. Pellentesque dictum, dolor nec interdum gravida, sapien velit venenatis libero, eu finibus elit purus et ipsum. Vivamus sed eros libero. Aenean a auctor quam. Vivamus blandit enim tellus, in posuere enim fermentum varius. Donec malesuada orci ac ex dapibus, nec condimentum metus hendrerit. Nam non efficitur sem, ac ornare sapien. In varius libero et malesuada gravida. Suspendisse sollicitudin euismod ante, quis tincidunt quam rhoncus ac. Proin in pulvinar tortor. Nullam condimentum vestibulum dignissim. In hac habitasse platea dictumst. Etiam laoreet volutpat urna, et euismod urna aliquet egestas. Nulla odio tellus, vestibulum sit amet auctor vitae, tristique ut turpis. Cras sed tellus accumsan, pellentesque urna eget,
16
+ sollicitudin felis. In euismod ac arcu vitae suscipit. Curabitur non consequat nibh. In purus nulla, egestas in commodo in, fermentum sit amet massa. Nullam luctus velit vel arcu dictum, vel fermentum dolor convallis. Nulla interdum quam nibh, in egestas turpis vestibulum vel. Nulla placerat sapien sit amet pharetra porttitor.
17
+ </p>
18
+ </bottom-sheet>
19
+ `;
20
+ },
21
+ };
22
+ export const Modal = {
23
+ ...Template,
24
+ args: {
25
+ open: false,
26
+ snapPoints: [0, 50, 100],
27
+ },
28
+ };
29
+ export const NonModal = {
30
+ ...Template,
31
+ args: {
32
+ open: false,
33
+ nonmodal: true,
34
+ snapPoints: [0, 50, 100],
35
+ },
36
+ };
37
+ export const Thirds = {
38
+ ...Template,
39
+ args: {
40
+ open: false,
41
+ snapPoints: [0, 33, 66],
42
+ },
43
+ };
@@ -0,0 +1,15 @@
1
+ import { $, expect } from '@wdio/globals';
2
+ import './bottom-sheet';
3
+ import { html, render } from 'lit';
4
+ describe('Lit component testing', () => {
5
+ it('should open the sheet', async () => {
6
+ render(html `
7
+ <bottom-sheet></bottom-sheet>,
8
+ `, document.body);
9
+ const sheet = await $('bottom-sheet');
10
+ it('should open sheet', async () => {
11
+ // sheet.showSheet(true);
12
+ await expect($(sheet)).toHaveAttr('open');
13
+ });
14
+ });
15
+ });
@@ -1 +1 @@
1
- import "./bottom-sheet.js";
1
+ import './bottom-sheet';
@@ -1,9 +1,10 @@
1
1
  import { EventEmitter } from '../../utils/EventEmitter';
2
+ import { LitElement } from 'lit';
3
+ import { client } from '../../utils/types';
2
4
  type EventMap = {
3
5
  click: [hex: string];
4
6
  move: [e: client];
5
7
  };
6
- import { LitElement } from 'lit';
7
8
  export declare class CanvasBase extends LitElement {
8
9
  canvas: HTMLCanvasElement;
9
10
  protected context: CanvasRenderingContext2D;
@@ -1 +1 @@
1
- {"version":3,"file":"canvas-base.d.ts","sourceRoot":"","sources":["../../../src/components/canvas/canvas-base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAGxD,KAAK,QAAQ,GAAG;IACf,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACrB,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;CAClB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAa,MAAM,KAAK,CAAC;AAG5C,qBACa,UAAW,SAAQ,UAAU;IAEzC,MAAM,EAAG,iBAAiB,CAAC;IAE3B,SAAS,CAAC,OAAO,EAAG,wBAAwB,CAAC;IAEtC,OAAO,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAgC;IAEtE,oBAAoB,IAAI,IAAI;IAK5B,MAAM;IAsBC,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAWpD"}
1
+ {"version":3,"file":"canvas-base.d.ts","sourceRoot":"","sources":["../../../src/components/canvas/canvas-base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,OAAO,EAAE,UAAU,EAAa,MAAM,KAAK,CAAC;AAE5C,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C,KAAK,QAAQ,GAAG;IACf,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACrB,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;CAClB,CAAC;AAEF,qBACa,UAAW,SAAQ,UAAU;IAEzC,MAAM,EAAG,iBAAiB,CAAC;IAE3B,SAAS,CAAC,OAAO,EAAG,wBAAwB,CAAC;IAEtC,OAAO,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAgC;IAEtE,oBAAoB,IAAI,IAAI;IAK5B,MAAM;IAsBC,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAWpD"}
@@ -1,48 +1,60 @@
1
- import m from "colorsea";
2
- import { EventEmitter as u } from "../../utils/EventEmitter.js";
3
- import { ref as f } from "lit/directives/ref.js";
4
- import { LitElement as h, html as x } from "lit";
5
- import { query as d, customElement as g } from "lit/decorators.js";
6
- var C = Object.defineProperty, b = Object.getOwnPropertyDescriptor, v = (i, e, r, a) => {
7
- for (var t = a > 1 ? void 0 : a ? b(e, r) : e, n = i.length - 1, s; n >= 0; n--)
8
- (s = i[n]) && (t = (a ? s(e, r, t) : s(t)) || t);
9
- return a && t && C(e, r, t), t;
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
10
6
  };
11
- let c = class extends h {
12
- constructor() {
13
- super(...arguments), this.emitter = new u();
14
- }
15
- disconnectedCallback() {
16
- super.disconnectedCallback(), this.emitter.clear();
17
- }
18
- render() {
19
- return x`
20
- <canvas
21
- @click=${(e) => this.emitter.emit("click", this.getCanvasHex(e.clientX, e.clientY))}
7
+ import colorsea from 'colorsea';
8
+ import { EventEmitter } from '../../utils/EventEmitter';
9
+ import { ref } from 'lit/directives/ref.js';
10
+ import { LitElement, html } from 'lit';
11
+ import { customElement, query } from 'lit/decorators.js';
12
+ let CanvasBase = class CanvasBase extends LitElement {
13
+ constructor() {
14
+ super(...arguments);
15
+ this.emitter = new EventEmitter();
16
+ }
17
+ disconnectedCallback() {
18
+ super.disconnectedCallback();
19
+ this.emitter.clear();
20
+ }
21
+ render() {
22
+ let canvasRef = (canvas) => {
23
+ if (!(canvas instanceof HTMLCanvasElement))
24
+ return;
25
+ this.context = canvas.getContext('2d', { willReadFrequently: true, alpha: true });
26
+ };
27
+ return html `
28
+ <canvas
29
+ @click=${(e) => this.emitter.emit('click', this.getCanvasHex(e.clientX, e.clientY))}
22
30
  @pointermove=${(e) => {
23
- e.isPrimary && this.emitter.emit("move", { x: e.clientX, y: e.clientY });
24
- }}
25
- ${f((e) => {
26
- e instanceof HTMLCanvasElement && (this.context = e.getContext("2d", { willReadFrequently: !0, alpha: !0 }));
27
- })}
28
- >
29
- </canvas>
31
+ // Track only the first touch
32
+ if (!e.isPrimary)
33
+ return;
34
+ this.emitter.emit('move', { x: e.clientX, y: e.clientY });
35
+ }}
36
+ ${ref(canvasRef)}
37
+ >
38
+ </canvas>
30
39
  `;
31
- }
32
- // Retrieves the hex from the mouse position
33
- getCanvasHex(i, e) {
34
- const r = this.canvas.getBoundingClientRect();
35
- let a = i - r.left, t = e - r.top, [n, s, o, p] = this.context.getImageData(a, t, 1, 1).data;
36
- const l = Math.round(p / 255 * 100);
37
- return l !== 255 ? m([n, s, o, l]).hex() : m([n, s, o]).hex();
38
- }
39
- };
40
- v([
41
- d("canvas")
42
- ], c.prototype, "canvas", 2);
43
- c = v([
44
- g("canvas-base")
45
- ], c);
46
- export {
47
- c as CanvasBase
40
+ }
41
+ // Retrieves the hex from the mouse position
42
+ getCanvasHex(clientX, clientY) {
43
+ const canvasBounds = this.canvas.getBoundingClientRect();
44
+ let x = clientX - canvasBounds.left;
45
+ let y = clientY - canvasBounds.top;
46
+ let [r, g, b, a] = this.context.getImageData(x, y, 1, 1).data;
47
+ // Convert alpha from 0-255 to 0-1
48
+ const alpha = Math.round((a / 255) * 100);
49
+ // Hide alpha value if not an alpha color
50
+ let hex = alpha !== 255 ? colorsea([r, g, b, alpha]).hex() : colorsea([r, g, b]).hex();
51
+ return hex;
52
+ }
48
53
  };
54
+ __decorate([
55
+ query('canvas')
56
+ ], CanvasBase.prototype, "canvas", void 0);
57
+ CanvasBase = __decorate([
58
+ customElement('canvas-base')
59
+ ], CanvasBase);
60
+ export { CanvasBase };
@@ -1 +1 @@
1
- {"version":3,"file":"canvas-gradient.d.ts","sourceRoot":"","sources":["../../../src/components/canvas/canvas-gradient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAGjE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,KAAK,CAAC;AAErC,qBACa,cAAe,SAAQ,UAAU;IAE7C,MAAM,EAAE,MAAM,EAAE,CAAM;IAGtB,MAAM,EAAE,MAAM,CAAK;IAGnB,KAAK,EAAE,MAAM,CAAK;IAGlB,SAAS,EAAE,SAAS,CAAoB;IAExC,SAAS,CAAC,OAAO,CAAC,kBAAkB,EAAE,cAAc,GAAG,IAAI;IAM3D;;OAEG;IACI,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS;CAyB3F;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,qBAAqB;QAC9B,iBAAiB,EAAE,cAAc,CAAC;KAClC;CACD"}
1
+ {"version":3,"file":"canvas-gradient.d.ts","sourceRoot":"","sources":["../../../src/components/canvas/canvas-gradient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAEjE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,KAAK,CAAC;AAGrC,qBACa,cAAe,SAAQ,UAAU;IAE7C,MAAM,EAAE,MAAM,EAAE,CAAM;IAGtB,MAAM,EAAE,MAAM,CAAK;IAGnB,KAAK,EAAE,MAAM,CAAK;IAGlB,SAAS,EAAE,SAAS,CAAoB;IAExC,SAAS,CAAC,OAAO,CAAC,kBAAkB,EAAE,cAAc,GAAG,IAAI;IAM3D;;OAEG;IACI,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS;CAyB3F;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,qBAAqB;QAC9B,iBAAiB,EAAE,cAAc,CAAC;KAClC;CACD"}
@@ -1,47 +1,63 @@
1
- import { Direction as l } from "../color-palette/color-palette-utils.js";
2
- import d from "validate-color";
3
- import { property as c, customElement as f } from "lit/decorators.js";
4
- import { CanvasBase as u } from "./canvas-base.js";
5
- var m = Object.defineProperty, v = Object.getOwnPropertyDescriptor, p = (r, e, o, i) => {
6
- for (var t = i > 1 ? void 0 : i ? v(e, o) : e, s = r.length - 1, a; s >= 0; s--)
7
- (a = r[s]) && (t = (i ? a(e, o, t) : a(t)) || t);
8
- return i && t && m(e, o, t), t;
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
9
6
  };
10
- let n = class extends u {
11
- constructor() {
12
- super(...arguments), this.colors = [], this.height = 0, this.width = 0, this.direction = l.Column;
13
- }
14
- updated(r) {
15
- super.updated(r), this.createGradient(this.colors, this.height, this.width, this.direction);
16
- }
17
- /**
18
- * Creates a new gradient canvas
19
- */
20
- createGradient(r, e, o, i) {
21
- if (!this.context) return;
22
- this.canvas.height = e, this.canvas.width = o;
23
- let t = i === l.Column ? this.context.createLinearGradient(0, 0, o, 0) : this.context.createLinearGradient(0, 0, 0, e), s = [];
24
- for (const [a, h] of r.entries())
25
- d(h) && (t.addColorStop(a / (r.length - 1), h), s.push(h));
26
- if (s.length <= 1) throw new Error("There are not enough valid color stops to create the gradient.");
27
- this.context.fillStyle = t || "#000", this.context.fillRect(0, 0, o, e), this.canvas.classList.add("gradient"), this.canvas.style.setProperty("--palette-column-flex-basis", (e / r.length / 2).toString() + "px");
28
- }
29
- };
30
- p([
31
- c({ type: Array })
32
- ], n.prototype, "colors", 2);
33
- p([
34
- c({ type: Number })
35
- ], n.prototype, "height", 2);
36
- p([
37
- c({ type: Number })
38
- ], n.prototype, "width", 2);
39
- p([
40
- c({ type: String })
41
- ], n.prototype, "direction", 2);
42
- n = p([
43
- f("canvas-gradient")
44
- ], n);
45
- export {
46
- n as CanvasGradient
7
+ import { Direction } from '../color-palette/color-palette-utils';
8
+ import { customElement, property } from 'lit/decorators.js';
9
+ import { CanvasBase } from './canvas-base';
10
+ import { isColorValid } from '../../utils/basicUtils';
11
+ let CanvasGradient = class CanvasGradient extends CanvasBase {
12
+ constructor() {
13
+ super(...arguments);
14
+ this.colors = [];
15
+ this.height = 0;
16
+ this.width = 0;
17
+ this.direction = Direction.Column;
18
+ }
19
+ updated(_changedProperties) {
20
+ super.updated(_changedProperties);
21
+ this.createGradient(this.colors, this.height, this.width, this.direction);
22
+ }
23
+ /**
24
+ * Creates a new gradient canvas
25
+ */
26
+ createGradient(colors, height, width, direction) {
27
+ if (!this.context)
28
+ return;
29
+ this.canvas.height = height;
30
+ this.canvas.width = width;
31
+ let gradient = direction === Direction.Column ? this.context.createLinearGradient(0, 0, width, 0) : this.context.createLinearGradient(0, 0, 0, height);
32
+ let colorStops = [];
33
+ for (const [i, color] of colors.entries()) {
34
+ // Skip non-colors, even with override enabled. This prevents errors, especially dealing with css-variables which cannot be parsed at run-time.
35
+ if (isColorValid(color)) {
36
+ gradient.addColorStop(i / (colors.length - 1), color);
37
+ colorStops.push(color);
38
+ }
39
+ }
40
+ if (colorStops.length <= 1)
41
+ throw new Error('There are not enough valid color stops to create the gradient.');
42
+ this.context.fillStyle = gradient || '#000';
43
+ this.context.fillRect(0, 0, width, height);
44
+ this.canvas.classList.add('gradient');
45
+ this.canvas.style.setProperty('--palette-column-flex-basis', (height / colors.length / 2).toString() + 'px');
46
+ }
47
47
  };
48
+ __decorate([
49
+ property({ type: Array })
50
+ ], CanvasGradient.prototype, "colors", void 0);
51
+ __decorate([
52
+ property({ type: Number })
53
+ ], CanvasGradient.prototype, "height", void 0);
54
+ __decorate([
55
+ property({ type: Number })
56
+ ], CanvasGradient.prototype, "width", void 0);
57
+ __decorate([
58
+ property({ type: String })
59
+ ], CanvasGradient.prototype, "direction", void 0);
60
+ CanvasGradient = __decorate([
61
+ customElement('canvas-gradient')
62
+ ], CanvasGradient);
63
+ export { CanvasGradient };
@@ -1,117 +1,164 @@
1
- import { css as p, html as w } from "lit";
2
- import { query as u, property as m, customElement as f } from "lit/decorators.js";
3
- import y from "quantize";
4
- import { CanvasBase as v } from "./canvas-base.js";
5
- var x = Object.defineProperty, L = Object.getOwnPropertyDescriptor, h = (e, a, i, s) => {
6
- for (var t = s > 1 ? void 0 : s ? L(a, i) : a, r = e.length - 1, n; r >= 0; r--)
7
- (n = e[r]) && (t = (s ? n(a, i, t) : n(t)) || t);
8
- return s && t && x(a, i, t), t;
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
9
6
  };
10
- let o = class extends v {
11
- constructor() {
12
- super(...arguments), this.imageURL = "", this.smoothing = !1, this.loading = !0, this.width = 0, this.height = 0, this.waitForLoading = () => new Promise((e) => {
13
- const a = setInterval(() => {
14
- this.loading || (clearInterval(a), e(!0));
15
- }, 100);
16
- });
17
- }
18
- firstUpdated(e) {
19
- super.firstUpdated(e), this.canvas.classList.add("image"), this.context.imageSmoothingEnabled = this.smoothing;
20
- }
21
- render() {
22
- return w`
23
- ${super.render()}
24
- <img
25
- src=${this.imageURL}
26
- crossorigin='anonymous'
27
- @load=${() => this.loading = !1}
28
- >
7
+ import { html, css } from 'lit';
8
+ import { customElement, property, query } from 'lit/decorators.js';
9
+ import quantize from 'quantize';
10
+ import { CanvasBase } from './canvas-base';
11
+ let CanvasImage = class CanvasImage extends CanvasBase {
12
+ constructor() {
13
+ super(...arguments);
14
+ this.imageURL = '';
15
+ this.smoothing = false;
16
+ this.loading = true;
17
+ this.width = 0;
18
+ this.height = 0;
19
+ /**
20
+ * Waits for loading variable to equal true
21
+ */
22
+ this.waitForLoading = () => new Promise((resolve) => {
23
+ const checkLoading = setInterval(() => {
24
+ if (!this.loading) {
25
+ clearInterval(checkLoading);
26
+ resolve(true);
27
+ }
28
+ }, 100);
29
+ });
30
+ }
31
+ firstUpdated(_changedProperties) {
32
+ super.firstUpdated(_changedProperties);
33
+ this.canvas.classList.add('image');
34
+ this.context.imageSmoothingEnabled = this.smoothing;
35
+ }
36
+ render() {
37
+ // Allows CORS requests for images from different domains
38
+ return html `
39
+ ${super.render()}
40
+ <img
41
+ src=${this.imageURL}
42
+ crossorigin='anonymous'
43
+ @load=${() => this.loading = false}
44
+ >
29
45
  `;
30
- }
31
- /**
32
- * Updates & loads the canvas image.
33
- * Attempts to preserve aspect ratio based on width.
34
- * @param width The canvas width
35
- * @param height The canvas height
36
- */
37
- updateImage(e, a, i) {
38
- this.image.src = e, this.waitForLoading().then(() => {
39
- const s = this.image.naturalHeight / this.image.naturalWidth;
40
- let t = a, r = t * s;
41
- r > i && (t = i / s, r = i), this.width = this.canvas.width = t, this.height = this.canvas.height = r, this.context.drawImage(this.image, 0, 0, t, r);
42
- });
43
- }
44
- /**
45
- * Gets the most frequent colors in an image
46
- * @param numColors Number of colors to return
47
- * @param quality Artificially reduce number of pixels (higher = less accurate but faster)
48
- * @returns Most frequent colors
49
- */
50
- async getPalette(e = 7, a = 10) {
51
- const i = e <= 7 ? e : e + 1;
52
- await this.waitForLoading();
53
- const s = await this.createPixelArray(a);
54
- if (!s) return null;
55
- const t = y(s, i);
56
- return t ? t.palette() : [];
57
- }
58
- /**
59
- * Creates an array of pixels from the image
60
- * Inspired by colorthief
61
- * @param quality Artificially reduce number of pixels (higher = less accurate but faster)
62
- * @returns
63
- */
64
- async createPixelArray(e) {
65
- var t;
66
- await this.waitForLoading();
67
- const a = [], i = (t = await this.getImageData()) == null ? void 0 : t.data;
68
- if (!i) return null;
69
- const s = this.height * this.width;
70
- for (let r = 0; r < s; r += e) {
71
- const n = r * 4, [g, c, l, d] = [i[n + 0], i[n + 1], i[n + 2], i[n + 3]];
72
- (typeof d > "u" || d >= 125) && (g > 250 && c > 250 && l > 250 || a.push([g, c, l]));
73
46
  }
74
- return a;
75
- }
76
- /**
77
- * Gets the image data from the canvas
78
- */
79
- async getImageData(e = 0, a = 0) {
80
- try {
81
- return await this.waitForLoading(), this.context.getImageData(e, a, this.width, this.height);
82
- } catch {
83
- throw new Error("Failed to get image data.");
47
+ /**
48
+ * Updates & loads the canvas image.
49
+ * Attempts to preserve aspect ratio based on width.
50
+ * @param width The canvas width
51
+ * @param height The canvas height
52
+ */
53
+ updateImage(imageURL, width, height) {
54
+ // Set URL
55
+ this.image.src = imageURL;
56
+ // Wait for image to load before calculating dimensions & drawing image to canvas
57
+ this.waitForLoading().then(() => {
58
+ // Calculate the new height based on the aspect ratio
59
+ const aspectRatio = this.image.naturalHeight / this.image.naturalWidth;
60
+ let newWidth = width;
61
+ let newHeight = newWidth * aspectRatio;
62
+ // Ensure the new height fits within the canvas height
63
+ if (newHeight > height) {
64
+ // Adjust width if height exceeds canvas height
65
+ newWidth = height / aspectRatio;
66
+ newHeight = height;
67
+ }
68
+ this.width = this.canvas.width = newWidth;
69
+ this.height = this.canvas.height = newHeight;
70
+ this.context.drawImage(this.image, 0, 0, newWidth, newHeight);
71
+ });
72
+ }
73
+ /**
74
+ * Gets the most frequent colors in an image
75
+ * @param numColors Number of colors to return
76
+ * @param quality Artificially reduce number of pixels (higher = less accurate but faster)
77
+ * @returns Most frequent colors
78
+ */
79
+ async getPalette(numColors = 7, quality = 10) {
80
+ /*
81
+ Quantize has an issue with number of colors which has been addressed here https://github.com/olivierlesnicki/quantize/issues/9
82
+ `nColors` is a simple fix for this.
83
+ */
84
+ const nColors = numColors <= 7 ? numColors : numColors + 1;
85
+ // Wait for image to load
86
+ await this.waitForLoading();
87
+ // Get an array of pixels
88
+ const pixels = await this.createPixelArray(quality);
89
+ if (!pixels)
90
+ return null;
91
+ // Reduce pixels array to a small number of the most common colors
92
+ const colorMap = quantize(pixels, nColors);
93
+ // Return palette
94
+ return colorMap ? colorMap.palette() : [];
95
+ }
96
+ /**
97
+ * Creates an array of pixels from the image
98
+ * Inspired by colorthief
99
+ * @param quality Artificially reduce number of pixels (higher = less accurate but faster)
100
+ * @returns
101
+ */
102
+ async createPixelArray(quality) {
103
+ await this.waitForLoading();
104
+ const pixelArray = [];
105
+ const imageData = (await this.getImageData())?.data;
106
+ if (!imageData)
107
+ return null;
108
+ // Get number of pixels in image
109
+ const pixelCount = this.height * this.width;
110
+ for (let i = 0; i < pixelCount; i += quality) {
111
+ // Offset to correct starting position of each pixel
112
+ const offset = i * 4;
113
+ const [r, g, b, a] = [imageData[offset + 0], imageData[offset + 1], imageData[offset + 2], imageData[offset + 3]];
114
+ // If pixel is mostly opaque and not white
115
+ if (typeof a === 'undefined' || a >= 125) {
116
+ if (!(r > 250 && g > 250 && b > 250)) {
117
+ pixelArray.push([r, g, b]);
118
+ }
119
+ }
120
+ }
121
+ return pixelArray;
122
+ }
123
+ /**
124
+ * Gets the image data from the canvas
125
+ */
126
+ async getImageData(x = 0, y = 0) {
127
+ try {
128
+ await this.waitForLoading();
129
+ return this.context.getImageData(x, y, this.width, this.height);
130
+ }
131
+ catch (e) {
132
+ throw new Error('Failed to get image data.');
133
+ }
84
134
  }
85
- }
86
135
  };
87
- o.styles = [
88
- p`
89
- :host {
90
- display: block;
91
- }
92
-
93
- /* Hide when width hasn't been set */
94
- canvas:not([width]) {
95
- display: none;
96
- }
97
-
98
- img {
99
- display: none;
100
- }
101
- `
136
+ CanvasImage.styles = [
137
+ css `
138
+ :host {
139
+ display: block;
140
+ }
141
+
142
+ /* Hide when width hasn't been set */
143
+ canvas:not([width]) {
144
+ display: none;
145
+ }
146
+
147
+ img {
148
+ display: none;
149
+ }
150
+ `,
102
151
  ];
103
- h([
104
- u("img")
105
- ], o.prototype, "image", 2);
106
- h([
107
- m({ type: String })
108
- ], o.prototype, "imageURL", 2);
109
- h([
110
- m({ type: Boolean })
111
- ], o.prototype, "smoothing", 2);
112
- o = h([
113
- f("canvas-image")
114
- ], o);
115
- export {
116
- o as CanvasImage
117
- };
152
+ __decorate([
153
+ query('img')
154
+ ], CanvasImage.prototype, "image", void 0);
155
+ __decorate([
156
+ property({ type: String })
157
+ ], CanvasImage.prototype, "imageURL", void 0);
158
+ __decorate([
159
+ property({ type: Boolean })
160
+ ], CanvasImage.prototype, "smoothing", void 0);
161
+ CanvasImage = __decorate([
162
+ customElement('canvas-image')
163
+ ], CanvasImage);
164
+ export { CanvasImage };
@@ -1,2 +1,2 @@
1
- import "./canvas-gradient.js";
2
- import "./canvas-image.js";
1
+ import './canvas-gradient';
2
+ import './canvas-image';