@estjs/template 0.0.11 → 0.0.12
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/dist/template.cjs.js +18 -17
- package/dist/template.cjs.js.map +1 -1
- package/dist/template.d.cts +62 -32
- package/dist/template.d.ts +62 -32
- package/dist/template.dev.cjs.js +190 -110
- package/dist/template.dev.esm.js +190 -111
- package/dist/template.esm.js +6 -6
- package/dist/template.esm.js.map +1 -1
- package/package.json +3 -3
package/dist/template.d.ts
CHANGED
|
@@ -2217,8 +2217,8 @@ declare class TemplateNode$1 implements JSX.Element {
|
|
|
2217
2217
|
template: HTMLTemplateElement;
|
|
2218
2218
|
props: Record<string, unknown>;
|
|
2219
2219
|
key?: string | undefined;
|
|
2220
|
-
treeMap: Map<number, Node>;
|
|
2221
2220
|
constructor(template: HTMLTemplateElement, props: Record<string, unknown>, key?: string | undefined);
|
|
2221
|
+
treeMap: Map<number, Node>;
|
|
2222
2222
|
mounted: boolean;
|
|
2223
2223
|
nodes: Node[];
|
|
2224
2224
|
provides: Record<string, unknown>;
|
|
@@ -2238,27 +2238,31 @@ declare class TemplateNode$1 implements JSX.Element {
|
|
|
2238
2238
|
}
|
|
2239
2239
|
|
|
2240
2240
|
type Hook = 'mounted' | 'destroy';
|
|
2241
|
-
declare class
|
|
2242
|
-
template: EssorComponent;
|
|
2243
|
-
props: Record<string, any>;
|
|
2244
|
-
constructor(template: EssorComponent, props: Record<string, any>);
|
|
2241
|
+
declare class Hooks {
|
|
2245
2242
|
addEventListener(): void;
|
|
2246
2243
|
removeEventListener(): void;
|
|
2247
|
-
static ref:
|
|
2244
|
+
static ref: Hooks | null;
|
|
2248
2245
|
static context: Record<symbol, Signal$1<any>>;
|
|
2249
|
-
|
|
2246
|
+
hooks: Record<Hook, Set<() => void>>;
|
|
2247
|
+
addHook(hook: Hook, cb: () => void): void;
|
|
2248
|
+
getContext<T>(context: symbol | string | number): T | undefined;
|
|
2249
|
+
setContext<T>(context: symbol | string | number, value: T): void;
|
|
2250
|
+
initRef(): void;
|
|
2251
|
+
removeRef(): void;
|
|
2252
|
+
}
|
|
2253
|
+
declare class ComponentNode$1 extends Hooks implements JSX.Element {
|
|
2254
|
+
template: EssorComponent;
|
|
2255
|
+
props: Record<string, any>;
|
|
2256
|
+
key?: string | undefined;
|
|
2257
|
+
constructor(template: EssorComponent, props: Record<string, any>, key?: string | undefined);
|
|
2250
2258
|
private proxyProps;
|
|
2251
|
-
context: Record<symbol | string | number, any>;
|
|
2252
2259
|
emitter: Set<Function>;
|
|
2253
2260
|
mounted: boolean;
|
|
2254
2261
|
rootNode: TemplateNode$1 | null;
|
|
2255
|
-
|
|
2262
|
+
context: Record<symbol | string | number, any>;
|
|
2256
2263
|
private trackMap;
|
|
2257
2264
|
get firstChild(): Node | null;
|
|
2258
2265
|
get isConnected(): boolean;
|
|
2259
|
-
addHook(hook: Hook, cb: () => void): void;
|
|
2260
|
-
getContext<T>(context: symbol | string | number): T | undefined;
|
|
2261
|
-
setContext<T>(context: symbol | string | number, value: T): void;
|
|
2262
2266
|
inheritNode(node: ComponentNode$1): void;
|
|
2263
2267
|
mount(parent: Node, before?: Node | null): Node[];
|
|
2264
2268
|
unmount(): void;
|
|
@@ -2266,7 +2270,8 @@ declare class ComponentNode$1 implements JSX.Element {
|
|
|
2266
2270
|
patchProps(props: Record<string, any>): void;
|
|
2267
2271
|
}
|
|
2268
2272
|
|
|
2269
|
-
declare function h<K extends keyof HTMLElementTagNameMap>(_template: EssorComponent | HTMLTemplateElement | K | '', props: Record<string,
|
|
2273
|
+
declare function h<K extends keyof HTMLElementTagNameMap>(_template: EssorComponent | HTMLTemplateElement | K | '', props: Record<string, unknown>, key?: string): JSX.Element;
|
|
2274
|
+
declare function isComponent(node: unknown): node is ComponentNode$1;
|
|
2270
2275
|
declare function isJsxElement(node: unknown): node is EssorNode;
|
|
2271
2276
|
declare function template(html: string): HTMLTemplateElement;
|
|
2272
2277
|
declare function Fragment(props: {
|
|
@@ -2281,29 +2286,54 @@ interface InjectionKey<T> extends Symbol {
|
|
|
2281
2286
|
}
|
|
2282
2287
|
declare function useProvide<T, K = InjectionKey<T> | string | number>(key: K, value: K extends InjectionKey<infer V> ? V : T): void;
|
|
2283
2288
|
declare function useInject<T, K = InjectionKey<T> | string | number>(key: K, defaultValue?: K extends InjectionKey<infer V> ? V : T): (K extends InjectionKey<infer V> ? V : T) | undefined;
|
|
2284
|
-
|
|
2285
|
-
type Props = Record<string, any>;
|
|
2286
2289
|
/**
|
|
2287
|
-
*
|
|
2288
|
-
*
|
|
2289
|
-
*
|
|
2290
|
-
* @
|
|
2291
|
-
*
|
|
2290
|
+
* Creates a reactive ref that can be used to reference a DOM node
|
|
2291
|
+
* or a component instance within the component function body.
|
|
2292
|
+
*
|
|
2293
|
+
* @returns a reactive ref signal
|
|
2294
|
+
*
|
|
2295
|
+
* @example
|
|
2296
|
+
* const inputRef = useRef(')
|
|
2297
|
+
*
|
|
2298
|
+
* <input ref={inputRef} />
|
|
2299
|
+
*
|
|
2300
|
+
* inputRef.value // input element
|
|
2292
2301
|
*/
|
|
2293
|
-
declare function
|
|
2302
|
+
declare function useRef<T>(): Signal$1<T | null>;
|
|
2303
|
+
|
|
2304
|
+
type Props = Record<string, any>;
|
|
2305
|
+
declare class ServerNode extends Hooks {
|
|
2306
|
+
private template;
|
|
2307
|
+
private props;
|
|
2308
|
+
key?: string | undefined;
|
|
2309
|
+
private childNodesMap;
|
|
2310
|
+
private processedTemplates;
|
|
2311
|
+
constructor(template: string[] | EssorNode | Function, props?: Props, key?: string | undefined);
|
|
2312
|
+
/**
|
|
2313
|
+
* Mount and render the component
|
|
2314
|
+
*/
|
|
2315
|
+
mount(): string;
|
|
2316
|
+
/**
|
|
2317
|
+
* Initialize template entries and props
|
|
2318
|
+
*/
|
|
2319
|
+
private initTemplates;
|
|
2320
|
+
/**
|
|
2321
|
+
* Render component and its children into a string
|
|
2322
|
+
*/
|
|
2323
|
+
render(): string;
|
|
2324
|
+
/**
|
|
2325
|
+
* Render child nodes into a string
|
|
2326
|
+
*/
|
|
2327
|
+
private renderChildren;
|
|
2328
|
+
}
|
|
2329
|
+
type ServerNodeType = (props: Props) => ServerNode;
|
|
2294
2330
|
/**
|
|
2295
|
-
*
|
|
2296
|
-
* @param component - The component function to be rendered.
|
|
2297
|
-
* @param props - The properties to be passed to the component.
|
|
2298
|
-
* @returns The rendered component as a string.
|
|
2331
|
+
* Create ServerNode for server-side generation (SSG)
|
|
2299
2332
|
*/
|
|
2300
|
-
declare function
|
|
2333
|
+
declare function ssg(component: string[] | ServerNodeType, props?: Props): ServerNode;
|
|
2301
2334
|
/**
|
|
2302
|
-
*
|
|
2303
|
-
* @param component - The component function to be rendered.
|
|
2304
|
-
* @param root - The root element in which to render the component.
|
|
2305
|
-
* @param props - The properties to be passed to the component.
|
|
2335
|
+
* Render a component to string for SSR
|
|
2306
2336
|
*/
|
|
2307
|
-
declare function
|
|
2337
|
+
declare function renderToString(component: any, props?: Props): string;
|
|
2308
2338
|
|
|
2309
|
-
export { ComponentNode$1 as ComponentNode, type EssorComponent, type EssorNode, Fragment, type Hook$1 as Hook, type InjectionKey, type NodeTrack, type Output, TemplateNode$1 as TemplateNode, h, isJsxElement, nextTick, onDestroy, onMount,
|
|
2339
|
+
export { ComponentNode$1 as ComponentNode, type EssorComponent, type EssorNode, Fragment, type Hook$1 as Hook, type InjectionKey, type NodeTrack, type Output, TemplateNode$1 as TemplateNode, h, isComponent, isJsxElement, nextTick, onDestroy, onMount, renderToString, ssg, template, useInject, useProvide, useRef };
|
package/dist/template.dev.cjs.js
CHANGED
|
@@ -4,33 +4,74 @@ var shared = require('@estjs/shared');
|
|
|
4
4
|
var signal = require('@estjs/signal');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* @estjs/template v0.0.
|
|
7
|
+
* @estjs/template v0.0.12
|
|
8
8
|
* (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
|
|
9
9
|
* @license MIT
|
|
10
10
|
**/
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
15
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
16
|
+
var __spreadValues = (a, b) => {
|
|
17
|
+
for (var prop in b || (b = {}))
|
|
18
|
+
if (__hasOwnProp.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
if (__getOwnPropSymbols)
|
|
21
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
22
|
+
if (__propIsEnum.call(b, prop))
|
|
23
|
+
__defNormalProp(a, prop, b[prop]);
|
|
24
|
+
}
|
|
25
|
+
return a;
|
|
26
|
+
};
|
|
27
|
+
var _Hooks = class _Hooks {
|
|
28
|
+
constructor() {
|
|
29
|
+
this.hooks = {
|
|
30
|
+
mounted: /* @__PURE__ */ new Set(),
|
|
31
|
+
destroy: /* @__PURE__ */ new Set()
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
addEventListener() {
|
|
35
|
+
}
|
|
36
|
+
removeEventListener() {
|
|
37
|
+
}
|
|
38
|
+
addHook(hook, cb) {
|
|
39
|
+
var _a;
|
|
40
|
+
(_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
|
|
41
|
+
}
|
|
42
|
+
getContext(context) {
|
|
43
|
+
return _Hooks.context[context];
|
|
44
|
+
}
|
|
45
|
+
setContext(context, value) {
|
|
46
|
+
_Hooks.context[context] = value;
|
|
47
|
+
}
|
|
48
|
+
initRef() {
|
|
49
|
+
_Hooks.ref = this;
|
|
50
|
+
}
|
|
51
|
+
removeRef() {
|
|
52
|
+
_Hooks.ref = null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
_Hooks.ref = null;
|
|
56
|
+
_Hooks.context = {};
|
|
57
|
+
var Hooks = _Hooks;
|
|
58
|
+
var ComponentNode = class extends Hooks {
|
|
59
|
+
constructor(template2, props, key) {
|
|
60
|
+
super();
|
|
14
61
|
this.template = template2;
|
|
15
62
|
this.props = props;
|
|
63
|
+
this.key = key;
|
|
16
64
|
this.proxyProps = {};
|
|
17
|
-
this.context = {};
|
|
18
65
|
this.emitter = /* @__PURE__ */ new Set();
|
|
19
66
|
this.mounted = false;
|
|
20
67
|
this.rootNode = null;
|
|
21
|
-
this.
|
|
22
|
-
mounted: /* @__PURE__ */ new Set(),
|
|
23
|
-
destroy: /* @__PURE__ */ new Set()
|
|
24
|
-
};
|
|
68
|
+
this.context = {};
|
|
25
69
|
this.trackMap = /* @__PURE__ */ new Map();
|
|
26
70
|
this.proxyProps = signal.signalObject(
|
|
27
71
|
props,
|
|
28
|
-
(
|
|
72
|
+
(key2) => shared.startsWith(key2, "on") || shared.startsWith(key2, "update")
|
|
29
73
|
);
|
|
30
|
-
|
|
31
|
-
addEventListener() {
|
|
32
|
-
}
|
|
33
|
-
removeEventListener() {
|
|
74
|
+
this.key = this.key || props.key;
|
|
34
75
|
}
|
|
35
76
|
get firstChild() {
|
|
36
77
|
var _a, _b;
|
|
@@ -40,16 +81,6 @@ var _ComponentNode = class _ComponentNode {
|
|
|
40
81
|
var _a, _b;
|
|
41
82
|
return (_b = (_a = this.rootNode) == null ? void 0 : _a.isConnected) != null ? _b : false;
|
|
42
83
|
}
|
|
43
|
-
addHook(hook, cb) {
|
|
44
|
-
var _a;
|
|
45
|
-
(_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
|
|
46
|
-
}
|
|
47
|
-
getContext(context) {
|
|
48
|
-
return _ComponentNode.context[context];
|
|
49
|
-
}
|
|
50
|
-
setContext(context, value) {
|
|
51
|
-
_ComponentNode.context[context] = value;
|
|
52
|
-
}
|
|
53
84
|
inheritNode(node) {
|
|
54
85
|
this.context = node.context;
|
|
55
86
|
this.hooks = node.hooks;
|
|
@@ -68,9 +99,9 @@ var _ComponentNode = class _ComponentNode {
|
|
|
68
99
|
if (this.isConnected) {
|
|
69
100
|
return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
|
|
70
101
|
}
|
|
71
|
-
|
|
102
|
+
this.initRef();
|
|
72
103
|
this.rootNode = this.template(signal.useReactive(this.proxyProps, ["children"]));
|
|
73
|
-
|
|
104
|
+
this.removeRef();
|
|
74
105
|
this.mounted = true;
|
|
75
106
|
const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
|
|
76
107
|
this.hooks.mounted.forEach((handler) => handler());
|
|
@@ -100,7 +131,7 @@ var _ComponentNode = class _ComponentNode {
|
|
|
100
131
|
return track;
|
|
101
132
|
}
|
|
102
133
|
patchProps(props) {
|
|
103
|
-
var _a, _b, _c, _d
|
|
134
|
+
var _a, _b, _c, _d;
|
|
104
135
|
for (const [key, prop] of Object.entries(props)) {
|
|
105
136
|
if (shared.startsWith(key, "on") && ((_a = this.rootNode) == null ? void 0 : _a.nodes)) {
|
|
106
137
|
const event = key.slice(2).toLowerCase();
|
|
@@ -108,15 +139,11 @@ var _ComponentNode = class _ComponentNode {
|
|
|
108
139
|
const cleanup = addEventListener(this.rootNode.nodes[0], event, listener);
|
|
109
140
|
this.emitter.add(cleanup);
|
|
110
141
|
} else if (key === "ref") {
|
|
111
|
-
|
|
112
|
-
props[key].value = (_b = this.rootNode) == null ? void 0 : _b.nodes[0];
|
|
113
|
-
} else if (shared.isFunction(prop)) {
|
|
114
|
-
props[key]((_c = this.rootNode) == null ? void 0 : _c.nodes[0]);
|
|
115
|
-
}
|
|
142
|
+
props[key].value = (_b = this.rootNode) == null ? void 0 : _b.nodes[0];
|
|
116
143
|
} else if (shared.startsWith(key, "update")) {
|
|
117
144
|
props[key] = signal.isSignal(prop) ? prop.value : prop;
|
|
118
145
|
} else if (key !== "children") {
|
|
119
|
-
const newValue = (
|
|
146
|
+
const newValue = (_d = (_c = this.proxyProps)[key]) != null ? _d : _c[key] = signal.useSignal(prop);
|
|
120
147
|
const track = this.getNodeTrack(key);
|
|
121
148
|
track.cleanup = signal.useEffect(() => {
|
|
122
149
|
newValue.value = shared.isFunction(prop) ? prop() : prop;
|
|
@@ -126,12 +153,9 @@ var _ComponentNode = class _ComponentNode {
|
|
|
126
153
|
this.props = props;
|
|
127
154
|
}
|
|
128
155
|
};
|
|
129
|
-
_ComponentNode.ref = null;
|
|
130
|
-
_ComponentNode.context = {};
|
|
131
|
-
var ComponentNode = _ComponentNode;
|
|
132
156
|
|
|
133
157
|
// src/template.ts
|
|
134
|
-
function h(_template, props) {
|
|
158
|
+
function h(_template, props, key) {
|
|
135
159
|
if (shared.isString(_template)) {
|
|
136
160
|
if (isHtmlTagName(_template)) {
|
|
137
161
|
_template = convertToHtmlTag(_template);
|
|
@@ -146,15 +170,17 @@ function h(_template, props) {
|
|
|
146
170
|
}
|
|
147
171
|
_template = template(_template);
|
|
148
172
|
}
|
|
149
|
-
return shared.isFunction(_template) ? new ComponentNode(_template, props) : new TemplateNode(_template, props);
|
|
173
|
+
return shared.isFunction(_template) ? new ComponentNode(_template, props, key) : new TemplateNode(_template, props, key);
|
|
174
|
+
}
|
|
175
|
+
function isComponent(node) {
|
|
176
|
+
return node instanceof ComponentNode;
|
|
150
177
|
}
|
|
151
178
|
function isJsxElement(node) {
|
|
152
179
|
return node instanceof ComponentNode || node instanceof TemplateNode;
|
|
153
180
|
}
|
|
154
181
|
function template(html) {
|
|
155
|
-
html = closeHtmlTags(html);
|
|
156
182
|
const template2 = document.createElement("template");
|
|
157
|
-
template2.innerHTML = html;
|
|
183
|
+
template2.innerHTML = closeHtmlTags(html);
|
|
158
184
|
return template2;
|
|
159
185
|
}
|
|
160
186
|
function Fragment(props) {
|
|
@@ -329,15 +355,6 @@ function convertToHtmlTag(tagName) {
|
|
|
329
355
|
return `<${tagName}></${tagName}>`;
|
|
330
356
|
}
|
|
331
357
|
}
|
|
332
|
-
function generateShortId() {
|
|
333
|
-
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
334
|
-
let result = "";
|
|
335
|
-
const charactersLength = characters.length;
|
|
336
|
-
for (let i = 0; i < 8; i++) {
|
|
337
|
-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
338
|
-
}
|
|
339
|
-
return result;
|
|
340
|
-
}
|
|
341
358
|
|
|
342
359
|
// src/patch.ts
|
|
343
360
|
function patchChildren(parent, childrenMap, nextChildren, before) {
|
|
@@ -454,6 +471,7 @@ var TemplateNode = class _TemplateNode {
|
|
|
454
471
|
this.provides = {};
|
|
455
472
|
this.trackMap = /* @__PURE__ */ new Map();
|
|
456
473
|
this.parent = null;
|
|
474
|
+
this.key = this.key || props.key;
|
|
457
475
|
}
|
|
458
476
|
get firstChild() {
|
|
459
477
|
var _a;
|
|
@@ -473,7 +491,6 @@ var TemplateNode = class _TemplateNode {
|
|
|
473
491
|
this.nodes.forEach((node) => insertChild(parent, node, before));
|
|
474
492
|
return this.nodes;
|
|
475
493
|
}
|
|
476
|
-
this.key = this.key || generateShortId();
|
|
477
494
|
const cloneNode = this.template.content.cloneNode(true);
|
|
478
495
|
const firstChild = cloneNode.firstChild;
|
|
479
496
|
if ((_a = firstChild == null ? void 0 : firstChild.hasAttribute) == null ? void 0 : _a.call(firstChild, "_svg_")) {
|
|
@@ -577,11 +594,7 @@ var TemplateNode = class _TemplateNode {
|
|
|
577
594
|
});
|
|
578
595
|
}
|
|
579
596
|
} else if (attr === "ref") {
|
|
580
|
-
|
|
581
|
-
props[attr].value = node;
|
|
582
|
-
} else if (shared.isFunction(props[attr])) {
|
|
583
|
-
props[attr](node);
|
|
584
|
-
}
|
|
597
|
+
props[attr].value = node;
|
|
585
598
|
} else if (shared.startsWith(attr, "on")) {
|
|
586
599
|
const eventName = attr.slice(2).toLocaleLowerCase();
|
|
587
600
|
const track = this.getNodeTrack(`${key}:${attr}`);
|
|
@@ -638,98 +651,165 @@ function patchChild(track, parent, child, before) {
|
|
|
638
651
|
});
|
|
639
652
|
}
|
|
640
653
|
}
|
|
641
|
-
|
|
642
|
-
// src/hooks.ts
|
|
643
654
|
function onMount(cb) {
|
|
644
655
|
var _a;
|
|
645
656
|
throwIfOutsideComponent("onMounted");
|
|
646
|
-
(_a =
|
|
657
|
+
(_a = Hooks.ref) == null ? void 0 : _a.addHook("mounted", cb);
|
|
647
658
|
}
|
|
648
659
|
function onDestroy(cb) {
|
|
649
660
|
var _a;
|
|
650
661
|
throwIfOutsideComponent("onDestroy");
|
|
651
|
-
(_a =
|
|
662
|
+
(_a = Hooks.ref) == null ? void 0 : _a.addHook("destroy", cb);
|
|
652
663
|
}
|
|
653
|
-
function throwIfOutsideComponent(hook) {
|
|
654
|
-
if (!
|
|
664
|
+
function throwIfOutsideComponent(hook, key) {
|
|
665
|
+
if (!Hooks.ref) {
|
|
655
666
|
console.error(
|
|
656
|
-
`"${hook}" can only be called within the component function body
|
|
667
|
+
`"${hook}"(key: ${shared.isSymbol(key) ? key.toString() : key}) can only be called within the component function body
|
|
657
668
|
and cannot be used in asynchronous or deferred calls.`
|
|
658
669
|
);
|
|
659
670
|
}
|
|
660
671
|
}
|
|
661
672
|
function useProvide(key, value) {
|
|
662
673
|
var _a;
|
|
663
|
-
throwIfOutsideComponent("useProvide");
|
|
664
|
-
(_a =
|
|
674
|
+
throwIfOutsideComponent("useProvide", key);
|
|
675
|
+
(_a = Hooks.ref) == null ? void 0 : _a.setContext(key, value);
|
|
665
676
|
}
|
|
666
677
|
function useInject(key, defaultValue) {
|
|
667
678
|
var _a;
|
|
668
|
-
throwIfOutsideComponent("useInject");
|
|
669
|
-
return ((_a =
|
|
670
|
-
}
|
|
671
|
-
function
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
679
|
+
throwIfOutsideComponent("useInject", key);
|
|
680
|
+
return ((_a = Hooks.ref) == null ? void 0 : _a.getContext(key)) || defaultValue;
|
|
681
|
+
}
|
|
682
|
+
function useRef() {
|
|
683
|
+
const ref = signal.shallowSignal(null);
|
|
684
|
+
return ref;
|
|
685
|
+
}
|
|
686
|
+
function generateAttributes(props) {
|
|
687
|
+
return Object.entries(props).map(([key, value]) => {
|
|
688
|
+
if (key === "children" || shared.isFunction(value)) return "";
|
|
689
|
+
return `${key}="${shared.escape(String(value))}"`;
|
|
690
|
+
}).filter(Boolean).join(" ");
|
|
691
|
+
}
|
|
692
|
+
function normalizeProps(props) {
|
|
693
|
+
Object.keys(props).forEach((propKey) => {
|
|
694
|
+
if (shared.isFunction(props[propKey])) {
|
|
695
|
+
delete props[propKey];
|
|
696
|
+
}
|
|
697
|
+
if (signal.isSignal(props[propKey])) {
|
|
698
|
+
props[propKey] = props[propKey].value;
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
function handleChildResult(result, prop, key, tmpl, childNodesMap, path) {
|
|
703
|
+
if (signal.isSignal(result)) {
|
|
704
|
+
tmpl.template += result.value;
|
|
705
|
+
} else if (result instanceof ServerNode) {
|
|
706
|
+
const mapKey = path ? String(path) : `${key}`;
|
|
707
|
+
if (!childNodesMap[mapKey]) childNodesMap[mapKey] = [];
|
|
708
|
+
const childResult = result.mount();
|
|
709
|
+
childNodesMap[mapKey].push(
|
|
710
|
+
shared.isFunction(childResult) ? childResult(prop) : signal.isSignal(childResult) ? childResult.value : childResult
|
|
711
|
+
);
|
|
712
|
+
} else {
|
|
713
|
+
tmpl.template += shared.isFunction(result) ? result(prop) : String(result);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
var ServerNode = class _ServerNode extends Hooks {
|
|
717
|
+
constructor(template2, props = {}, key) {
|
|
718
|
+
super();
|
|
719
|
+
this.template = template2;
|
|
720
|
+
this.props = props;
|
|
721
|
+
this.key = key;
|
|
722
|
+
this.childNodesMap = {};
|
|
723
|
+
this.processedTemplates = {};
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Mount and render the component
|
|
727
|
+
*/
|
|
728
|
+
mount() {
|
|
729
|
+
this.initRef();
|
|
730
|
+
const output = this.render();
|
|
731
|
+
this.removeRef();
|
|
732
|
+
return output;
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Initialize template entries and props
|
|
736
|
+
*/
|
|
737
|
+
initTemplates() {
|
|
738
|
+
const templateCollection = Array.isArray(this.template) ? this.template.reduce((acc, tmpl, index) => {
|
|
739
|
+
acc[index + 1] = { template: tmpl };
|
|
740
|
+
return acc;
|
|
741
|
+
}, {}) : this.template;
|
|
742
|
+
if (shared.isObject(templateCollection)) {
|
|
743
|
+
Object.entries(templateCollection).forEach(([key, tmpl]) => {
|
|
744
|
+
const prop = __spreadValues({}, this.props[key]);
|
|
745
|
+
normalizeProps(prop);
|
|
693
746
|
if (prop.children) {
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
747
|
+
prop.children.forEach((item) => {
|
|
748
|
+
const [child, path] = shared.isArray(item) ? item : [item, null];
|
|
749
|
+
if (shared.isFunction(child)) {
|
|
750
|
+
const result = child(prop);
|
|
751
|
+
handleChildResult(result, prop, key, tmpl, this.childNodesMap, path);
|
|
752
|
+
} else {
|
|
753
|
+
tmpl.template += signal.isSignal(child) ? child.value : String(child);
|
|
754
|
+
}
|
|
755
|
+
});
|
|
699
756
|
}
|
|
700
|
-
|
|
701
|
-
|
|
757
|
+
this.processedTemplates[key] = {
|
|
758
|
+
template: tmpl.template,
|
|
759
|
+
props: prop
|
|
760
|
+
};
|
|
761
|
+
});
|
|
702
762
|
}
|
|
703
763
|
}
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
764
|
+
/**
|
|
765
|
+
* Render component and its children into a string
|
|
766
|
+
*/
|
|
767
|
+
render() {
|
|
768
|
+
if (shared.isFunction(this.template)) {
|
|
769
|
+
const root = this.template(this.props);
|
|
770
|
+
return root instanceof _ServerNode ? root.mount() : String(root);
|
|
708
771
|
}
|
|
709
|
-
if (
|
|
710
|
-
|
|
772
|
+
if (this.template instanceof _ServerNode) {
|
|
773
|
+
return this.template.mount();
|
|
711
774
|
}
|
|
712
|
-
|
|
713
|
-
|
|
775
|
+
this.initTemplates();
|
|
776
|
+
return Object.entries(this.processedTemplates).map(([key, { template: template2, props }]) => {
|
|
777
|
+
let content = template2;
|
|
778
|
+
if (props && Object.keys(props).length > 0) {
|
|
779
|
+
content += ` ${generateAttributes(props)}`;
|
|
780
|
+
}
|
|
781
|
+
if (this.childNodesMap[key]) {
|
|
782
|
+
content = content.replace("<!>", this.renderChildren(this.childNodesMap[key]));
|
|
783
|
+
}
|
|
784
|
+
return content;
|
|
785
|
+
}).join("");
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Render child nodes into a string
|
|
789
|
+
*/
|
|
790
|
+
renderChildren(children) {
|
|
791
|
+
return shared.coerceArray(children).map(String).join("");
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
function ssg(component, props) {
|
|
795
|
+
return new ServerNode(component, props);
|
|
714
796
|
}
|
|
715
797
|
function renderToString(component, props) {
|
|
716
|
-
return
|
|
717
|
-
}
|
|
718
|
-
function renderSSG(component, root, props = {}) {
|
|
719
|
-
root.innerHTML = renderTemplate(component, props);
|
|
798
|
+
return ssg(component, props).mount();
|
|
720
799
|
}
|
|
721
800
|
|
|
722
801
|
exports.ComponentNode = ComponentNode;
|
|
723
802
|
exports.Fragment = Fragment;
|
|
724
803
|
exports.TemplateNode = TemplateNode;
|
|
725
804
|
exports.h = h;
|
|
805
|
+
exports.isComponent = isComponent;
|
|
726
806
|
exports.isJsxElement = isJsxElement;
|
|
727
807
|
exports.nextTick = nextTick;
|
|
728
808
|
exports.onDestroy = onDestroy;
|
|
729
809
|
exports.onMount = onMount;
|
|
730
|
-
exports.renderSSG = renderSSG;
|
|
731
|
-
exports.renderTemplate = renderTemplate;
|
|
732
810
|
exports.renderToString = renderToString;
|
|
811
|
+
exports.ssg = ssg;
|
|
733
812
|
exports.template = template;
|
|
734
813
|
exports.useInject = useInject;
|
|
735
814
|
exports.useProvide = useProvide;
|
|
815
|
+
exports.useRef = useRef;
|