web-signature 0.0.1 → 0.0.3

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.
@@ -0,0 +1 @@
1
+ class e{components={};constructor(){}contact(e){let n=document.querySelector(e);if(!n)throw new Error(`Element not found for selector: ${e}`);let t=document.createElement("div");t.innerHTML=n.innerHTML,this.render(t),n.replaceChildren(...Array.from(t.childNodes))}add(e){this.components[e.name]&&console.warn(new Error(`Component with name ${e.name} already exists.`)),this.components[e.name]=e}render(e){for(const n of Object.keys(this.components)){let t=this.components[n];for(const r of Array.from(e.querySelectorAll(n))){let e=new t;e.onInit(),r instanceof HTMLElement&&(e.data=r.innerHTML.trim());let o=document.createElement("template");if(o.innerHTML=e.render().trim(),o.content.children.length>1)throw new Error(`Component '${n}' must render a single root element.`);this.render(o),e.onRender();let c=o.content.firstElementChild;r.replaceWith(o.content),e.onMount(c)}}}}class n{data}function t(){return new e}export{n as Component,e as Signature,t as default};
package/dist/Component.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export default class Component {
2
+ data;
2
3
  }
package/dist/Signature.js CHANGED
@@ -2,6 +2,10 @@ export default class Signature {
2
2
  components = {};
3
3
  constructor() {
4
4
  }
5
+ /**
6
+ * Starts rendering in the specified area.
7
+ * @param {string} selector The selector of the element where the signature should be rendered.
8
+ */
5
9
  contact(selector) {
6
10
  let mainFrame = document.querySelector(selector);
7
11
  if (!mainFrame) {
@@ -12,6 +16,10 @@ export default class Signature {
12
16
  this.render(secondaryFrame);
13
17
  mainFrame.replaceChildren(...Array.from(secondaryFrame.childNodes));
14
18
  }
19
+ /**
20
+ * Adds a component to the signature.
21
+ * @param {ComponentConstructor} component The component to add.
22
+ */
15
23
  add(component) {
16
24
  if (this.components[component.name]) {
17
25
  console.warn(new Error(`Component with name ${component.name} already exists.`));
@@ -19,15 +27,25 @@ export default class Signature {
19
27
  this.components[component.name] = component;
20
28
  }
21
29
  render(frame) {
22
- Object.keys(this.components).forEach((com) => {
30
+ for (const com of Object.keys(this.components)) {
23
31
  let component = this.components[com];
24
- frame.querySelectorAll(com).forEach((el) => {
32
+ for (const el of Array.from(frame.querySelectorAll(com))) {
25
33
  let renderer = new component();
34
+ renderer.onInit(); // lifecycle hook
35
+ if (el instanceof HTMLElement) {
36
+ renderer.data = el.innerHTML.trim();
37
+ }
26
38
  let body = document.createElement("template");
27
39
  body.innerHTML = renderer.render().trim();
40
+ if (body.content.children.length > 1) {
41
+ throw new Error(`Component '${com}' must render a single root element.`);
42
+ }
28
43
  this.render(body);
44
+ renderer.onRender(); // lifecycle hook
45
+ let mountEl = body.content.firstElementChild;
29
46
  el.replaceWith(body.content);
30
- });
31
- });
47
+ renderer.onMount(mountEl); // lifecycle hook
48
+ }
49
+ }
32
50
  }
33
51
  }
@@ -1,6 +1,10 @@
1
1
  import component from "./types/Component.js";
2
2
  export default abstract class Component implements component {
3
3
  abstract readonly name: string;
4
+ data?: string;
4
5
  abstract render(): string;
6
+ abstract onInit(): void;
7
+ abstract onRender(): void;
8
+ abstract onMount(el: Element): void;
5
9
  }
6
10
  export type ComponentConstructor = new () => Component;
@@ -2,7 +2,15 @@ import { ComponentConstructor } from "./Component.js";
2
2
  export default class Signature {
3
3
  private components;
4
4
  constructor();
5
+ /**
6
+ * Starts rendering in the specified area.
7
+ * @param {string} selector The selector of the element where the signature should be rendered.
8
+ */
5
9
  contact(selector: string): void;
10
+ /**
11
+ * Adds a component to the signature.
12
+ * @param {ComponentConstructor} component The component to add.
13
+ */
6
14
  add(component: ComponentConstructor): void;
7
15
  private render;
8
16
  }
package/dist/d/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  import Signature from './Signature.js';
2
2
  export default function (): Signature;
3
+ export { default as Signature } from './Signature.js';
4
+ export { default as Component } from './Component.js';
@@ -1,5 +1,26 @@
1
1
  interface Component {
2
2
  name: string;
3
+ /**
4
+ * Optional content that is specified in the component tag.
5
+ */
6
+ data?: string;
7
+ /**
8
+ * Returns the component as a string.
9
+ * @returns {string} The rendered component as a string.
10
+ */
3
11
  render(): string;
12
+ /**
13
+ * Lifecycle hook that is called when the component is initialized.
14
+ */
15
+ onInit(): void;
16
+ /**
17
+ * Lifecycle hook that is called when the component is rendered.
18
+ */
19
+ onRender(): void;
20
+ /**
21
+ * Lifecycle hook that is called when the component is mounted to the DOM.
22
+ * @param {Element} el The element to which the component is mounted.
23
+ */
24
+ onMount(el: Element): void;
4
25
  }
5
26
  export default Component;
package/dist/index.js CHANGED
@@ -2,3 +2,5 @@ import Signature from './Signature.js';
2
2
  export default function () {
3
3
  return new Signature();
4
4
  }
5
+ export { default as Signature } from './Signature.js';
6
+ export { default as Component } from './Component.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-signature",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Primitive and fast framework for rendering web interfaces",
5
5
  "license": "ISC",
6
6
  "author": "PinBib",
@@ -9,15 +9,17 @@
9
9
  "types": "./dist/d/index.d.ts",
10
10
  "module": "./dist/index.js",
11
11
  "scripts": {
12
- "build": "tsc -p tsconfig.json",
12
+ "build": "tsc -p tsconfig.json && rollup -c rollup.config.js",
13
13
  "watch": "tsc -p tsconfig.json --watch"
14
14
  },
15
15
  "files": [
16
+ "bundle",
16
17
  "dist",
17
18
  "src",
18
19
  "README.md",
19
20
  "LICENSE",
20
- "tsconfig.json"
21
+ "tsconfig.json",
22
+ "rollup.config.js"
21
23
  ],
22
24
  "keywords": [
23
25
  "UI",
@@ -52,6 +54,16 @@
52
54
  "types": "./dist/d/Signature.d.ts",
53
55
  "browser": "./dist/Signature.js",
54
56
  "default": "./dist/Signature.js"
57
+ },
58
+ "./bundle": {
59
+ "import": "./bundle/index.js",
60
+ "browser": "./bundle/index.js",
61
+ "default": "./bundle/index.js"
55
62
  }
63
+ },
64
+ "devDependencies": {
65
+ "@rollup/plugin-node-resolve": "^16.0.1",
66
+ "@rollup/plugin-terser": "^0.4.4",
67
+ "rollup": "^4.44.2"
56
68
  }
57
69
  }
@@ -0,0 +1,12 @@
1
+ import {nodeResolve} from '@rollup/plugin-node-resolve';
2
+ import terser from "@rollup/plugin-terser";
3
+
4
+ export default {
5
+ input: "dist/index.js",
6
+ output: {
7
+ file: "bundle/index.js",
8
+ plugins: [terser()]
9
+ },
10
+ external: [],
11
+ plugins: [nodeResolve()]
12
+ };
package/src/Component.ts CHANGED
@@ -2,8 +2,15 @@ import component from "./types/Component.js";
2
2
 
3
3
  export default abstract class Component implements component {
4
4
  abstract readonly name: string;
5
+ data?: string;
5
6
 
6
7
  abstract render(): string;
8
+
9
+ abstract onInit(): void;
10
+
11
+ abstract onRender(): void;
12
+
13
+ abstract onMount(el: Element): void;
7
14
  }
8
15
 
9
16
  export type ComponentConstructor = new () => Component;
package/src/Signature.ts CHANGED
@@ -6,6 +6,10 @@ export default class Signature {
6
6
  constructor() {
7
7
  }
8
8
 
9
+ /**
10
+ * Starts rendering in the specified area.
11
+ * @param {string} selector The selector of the element where the signature should be rendered.
12
+ */
9
13
  public contact(selector: string): void {
10
14
  let mainFrame = document.querySelector(selector);
11
15
 
@@ -21,6 +25,10 @@ export default class Signature {
21
25
  mainFrame.replaceChildren(...Array.from(secondaryFrame.childNodes));
22
26
  }
23
27
 
28
+ /**
29
+ * Adds a component to the signature.
30
+ * @param {ComponentConstructor} component The component to add.
31
+ */
24
32
  public add(component: ComponentConstructor): void {
25
33
  if (this.components[component.name]) {
26
34
  console.warn(new Error(`Component with name ${component.name} already exists.`));
@@ -30,19 +38,33 @@ export default class Signature {
30
38
  }
31
39
 
32
40
  private render(frame: Element): void {
33
- Object.keys(this.components).forEach((com: string) => {
41
+ for (const com of Object.keys(this.components)) {
34
42
  let component = this.components[com];
35
43
 
36
- frame.querySelectorAll(com).forEach((el: Element) => {
44
+ for (const el of Array.from(frame.querySelectorAll(com))) {
37
45
  let renderer: Component = new component();
46
+ renderer.onInit(); // lifecycle hook
47
+
48
+ if (el instanceof HTMLElement) {
49
+ renderer.data = el.innerHTML.trim();
50
+ }
38
51
 
39
52
  let body = document.createElement("template");
40
53
  body.innerHTML = renderer.render().trim();
41
54
 
55
+ if (body.content.children.length > 1) {
56
+ throw new Error(`Component '${com}' must render a single root element.`);
57
+ }
58
+
42
59
  this.render(body);
60
+ renderer.onRender(); // lifecycle hook
61
+
62
+ let mountEl: Element = body.content.firstElementChild as Element;
43
63
 
44
64
  el.replaceWith(body.content);
45
- });
46
- });
65
+
66
+ renderer.onMount(mountEl); // lifecycle hook
67
+ }
68
+ }
47
69
  }
48
70
  }
package/src/index.ts CHANGED
@@ -2,4 +2,7 @@ import Signature from './Signature.js';
2
2
 
3
3
  export default function (): Signature {
4
4
  return new Signature();
5
- }
5
+ }
6
+
7
+ export {default as Signature} from './Signature.js';
8
+ export {default as Component} from './Component.js';
@@ -1,7 +1,32 @@
1
1
  interface Component {
2
2
  name: string;
3
3
 
4
+ /**
5
+ * Optional content that is specified in the component tag.
6
+ */
7
+ data?: string;
8
+
9
+ /**
10
+ * Returns the component as a string.
11
+ * @returns {string} The rendered component as a string.
12
+ */
4
13
  render(): string;
14
+
15
+ /**
16
+ * Lifecycle hook that is called when the component is initialized.
17
+ */
18
+ onInit(): void;
19
+
20
+ /**
21
+ * Lifecycle hook that is called when the component is rendered.
22
+ */
23
+ onRender(): void;
24
+
25
+ /**
26
+ * Lifecycle hook that is called when the component is mounted to the DOM.
27
+ * @param {Element} el The element to which the component is mounted.
28
+ */
29
+ onMount(el: Element): void;
5
30
  }
6
31
 
7
32
  export default Component;