amateras 0.3.0 → 0.4.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.
package/src/node/$Node.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { _Array_from, _document, _instanceof, _JSON_stringify, forEach, isFunction, isNull, isObject, isUndefined } from "#lib/native";
1
+ import { chain } from "#lib/chain";
2
+ import { _document } from "#lib/env";
3
+ import { _Array_from, _instanceof, _JSON_stringify, forEach, isFunction, isNull, isObject, isUndefined } from "#lib/native";
2
4
  import { Signal } from "#structure/Signal";
3
5
 
4
6
  export class $Node {
@@ -10,84 +12,100 @@ export class $Node {
10
12
  }
11
13
 
12
14
  content(children: $NodeContentResolver<this>) {
13
- forEach(_Array_from(this.childNodes), node => node.remove());
14
- return this.insert(children);
15
+ return chain(this, null, null, children, children => {
16
+ forEach(this.childNodes, node => node.remove());
17
+ this.insert(children);
18
+ })
15
19
  }
16
20
 
17
21
  insert(resolver: $NodeContentResolver<this>, position = -1) {
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.childNodes).filter(node => node.nodeType !== node.TEXT_NODE).at(position);
22
- forEach($.toArray(children), child => {
23
- if (!child) return;
24
- if (_instanceof(child, Array)) this.insert(child);
25
- else if (!positionChild) this.appendChild(child.node);
26
- else this.insertBefore(child.node, position < 0 ? positionChild.nextSibling : positionChild);
27
- })
28
- }
29
22
  // process nodes
30
- for (const child of $.toArray(resolver)) !isUndefined(child) && appendChild(processContent(this, child))
23
+ forEach($.toArray(resolver), resolve_child => forEach($Node.process(this, resolve_child), $node => $Node.append(this, $node, position)));
31
24
  return this;
32
25
  }
33
26
 
34
- await<T>(promise: Promise<T>, callback: ($node: this, result: T) => void): this {
35
- return promise.then(result => callback(this, result)), this;
27
+ await<T>(promise: OrPromise<T>, callback: ($node: this, result: T) => void): this {
28
+ if (_instanceof(promise, Promise)) promise.then(result => callback(this, result));
29
+ else callback(this, promise);
30
+ return this;
36
31
  }
37
32
 
38
33
  replace($node: $NodeContentResolver<$Node>) {
39
- if (!$node) return this;
40
- this.replaceWith(
41
- ...$.toArray(processContent(this, $node)).filter($node => $node).map($node => $node?.node) as Node[]
42
- )
34
+ if ($node)
35
+ this.replaceWith(
36
+ ...$.toArray($Node.process(this, $node)).filter($node => $node).map($node => $node?.node) as Node[]
37
+ )
43
38
  return this;
44
39
  }
45
40
 
41
+ inDOM() {
42
+ return _document.contains(this.node);
43
+ }
44
+
46
45
  toString() {
47
46
  return this.textContent();
48
47
  }
49
- }
50
48
 
51
- function processContent<T extends $Node>($node: T, content: $NodeContentResolver<any>): OrArray<$Node | undefined | null> {
52
- if (isUndefined(content)) return;
53
- if (isNull(content)) return content;
54
- // is $Element
55
- if (_instanceof(content, $Node)) return content;
56
- // is Promise
57
- if (_instanceof(content, Promise)) return $('async').await(content, ($async, $child) => $async.replace($child as any));
58
- // is SignalFunction or ContentHandler
59
- if (isFunction(content)) {
60
- const signal = (content as any).signal;
61
- if (_instanceof(signal, Signal)) {
62
- const resolver = (content as $.SignalFunction<any>)();
63
- if (_instanceof(resolver, $Node)) {
64
- // handler signal $Node result
65
- let node = resolver;
66
- const set = (value: any) => {
67
- node.replace(value);
68
- node = value;
49
+ mounted($parent: $Node) {}
50
+
51
+ use<F extends ($ele: this, ...args: any) => void>(callback: F, ...args: F extends ($ele: this, ...args: infer P) => void ? P : never) {
52
+ callback(this, ...args);
53
+ return this;
54
+ }
55
+
56
+ is<T extends (abstract new (...args: any[]) => $Node)>(instance: T): InstanceType<T> | null {
57
+ return _instanceof(this, instance) ? this : null;
58
+ }
59
+
60
+ static process<T extends $Node>($node: T, content: $NodeContentResolver<any>): Array<$Node | undefined | null> {
61
+ if (isUndefined(content) || isNull(content) || _instanceof(content, $Node)) return [content];
62
+ // is Promise
63
+ if (_instanceof(content, Promise)) return [$('async').await(content, ($async, $child) => $async.replace($child as any))];
64
+ // is SignalFunction or ContentHandler
65
+ if (isFunction(content)) {
66
+ const signal = (content as any).signal;
67
+ if (_instanceof(signal, Signal)) {
68
+ const resolver = (content as $.SignalFunction<any>)();
69
+ if (_instanceof(resolver, $Node)) {
70
+ // handler signal $Node result
71
+ let node = resolver;
72
+ const set = (value: any) => {
73
+ node.replace(value);
74
+ node = value;
75
+ }
76
+ signal.subscribe(set);
77
+ return [resolver];
78
+ } else {
79
+ // handler signal other type result
80
+ const $text = _document ? new $Text() : $('signal').attr({ type: typeof signal.value() });
81
+ const set = (value: any) => $text.textContent(isObject(value) ? _JSON_stringify(value) : value);
82
+ if (_instanceof($text, $Text)) $text.signals.add(signal);
83
+ signal.subscribe(set);
84
+ set(resolver);
85
+ return [$text];
69
86
  }
70
- signal.subscribe(set);
71
- return resolver;
72
87
  } else {
73
- // handler signal other type result
74
- const $text = _document ? new $Text() : $('signal').attr({ type: typeof signal.value() });
75
- const set = (value: any) => $text.textContent(isObject(value) ? _JSON_stringify(value) : value);
76
- if (_instanceof($text, $Text)) $text.signals.add(signal);
77
- signal.subscribe(set);
78
- set(resolver);
79
- return $text;
88
+ const _content = content($node) as $NodeContentResolver<$Node>;
89
+ if (_instanceof(_content, Promise)) return this.process($node, _content as any);
90
+ else return $.toArray(_content).map(content => this.process($node, content)).flat();
80
91
  }
81
- } else {
82
- const _content = content($node) as $NodeContentResolver<$Node>;
83
- if (_instanceof(_content, Promise)) return processContent($node, _content as any);
84
- else return $.toArray(_content).map(content => processContent($node, content) as $Node);
92
+ }
93
+ // is nested array
94
+ if (_instanceof(content, Array)) return content.map(c => this.process($node, c)).flat();
95
+ // is string | number | boolean
96
+ return [new $Text(`${content}`)];
97
+ }
98
+
99
+ /** */
100
+ static append($node: $Node, child: $Node | undefined | null, position: number) {
101
+ if (child) {
102
+ // get child node at position
103
+ let positionChild = _Array_from($node.childNodes).at(position);
104
+ if (!positionChild) $node.appendChild(child.node);
105
+ else $node.insertBefore(child.node, position < 0 ? positionChild.nextSibling : positionChild);
106
+ child.mounted($node);
85
107
  }
86
108
  }
87
- // is nested array
88
- if (_instanceof(content, Array)) return content.map(c => processContent($node, c) as $Node)
89
- // is string | number | boolean
90
- return new $Text(`${content}`);
91
109
  }
92
110
 
93
111
  export class $Text extends $Node {
@@ -163,6 +181,12 @@ export interface $Node {
163
181
  remove(): this;
164
182
  /** {@link Node.replaceChild} */
165
183
  replaceWith(...nodes: (Node | string)[]): this;
184
+ /** {@link EventTarget.addEventListener} */
185
+ addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
186
+ /** {@link EventTarget.removeEventListener} */
187
+ removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
188
+ /** {@link EventTarget.dispatchEvent} */
189
+ dispatchEvent(event: Event): boolean;
166
190
 
167
191
  /** {@link Node.nodeValue} */
168
192
  nodeValue(nodeValue: $Parameter<string | null>): this;
@@ -1,12 +1,9 @@
1
1
  import { $HTMLElement } from '#node/$HTMLElement';
2
2
  import { assignHelper } from '#lib/assignHelper';
3
3
  import { $Element } from '#node/$Element';
4
- import { $Node, $Text } from './$Node';
5
-
6
- export * from './$Node';
7
- export * from './$Element';
8
- export * from './$HTMLElement';
4
+ import { $Node, $Text } from '#node/$Node';
9
5
 
6
+ assignHelper(EventTarget, $Node);
10
7
  assignHelper(Node, $Node);
11
8
  assignHelper(Text, $Text);
12
9
  assignHelper(Element, $Element);