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/README.md +6 -4
- package/ext/css/README.md +19 -0
- package/ext/css/src/index.ts +347 -331
- package/ext/css/src/lib/colorAssign.ts +1 -1
- package/ext/css/src/structure/$CSSContainerRule.ts +13 -0
- package/ext/css/src/structure/$CSSRule.ts +1 -1
- package/ext/css/src/structure/$CSSStyleRule.ts +0 -7
- package/ext/css/src/structure/$CSSVariable.ts +3 -3
- package/ext/html/html.ts +1 -13
- package/ext/i18n/README.md +53 -0
- package/ext/i18n/package.json +10 -0
- package/ext/i18n/src/index.ts +54 -0
- package/ext/i18n/src/node/I18nText.ts +35 -0
- package/ext/i18n/src/structure/I18n.ts +40 -0
- package/ext/i18n/src/structure/I18nDictionary.ts +31 -0
- package/ext/router/index.ts +13 -6
- package/ext/router/node/Page.ts +3 -3
- package/ext/router/node/Route.ts +2 -1
- package/ext/router/node/Router.ts +33 -22
- package/ext/ssr/index.ts +4 -2
- package/package.json +4 -7
- package/src/core.ts +33 -23
- package/src/lib/assign.ts +11 -12
- package/src/lib/assignHelper.ts +9 -8
- package/src/lib/chain.ts +13 -0
- package/src/lib/debounce.ts +7 -0
- package/src/lib/env.ts +2 -0
- package/src/lib/native.ts +25 -35
- package/src/lib/randomId.ts +2 -4
- package/src/lib/sleep.ts +1 -3
- package/src/node/$Element.ts +183 -24
- package/src/node/$HTMLElement.ts +23 -0
- package/src/node/$Node.ts +81 -57
- package/src/{node/node.ts → node.ts} +2 -5
package/src/node/$Node.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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:
|
|
35
|
-
|
|
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 (
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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 '
|
|
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);
|