@ktjs/core 0.29.7 → 0.29.9
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/index.mjs +4 -599
- package/dist/jsx-runtime-DGJHUQEM.js +600 -0
- package/dist/jsx-runtime.d.ts +245 -0
- package/dist/jsx-runtime.mjs +1 -0
- package/package.json +7 -7
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import { $isArray, $isThenable, $append, $isNode, $emptyFn, $is, $replaceNode, $entries, $applyModel, $forEach } from '@ktjs/shared';
|
|
2
|
+
|
|
3
|
+
var KTReactiveType = /* @__PURE__ */ ((KTReactiveType2) => {
|
|
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
|
+
};
|
|
313
|
+
|
|
314
|
+
const toReactive = (value, onChange) => {
|
|
315
|
+
if (isKT(value)) {
|
|
316
|
+
if (onChange) {
|
|
317
|
+
value.addOnChange(onChange);
|
|
318
|
+
}
|
|
319
|
+
return value;
|
|
320
|
+
} else {
|
|
321
|
+
return ref(value, onChange);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
function dereactive(value) {
|
|
325
|
+
return isKT(value) ? value.value : value;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function applyKModel(element, valueRef) {
|
|
329
|
+
if (!isKT(valueRef)) {
|
|
330
|
+
console.warn('[@ktjs/core warn]',"k-model value must be a KTRef.");
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (element instanceof HTMLInputElement) {
|
|
334
|
+
if (element.type === "radio" || element.type === "checkbox") {
|
|
335
|
+
$applyModel(element, valueRef, "checked", "change");
|
|
336
|
+
} else {
|
|
337
|
+
$applyModel(element, valueRef, "value", "input");
|
|
338
|
+
}
|
|
339
|
+
} else if (element instanceof HTMLSelectElement) {
|
|
340
|
+
$applyModel(element, valueRef, "value", "change");
|
|
341
|
+
} else if (element instanceof HTMLTextAreaElement) {
|
|
342
|
+
$applyModel(element, valueRef, "value", "input");
|
|
343
|
+
} else {
|
|
344
|
+
console.warn('[@ktjs/core warn]',"not supported element for k-model:");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const htmlCreator = (tag) => document.createElement(tag);
|
|
349
|
+
const svgCreator = (tag) => document.createElementNS("http://www.w3.org/2000/svg", tag);
|
|
350
|
+
const mathMLCreator = (tag) => document.createElementNS("http://www.w3.org/1998/Math/MathML", tag);
|
|
351
|
+
let creator = htmlCreator;
|
|
352
|
+
const h = (tag, attr, content) => {
|
|
353
|
+
if (typeof tag !== "string") {
|
|
354
|
+
throw new Error("[@ktjs/core error] tagName must be a string.");
|
|
355
|
+
}
|
|
356
|
+
if (attr) {
|
|
357
|
+
if ("__svg" in attr) {
|
|
358
|
+
delete attr["__svg"];
|
|
359
|
+
creator = svgCreator;
|
|
360
|
+
} else if ("__mathml" in attr) {
|
|
361
|
+
delete attr["__mathml"];
|
|
362
|
+
creator = mathMLCreator;
|
|
363
|
+
} else {
|
|
364
|
+
creator = htmlCreator;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
const element = creator(tag);
|
|
368
|
+
applyAttr(element, attr);
|
|
369
|
+
applyContent(element, content);
|
|
370
|
+
if (typeof attr === "object" && attr !== null && "k-model" in attr) {
|
|
371
|
+
applyKModel(element, attr["k-model"]);
|
|
372
|
+
}
|
|
373
|
+
return element;
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const FRAGMENT_MOUNT_PATCHED = "__kt_fragment_mount_patched__";
|
|
377
|
+
const FRAGMENT_MOUNT = "__kt_fragment_mount__";
|
|
378
|
+
if (typeof Node !== "undefined" && !globalThis[FRAGMENT_MOUNT_PATCHED]) {
|
|
379
|
+
globalThis[FRAGMENT_MOUNT_PATCHED] = true;
|
|
380
|
+
const originAppendChild = Node.prototype.appendChild;
|
|
381
|
+
Node.prototype.appendChild = function(node) {
|
|
382
|
+
const result = originAppendChild.call(this, node);
|
|
383
|
+
const mount = node[FRAGMENT_MOUNT];
|
|
384
|
+
if (typeof mount === "function") {
|
|
385
|
+
mount();
|
|
386
|
+
}
|
|
387
|
+
return result;
|
|
388
|
+
};
|
|
389
|
+
const originInsertBefore = Node.prototype.insertBefore;
|
|
390
|
+
Node.prototype.insertBefore = function(node, child) {
|
|
391
|
+
const result = originInsertBefore.call(this, node, child);
|
|
392
|
+
const mount = node[FRAGMENT_MOUNT];
|
|
393
|
+
if (typeof mount === "function") {
|
|
394
|
+
mount();
|
|
395
|
+
}
|
|
396
|
+
return result;
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function Fragment$1(props) {
|
|
400
|
+
const elements = [];
|
|
401
|
+
const anchor = document.createComment("kt-fragment");
|
|
402
|
+
let inserted = false;
|
|
403
|
+
let observer;
|
|
404
|
+
const redraw = () => {
|
|
405
|
+
const newElements = childrenRef.value;
|
|
406
|
+
const parent = anchor.parentNode;
|
|
407
|
+
if (!parent) {
|
|
408
|
+
elements.length = 0;
|
|
409
|
+
for (let i = 0; i < newElements.length; i++) {
|
|
410
|
+
elements.push(newElements[i]);
|
|
411
|
+
}
|
|
412
|
+
anchor.__kt_fragment_list__ = elements;
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
for (let i = 0; i < elements.length; i++) {
|
|
416
|
+
elements[i].remove();
|
|
417
|
+
}
|
|
418
|
+
const fragment = document.createDocumentFragment();
|
|
419
|
+
elements.length = 0;
|
|
420
|
+
for (let i = 0; i < newElements.length; i++) {
|
|
421
|
+
const element = newElements[i];
|
|
422
|
+
elements.push(element);
|
|
423
|
+
fragment.appendChild(element);
|
|
424
|
+
}
|
|
425
|
+
parent.insertBefore(fragment, anchor.nextSibling);
|
|
426
|
+
inserted = true;
|
|
427
|
+
delete anchor[FRAGMENT_MOUNT];
|
|
428
|
+
observer?.disconnect();
|
|
429
|
+
observer = void 0;
|
|
430
|
+
anchor.__kt_fragment_list__ = elements;
|
|
431
|
+
};
|
|
432
|
+
const childrenRef = toReactive(props.children, redraw);
|
|
433
|
+
const renderInitial = () => {
|
|
434
|
+
const current = childrenRef.value;
|
|
435
|
+
elements.length = 0;
|
|
436
|
+
const fragment = document.createDocumentFragment();
|
|
437
|
+
for (let i = 0; i < current.length; i++) {
|
|
438
|
+
const element = current[i];
|
|
439
|
+
elements.push(element);
|
|
440
|
+
fragment.appendChild(element);
|
|
441
|
+
}
|
|
442
|
+
anchor.__kt_fragment_list__ = elements;
|
|
443
|
+
const parent = anchor.parentNode;
|
|
444
|
+
if (parent && !inserted) {
|
|
445
|
+
parent.insertBefore(fragment, anchor.nextSibling);
|
|
446
|
+
inserted = true;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
renderInitial();
|
|
450
|
+
anchor[FRAGMENT_MOUNT] = () => {
|
|
451
|
+
if (!inserted && anchor.parentNode) {
|
|
452
|
+
redraw();
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
observer = new MutationObserver(() => {
|
|
456
|
+
if (anchor.parentNode && !inserted) {
|
|
457
|
+
redraw();
|
|
458
|
+
observer?.disconnect();
|
|
459
|
+
observer = void 0;
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
463
|
+
$initRef(props, anchor);
|
|
464
|
+
return anchor;
|
|
465
|
+
}
|
|
466
|
+
function convertChildrenToElements(children) {
|
|
467
|
+
const elements = [];
|
|
468
|
+
const processChild = (child) => {
|
|
469
|
+
if (child === void 0 || child === null || child === false || child === true) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
if ($isArray(child)) {
|
|
473
|
+
$forEach(child, processChild);
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
477
|
+
const span = document.createElement("span");
|
|
478
|
+
span.textContent = String(child);
|
|
479
|
+
elements.push(span);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (child instanceof HTMLElement) {
|
|
483
|
+
elements.push(child);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (isKT(child)) {
|
|
487
|
+
processChild(child.value);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
console.warn("Fragment: unsupported child type", child);
|
|
491
|
+
};
|
|
492
|
+
processChild(children);
|
|
493
|
+
return elements;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const jsxh = (tag, props) => {
|
|
497
|
+
if (typeof tag === "function") {
|
|
498
|
+
return tag(props);
|
|
499
|
+
} else {
|
|
500
|
+
return h(tag, props, props.children);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
const placeholder = (data) => document.createComment(data);
|
|
504
|
+
|
|
505
|
+
function kif(tag, props) {
|
|
506
|
+
const kif2 = toReactive(props["k-if"]);
|
|
507
|
+
let el = kif2.value ? jsxh(tag, props) : placeholder("k-if");
|
|
508
|
+
el.__kif__ = kif2;
|
|
509
|
+
const setter = $initRef(props, el);
|
|
510
|
+
kif2.addOnChange((newValue) => {
|
|
511
|
+
const old = el;
|
|
512
|
+
el = newValue ? jsxh(tag, props) : placeholder("k-if");
|
|
513
|
+
el.__kif__ = kif2;
|
|
514
|
+
setter(props, el);
|
|
515
|
+
$replaceNode(old, el);
|
|
516
|
+
});
|
|
517
|
+
return el;
|
|
518
|
+
}
|
|
519
|
+
function kelse(tag, props) {
|
|
520
|
+
let el = placeholder("k-else");
|
|
521
|
+
const setter = $initRef(props, el);
|
|
522
|
+
el.__kelse__ = (newValue) => {
|
|
523
|
+
const old = el;
|
|
524
|
+
el = newValue ? placeholder("k-else") : jsxh(tag, props);
|
|
525
|
+
el.__kelse__ = old.__kelse__;
|
|
526
|
+
setter(props, el);
|
|
527
|
+
$replaceNode(old, el);
|
|
528
|
+
};
|
|
529
|
+
return el;
|
|
530
|
+
}
|
|
531
|
+
function kifelseApply(el) {
|
|
532
|
+
const childNodes = el.childNodes;
|
|
533
|
+
if (childNodes.length === 0) {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (childNodes[0].__kelse__) {
|
|
537
|
+
throw new Error("[@ktjs/core error] k-else cannot be the first child of its parent element.");
|
|
538
|
+
}
|
|
539
|
+
for (let i = 1; i < childNodes.length; i++) {
|
|
540
|
+
const child = childNodes[i];
|
|
541
|
+
if (!child.__kelse__) {
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
const last = childNodes[i - 1];
|
|
545
|
+
if (!("__kif__" in last)) {
|
|
546
|
+
throw new Error("[@ktjs/core error] k-else must be immediately preceded by a k-if element.");
|
|
547
|
+
}
|
|
548
|
+
const kif2 = last.__kif__;
|
|
549
|
+
if (!kif2) {
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (!isKT(kif2)) {
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
kif2.addOnChange(child.__kelse__);
|
|
556
|
+
if (!kif2.value) {
|
|
557
|
+
child.__kelse__(false);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function jsx(tag, props) {
|
|
563
|
+
if (isComputed(props.ref)) {
|
|
564
|
+
throw new Error("[@ktjs/core error] Cannot assign a computed value to an element.");
|
|
565
|
+
}
|
|
566
|
+
if ("k-if" in props) {
|
|
567
|
+
return kif(tag, props);
|
|
568
|
+
}
|
|
569
|
+
if ("k-else" in props) {
|
|
570
|
+
return kelse(tag, props);
|
|
571
|
+
}
|
|
572
|
+
const el = jsxh(tag, props);
|
|
573
|
+
$initRef(props, el);
|
|
574
|
+
kifelseApply(el);
|
|
575
|
+
return el;
|
|
576
|
+
}
|
|
577
|
+
function Fragment(props) {
|
|
578
|
+
const { children } = props ?? {};
|
|
579
|
+
if (!children) {
|
|
580
|
+
return placeholder("kt-fragment-empty");
|
|
581
|
+
}
|
|
582
|
+
const elements = convertChildrenToElements(children);
|
|
583
|
+
return Fragment$1({ children: elements });
|
|
584
|
+
}
|
|
585
|
+
const jsxDEV = (...args) => {
|
|
586
|
+
return jsx(...args);
|
|
587
|
+
};
|
|
588
|
+
const jsxs = jsx;
|
|
589
|
+
function createRedrawable(creator) {
|
|
590
|
+
const elRef = ref();
|
|
591
|
+
const redraw = () => {
|
|
592
|
+
elRef.value = creator();
|
|
593
|
+
elRef.redraw = redraw;
|
|
594
|
+
return elRef.value;
|
|
595
|
+
};
|
|
596
|
+
redraw();
|
|
597
|
+
return elRef;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
export { $initRef as $, Fragment as F, KTReactiveType as K, jsxDEV as a, jsxs as b, createRedrawable as c, $modelOrRef as d, KTRef as e, dereactive as f, isComputed as g, h, isKT as i, jsx as j, isRef as k, toRef as l, ref as r, surfaceRef as s, toReactive as t };
|