amateras 0.1.0 → 0.2.0

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/src/node/$Node.ts CHANGED
@@ -2,8 +2,8 @@ import { _Array_from, _instanceof, isFunction, isNull, isObject, isUndefined } f
2
2
  import { Signal } from "#structure/Signal";
3
3
 
4
4
  export class $Node {
5
- node: Node;
6
- constructor(node: Node) {
5
+ node: Node & ChildNode;
6
+ constructor(node: Node & ChildNode) {
7
7
  this.node = node;
8
8
  //@ts-expect-error
9
9
  this.node.$ = this;
@@ -15,67 +15,88 @@ export class $Node {
15
15
  }
16
16
 
17
17
  insert(resolver: $NodeContentResolver<this>, position = -1) {
18
- if (isFunction(resolver)) {
19
- const content = resolver(this);
20
- if (_instanceof(content, Promise)) content.then(content => $Node.insertChild(this.node, content, position))
21
- else $Node.insertChild(this.node, content, position);
22
- } else $Node.insertChild(this.node, resolver, position);
18
+ // insert node helper function for depend position
19
+ const appendChild = (children: OrArray<$Node | undefined | null>) => {
20
+ // get child node at position
21
+ const positionChild = _Array_from(this.node.childNodes).filter(node => node.nodeType !== node.TEXT_NODE).at(position);
22
+ $.orArrayResolver(children).forEach(child => {
23
+ if (!child) return;
24
+ if (_instanceof(child, Array)) this.insert(child);
25
+ else if (!positionChild) this.node.appendChild(child.node);
26
+ else this.insertBefore(child.node, position < 0 ? positionChild.nextSibling : positionChild);
27
+ })
28
+ }
29
+ // process nodes
30
+ for (const child of $.orArrayResolver(resolver)) !isUndefined(child) && appendChild(processContent(this, child))
23
31
  return this;
24
32
  }
25
33
 
26
34
  await<T>(promise: Promise<T>, callback: ($node: this, result: T) => void): this {
27
- promise.then(result => callback(this, result));
28
- return this;
35
+ return promise.then(result => callback(this, result)), this;
29
36
  }
30
37
 
31
- replace($node: $NodeContentTypes | undefined | null) {
32
- if (!$node) return this;
33
- this.node.parentNode?.replaceChild($Node.processContent($node), this.node);
34
- return this;
38
+ remove() {
39
+ return this.node.remove(), this
35
40
  }
36
41
 
37
- static insertChild(node: Node, children: OrArray<OrPromise<$NodeContentTypes>>, position = -1) {
38
- children = $.orArrayResolver(children);
39
- // get child node at position
40
- const positionChild = _Array_from(node.childNodes).filter(node => node.nodeType !== node.TEXT_NODE).at(position);
41
- // insert node helper function for depend position
42
- const append = (child: Node | undefined | null) => {
43
- if (!child) return;
44
- if (!positionChild) node.appendChild(child);
45
- else node.insertBefore(child, position < 0 ? positionChild.nextSibling : positionChild);
46
- }
47
- // process nodes
48
- for (const child of children) !isUndefined(child) && append($Node.processContent(child))
42
+ replace($node: $NodeContentResolver<$Node>) {
43
+ if (!$node) return this.remove();
44
+ const index = _Array_from(this.parentNode!.childNodes).indexOf(this.node) + 1;
45
+ const parentNode = this.parentNode;
46
+ this.remove();
47
+ parentNode?.$.insert($node, index)
48
+ return this;
49
49
  }
50
50
 
51
- static processContent(content: $NodeContentTypes): Node;
52
- static processContent(content: undefined | null): undefined | null;
53
- static processContent(content: OrPromise<undefined | null>): undefined | null;
54
- static processContent(content: OrPromise<$NodeContentTypes>): Node;
55
- static processContent(content: OrPromise<$NodeContentTypes | undefined | null>): Node | undefined | null
56
- static processContent(content: OrPromise<$NodeContentTypes | undefined | null>) {
57
- if (isUndefined(content)) return;
58
- if (isNull(content)) return content;
59
- // is $Element
60
- if (_instanceof(content, $Node)) return content.node;
61
- // is Promise
62
- if (_instanceof(content, Promise)) {
63
- const async = $('async').await(content, ($async, $child) => $async.replace($child));
64
- return async.node;
65
- }
66
- // is SignalFunction
67
- if (isFunction(content) && _instanceof((content as $.SignalFunction<any>).signal ,Signal)) {
68
- const text = new Text();
69
- const set = (value: any) => text.textContent = isObject(value) ? JSON.stringify(value) : value;
70
- (content as $.SignalFunction<any>).signal.subscribe(set);
71
- set((content as $.SignalFunction<any>)());
72
- return text;
51
+ toString() {
52
+ return this.node.textContent;
53
+ }
54
+ }
55
+
56
+ function processContent<T extends $Node>($node: T, content: OrPromise<$NodeContentTypes | $NodeContentHandler<T>>): OrArray<$Node | undefined | null> {
57
+ if (isUndefined(content)) return;
58
+ if (isNull(content)) return content;
59
+ // is $Element
60
+ if (_instanceof(content, $Node)) return content;
61
+ // is Promise
62
+ if (_instanceof(content, Promise)) return $('async').await(content, ($async, $child) => $async.replace($child as any));
63
+ // is SignalFunction or ContentHandler
64
+ if (isFunction(content)) {
65
+ if (_instanceof((content as $.SignalFunction<any>).signal, Signal)) {
66
+ const signalFn = content as $.SignalFunction<any>;
67
+ const $text = document ? new $Text() : $('signal').attr({ type: typeof signalFn.signal.value() });
68
+ const set = (value: any) => $text.textContent(isObject(value) ? JSON.stringify(value) : value);
69
+ if (_instanceof($text, $Text)) $text.signals.add(signalFn.signal);
70
+ signalFn.signal.subscribe(set);
71
+ set(signalFn());
72
+ return $text;
73
+ } else {
74
+ const _content = content($node) as $NodeContentResolver<$Node>;
75
+ if (_instanceof(_content, Promise)) return processContent($node, _content as any);
76
+ else return $.orArrayResolver(_content).map(content => processContent($node, content) as $Node);
73
77
  }
74
- // is string | number | boolean
75
- return new Text(`${content}`);
78
+ }
79
+ // is string | number | boolean
80
+ return new $Text(`${content}`);
81
+ }
82
+
83
+ export class $Text extends $Node {
84
+ signals = new Set<Signal<any>>();
85
+ constructor(textContent?: string) {
86
+ super(new Text(textContent));
76
87
  }
77
88
  }
78
89
 
79
- export type $NodeContentTypes = $Node | string | number | boolean | $.SignalFunction<any>;
80
- export type $NodeContentHandler<T extends $Node> = ($node: T) => OrPromise<OrArray<$NodeContentTypes>>;
81
- export type $NodeContentResolver<T extends $Node> = $NodeContentHandler<T> | OrArray<OrPromise<$NodeContentTypes>>
90
+ export type $NodeContentHandler<T extends $Node> = ($node: T) => OrPromise<$NodeContentResolver<T>>;
91
+ export type $NodeContentTypes = $Node | string | number | boolean | $.SignalFunction<any> | null | undefined;
92
+ export type $NodeContentResolver<T extends $Node> = OrArray<OrPromise<$NodeContentTypes | $NodeContentHandler<T>>>;
93
+
94
+ export interface $Node {
95
+ readonly parentNode?: Node;
96
+ readonly childNodes: NodeListOf<ChildNode>;
97
+ appendChild<T extends Node>(node: T): T;
98
+ insertBefore<T extends Node>(node: T, child: Node | null): T;
99
+
100
+ textContent(content: string | null): this;
101
+ textContent(): string | null;
102
+ }
package/src/node/node.ts CHANGED
@@ -1,11 +1,12 @@
1
+ import { $HTMLElement } from '#node/$HTMLElement';
1
2
  import { assignHelper } from '#lib/assignHelper';
2
3
  import { $Element } from '#node/$Element';
3
- import { $Node } from './$Node';
4
+ import { $Node, $Text } from './$Node';
4
5
 
5
6
  export * from './$Element';
6
7
  export * from './$Node';
7
8
 
8
- assignHelper([
9
- [Node, $Node],
10
- [Element, $Element],
11
- ])
9
+ assignHelper(Node, $Node);
10
+ assignHelper(Text, $Text);
11
+ assignHelper(Element, $Element);
12
+ assignHelper(HTMLElement, $HTMLElement);
@@ -1,4 +1,4 @@
1
- import { isUndefined } from "#lib/native";
1
+ import { _instanceof, isUndefined } from "#lib/native";
2
2
 
3
3
  export class Signal<T> {
4
4
  #value: T;
@@ -16,7 +16,7 @@ export class Signal<T> {
16
16
  Signal.listeners.forEach(fn => fn(this));
17
17
  return this.#value;
18
18
  }
19
- if (resolver instanceof Function) this.value(resolver(this.#value));
19
+ if (_instanceof(resolver, Function)) this.value(resolver(this.#value));
20
20
  else if (!isUndefined(resolver)) {
21
21
  this.#value = resolver;
22
22
  this.emit();
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  // Environment setup & latest features
4
- "lib": ["ESNext", "DOM"],
4
+ "lib": ["ESNext", "DOM", "DOM.Iterable"],
5
5
  "target": "ESNext",
6
6
  "module": "ESNext",
7
7
  "moduleDetection": "force",
@@ -1,96 +0,0 @@
1
- import type { $HTMLElement } from "./$HTMLElement";
2
- import type { Signal } from "#structure/Signal";
3
-
4
- type $Parameter<T> = T | undefined | Signal<T> | Signal<T | undefined>
5
-
6
- declare module '#node/$Element' {
7
- export interface $Element<Ele extends Element> {
8
- /** {@link Element.attributes} */
9
- readonly attributes: NamedNodeMap;
10
- /** {@link Element.clientHeight} */
11
- readonly clientHeight: number;
12
- /** {@link Element.clientLeft} */
13
- readonly clientLeft: number;
14
- /** {@link Element.clientTop} */
15
- readonly clientTop: number;
16
- /** {@link Element.clientWidth} */
17
- readonly clientWidth: number;
18
- /** {@link Element.currentCSSZoom} */
19
- readonly currentCSSZoom: number;
20
- /** {@link Element.localName} */
21
- readonly localName: string;
22
- /** {@link Element.namespaceURI} */
23
- readonly namespaceURI: string | null;
24
- /** {@link Element.prefix} */
25
- readonly prefix: string | null;
26
- /** {@link Element.ownerDocument} */
27
- readonly ownerDocument: Document;
28
- /** {@link Element.scrollHeight} */
29
- readonly scrollHeight: number;
30
- /** {@link Element.scrollWidth} */
31
- readonly scrollWidth: number;
32
- /** {@link Element.shadowRoot} */
33
- readonly shadowRoot: ShadowRoot | null;
34
- /** {@link Element.tagName} */
35
- readonly tagName: string;
36
-
37
- /** {@link Element.classList} */
38
- classList(): DOMTokenList;
39
- classList(value: $Parameter<string>): this;
40
- /** {@link Element.className} */
41
- className(): string;
42
- className(value: $Parameter<string>): this;
43
- /** {@link Element.id} */
44
- id(): string;
45
- id(id: $Parameter<string>): this;
46
- /** {@link Element.innerHTML} */
47
- innerHTML(): string;
48
- innerHTML(innerHTML: $Parameter<string>): this;
49
- /** {@link Element.outerHTML} */
50
- outerHTML(): string;
51
- outerHTML(outerHTML: $Parameter<string>): this;
52
- /** {@link Element.part} */
53
- part(): DOMTokenList;
54
- part(part: $Parameter<string>): this;
55
- /** {@link Element.scrollLeft} */
56
- scrollLeft(): number;
57
- scrollLeft(scrollLeft: $Parameter<number>): this;
58
- /** {@link Element.scrollTop} */
59
- scrollTop(): number;
60
- scrollTop(scrollTop: $Parameter<number>): this;
61
- /** {@link Element.slot} */
62
- slot(): string;
63
- slot(slot: $Parameter<string>): this;
64
- }
65
- }
66
-
67
- export interface $Input extends $HTMLElement<HTMLInputElement> {}
68
- export interface $Anchor extends $HTMLElement<HTMLAnchorElement> {}
69
- export interface $Image extends $HTMLElement<HTMLImageElement> {
70
- src(src: string): this;
71
- src(): string;
72
- }
73
- export interface $Canvas extends $HTMLElement<HTMLCanvasElement> {}
74
- export interface $Dialog extends $HTMLElement<HTMLDialogElement> {}
75
- export interface $Form extends $HTMLElement<HTMLFormElement> {}
76
- export interface $Label extends $HTMLElement<HTMLLabelElement> {}
77
- export interface $Media extends $HTMLElement<HTMLMediaElement> {}
78
- export interface $Select extends $HTMLElement<HTMLSelectElement> {}
79
- export interface $Option extends $HTMLElement<HTMLOptionElement> {}
80
- export interface $OptGroup extends $HTMLElement<HTMLOptGroupElement> {}
81
- export interface $TextArea extends $HTMLElement<HTMLTextAreaElement> {}
82
-
83
- declare module '#core' {
84
- export function $(tagname: 'input'): $Input
85
- export function $(tagname: 'anchor'): $Anchor
86
- export function $(tagname: 'img'): $Image
87
- export function $(tagname: 'dialog'): $Dialog
88
- export function $(tagname: 'form'): $Form
89
- export function $(tagname: 'label'): $Label
90
- export function $(tagname: 'media'): $Media
91
- export function $(tagname: 'select'): $Select
92
- export function $(tagname: 'option'): $Option
93
- export function $(tagname: 'otpgroup'): $OptGroup
94
- export function $(tagname: 'textarea'): $TextArea
95
- export function $(tagname: string): $HTMLElement
96
- }
File without changes