@ktjs/core 0.29.6 → 0.29.8
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/h/attr-helpers.d.ts +6 -0
- package/dist/h/attr.d.ts +2 -0
- package/dist/h/content.d.ts +2 -0
- package/dist/h/index.d.ts +12 -0
- package/dist/h/model.d.ts +3 -0
- package/dist/index.d.ts +10 -428
- package/dist/index.mjs +4 -599
- package/dist/jsx/async.d.ts +13 -0
- package/dist/jsx/common.d.ts +4 -0
- package/dist/jsx/for.d.ts +14 -0
- package/dist/jsx/fragment.d.ts +35 -0
- package/dist/jsx/if.d.ts +10 -0
- package/dist/jsx/index.d.ts +2 -0
- package/dist/jsx/index.mjs +1 -0
- package/dist/jsx/jsx-runtime.d.ts +43 -0
- package/dist/jsx/jsx-runtime.mjs +2 -0
- package/dist/jsx-runtime-DGJHUQEM.js +600 -0
- package/dist/reactive/computed.d.ts +63 -0
- package/dist/reactive/core.d.ts +9 -0
- package/dist/reactive/effect.d.ts +15 -0
- package/dist/reactive/index.d.ts +10 -0
- package/dist/reactive/ref.d.ts +89 -0
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,315 +1,6 @@
|
|
|
1
|
-
import { $
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
KTReactiveType2[KTReactiveType2["REF"] = 1] = "REF";
|
|
5
|
-
KTReactiveType2[KTReactiveType2["COMPUTED"] = 2] = "COMPUTED";
|
|
6
|
-
return KTReactiveType2;
|
|
7
|
-
})(KTReactiveType || {});
|
|
8
|
-
const isKT = (obj) => obj?.isKT;
|
|
9
|
-
const isRef = (obj) => obj?.ktType === 1 /* REF */;
|
|
10
|
-
const isComputed = (obj) => obj?.ktType === 2 /* COMPUTED */;
|
|
11
|
-
|
|
12
|
-
const booleanHandler = (element, key, value) => {
|
|
13
|
-
if (key in element) {
|
|
14
|
-
element[key] = !!value;
|
|
15
|
-
} else {
|
|
16
|
-
element.setAttribute(key, value);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
const valueHandler = (element, key, value) => {
|
|
20
|
-
if (key in element) {
|
|
21
|
-
element[key] = value;
|
|
22
|
-
} else {
|
|
23
|
-
element.setAttribute(key, value);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
const handlers = {
|
|
27
|
-
checked: booleanHandler,
|
|
28
|
-
selected: booleanHandler,
|
|
29
|
-
value: valueHandler,
|
|
30
|
-
valueAsDate: valueHandler,
|
|
31
|
-
valueAsNumber: valueHandler,
|
|
32
|
-
defaultValue: valueHandler,
|
|
33
|
-
defaultChecked: booleanHandler,
|
|
34
|
-
defaultSelected: booleanHandler,
|
|
35
|
-
disabled: booleanHandler,
|
|
36
|
-
readOnly: booleanHandler,
|
|
37
|
-
multiple: booleanHandler,
|
|
38
|
-
required: booleanHandler,
|
|
39
|
-
autofocus: booleanHandler,
|
|
40
|
-
open: booleanHandler,
|
|
41
|
-
controls: booleanHandler,
|
|
42
|
-
autoplay: booleanHandler,
|
|
43
|
-
loop: booleanHandler,
|
|
44
|
-
muted: booleanHandler,
|
|
45
|
-
defer: booleanHandler,
|
|
46
|
-
async: booleanHandler,
|
|
47
|
-
hidden: (element, _key, value) => element.hidden = !!value
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const defaultHandler = (element, key, value) => element.setAttribute(key, value);
|
|
51
|
-
const setElementStyle = (element, style) => {
|
|
52
|
-
if (typeof style === "string") {
|
|
53
|
-
element.style.cssText = style;
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
for (const key in style) {
|
|
57
|
-
element.style[key] = style[key];
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
function attrIsObject(element, attr) {
|
|
61
|
-
const classValue = attr.class || attr.className;
|
|
62
|
-
if (classValue !== void 0) {
|
|
63
|
-
if (isKT(classValue)) {
|
|
64
|
-
element.setAttribute("class", classValue.value);
|
|
65
|
-
classValue.addOnChange((v) => element.setAttribute("class", v));
|
|
66
|
-
} else {
|
|
67
|
-
element.setAttribute("class", classValue);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
const style = attr.style;
|
|
71
|
-
if (style) {
|
|
72
|
-
if (typeof style === "string") {
|
|
73
|
-
element.setAttribute("style", style);
|
|
74
|
-
} else if (typeof style === "object") {
|
|
75
|
-
if (isKT(style)) {
|
|
76
|
-
setElementStyle(element, style.value);
|
|
77
|
-
style.addOnChange((v) => setElementStyle(element, v));
|
|
78
|
-
} else {
|
|
79
|
-
setElementStyle(element, style);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if ("k-html" in attr) {
|
|
84
|
-
const html = attr["k-html"];
|
|
85
|
-
if (isKT(html)) {
|
|
86
|
-
element.innerHTML = html.value;
|
|
87
|
-
html.addOnChange((v) => element.innerHTML = v);
|
|
88
|
-
} else {
|
|
89
|
-
element.innerHTML = html;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
for (const key in attr) {
|
|
93
|
-
if (key === "k-if" || key === "k-model" || key === "k-for" || key === "k-key" || key === "ref" || key === "class" || key === "className" || key === "style" || key === "children" || key === "k-html" || key === "k-else") {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const o = attr[key];
|
|
97
|
-
if (key.startsWith("on:")) {
|
|
98
|
-
element.addEventListener(key.slice(3), o);
|
|
99
|
-
} else {
|
|
100
|
-
const handler = handlers[key] || defaultHandler;
|
|
101
|
-
if (isKT(o)) {
|
|
102
|
-
handler(element, key, o.value);
|
|
103
|
-
o.addOnChange((v) => handler(element, key, v));
|
|
104
|
-
} else {
|
|
105
|
-
handler(element, key, o);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
function applyAttr(element, attr) {
|
|
111
|
-
if (!attr) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
if (typeof attr === "object" && attr !== null) {
|
|
115
|
-
attrIsObject(element, attr);
|
|
116
|
-
} else {
|
|
117
|
-
throw new Error("[@ktjs/core error] attr must be an object.");
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const assureNode = (o) => $isNode(o) ? o : document.createTextNode(o);
|
|
122
|
-
function apdSingle(element, c) {
|
|
123
|
-
if (c === false || c === void 0 || c === null) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
if (isKT(c)) {
|
|
127
|
-
let node = assureNode(c.value);
|
|
128
|
-
$append.call(element, node);
|
|
129
|
-
c.addOnChange((newValue, oldValue) => {
|
|
130
|
-
if ($isNode(newValue) && $isNode(oldValue)) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const oldNode = node;
|
|
134
|
-
node = assureNode(newValue);
|
|
135
|
-
oldNode.replaceWith(node);
|
|
136
|
-
});
|
|
137
|
-
} else {
|
|
138
|
-
$append.call(element, c);
|
|
139
|
-
const list = c.__kt_for_list__;
|
|
140
|
-
if ($isArray(list)) {
|
|
141
|
-
apd(element, list);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
function apd(element, c) {
|
|
146
|
-
if ($isThenable(c)) {
|
|
147
|
-
c.then((r) => apd(element, r));
|
|
148
|
-
} else if ($isArray(c)) {
|
|
149
|
-
for (let i = 0; i < c.length; i++) {
|
|
150
|
-
const ci = c[i];
|
|
151
|
-
if ($isThenable(ci)) {
|
|
152
|
-
const comment = document.createComment("ktjs-promise-placeholder");
|
|
153
|
-
$append.call(element, comment);
|
|
154
|
-
ci.then((awaited) => comment.replaceWith(awaited));
|
|
155
|
-
} else {
|
|
156
|
-
apdSingle(element, ci);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
} else {
|
|
160
|
-
apdSingle(element, c);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
function applyContent(element, content) {
|
|
164
|
-
if ($isArray(content)) {
|
|
165
|
-
for (let i = 0; i < content.length; i++) {
|
|
166
|
-
apd(element, content[i]);
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
apd(element, content);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
class KTRef {
|
|
174
|
-
/**
|
|
175
|
-
* Indicates that this is a KTRef instance
|
|
176
|
-
*/
|
|
177
|
-
isKT = true;
|
|
178
|
-
ktType = KTReactiveType.REF;
|
|
179
|
-
/**
|
|
180
|
-
* @internal
|
|
181
|
-
*/
|
|
182
|
-
_value;
|
|
183
|
-
/**
|
|
184
|
-
* @internal
|
|
185
|
-
*/
|
|
186
|
-
_onChanges;
|
|
187
|
-
/**
|
|
188
|
-
* @internal
|
|
189
|
-
*/
|
|
190
|
-
_emit(newValue, oldValue) {
|
|
191
|
-
for (let i = 0; i < this._onChanges.length; i++) {
|
|
192
|
-
this._onChanges[i](newValue, oldValue);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
constructor(_value, _onChanges) {
|
|
196
|
-
this._value = _value;
|
|
197
|
-
this._onChanges = _onChanges;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* If new value and old value are both nodes, the old one will be replaced in the DOM
|
|
201
|
-
*/
|
|
202
|
-
get value() {
|
|
203
|
-
return this._value;
|
|
204
|
-
}
|
|
205
|
-
set value(newValue) {
|
|
206
|
-
if ($is(newValue, this._value)) {
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
const oldValue = this._value;
|
|
210
|
-
$replaceNode(oldValue, newValue);
|
|
211
|
-
this._value = newValue;
|
|
212
|
-
this._emit(newValue, oldValue);
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Force all listeners to run even when reference identity has not changed.
|
|
216
|
-
* Useful for in-place array/object mutations.
|
|
217
|
-
*/
|
|
218
|
-
notify() {
|
|
219
|
-
this._emit(this._value, this._value);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Mutate current value in-place and notify listeners once.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* const items = ref<number[]>([1, 2]);
|
|
226
|
-
* items.mutate((list) => list.push(3));
|
|
227
|
-
*/
|
|
228
|
-
mutate(mutator) {
|
|
229
|
-
if (typeof mutator !== "function") {
|
|
230
|
-
throw new Error("[@ktjs/core error] KTRef.mutate: mutator must be a function");
|
|
231
|
-
}
|
|
232
|
-
const oldValue = this._value;
|
|
233
|
-
const result = mutator(this._value);
|
|
234
|
-
this._emit(this._value, oldValue);
|
|
235
|
-
return result;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Register a callback when the value changes
|
|
239
|
-
* @param callback (newValue, oldValue) => xxx
|
|
240
|
-
*/
|
|
241
|
-
addOnChange(callback) {
|
|
242
|
-
if (typeof callback !== "function") {
|
|
243
|
-
throw new Error("[@ktjs/core error] KTRef.addOnChange: callback must be a function");
|
|
244
|
-
}
|
|
245
|
-
this._onChanges.push(callback);
|
|
246
|
-
}
|
|
247
|
-
removeOnChange(callback) {
|
|
248
|
-
for (let i = this._onChanges.length - 1; i >= 0; i--) {
|
|
249
|
-
if (this._onChanges[i] === callback) {
|
|
250
|
-
this._onChanges.splice(i, 1);
|
|
251
|
-
return true;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return false;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
function ref(value, onChange) {
|
|
258
|
-
return new KTRef(value, onChange ? [onChange] : []);
|
|
259
|
-
}
|
|
260
|
-
const toRef = (o) => {
|
|
261
|
-
if (isRef(o)) {
|
|
262
|
-
return o;
|
|
263
|
-
} else if (isComputed(o)) {
|
|
264
|
-
throw new Error("[@ktjs/core error] Computed values cannot be used as KTRef.");
|
|
265
|
-
} else {
|
|
266
|
-
return ref(o);
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
function kcollect() {
|
|
270
|
-
const newObj = {};
|
|
271
|
-
const entries = $entries(this);
|
|
272
|
-
for (let i = 0; i < entries.length; i++) {
|
|
273
|
-
const key = entries[i][0];
|
|
274
|
-
if (key === "kcollect") {
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
newObj[key] = entries[i][1].value;
|
|
278
|
-
}
|
|
279
|
-
return newObj;
|
|
280
|
-
}
|
|
281
|
-
const surfaceRef = (obj) => {
|
|
282
|
-
const entries = $entries(obj);
|
|
283
|
-
const newObj = { kcollect };
|
|
284
|
-
for (let i = 0; i < entries.length; i++) {
|
|
285
|
-
newObj[entries[i][0]] = ref(entries[i][1]);
|
|
286
|
-
}
|
|
287
|
-
return newObj;
|
|
288
|
-
};
|
|
289
|
-
const $modelOrRef = (props, defaultValue) => {
|
|
290
|
-
if ("k-model" in props) {
|
|
291
|
-
const kmodel = props["k-model"];
|
|
292
|
-
if (isRef(kmodel)) {
|
|
293
|
-
return kmodel;
|
|
294
|
-
} else {
|
|
295
|
-
throw new Error(`[@ktjs/core error] k-model data must be a KTRef object, please use 'ref(...)' to wrap it.`);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return ref(defaultValue);
|
|
299
|
-
};
|
|
300
|
-
const $refSetter = (props, node) => props.ref.value = node;
|
|
301
|
-
const $initRef = (props, node) => {
|
|
302
|
-
if (!("ref" in props)) {
|
|
303
|
-
return $emptyFn;
|
|
304
|
-
}
|
|
305
|
-
const r = props.ref;
|
|
306
|
-
if (isRef(r)) {
|
|
307
|
-
r.value = node;
|
|
308
|
-
return $refSetter;
|
|
309
|
-
} else {
|
|
310
|
-
throw new Error("[@ktjs/core error] Fragment: ref must be a KTRef");
|
|
311
|
-
}
|
|
312
|
-
};
|
|
1
|
+
import { $replaceNode, $emptyFn, $isThenable } from '@ktjs/shared';
|
|
2
|
+
import { K as KTReactiveType, i as isKT, t as toReactive, $ as $initRef } from './jsx-runtime-DGJHUQEM.js';
|
|
3
|
+
export { d as $modelOrRef, F as Fragment, e as KTRef, h as createElement, c as createRedrawable, f as dereactive, h, g as isComputed, k as isRef, j as jsx, a as jsxDEV, b as jsxs, r as ref, s as surfaceRef, l as toRef } from './jsx-runtime-DGJHUQEM.js';
|
|
313
4
|
|
|
314
5
|
class KTComputed {
|
|
315
6
|
/**
|
|
@@ -452,292 +143,6 @@ function effect(effectFn, reactives, options) {
|
|
|
452
143
|
};
|
|
453
144
|
}
|
|
454
145
|
|
|
455
|
-
const toReactive = (value, onChange) => {
|
|
456
|
-
if (isKT(value)) {
|
|
457
|
-
if (onChange) {
|
|
458
|
-
value.addOnChange(onChange);
|
|
459
|
-
}
|
|
460
|
-
return value;
|
|
461
|
-
} else {
|
|
462
|
-
return ref(value, onChange);
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
function dereactive(value) {
|
|
466
|
-
return isKT(value) ? value.value : value;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
function applyKModel(element, valueRef) {
|
|
470
|
-
if (!isKT(valueRef)) {
|
|
471
|
-
console.warn('[@ktjs/core warn]',"k-model value must be a KTRef.");
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
if (element instanceof HTMLInputElement) {
|
|
475
|
-
if (element.type === "radio" || element.type === "checkbox") {
|
|
476
|
-
$applyModel(element, valueRef, "checked", "change");
|
|
477
|
-
} else {
|
|
478
|
-
$applyModel(element, valueRef, "value", "input");
|
|
479
|
-
}
|
|
480
|
-
} else if (element instanceof HTMLSelectElement) {
|
|
481
|
-
$applyModel(element, valueRef, "value", "change");
|
|
482
|
-
} else if (element instanceof HTMLTextAreaElement) {
|
|
483
|
-
$applyModel(element, valueRef, "value", "input");
|
|
484
|
-
} else {
|
|
485
|
-
console.warn('[@ktjs/core warn]',"not supported element for k-model:");
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
const htmlCreator = (tag) => document.createElement(tag);
|
|
490
|
-
const svgCreator = (tag) => document.createElementNS("http://www.w3.org/2000/svg", tag);
|
|
491
|
-
const mathMLCreator = (tag) => document.createElementNS("http://www.w3.org/1998/Math/MathML", tag);
|
|
492
|
-
let creator = htmlCreator;
|
|
493
|
-
const h = (tag, attr, content) => {
|
|
494
|
-
if (typeof tag !== "string") {
|
|
495
|
-
throw new Error("[@ktjs/core error] tagName must be a string.");
|
|
496
|
-
}
|
|
497
|
-
if (attr) {
|
|
498
|
-
if ("__svg" in attr) {
|
|
499
|
-
delete attr["__svg"];
|
|
500
|
-
creator = svgCreator;
|
|
501
|
-
} else if ("__mathml" in attr) {
|
|
502
|
-
delete attr["__mathml"];
|
|
503
|
-
creator = mathMLCreator;
|
|
504
|
-
} else {
|
|
505
|
-
creator = htmlCreator;
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
const element = creator(tag);
|
|
509
|
-
applyAttr(element, attr);
|
|
510
|
-
applyContent(element, content);
|
|
511
|
-
if (typeof attr === "object" && attr !== null && "k-model" in attr) {
|
|
512
|
-
applyKModel(element, attr["k-model"]);
|
|
513
|
-
}
|
|
514
|
-
return element;
|
|
515
|
-
};
|
|
516
|
-
|
|
517
|
-
const FRAGMENT_MOUNT_PATCHED = "__kt_fragment_mount_patched__";
|
|
518
|
-
const FRAGMENT_MOUNT = "__kt_fragment_mount__";
|
|
519
|
-
if (typeof Node !== "undefined" && !globalThis[FRAGMENT_MOUNT_PATCHED]) {
|
|
520
|
-
globalThis[FRAGMENT_MOUNT_PATCHED] = true;
|
|
521
|
-
const originAppendChild = Node.prototype.appendChild;
|
|
522
|
-
Node.prototype.appendChild = function(node) {
|
|
523
|
-
const result = originAppendChild.call(this, node);
|
|
524
|
-
const mount = node[FRAGMENT_MOUNT];
|
|
525
|
-
if (typeof mount === "function") {
|
|
526
|
-
mount();
|
|
527
|
-
}
|
|
528
|
-
return result;
|
|
529
|
-
};
|
|
530
|
-
const originInsertBefore = Node.prototype.insertBefore;
|
|
531
|
-
Node.prototype.insertBefore = function(node, child) {
|
|
532
|
-
const result = originInsertBefore.call(this, node, child);
|
|
533
|
-
const mount = node[FRAGMENT_MOUNT];
|
|
534
|
-
if (typeof mount === "function") {
|
|
535
|
-
mount();
|
|
536
|
-
}
|
|
537
|
-
return result;
|
|
538
|
-
};
|
|
539
|
-
}
|
|
540
|
-
function Fragment$1(props) {
|
|
541
|
-
const elements = [];
|
|
542
|
-
const anchor = document.createComment("kt-fragment");
|
|
543
|
-
let inserted = false;
|
|
544
|
-
let observer;
|
|
545
|
-
const redraw = () => {
|
|
546
|
-
const newElements = childrenRef.value;
|
|
547
|
-
const parent = anchor.parentNode;
|
|
548
|
-
if (!parent) {
|
|
549
|
-
elements.length = 0;
|
|
550
|
-
for (let i = 0; i < newElements.length; i++) {
|
|
551
|
-
elements.push(newElements[i]);
|
|
552
|
-
}
|
|
553
|
-
anchor.__kt_fragment_list__ = elements;
|
|
554
|
-
return;
|
|
555
|
-
}
|
|
556
|
-
for (let i = 0; i < elements.length; i++) {
|
|
557
|
-
elements[i].remove();
|
|
558
|
-
}
|
|
559
|
-
const fragment = document.createDocumentFragment();
|
|
560
|
-
elements.length = 0;
|
|
561
|
-
for (let i = 0; i < newElements.length; i++) {
|
|
562
|
-
const element = newElements[i];
|
|
563
|
-
elements.push(element);
|
|
564
|
-
fragment.appendChild(element);
|
|
565
|
-
}
|
|
566
|
-
parent.insertBefore(fragment, anchor.nextSibling);
|
|
567
|
-
inserted = true;
|
|
568
|
-
delete anchor[FRAGMENT_MOUNT];
|
|
569
|
-
observer?.disconnect();
|
|
570
|
-
observer = void 0;
|
|
571
|
-
anchor.__kt_fragment_list__ = elements;
|
|
572
|
-
};
|
|
573
|
-
const childrenRef = toReactive(props.children, redraw);
|
|
574
|
-
const renderInitial = () => {
|
|
575
|
-
const current = childrenRef.value;
|
|
576
|
-
elements.length = 0;
|
|
577
|
-
const fragment = document.createDocumentFragment();
|
|
578
|
-
for (let i = 0; i < current.length; i++) {
|
|
579
|
-
const element = current[i];
|
|
580
|
-
elements.push(element);
|
|
581
|
-
fragment.appendChild(element);
|
|
582
|
-
}
|
|
583
|
-
anchor.__kt_fragment_list__ = elements;
|
|
584
|
-
const parent = anchor.parentNode;
|
|
585
|
-
if (parent && !inserted) {
|
|
586
|
-
parent.insertBefore(fragment, anchor.nextSibling);
|
|
587
|
-
inserted = true;
|
|
588
|
-
}
|
|
589
|
-
};
|
|
590
|
-
renderInitial();
|
|
591
|
-
anchor[FRAGMENT_MOUNT] = () => {
|
|
592
|
-
if (!inserted && anchor.parentNode) {
|
|
593
|
-
redraw();
|
|
594
|
-
}
|
|
595
|
-
};
|
|
596
|
-
observer = new MutationObserver(() => {
|
|
597
|
-
if (anchor.parentNode && !inserted) {
|
|
598
|
-
redraw();
|
|
599
|
-
observer?.disconnect();
|
|
600
|
-
observer = void 0;
|
|
601
|
-
}
|
|
602
|
-
});
|
|
603
|
-
observer.observe(document.body, { childList: true, subtree: true });
|
|
604
|
-
$initRef(props, anchor);
|
|
605
|
-
return anchor;
|
|
606
|
-
}
|
|
607
|
-
function convertChildrenToElements(children) {
|
|
608
|
-
const elements = [];
|
|
609
|
-
const processChild = (child) => {
|
|
610
|
-
if (child === void 0 || child === null || child === false || child === true) {
|
|
611
|
-
return;
|
|
612
|
-
}
|
|
613
|
-
if ($isArray(child)) {
|
|
614
|
-
$forEach(child, processChild);
|
|
615
|
-
return;
|
|
616
|
-
}
|
|
617
|
-
if (typeof child === "string" || typeof child === "number") {
|
|
618
|
-
const span = document.createElement("span");
|
|
619
|
-
span.textContent = String(child);
|
|
620
|
-
elements.push(span);
|
|
621
|
-
return;
|
|
622
|
-
}
|
|
623
|
-
if (child instanceof HTMLElement) {
|
|
624
|
-
elements.push(child);
|
|
625
|
-
return;
|
|
626
|
-
}
|
|
627
|
-
if (isKT(child)) {
|
|
628
|
-
processChild(child.value);
|
|
629
|
-
return;
|
|
630
|
-
}
|
|
631
|
-
console.warn("Fragment: unsupported child type", child);
|
|
632
|
-
};
|
|
633
|
-
processChild(children);
|
|
634
|
-
return elements;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
const jsxh = (tag, props) => {
|
|
638
|
-
if (typeof tag === "function") {
|
|
639
|
-
return tag(props);
|
|
640
|
-
} else {
|
|
641
|
-
return h(tag, props, props.children);
|
|
642
|
-
}
|
|
643
|
-
};
|
|
644
|
-
const placeholder = (data) => document.createComment(data);
|
|
645
|
-
|
|
646
|
-
function kif(tag, props) {
|
|
647
|
-
const kif2 = toReactive(props["k-if"]);
|
|
648
|
-
let el = kif2.value ? jsxh(tag, props) : placeholder("k-if");
|
|
649
|
-
el.__kif__ = kif2;
|
|
650
|
-
const setter = $initRef(props, el);
|
|
651
|
-
kif2.addOnChange((newValue) => {
|
|
652
|
-
const old = el;
|
|
653
|
-
el = newValue ? jsxh(tag, props) : placeholder("k-if");
|
|
654
|
-
el.__kif__ = kif2;
|
|
655
|
-
setter(props, el);
|
|
656
|
-
$replaceNode(old, el);
|
|
657
|
-
});
|
|
658
|
-
return el;
|
|
659
|
-
}
|
|
660
|
-
function kelse(tag, props) {
|
|
661
|
-
let el = placeholder("k-else");
|
|
662
|
-
const setter = $initRef(props, el);
|
|
663
|
-
el.__kelse__ = (newValue) => {
|
|
664
|
-
const old = el;
|
|
665
|
-
el = newValue ? placeholder("k-else") : jsxh(tag, props);
|
|
666
|
-
el.__kelse__ = old.__kelse__;
|
|
667
|
-
setter(props, el);
|
|
668
|
-
$replaceNode(old, el);
|
|
669
|
-
};
|
|
670
|
-
return el;
|
|
671
|
-
}
|
|
672
|
-
function kifelseApply(el) {
|
|
673
|
-
const childNodes = el.childNodes;
|
|
674
|
-
if (childNodes.length === 0) {
|
|
675
|
-
return;
|
|
676
|
-
}
|
|
677
|
-
if (childNodes[0].__kelse__) {
|
|
678
|
-
throw new Error("[@ktjs/core error] k-else cannot be the first child of its parent element.");
|
|
679
|
-
}
|
|
680
|
-
for (let i = 1; i < childNodes.length; i++) {
|
|
681
|
-
const child = childNodes[i];
|
|
682
|
-
if (!child.__kelse__) {
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
const last = childNodes[i - 1];
|
|
686
|
-
if (!("__kif__" in last)) {
|
|
687
|
-
throw new Error("[@ktjs/core error] k-else must be immediately preceded by a k-if element.");
|
|
688
|
-
}
|
|
689
|
-
const kif2 = last.__kif__;
|
|
690
|
-
if (!kif2) {
|
|
691
|
-
continue;
|
|
692
|
-
}
|
|
693
|
-
if (!isKT(kif2)) {
|
|
694
|
-
continue;
|
|
695
|
-
}
|
|
696
|
-
kif2.addOnChange(child.__kelse__);
|
|
697
|
-
if (!kif2.value) {
|
|
698
|
-
child.__kelse__(false);
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
function jsx(tag, props) {
|
|
704
|
-
if (isComputed(props.ref)) {
|
|
705
|
-
throw new Error("[@ktjs/core error] Cannot assign a computed value to an element.");
|
|
706
|
-
}
|
|
707
|
-
if ("k-if" in props) {
|
|
708
|
-
return kif(tag, props);
|
|
709
|
-
}
|
|
710
|
-
if ("k-else" in props) {
|
|
711
|
-
return kelse(tag, props);
|
|
712
|
-
}
|
|
713
|
-
const el = jsxh(tag, props);
|
|
714
|
-
$initRef(props, el);
|
|
715
|
-
kifelseApply(el);
|
|
716
|
-
return el;
|
|
717
|
-
}
|
|
718
|
-
function Fragment(props) {
|
|
719
|
-
const { children } = props ?? {};
|
|
720
|
-
if (!children) {
|
|
721
|
-
return placeholder("kt-fragment-empty");
|
|
722
|
-
}
|
|
723
|
-
const elements = convertChildrenToElements(children);
|
|
724
|
-
return Fragment$1({ children: elements });
|
|
725
|
-
}
|
|
726
|
-
const jsxDEV = (...args) => {
|
|
727
|
-
return jsx(...args);
|
|
728
|
-
};
|
|
729
|
-
const jsxs = jsx;
|
|
730
|
-
function createRedrawable(creator) {
|
|
731
|
-
const elRef = ref();
|
|
732
|
-
const redraw = () => {
|
|
733
|
-
elRef.value = creator();
|
|
734
|
-
elRef.redraw = redraw;
|
|
735
|
-
return elRef.value;
|
|
736
|
-
};
|
|
737
|
-
redraw();
|
|
738
|
-
return elRef;
|
|
739
|
-
}
|
|
740
|
-
|
|
741
146
|
function KTAsync(props) {
|
|
742
147
|
const raw = props.component(props);
|
|
743
148
|
let comp = props.skeleton ?? document.createComment("ktjs-suspense-placeholder");
|
|
@@ -915,4 +320,4 @@ function getSequence(arr) {
|
|
|
915
320
|
return result;
|
|
916
321
|
}
|
|
917
322
|
|
|
918
|
-
export { $initRef,
|
|
323
|
+
export { $initRef, KTAsync, KTComputed, KTFor, KTReactiveType, computed, effect, isKT, toReactive };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { KTComponent, KTRawContent } from '../types/h.js';
|
|
2
|
+
import { KTRef } from '../reactive/ref.js';
|
|
3
|
+
/**
|
|
4
|
+
* Extract component props type (excluding ref and children)
|
|
5
|
+
*/
|
|
6
|
+
type ExtractComponentProps<T> = T extends (props: infer P) => any ? Omit<P, 'ref' | 'children'> : {};
|
|
7
|
+
export declare function KTAsync<T extends KTComponent>(props: {
|
|
8
|
+
ref?: KTRef<JSX.Element>;
|
|
9
|
+
skeleton?: JSX.Element;
|
|
10
|
+
component: T;
|
|
11
|
+
children?: KTRawContent;
|
|
12
|
+
} & ExtractComponentProps<T>): JSX.Element;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { KTRef } from '../reactive/ref.js';
|
|
2
|
+
import { KTReactive } from '../types/reactive.js';
|
|
3
|
+
export type KTForElement = JSX.Element;
|
|
4
|
+
export interface KTForProps<T> {
|
|
5
|
+
ref?: KTRef<KTForElement>;
|
|
6
|
+
list: T[] | KTReactive<T[]>;
|
|
7
|
+
key?: (item: T, index: number, array: T[]) => any;
|
|
8
|
+
map: (item: T, index: number, array: T[]) => HTMLElement;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* KTFor - List rendering component with key-based optimization
|
|
12
|
+
* Returns a Comment anchor node with rendered elements in __kt_for_list__
|
|
13
|
+
*/
|
|
14
|
+
export declare function KTFor<T>(props: KTForProps<T>): KTForElement;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { KTRef } from '../reactive/ref.js';
|
|
2
|
+
import { KTReactive } from '../types/reactive.js';
|
|
3
|
+
import { KTRawContent } from '../types/h.js';
|
|
4
|
+
export interface FragmentProps<T extends HTMLElement = HTMLElement> {
|
|
5
|
+
/** Array of child elements, supports reactive arrays */
|
|
6
|
+
children: T[] | KTReactive<T[]>;
|
|
7
|
+
/** element key function for optimization (future enhancement) */
|
|
8
|
+
key?: (element: T, index: number, array: T[]) => any;
|
|
9
|
+
/** ref to get the anchor node */
|
|
10
|
+
ref?: KTRef<JSX.Element>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Fragment - Container component for managing arrays of child elements
|
|
14
|
+
*
|
|
15
|
+
* Features:
|
|
16
|
+
* 1. Returns a comment anchor node, child elements are inserted after the anchor
|
|
17
|
+
* 2. Supports reactive arrays, automatically updates DOM when array changes
|
|
18
|
+
* 3. Basic version uses simple replacement algorithm (remove all old elements, insert all new elements)
|
|
19
|
+
* 4. Future enhancement: key-based optimization
|
|
20
|
+
*
|
|
21
|
+
* Usage example:
|
|
22
|
+
* ```tsx
|
|
23
|
+
* const children = ref([<div>A</div>, <div>B</div>]);
|
|
24
|
+
* const fragment = <Fragment children={children} />;
|
|
25
|
+
* document.body.appendChild(fragment);
|
|
26
|
+
*
|
|
27
|
+
* // Automatic update
|
|
28
|
+
* children.value = [<div>C</div>, <div>D</div>];
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function Fragment<T extends HTMLElement = HTMLElement>(props: FragmentProps<T>): JSX.Element;
|
|
32
|
+
/**
|
|
33
|
+
* Convert KTRawContent to HTMLElement array
|
|
34
|
+
*/
|
|
35
|
+
export declare function convertChildrenToElements(children: KTRawContent): HTMLElement[];
|
package/dist/jsx/if.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { JSXTag } from '@ktjs/shared';
|
|
2
|
+
import { KTAttribute } from '../types/h.js';
|
|
3
|
+
import { KElseElement, KIfElement } from '../types/directives.js';
|
|
4
|
+
export declare function kif(tag: JSXTag, props: KTAttribute): KIfElement;
|
|
5
|
+
export declare function kelse(tag: JSXTag, props: KTAttribute): KElseElement;
|
|
6
|
+
/**
|
|
7
|
+
* Deal with `k-if` and `k-else`, checking syntax and applying listeners
|
|
8
|
+
* @param el parent element
|
|
9
|
+
*/
|
|
10
|
+
export declare function kifelseApply(el: HTMLElement): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { F as Fragment, h as createElement, c as createRedrawable, j as jsx, a as jsxDEV, b as jsxs } from '../jsx-runtime-DGJHUQEM.js';
|