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/README.md +6 -5
- package/ext/css/src/index.ts +4 -6
- package/ext/css/src/structure/$CSSKeyframeRule.ts +2 -1
- package/ext/css/src/structure/$CSSKeyframesRule.ts +2 -1
- package/ext/html/html.ts +24 -58
- package/ext/html/node/$Anchor.ts +19 -0
- package/ext/html/node/$Canvas.ts +16 -0
- package/ext/html/node/$Dialog.ts +16 -0
- package/ext/html/node/$Form.ts +16 -0
- package/ext/html/node/$Image.ts +19 -0
- package/ext/html/node/$Input.ts +16 -0
- package/ext/html/node/$Label.ts +16 -0
- package/ext/html/node/$Media.ts +16 -0
- package/ext/html/node/$OptGroup.ts +16 -0
- package/ext/html/node/$Option.ts +16 -0
- package/ext/html/node/$Select.ts +16 -0
- package/ext/html/node/$TextArea.ts +16 -0
- package/ext/router/README.md +81 -0
- package/ext/router/index.ts +64 -0
- package/ext/router/node/Page.ts +27 -0
- package/ext/router/node/Route.ts +53 -0
- package/ext/router/node/Router.ts +104 -0
- package/ext/router/node/RouterAnchor.ts +8 -0
- package/ext/ssr/env.ts +61 -0
- package/ext/ssr/index.ts +47 -0
- package/ext/ssr/package.json +10 -0
- package/package.json +6 -4
- package/src/core.ts +38 -25
- package/src/global.ts +2 -0
- package/src/lib/assignHelper.ts +11 -13
- package/src/node/$Element.ts +92 -15
- package/src/node/$Node.ts +73 -52
- package/src/node/node.ts +6 -5
- package/src/structure/Signal.ts +2 -2
- package/tsconfig.json +1 -1
- package/ext/html/node/type.ts +0 -96
- /package/{ext/html → src}/node/$HTMLElement.ts +0 -0
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
32
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
set((
|
|
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
|
-
|
|
75
|
-
|
|
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 $
|
|
80
|
-
export type $
|
|
81
|
-
export type $NodeContentResolver<T extends $Node> =
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
assignHelper(Node, $Node);
|
|
10
|
+
assignHelper(Text, $Text);
|
|
11
|
+
assignHelper(Element, $Element);
|
|
12
|
+
assignHelper(HTMLElement, $HTMLElement);
|
package/src/structure/Signal.ts
CHANGED
|
@@ -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
|
|
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
package/ext/html/node/type.ts
DELETED
|
@@ -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
|