@ktjs/core 0.20.2 → 0.21.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 +1 -453
- package/dist/index.d.ts +33 -6
- package/dist/index.iife.js +74 -25
- package/dist/index.legacy.js +68 -26
- package/dist/index.mjs +73 -26
- package/dist/jsx/index.d.ts +33 -5
- package/dist/jsx/index.mjs +73 -26
- package/dist/jsx/jsx-runtime.d.ts +16 -4
- package/dist/jsx/jsx-runtime.mjs +47 -25
- package/package.json +2 -2
package/dist/index.iife.js
CHANGED
|
@@ -3,6 +3,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
3
3
|
|
|
4
4
|
// Cached native methods for performance optimization
|
|
5
5
|
const $isArray = Array.isArray;
|
|
6
|
+
const $entries = Object.entries;
|
|
6
7
|
const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
|
|
7
8
|
|
|
8
9
|
// Error handling utilities
|
|
@@ -12,6 +13,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
12
13
|
|
|
13
14
|
// DOM manipulation utilities
|
|
14
15
|
// # dom natives
|
|
16
|
+
const $replaceWith = Element.prototype.replaceWith;
|
|
15
17
|
/**
|
|
16
18
|
* & Remove `bind` because it is shockingly slower than wrapper
|
|
17
19
|
* & `window.document` is safe because it is not configurable and its setter is undefined
|
|
@@ -51,7 +53,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
51
53
|
|
|
52
54
|
// Shared utilities and cached native methods for kt.js framework
|
|
53
55
|
// Re-export all utilities
|
|
54
|
-
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.
|
|
56
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.2' });
|
|
55
57
|
|
|
56
58
|
const booleanHandler = (element, key, value) => {
|
|
57
59
|
if (key in element) {
|
|
@@ -194,7 +196,6 @@ var __ktjs_core__ = (function (exports) {
|
|
|
194
196
|
}
|
|
195
197
|
}
|
|
196
198
|
|
|
197
|
-
document.createElement('div');
|
|
198
199
|
const htmlCreator = (tag) => document.createElement(tag);
|
|
199
200
|
const svgCreator = (tag) => document.createElementNS('http://www.w3.org/2000/svg', tag);
|
|
200
201
|
const mathMLCreator = (tag) => document.createElementNS('http://www.w3.org/1998/Math/MathML', tag);
|
|
@@ -212,7 +213,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
212
213
|
* ## About
|
|
213
214
|
* @package @ktjs/core
|
|
214
215
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
215
|
-
* @version 0.
|
|
216
|
+
* @version 0.21.1 (Last Update: 2026.02.02 09:20:07.959)
|
|
216
217
|
* @license MIT
|
|
217
218
|
* @link https://github.com/baendlorel/kt.js
|
|
218
219
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -241,14 +242,6 @@ var __ktjs_core__ = (function (exports) {
|
|
|
241
242
|
// * Handle content
|
|
242
243
|
applyAttr(element, attr);
|
|
243
244
|
applyContent(element, content);
|
|
244
|
-
// if (tag === 'svg') {
|
|
245
|
-
// tempWrapper.innerHTML = element.outerHTML;
|
|
246
|
-
// return tempWrapper.firstChild as HTML<T>;
|
|
247
|
-
// }
|
|
248
|
-
// if (tag === 'math') {
|
|
249
|
-
// tempWrapper.innerHTML = element.outerHTML;
|
|
250
|
-
// return tempWrapper.firstChild as HTML<T>;
|
|
251
|
-
// }
|
|
252
245
|
return element;
|
|
253
246
|
};
|
|
254
247
|
|
|
@@ -257,7 +250,13 @@ var __ktjs_core__ = (function (exports) {
|
|
|
257
250
|
* Indicates that this is a KTRef instance
|
|
258
251
|
*/
|
|
259
252
|
isKT = true;
|
|
253
|
+
/**
|
|
254
|
+
* @internal
|
|
255
|
+
*/
|
|
260
256
|
_value;
|
|
257
|
+
/**
|
|
258
|
+
* @internal
|
|
259
|
+
*/
|
|
261
260
|
_onChanges;
|
|
262
261
|
constructor(_value, _onChanges) {
|
|
263
262
|
this._value = _value;
|
|
@@ -299,37 +298,85 @@ var __ktjs_core__ = (function (exports) {
|
|
|
299
298
|
return false;
|
|
300
299
|
}
|
|
301
300
|
}
|
|
301
|
+
const isKTRef = (obj) => {
|
|
302
|
+
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
303
|
+
};
|
|
302
304
|
/**
|
|
303
305
|
* Reference to the created HTML element.
|
|
306
|
+
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
304
307
|
* - can alse be used to store normal values, but it is not reactive.
|
|
305
308
|
* @param value mostly an HTMLElement
|
|
306
309
|
*/
|
|
307
310
|
function ref(value, onChange) {
|
|
308
311
|
return new KTRef(value, onChange ? [onChange] : []);
|
|
309
312
|
}
|
|
313
|
+
function kcollect() {
|
|
314
|
+
const newObj = {};
|
|
315
|
+
const entries = $entries(this);
|
|
316
|
+
for (let i = 0; i < entries.length; i++) {
|
|
317
|
+
const key = entries[i][0];
|
|
318
|
+
if (key === 'kcollect') {
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
newObj[key] = entries[i][1].value;
|
|
322
|
+
}
|
|
323
|
+
return newObj;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Make all first-level properties of the object a `KTRef`.
|
|
327
|
+
* - `obj.a.b` is not reactive
|
|
328
|
+
*/
|
|
329
|
+
const surfaceRef = (obj) => {
|
|
330
|
+
const entries = $entries(obj);
|
|
331
|
+
const newObj = { kcollect };
|
|
332
|
+
for (let i = 0; i < entries.length; i++) {
|
|
333
|
+
newObj[entries[i][0]] = ref(entries[i][1]);
|
|
334
|
+
}
|
|
335
|
+
return newObj;
|
|
336
|
+
};
|
|
310
337
|
|
|
311
338
|
const dummyRef = { value: null };
|
|
339
|
+
const create = (tag, props) => {
|
|
340
|
+
if (typeof tag === 'function') {
|
|
341
|
+
return tag(props);
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
return h(tag, props, props.children);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
const placeholder = () => document.createComment('k-if');
|
|
312
348
|
/**
|
|
313
349
|
* @param tag html tag or function component
|
|
314
350
|
* @param props properties/attributes
|
|
315
351
|
*/
|
|
316
352
|
function jsx(tag, props) {
|
|
317
|
-
const
|
|
353
|
+
const maybeDummyRef = isKTRef(props.ref) ? props.ref : dummyRef;
|
|
318
354
|
let el;
|
|
319
|
-
if ('k-if' in props
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
355
|
+
if ('k-if' in props) {
|
|
356
|
+
const kif = props['k-if'];
|
|
357
|
+
let condition = kif; // assume boolean by default
|
|
358
|
+
// Handle reactive k-if
|
|
359
|
+
if (isKTRef(kif)) {
|
|
360
|
+
kif.addOnChange((newValue, oldValue) => {
|
|
361
|
+
if (newValue === oldValue) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const oldEl = el;
|
|
365
|
+
el = newValue ? create(tag, props) : placeholder();
|
|
366
|
+
$replaceWith.call(oldEl, el);
|
|
367
|
+
maybeDummyRef.value = el;
|
|
368
|
+
});
|
|
369
|
+
condition = kif.value;
|
|
370
|
+
}
|
|
371
|
+
if (!condition) {
|
|
372
|
+
// & make comment placeholder in case that ref might be redrawn later
|
|
373
|
+
el = placeholder();
|
|
374
|
+
maybeDummyRef.value = el;
|
|
375
|
+
return el;
|
|
376
|
+
}
|
|
331
377
|
}
|
|
332
|
-
|
|
378
|
+
el = create(tag, props);
|
|
379
|
+
maybeDummyRef.value = el;
|
|
333
380
|
return el;
|
|
334
381
|
}
|
|
335
382
|
/**
|
|
@@ -628,10 +675,12 @@ var __ktjs_core__ = (function (exports) {
|
|
|
628
675
|
exports.createElement = h;
|
|
629
676
|
exports.createRedrawable = createRedrawable;
|
|
630
677
|
exports.h = h;
|
|
678
|
+
exports.isKTRef = isKTRef;
|
|
631
679
|
exports.jsx = jsx;
|
|
632
680
|
exports.jsxDEV = jsxDEV;
|
|
633
681
|
exports.jsxs = jsxs;
|
|
634
682
|
exports.ref = ref;
|
|
683
|
+
exports.surfaceRef = surfaceRef;
|
|
635
684
|
|
|
636
685
|
return exports;
|
|
637
686
|
|
package/dist/index.legacy.js
CHANGED
|
@@ -3,6 +3,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
3
3
|
|
|
4
4
|
// Cached native methods for performance optimization
|
|
5
5
|
const $isArray = Array.isArray;
|
|
6
|
+
const $entries = Object.entries;
|
|
6
7
|
const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
|
|
7
8
|
|
|
8
9
|
// Error handling utilities
|
|
@@ -12,6 +13,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
12
13
|
|
|
13
14
|
// DOM manipulation utilities
|
|
14
15
|
// # dom natives
|
|
16
|
+
const $replaceWith = Element.prototype.replaceWith;
|
|
15
17
|
/**
|
|
16
18
|
* & Remove `bind` because it is shockingly slower than wrapper
|
|
17
19
|
* & `window.document` is safe because it is not configurable and its setter is undefined
|
|
@@ -51,7 +53,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
51
53
|
|
|
52
54
|
// Shared utilities and cached native methods for kt.js framework
|
|
53
55
|
// Re-export all utilities
|
|
54
|
-
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.
|
|
56
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.2' });
|
|
55
57
|
|
|
56
58
|
var booleanHandler = function (element, key, value) {
|
|
57
59
|
if (key in element) {
|
|
@@ -199,7 +201,6 @@ var __ktjs_core__ = (function (exports) {
|
|
|
199
201
|
}
|
|
200
202
|
}
|
|
201
203
|
|
|
202
|
-
document.createElement('div');
|
|
203
204
|
var htmlCreator = function (tag) { return document.createElement(tag); };
|
|
204
205
|
var svgCreator = function (tag) { return document.createElementNS('http://www.w3.org/2000/svg', tag); };
|
|
205
206
|
var mathMLCreator = function (tag) { return document.createElementNS('http://www.w3.org/1998/Math/MathML', tag); };
|
|
@@ -217,7 +218,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
217
218
|
* ## About
|
|
218
219
|
* @package @ktjs/core
|
|
219
220
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
220
|
-
* @version 0.
|
|
221
|
+
* @version 0.21.1 (Last Update: 2026.02.02 09:20:07.959)
|
|
221
222
|
* @license MIT
|
|
222
223
|
* @link https://github.com/baendlorel/kt.js
|
|
223
224
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -246,14 +247,6 @@ var __ktjs_core__ = (function (exports) {
|
|
|
246
247
|
// * Handle content
|
|
247
248
|
applyAttr(element, attr);
|
|
248
249
|
applyContent(element, content);
|
|
249
|
-
// if (tag === 'svg') {
|
|
250
|
-
// tempWrapper.innerHTML = element.outerHTML;
|
|
251
|
-
// return tempWrapper.firstChild as HTML<T>;
|
|
252
|
-
// }
|
|
253
|
-
// if (tag === 'math') {
|
|
254
|
-
// tempWrapper.innerHTML = element.outerHTML;
|
|
255
|
-
// return tempWrapper.firstChild as HTML<T>;
|
|
256
|
-
// }
|
|
257
250
|
return element;
|
|
258
251
|
};
|
|
259
252
|
|
|
@@ -307,38 +300,85 @@ var __ktjs_core__ = (function (exports) {
|
|
|
307
300
|
};
|
|
308
301
|
return KTRef;
|
|
309
302
|
}());
|
|
303
|
+
var isKTRef = function (obj) {
|
|
304
|
+
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
305
|
+
};
|
|
310
306
|
/**
|
|
311
307
|
* Reference to the created HTML element.
|
|
308
|
+
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
312
309
|
* - can alse be used to store normal values, but it is not reactive.
|
|
313
310
|
* @param value mostly an HTMLElement
|
|
314
311
|
*/
|
|
315
312
|
function ref(value, onChange) {
|
|
316
313
|
return new KTRef(value, onChange ? [onChange] : []);
|
|
317
314
|
}
|
|
315
|
+
function kcollect() {
|
|
316
|
+
var newObj = {};
|
|
317
|
+
var entries = $entries(this);
|
|
318
|
+
for (var i = 0; i < entries.length; i++) {
|
|
319
|
+
var key = entries[i][0];
|
|
320
|
+
if (key === 'kcollect') {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
newObj[key] = entries[i][1].value;
|
|
324
|
+
}
|
|
325
|
+
return newObj;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Make all first-level properties of the object a `KTRef`.
|
|
329
|
+
* - `obj.a.b` is not reactive
|
|
330
|
+
*/
|
|
331
|
+
var surfaceRef = function (obj) {
|
|
332
|
+
var entries = $entries(obj);
|
|
333
|
+
var newObj = { kcollect: kcollect };
|
|
334
|
+
for (var i = 0; i < entries.length; i++) {
|
|
335
|
+
newObj[entries[i][0]] = ref(entries[i][1]);
|
|
336
|
+
}
|
|
337
|
+
return newObj;
|
|
338
|
+
};
|
|
318
339
|
|
|
319
340
|
var dummyRef = { value: null };
|
|
341
|
+
var create = function (tag, props) {
|
|
342
|
+
if (typeof tag === 'function') {
|
|
343
|
+
return tag(props);
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
return h(tag, props, props.children);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
var placeholder = function () { return document.createComment('k-if'); };
|
|
320
350
|
/**
|
|
321
351
|
* @param tag html tag or function component
|
|
322
352
|
* @param props properties/attributes
|
|
323
353
|
*/
|
|
324
354
|
function jsx(tag, props) {
|
|
325
|
-
var
|
|
326
|
-
var ref = ((_a = props.ref) === null || _a === void 0 ? void 0 : _a.isKT) ? props.ref : dummyRef;
|
|
355
|
+
var maybeDummyRef = isKTRef(props.ref) ? props.ref : dummyRef;
|
|
327
356
|
var el;
|
|
328
|
-
if ('k-if' in props
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
357
|
+
if ('k-if' in props) {
|
|
358
|
+
var kif = props['k-if'];
|
|
359
|
+
var condition = kif; // assume boolean by default
|
|
360
|
+
// Handle reactive k-if
|
|
361
|
+
if (isKTRef(kif)) {
|
|
362
|
+
kif.addOnChange(function (newValue, oldValue) {
|
|
363
|
+
if (newValue === oldValue) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
var oldEl = el;
|
|
367
|
+
el = newValue ? create(tag, props) : placeholder();
|
|
368
|
+
$replaceWith.call(oldEl, el);
|
|
369
|
+
maybeDummyRef.value = el;
|
|
370
|
+
});
|
|
371
|
+
condition = kif.value;
|
|
372
|
+
}
|
|
373
|
+
if (!condition) {
|
|
374
|
+
// & make comment placeholder in case that ref might be redrawn later
|
|
375
|
+
el = placeholder();
|
|
376
|
+
maybeDummyRef.value = el;
|
|
377
|
+
return el;
|
|
378
|
+
}
|
|
340
379
|
}
|
|
341
|
-
|
|
380
|
+
el = create(tag, props);
|
|
381
|
+
maybeDummyRef.value = el;
|
|
342
382
|
return el;
|
|
343
383
|
}
|
|
344
384
|
/**
|
|
@@ -644,10 +684,12 @@ var __ktjs_core__ = (function (exports) {
|
|
|
644
684
|
exports.createElement = h;
|
|
645
685
|
exports.createRedrawable = createRedrawable;
|
|
646
686
|
exports.h = h;
|
|
687
|
+
exports.isKTRef = isKTRef;
|
|
647
688
|
exports.jsx = jsx;
|
|
648
689
|
exports.jsxDEV = jsxDEV;
|
|
649
690
|
exports.jsxs = jsxs;
|
|
650
691
|
exports.ref = ref;
|
|
692
|
+
exports.surfaceRef = surfaceRef;
|
|
651
693
|
|
|
652
694
|
return exports;
|
|
653
695
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Cached native methods for performance optimization
|
|
2
2
|
const $isArray = Array.isArray;
|
|
3
|
+
const $entries = Object.entries;
|
|
3
4
|
const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
|
|
4
5
|
|
|
5
6
|
// Error handling utilities
|
|
@@ -9,6 +10,7 @@ const $throw = (message) => {
|
|
|
9
10
|
|
|
10
11
|
// DOM manipulation utilities
|
|
11
12
|
// # dom natives
|
|
13
|
+
const $replaceWith = Element.prototype.replaceWith;
|
|
12
14
|
/**
|
|
13
15
|
* & Remove `bind` because it is shockingly slower than wrapper
|
|
14
16
|
* & `window.document` is safe because it is not configurable and its setter is undefined
|
|
@@ -48,7 +50,7 @@ const { get: $buttonDisabledGetter, set: $buttonDisabledSetter } = Object.getOwn
|
|
|
48
50
|
|
|
49
51
|
// Shared utilities and cached native methods for kt.js framework
|
|
50
52
|
// Re-export all utilities
|
|
51
|
-
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.
|
|
53
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.2' });
|
|
52
54
|
|
|
53
55
|
const booleanHandler = (element, key, value) => {
|
|
54
56
|
if (key in element) {
|
|
@@ -191,7 +193,6 @@ function applyContent(element, content) {
|
|
|
191
193
|
}
|
|
192
194
|
}
|
|
193
195
|
|
|
194
|
-
document.createElement('div');
|
|
195
196
|
const htmlCreator = (tag) => document.createElement(tag);
|
|
196
197
|
const svgCreator = (tag) => document.createElementNS('http://www.w3.org/2000/svg', tag);
|
|
197
198
|
const mathMLCreator = (tag) => document.createElementNS('http://www.w3.org/1998/Math/MathML', tag);
|
|
@@ -209,7 +210,7 @@ const MATHML_ATTR_FLAG = '__kt_mathml__';
|
|
|
209
210
|
* ## About
|
|
210
211
|
* @package @ktjs/core
|
|
211
212
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
212
|
-
* @version 0.
|
|
213
|
+
* @version 0.21.1 (Last Update: 2026.02.02 09:20:07.959)
|
|
213
214
|
* @license MIT
|
|
214
215
|
* @link https://github.com/baendlorel/kt.js
|
|
215
216
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -238,14 +239,6 @@ const h = (tag, attr, content) => {
|
|
|
238
239
|
// * Handle content
|
|
239
240
|
applyAttr(element, attr);
|
|
240
241
|
applyContent(element, content);
|
|
241
|
-
// if (tag === 'svg') {
|
|
242
|
-
// tempWrapper.innerHTML = element.outerHTML;
|
|
243
|
-
// return tempWrapper.firstChild as HTML<T>;
|
|
244
|
-
// }
|
|
245
|
-
// if (tag === 'math') {
|
|
246
|
-
// tempWrapper.innerHTML = element.outerHTML;
|
|
247
|
-
// return tempWrapper.firstChild as HTML<T>;
|
|
248
|
-
// }
|
|
249
242
|
return element;
|
|
250
243
|
};
|
|
251
244
|
|
|
@@ -254,7 +247,13 @@ class KTRef {
|
|
|
254
247
|
* Indicates that this is a KTRef instance
|
|
255
248
|
*/
|
|
256
249
|
isKT = true;
|
|
250
|
+
/**
|
|
251
|
+
* @internal
|
|
252
|
+
*/
|
|
257
253
|
_value;
|
|
254
|
+
/**
|
|
255
|
+
* @internal
|
|
256
|
+
*/
|
|
258
257
|
_onChanges;
|
|
259
258
|
constructor(_value, _onChanges) {
|
|
260
259
|
this._value = _value;
|
|
@@ -296,37 +295,85 @@ class KTRef {
|
|
|
296
295
|
return false;
|
|
297
296
|
}
|
|
298
297
|
}
|
|
298
|
+
const isKTRef = (obj) => {
|
|
299
|
+
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
300
|
+
};
|
|
299
301
|
/**
|
|
300
302
|
* Reference to the created HTML element.
|
|
303
|
+
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
301
304
|
* - can alse be used to store normal values, but it is not reactive.
|
|
302
305
|
* @param value mostly an HTMLElement
|
|
303
306
|
*/
|
|
304
307
|
function ref(value, onChange) {
|
|
305
308
|
return new KTRef(value, onChange ? [onChange] : []);
|
|
306
309
|
}
|
|
310
|
+
function kcollect() {
|
|
311
|
+
const newObj = {};
|
|
312
|
+
const entries = $entries(this);
|
|
313
|
+
for (let i = 0; i < entries.length; i++) {
|
|
314
|
+
const key = entries[i][0];
|
|
315
|
+
if (key === 'kcollect') {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
newObj[key] = entries[i][1].value;
|
|
319
|
+
}
|
|
320
|
+
return newObj;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Make all first-level properties of the object a `KTRef`.
|
|
324
|
+
* - `obj.a.b` is not reactive
|
|
325
|
+
*/
|
|
326
|
+
const surfaceRef = (obj) => {
|
|
327
|
+
const entries = $entries(obj);
|
|
328
|
+
const newObj = { kcollect };
|
|
329
|
+
for (let i = 0; i < entries.length; i++) {
|
|
330
|
+
newObj[entries[i][0]] = ref(entries[i][1]);
|
|
331
|
+
}
|
|
332
|
+
return newObj;
|
|
333
|
+
};
|
|
307
334
|
|
|
308
335
|
const dummyRef = { value: null };
|
|
336
|
+
const create = (tag, props) => {
|
|
337
|
+
if (typeof tag === 'function') {
|
|
338
|
+
return tag(props);
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
return h(tag, props, props.children);
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
const placeholder = () => document.createComment('k-if');
|
|
309
345
|
/**
|
|
310
346
|
* @param tag html tag or function component
|
|
311
347
|
* @param props properties/attributes
|
|
312
348
|
*/
|
|
313
349
|
function jsx(tag, props) {
|
|
314
|
-
const
|
|
350
|
+
const maybeDummyRef = isKTRef(props.ref) ? props.ref : dummyRef;
|
|
315
351
|
let el;
|
|
316
|
-
if ('k-if' in props
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
352
|
+
if ('k-if' in props) {
|
|
353
|
+
const kif = props['k-if'];
|
|
354
|
+
let condition = kif; // assume boolean by default
|
|
355
|
+
// Handle reactive k-if
|
|
356
|
+
if (isKTRef(kif)) {
|
|
357
|
+
kif.addOnChange((newValue, oldValue) => {
|
|
358
|
+
if (newValue === oldValue) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const oldEl = el;
|
|
362
|
+
el = newValue ? create(tag, props) : placeholder();
|
|
363
|
+
$replaceWith.call(oldEl, el);
|
|
364
|
+
maybeDummyRef.value = el;
|
|
365
|
+
});
|
|
366
|
+
condition = kif.value;
|
|
367
|
+
}
|
|
368
|
+
if (!condition) {
|
|
369
|
+
// & make comment placeholder in case that ref might be redrawn later
|
|
370
|
+
el = placeholder();
|
|
371
|
+
maybeDummyRef.value = el;
|
|
372
|
+
return el;
|
|
373
|
+
}
|
|
328
374
|
}
|
|
329
|
-
|
|
375
|
+
el = create(tag, props);
|
|
376
|
+
maybeDummyRef.value = el;
|
|
330
377
|
return el;
|
|
331
378
|
}
|
|
332
379
|
/**
|
|
@@ -618,4 +665,4 @@ function getSequence(arr) {
|
|
|
618
665
|
return result;
|
|
619
666
|
}
|
|
620
667
|
|
|
621
|
-
export { Fragment, KTAsync, KTFor, KTRef, h as createElement, createRedrawable, h, jsx, jsxDEV, jsxs, ref };
|
|
668
|
+
export { Fragment, KTAsync, KTFor, KTRef, h as createElement, createRedrawable, h, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
package/dist/jsx/index.d.ts
CHANGED
|
@@ -13,8 +13,6 @@ declare class KTRef<T> {
|
|
|
13
13
|
* Indicates that this is a KTRef instance
|
|
14
14
|
*/
|
|
15
15
|
isKT: boolean;
|
|
16
|
-
private _value;
|
|
17
|
-
private _onChanges;
|
|
18
16
|
constructor(_value: T, _onChanges: Array<RefChangeHandler<T>>);
|
|
19
17
|
/**
|
|
20
18
|
* If new value and old value are both nodes, the old one will be replaced in the DOM
|
|
@@ -24,12 +22,35 @@ declare class KTRef<T> {
|
|
|
24
22
|
addOnChange(callback: RefChangeHandler<T>): void;
|
|
25
23
|
removeOnChange(callback: RefChangeHandler<T>): boolean;
|
|
26
24
|
}
|
|
25
|
+
declare const isKTRef: <T = any>(obj: any) => obj is KTRef<T>;
|
|
27
26
|
/**
|
|
28
27
|
* Reference to the created HTML element.
|
|
28
|
+
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
29
29
|
* - can alse be used to store normal values, but it is not reactive.
|
|
30
30
|
* @param value mostly an HTMLElement
|
|
31
31
|
*/
|
|
32
32
|
declare function ref<T = JSX.Element>(value?: T, onChange?: RefChangeHandler<T>): KTRef<T>;
|
|
33
|
+
type KTSurfaceRef<T extends Object> = {
|
|
34
|
+
[K in keyof T]: KTRef<T[K]>;
|
|
35
|
+
} & {
|
|
36
|
+
/**
|
|
37
|
+
* Get the dereferenced object like the original one
|
|
38
|
+
*/
|
|
39
|
+
kcollect: () => T;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Make all first-level properties of the object a `KTRef`.
|
|
43
|
+
* - `obj.a.b` is not reactive
|
|
44
|
+
*/
|
|
45
|
+
declare const surfaceRef: <T extends Object>(obj: T) => KTSurfaceRef<T>;
|
|
46
|
+
|
|
47
|
+
type HTML<T extends (HTMLTag | SVGTag | MathMLTag) & otherstring> = T extends SVGTag
|
|
48
|
+
? SVGElementTagNameMap[T]
|
|
49
|
+
: T extends HTMLTag
|
|
50
|
+
? HTMLElementTagNameMap[T]
|
|
51
|
+
: T extends MathMLTag
|
|
52
|
+
? MathMLElementTagNameMap[T]
|
|
53
|
+
: HTMLElement;
|
|
33
54
|
|
|
34
55
|
type SingleContent = KTRef<any> | HTMLElement | Element | Node | string | number | boolean | null | undefined;
|
|
35
56
|
type KTAvailableContent = SingleContent | KTAvailableContent[];
|
|
@@ -44,7 +65,14 @@ interface KTBaseAttribute {
|
|
|
44
65
|
|
|
45
66
|
// # kt-specific attributes
|
|
46
67
|
ref?: KTRef<JSX.Element>;
|
|
68
|
+
|
|
69
|
+
// todo 是否要让k-if是KTRef的时候具备响应能力?
|
|
70
|
+
/**
|
|
71
|
+
* If a `KTRef` is bound, it will be reactive; otherwise, it will be static.
|
|
72
|
+
*/
|
|
47
73
|
'k-if'?: any;
|
|
74
|
+
// todo k-model如何指定value还是checked还是别的什么?
|
|
75
|
+
'k-model'?: KTRef<any>;
|
|
48
76
|
|
|
49
77
|
// # normal HTML attributes
|
|
50
78
|
id?: string;
|
|
@@ -105,7 +133,6 @@ type KTPrefixedEventHandlers = {
|
|
|
105
133
|
|
|
106
134
|
type KTAttribute = KTBaseAttribute & KTPrefixedEventHandlers;
|
|
107
135
|
|
|
108
|
-
type HTML<T extends (HTMLTag | SVGTag | MathMLTag) & otherstring> = T extends SVGTag ? SVGElementTagNameMap[T] : T extends HTMLTag ? HTMLElementTagNameMap[T] : T extends MathMLTag ? MathMLElementTagNameMap[T] : HTMLElement;
|
|
109
136
|
/**
|
|
110
137
|
* Create an enhanced HTMLElement.
|
|
111
138
|
* - Only supports HTMLElements, **NOT** SVGElements or other Elements.
|
|
@@ -116,7 +143,7 @@ type HTML<T extends (HTMLTag | SVGTag | MathMLTag) & otherstring> = T extends SV
|
|
|
116
143
|
* ## About
|
|
117
144
|
* @package @ktjs/core
|
|
118
145
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
119
|
-
* @version 0.
|
|
146
|
+
* @version 0.21.1 (Last Update: 2026.02.02 09:20:07.959)
|
|
120
147
|
* @license MIT
|
|
121
148
|
* @link https://github.com/baendlorel/kt.js
|
|
122
149
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -1395,4 +1422,5 @@ declare global {
|
|
|
1395
1422
|
}
|
|
1396
1423
|
}
|
|
1397
1424
|
|
|
1398
|
-
export { Fragment, KTRef, h as createElement, createRedrawable, jsx, jsxDEV, jsxs, ref };
|
|
1425
|
+
export { Fragment, KTRef, h as createElement, createRedrawable, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
1426
|
+
export type { KTSurfaceRef };
|