@ktjs/core 0.27.2 → 0.28.2
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.d.ts +80 -16
- package/dist/index.iife.js +129 -79
- package/dist/index.legacy.js +131 -80
- package/dist/index.mjs +129 -79
- package/dist/jsx/index.d.ts +61 -34
- package/dist/jsx/index.mjs +78 -64
- package/dist/jsx/jsx-runtime.d.ts +52 -2
- package/dist/jsx/jsx-runtime.mjs +78 -64
- package/package.json +2 -2
package/dist/index.legacy.js
CHANGED
|
@@ -14,29 +14,6 @@ var __ktjs_core__ = (function (exports) {
|
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
// String manipulation utilities
|
|
18
|
-
/**
|
|
19
|
-
* Default empty function
|
|
20
|
-
*/
|
|
21
|
-
const $emptyFn = (() => true);
|
|
22
|
-
const $emptyArray = [];
|
|
23
|
-
/**
|
|
24
|
-
* Safe and quick forEach implementation that works with array-like objects and handles sparse arrays.
|
|
25
|
-
*/
|
|
26
|
-
const $forEach = (array, callback) => {
|
|
27
|
-
const len = array.length;
|
|
28
|
-
for (let i = 0; i < len; i++) {
|
|
29
|
-
callback(array[i], i, array);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const $emptyChildrenRef = { value: $emptyArray };
|
|
34
|
-
// each instance shares the same empty array, but it will be replaced when used
|
|
35
|
-
Comment.prototype.kisFragmentAnchor = false;
|
|
36
|
-
Comment.prototype.kFragmentList = $emptyArray;
|
|
37
|
-
Comment.prototype.kredraw = $emptyFn;
|
|
38
|
-
Comment.prototype.kchildrenRef = $emptyChildrenRef;
|
|
39
|
-
|
|
40
17
|
// Shared constants
|
|
41
18
|
// Empty for now - can be extended with framework-wide constants
|
|
42
19
|
/**
|
|
@@ -107,8 +84,23 @@ var __ktjs_core__ = (function (exports) {
|
|
|
107
84
|
element.addEventListener(eventName, () => (valueRef.value = element[propName]));
|
|
108
85
|
};
|
|
109
86
|
|
|
87
|
+
// String manipulation utilities
|
|
88
|
+
/**
|
|
89
|
+
* Default empty function
|
|
90
|
+
*/
|
|
91
|
+
const $emptyFn = (() => true);
|
|
92
|
+
/**
|
|
93
|
+
* Safe and quick forEach implementation that works with array-like objects and handles sparse arrays.
|
|
94
|
+
*/
|
|
95
|
+
const $forEach = (array, callback) => {
|
|
96
|
+
const len = array.length;
|
|
97
|
+
for (let i = 0; i < len; i++) {
|
|
98
|
+
callback(array[i], i, array);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
110
102
|
// incase that symbol is not supported
|
|
111
|
-
Object.defineProperty(window, '__ktjs__', { value: '0.23.
|
|
103
|
+
Object.defineProperty(window, '__ktjs__', { value: '0.23.11' });
|
|
112
104
|
|
|
113
105
|
var isKT = function (obj) { return obj === null || obj === void 0 ? void 0 : obj.isKT; };
|
|
114
106
|
var isRef = function (obj) { return (obj === null || obj === void 0 ? void 0 : obj.ktType) === 1 /* KTReactiveType.REF */; };
|
|
@@ -323,6 +315,14 @@ var __ktjs_core__ = (function (exports) {
|
|
|
323
315
|
this._value = _value;
|
|
324
316
|
this._onChanges = _onChanges;
|
|
325
317
|
}
|
|
318
|
+
/**
|
|
319
|
+
* @internal
|
|
320
|
+
*/
|
|
321
|
+
KTRef.prototype._emit = function (newValue, oldValue) {
|
|
322
|
+
for (var i = 0; i < this._onChanges.length; i++) {
|
|
323
|
+
this._onChanges[i](newValue, oldValue);
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
326
|
Object.defineProperty(KTRef.prototype, "value", {
|
|
327
327
|
/**
|
|
328
328
|
* If new value and old value are both nodes, the old one will be replaced in the DOM
|
|
@@ -337,13 +337,34 @@ var __ktjs_core__ = (function (exports) {
|
|
|
337
337
|
var oldValue = this._value;
|
|
338
338
|
$replaceNode(oldValue, newValue);
|
|
339
339
|
this._value = newValue;
|
|
340
|
-
|
|
341
|
-
this._onChanges[i](newValue, oldValue);
|
|
342
|
-
}
|
|
340
|
+
this._emit(newValue, oldValue);
|
|
343
341
|
},
|
|
344
342
|
enumerable: false,
|
|
345
343
|
configurable: true
|
|
346
344
|
});
|
|
345
|
+
/**
|
|
346
|
+
* Force all listeners to run even when reference identity has not changed.
|
|
347
|
+
* Useful for in-place array/object mutations.
|
|
348
|
+
*/
|
|
349
|
+
KTRef.prototype.notify = function () {
|
|
350
|
+
this._emit(this._value, this._value);
|
|
351
|
+
};
|
|
352
|
+
/**
|
|
353
|
+
* Mutate current value in-place and notify listeners once.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* const items = ref<number[]>([1, 2]);
|
|
357
|
+
* items.mutate((list) => list.push(3));
|
|
358
|
+
*/
|
|
359
|
+
KTRef.prototype.mutate = function (mutator) {
|
|
360
|
+
if (typeof mutator !== 'function') {
|
|
361
|
+
throw new Error('[kt.js error] KTRef.mutate: mutator must be a function');
|
|
362
|
+
}
|
|
363
|
+
var oldValue = this._value;
|
|
364
|
+
var result = mutator(this._value);
|
|
365
|
+
this._emit(this._value, oldValue);
|
|
366
|
+
return result;
|
|
367
|
+
};
|
|
347
368
|
/**
|
|
348
369
|
* Register a callback when the value changes
|
|
349
370
|
* @param callback (newValue, oldValue) => xxx
|
|
@@ -393,9 +414,6 @@ var __ktjs_core__ = (function (exports) {
|
|
|
393
414
|
return ref(o);
|
|
394
415
|
}
|
|
395
416
|
};
|
|
396
|
-
function deref(value) {
|
|
397
|
-
return isKT(value) ? value.value : value;
|
|
398
|
-
}
|
|
399
417
|
function kcollect() {
|
|
400
418
|
var newObj = {};
|
|
401
419
|
var entries = $entries(this);
|
|
@@ -464,6 +482,31 @@ var __ktjs_core__ = (function (exports) {
|
|
|
464
482
|
this._value = _calculator();
|
|
465
483
|
this._subscribe(reactives);
|
|
466
484
|
}
|
|
485
|
+
/**
|
|
486
|
+
* @internal
|
|
487
|
+
*/
|
|
488
|
+
KTComputed.prototype._emit = function (newValue, oldValue) {
|
|
489
|
+
for (var i = 0; i < this._onChanges.length; i++) {
|
|
490
|
+
this._onChanges[i](newValue, oldValue);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
/**
|
|
494
|
+
* @internal
|
|
495
|
+
*/
|
|
496
|
+
KTComputed.prototype._recalculate = function (forceEmit) {
|
|
497
|
+
if (forceEmit === void 0) { forceEmit = false; }
|
|
498
|
+
var oldValue = this._value;
|
|
499
|
+
var newValue = this._calculator();
|
|
500
|
+
if (oldValue === newValue) {
|
|
501
|
+
if (forceEmit) {
|
|
502
|
+
this._emit(newValue, oldValue);
|
|
503
|
+
}
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
this._value = newValue;
|
|
507
|
+
$replaceNode(oldValue, newValue);
|
|
508
|
+
this._emit(newValue, oldValue);
|
|
509
|
+
};
|
|
467
510
|
/**
|
|
468
511
|
* @internal
|
|
469
512
|
*/
|
|
@@ -471,17 +514,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
471
514
|
var _this = this;
|
|
472
515
|
for (var i = 0; i < reactives.length; i++) {
|
|
473
516
|
var reactive = reactives[i];
|
|
474
|
-
reactive.addOnChange(function () {
|
|
475
|
-
var oldValue = _this._value;
|
|
476
|
-
_this._value = _this._calculator();
|
|
477
|
-
if (oldValue === _this._value) {
|
|
478
|
-
return;
|
|
479
|
-
}
|
|
480
|
-
$replaceNode(oldValue, _this._value);
|
|
481
|
-
for (var i_1 = 0; i_1 < _this._onChanges.length; i_1++) {
|
|
482
|
-
_this._onChanges[i_1](_this._value, oldValue);
|
|
483
|
-
}
|
|
484
|
-
});
|
|
517
|
+
reactive.addOnChange(function () { return _this._recalculate(); });
|
|
485
518
|
}
|
|
486
519
|
};
|
|
487
520
|
Object.defineProperty(KTComputed.prototype, "value", {
|
|
@@ -497,6 +530,19 @@ var __ktjs_core__ = (function (exports) {
|
|
|
497
530
|
enumerable: false,
|
|
498
531
|
configurable: true
|
|
499
532
|
});
|
|
533
|
+
/**
|
|
534
|
+
* Force listeners to run once with the latest computed result.
|
|
535
|
+
*/
|
|
536
|
+
KTComputed.prototype.notify = function () {
|
|
537
|
+
this._recalculate(true);
|
|
538
|
+
};
|
|
539
|
+
/**
|
|
540
|
+
* Computed values are derived from dependencies and should not be mutated manually.
|
|
541
|
+
*/
|
|
542
|
+
KTComputed.prototype.mutate = function () {
|
|
543
|
+
console.warn('[kt.js warn]','KTComputed.mutate: computed is derived automatically; manual mutate is ignored. Use notify() instead');
|
|
544
|
+
return this._value;
|
|
545
|
+
};
|
|
500
546
|
/**
|
|
501
547
|
* Register a callback when the value changes
|
|
502
548
|
* @param callback (newValue, oldValue) => xxx
|
|
@@ -590,6 +636,12 @@ var __ktjs_core__ = (function (exports) {
|
|
|
590
636
|
return ref(value, onChange);
|
|
591
637
|
}
|
|
592
638
|
};
|
|
639
|
+
/**
|
|
640
|
+
* Extracts the value from a KTReactive, or returns the value directly if it's not reactive.
|
|
641
|
+
*/
|
|
642
|
+
function dereactive(value) {
|
|
643
|
+
return isKT(value) ? value.value : value;
|
|
644
|
+
}
|
|
593
645
|
|
|
594
646
|
function applyKModel(element, valueRef) {
|
|
595
647
|
if (!isKT(valueRef)) {
|
|
@@ -629,7 +681,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
629
681
|
* ## About
|
|
630
682
|
* @package @ktjs/core
|
|
631
683
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
632
|
-
* @version 0.
|
|
684
|
+
* @version 0.28.2 (Last Update: 2026.02.10 14:46:21.243)
|
|
633
685
|
* @license MIT
|
|
634
686
|
* @link https://github.com/baendlorel/kt.js
|
|
635
687
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -664,34 +716,6 @@ var __ktjs_core__ = (function (exports) {
|
|
|
664
716
|
return element;
|
|
665
717
|
};
|
|
666
718
|
|
|
667
|
-
var kredraw = function () {
|
|
668
|
-
var newElements = this.kchildrenRef.value;
|
|
669
|
-
var parent = this.parentNode;
|
|
670
|
-
if (!parent) {
|
|
671
|
-
// If anchor is not in DOM, only update internal state
|
|
672
|
-
this.kFragmentList.length = 0;
|
|
673
|
-
for (var i = 0; i < newElements.length; i++) {
|
|
674
|
-
this.kFragmentList.push(newElements[i]);
|
|
675
|
-
}
|
|
676
|
-
return;
|
|
677
|
-
}
|
|
678
|
-
// Simple replacement algorithm: remove all old elements, insert all new elements
|
|
679
|
-
// todo Future enhancement: key-based optimization
|
|
680
|
-
// 1. Remove all old elements
|
|
681
|
-
for (var i = 0; i < this.kFragmentList.length; i++) {
|
|
682
|
-
this.kFragmentList[i].remove();
|
|
683
|
-
}
|
|
684
|
-
// 2. Insert all new elements
|
|
685
|
-
var fragment = document.createDocumentFragment();
|
|
686
|
-
this.kFragmentList.length = 0;
|
|
687
|
-
for (var i = 0; i < newElements.length; i++) {
|
|
688
|
-
var element = newElements[i];
|
|
689
|
-
this.kFragmentList.push(element);
|
|
690
|
-
fragment.appendChild(element);
|
|
691
|
-
}
|
|
692
|
-
// Insert after anchor
|
|
693
|
-
parent.insertBefore(fragment, this.nextSibling);
|
|
694
|
-
};
|
|
695
719
|
/**
|
|
696
720
|
* Fragment - Container component for managing arrays of child elements
|
|
697
721
|
*
|
|
@@ -713,17 +737,44 @@ var __ktjs_core__ = (function (exports) {
|
|
|
713
737
|
*/
|
|
714
738
|
function Fragment$1(props) {
|
|
715
739
|
// key parameter reserved for future enhancement, currently unused
|
|
716
|
-
props
|
|
717
|
-
var
|
|
740
|
+
// const { key: _key } = props;
|
|
741
|
+
var redraw = function () {
|
|
742
|
+
var newElements = childrenRef.value;
|
|
743
|
+
var parent = anchor.parentNode;
|
|
744
|
+
if (!parent) {
|
|
745
|
+
// If anchor is not in DOM, only update internal state
|
|
746
|
+
elements.length = 0;
|
|
747
|
+
for (var i = 0; i < newElements.length; i++) {
|
|
748
|
+
elements.push(newElements[i]);
|
|
749
|
+
}
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
// Simple replacement algorithm: remove all old elements, insert all new elements
|
|
753
|
+
// todo Future enhancement: key-based optimization
|
|
754
|
+
// 1. Remove all old elements
|
|
755
|
+
for (var i = 0; i < elements.length; i++) {
|
|
756
|
+
elements[i].remove();
|
|
757
|
+
}
|
|
758
|
+
// 2. Insert all new elements
|
|
759
|
+
var fragment = document.createDocumentFragment();
|
|
760
|
+
elements.length = 0;
|
|
761
|
+
for (var i = 0; i < newElements.length; i++) {
|
|
762
|
+
var element = newElements[i];
|
|
763
|
+
elements.push(element);
|
|
764
|
+
fragment.appendChild(element);
|
|
765
|
+
}
|
|
766
|
+
// Insert after anchor
|
|
767
|
+
parent.insertBefore(fragment, anchor.nextSibling);
|
|
768
|
+
};
|
|
769
|
+
var initialized = false;
|
|
770
|
+
var childrenRef = toReactive(props.children, redraw);
|
|
771
|
+
var elements = [];
|
|
718
772
|
var anchor = document.createComment('kt-fragment');
|
|
719
|
-
anchor.kredraw = kredraw;
|
|
720
|
-
anchor.kchildrenRef = childrenRef;
|
|
721
|
-
anchor.kFragmentList = [];
|
|
722
|
-
anchor.kisFragmentAnchor = true;
|
|
723
773
|
// Observe DOM insertion
|
|
724
774
|
var observer = new MutationObserver(function () {
|
|
725
|
-
if (anchor.isConnected) {
|
|
726
|
-
|
|
775
|
+
if (anchor.isConnected && !initialized) {
|
|
776
|
+
initialized = true;
|
|
777
|
+
redraw();
|
|
727
778
|
observer.disconnect();
|
|
728
779
|
}
|
|
729
780
|
});
|
|
@@ -738,7 +789,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
738
789
|
function convertChildrenToElements(children) {
|
|
739
790
|
var elements = [];
|
|
740
791
|
var processChild = function (child) {
|
|
741
|
-
if (child
|
|
792
|
+
if (child === undefined || child === null || child === false || child === true) {
|
|
742
793
|
// Ignore null, undefined, false, true
|
|
743
794
|
return;
|
|
744
795
|
}
|
|
@@ -1083,7 +1134,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
1083
1134
|
exports.computed = computed;
|
|
1084
1135
|
exports.createElement = h;
|
|
1085
1136
|
exports.createRedrawable = createRedrawable;
|
|
1086
|
-
exports.
|
|
1137
|
+
exports.dereactive = dereactive;
|
|
1087
1138
|
exports.effect = effect;
|
|
1088
1139
|
exports.h = h;
|
|
1089
1140
|
exports.isComputed = isComputed;
|
package/dist/index.mjs
CHANGED
|
@@ -11,29 +11,6 @@ if (typeof Symbol === 'undefined') {
|
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
// String manipulation utilities
|
|
15
|
-
/**
|
|
16
|
-
* Default empty function
|
|
17
|
-
*/
|
|
18
|
-
const $emptyFn = (() => true);
|
|
19
|
-
const $emptyArray = [];
|
|
20
|
-
/**
|
|
21
|
-
* Safe and quick forEach implementation that works with array-like objects and handles sparse arrays.
|
|
22
|
-
*/
|
|
23
|
-
const $forEach = (array, callback) => {
|
|
24
|
-
const len = array.length;
|
|
25
|
-
for (let i = 0; i < len; i++) {
|
|
26
|
-
callback(array[i], i, array);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const $emptyChildrenRef = { value: $emptyArray };
|
|
31
|
-
// each instance shares the same empty array, but it will be replaced when used
|
|
32
|
-
Comment.prototype.kisFragmentAnchor = false;
|
|
33
|
-
Comment.prototype.kFragmentList = $emptyArray;
|
|
34
|
-
Comment.prototype.kredraw = $emptyFn;
|
|
35
|
-
Comment.prototype.kchildrenRef = $emptyChildrenRef;
|
|
36
|
-
|
|
37
14
|
// Shared constants
|
|
38
15
|
// Empty for now - can be extended with framework-wide constants
|
|
39
16
|
/**
|
|
@@ -104,8 +81,23 @@ const $applyModel = (element, valueRef, propName, eventName) => {
|
|
|
104
81
|
element.addEventListener(eventName, () => (valueRef.value = element[propName]));
|
|
105
82
|
};
|
|
106
83
|
|
|
84
|
+
// String manipulation utilities
|
|
85
|
+
/**
|
|
86
|
+
* Default empty function
|
|
87
|
+
*/
|
|
88
|
+
const $emptyFn = (() => true);
|
|
89
|
+
/**
|
|
90
|
+
* Safe and quick forEach implementation that works with array-like objects and handles sparse arrays.
|
|
91
|
+
*/
|
|
92
|
+
const $forEach = (array, callback) => {
|
|
93
|
+
const len = array.length;
|
|
94
|
+
for (let i = 0; i < len; i++) {
|
|
95
|
+
callback(array[i], i, array);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
107
99
|
// incase that symbol is not supported
|
|
108
|
-
Object.defineProperty(window, '__ktjs__', { value: '0.23.
|
|
100
|
+
Object.defineProperty(window, '__ktjs__', { value: '0.23.11' });
|
|
109
101
|
|
|
110
102
|
const isKT = (obj) => obj?.isKT;
|
|
111
103
|
const isRef = (obj) => obj?.ktType === 1 /* KTReactiveType.REF */;
|
|
@@ -316,6 +308,14 @@ class KTRef {
|
|
|
316
308
|
* @internal
|
|
317
309
|
*/
|
|
318
310
|
_onChanges;
|
|
311
|
+
/**
|
|
312
|
+
* @internal
|
|
313
|
+
*/
|
|
314
|
+
_emit(newValue, oldValue) {
|
|
315
|
+
for (let i = 0; i < this._onChanges.length; i++) {
|
|
316
|
+
this._onChanges[i](newValue, oldValue);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
319
|
constructor(_value, _onChanges) {
|
|
320
320
|
this._value = _value;
|
|
321
321
|
this._onChanges = _onChanges;
|
|
@@ -333,9 +333,30 @@ class KTRef {
|
|
|
333
333
|
const oldValue = this._value;
|
|
334
334
|
$replaceNode(oldValue, newValue);
|
|
335
335
|
this._value = newValue;
|
|
336
|
-
|
|
337
|
-
|
|
336
|
+
this._emit(newValue, oldValue);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Force all listeners to run even when reference identity has not changed.
|
|
340
|
+
* Useful for in-place array/object mutations.
|
|
341
|
+
*/
|
|
342
|
+
notify() {
|
|
343
|
+
this._emit(this._value, this._value);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Mutate current value in-place and notify listeners once.
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* const items = ref<number[]>([1, 2]);
|
|
350
|
+
* items.mutate((list) => list.push(3));
|
|
351
|
+
*/
|
|
352
|
+
mutate(mutator) {
|
|
353
|
+
if (typeof mutator !== 'function') {
|
|
354
|
+
throw new Error('[kt.js error] KTRef.mutate: mutator must be a function');
|
|
338
355
|
}
|
|
356
|
+
const oldValue = this._value;
|
|
357
|
+
const result = mutator(this._value);
|
|
358
|
+
this._emit(this._value, oldValue);
|
|
359
|
+
return result;
|
|
339
360
|
}
|
|
340
361
|
/**
|
|
341
362
|
* Register a callback when the value changes
|
|
@@ -385,9 +406,6 @@ const toRef = (o) => {
|
|
|
385
406
|
return ref(o);
|
|
386
407
|
}
|
|
387
408
|
};
|
|
388
|
-
function deref(value) {
|
|
389
|
-
return isKT(value) ? value.value : value;
|
|
390
|
-
}
|
|
391
409
|
function kcollect() {
|
|
392
410
|
const newObj = {};
|
|
393
411
|
const entries = $entries(this);
|
|
@@ -459,23 +477,37 @@ class KTComputed {
|
|
|
459
477
|
* @internal
|
|
460
478
|
*/
|
|
461
479
|
_onChanges = [];
|
|
480
|
+
/**
|
|
481
|
+
* @internal
|
|
482
|
+
*/
|
|
483
|
+
_emit(newValue, oldValue) {
|
|
484
|
+
for (let i = 0; i < this._onChanges.length; i++) {
|
|
485
|
+
this._onChanges[i](newValue, oldValue);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* @internal
|
|
490
|
+
*/
|
|
491
|
+
_recalculate(forceEmit = false) {
|
|
492
|
+
const oldValue = this._value;
|
|
493
|
+
const newValue = this._calculator();
|
|
494
|
+
if (oldValue === newValue) {
|
|
495
|
+
if (forceEmit) {
|
|
496
|
+
this._emit(newValue, oldValue);
|
|
497
|
+
}
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
this._value = newValue;
|
|
501
|
+
$replaceNode(oldValue, newValue);
|
|
502
|
+
this._emit(newValue, oldValue);
|
|
503
|
+
}
|
|
462
504
|
/**
|
|
463
505
|
* @internal
|
|
464
506
|
*/
|
|
465
507
|
_subscribe(reactives) {
|
|
466
508
|
for (let i = 0; i < reactives.length; i++) {
|
|
467
509
|
const reactive = reactives[i];
|
|
468
|
-
reactive.addOnChange(() =>
|
|
469
|
-
const oldValue = this._value;
|
|
470
|
-
this._value = this._calculator();
|
|
471
|
-
if (oldValue === this._value) {
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
$replaceNode(oldValue, this._value);
|
|
475
|
-
for (let i = 0; i < this._onChanges.length; i++) {
|
|
476
|
-
this._onChanges[i](this._value, oldValue);
|
|
477
|
-
}
|
|
478
|
-
});
|
|
510
|
+
reactive.addOnChange(() => this._recalculate());
|
|
479
511
|
}
|
|
480
512
|
}
|
|
481
513
|
constructor(_calculator, reactives) {
|
|
@@ -492,6 +524,19 @@ class KTComputed {
|
|
|
492
524
|
set value(_newValue) {
|
|
493
525
|
throw new Error('[kt.js error] KTComputed: cannot set value of a computed value');
|
|
494
526
|
}
|
|
527
|
+
/**
|
|
528
|
+
* Force listeners to run once with the latest computed result.
|
|
529
|
+
*/
|
|
530
|
+
notify() {
|
|
531
|
+
this._recalculate(true);
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Computed values are derived from dependencies and should not be mutated manually.
|
|
535
|
+
*/
|
|
536
|
+
mutate() {
|
|
537
|
+
console.warn('[kt.js warn]','KTComputed.mutate: computed is derived automatically; manual mutate is ignored. Use notify() instead');
|
|
538
|
+
return this._value;
|
|
539
|
+
}
|
|
495
540
|
/**
|
|
496
541
|
* Register a callback when the value changes
|
|
497
542
|
* @param callback (newValue, oldValue) => xxx
|
|
@@ -584,6 +629,12 @@ const toReactive = (value, onChange) => {
|
|
|
584
629
|
return ref(value, onChange);
|
|
585
630
|
}
|
|
586
631
|
};
|
|
632
|
+
/**
|
|
633
|
+
* Extracts the value from a KTReactive, or returns the value directly if it's not reactive.
|
|
634
|
+
*/
|
|
635
|
+
function dereactive(value) {
|
|
636
|
+
return isKT(value) ? value.value : value;
|
|
637
|
+
}
|
|
587
638
|
|
|
588
639
|
function applyKModel(element, valueRef) {
|
|
589
640
|
if (!isKT(valueRef)) {
|
|
@@ -623,7 +674,7 @@ let creator = htmlCreator;
|
|
|
623
674
|
* ## About
|
|
624
675
|
* @package @ktjs/core
|
|
625
676
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
626
|
-
* @version 0.
|
|
677
|
+
* @version 0.28.2 (Last Update: 2026.02.10 14:46:21.243)
|
|
627
678
|
* @license MIT
|
|
628
679
|
* @link https://github.com/baendlorel/kt.js
|
|
629
680
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -658,34 +709,6 @@ const h = (tag, attr, content) => {
|
|
|
658
709
|
return element;
|
|
659
710
|
};
|
|
660
711
|
|
|
661
|
-
const kredraw = function () {
|
|
662
|
-
const newElements = this.kchildrenRef.value;
|
|
663
|
-
const parent = this.parentNode;
|
|
664
|
-
if (!parent) {
|
|
665
|
-
// If anchor is not in DOM, only update internal state
|
|
666
|
-
this.kFragmentList.length = 0;
|
|
667
|
-
for (let i = 0; i < newElements.length; i++) {
|
|
668
|
-
this.kFragmentList.push(newElements[i]);
|
|
669
|
-
}
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
// Simple replacement algorithm: remove all old elements, insert all new elements
|
|
673
|
-
// todo Future enhancement: key-based optimization
|
|
674
|
-
// 1. Remove all old elements
|
|
675
|
-
for (let i = 0; i < this.kFragmentList.length; i++) {
|
|
676
|
-
this.kFragmentList[i].remove();
|
|
677
|
-
}
|
|
678
|
-
// 2. Insert all new elements
|
|
679
|
-
const fragment = document.createDocumentFragment();
|
|
680
|
-
this.kFragmentList.length = 0;
|
|
681
|
-
for (let i = 0; i < newElements.length; i++) {
|
|
682
|
-
const element = newElements[i];
|
|
683
|
-
this.kFragmentList.push(element);
|
|
684
|
-
fragment.appendChild(element);
|
|
685
|
-
}
|
|
686
|
-
// Insert after anchor
|
|
687
|
-
parent.insertBefore(fragment, this.nextSibling);
|
|
688
|
-
};
|
|
689
712
|
/**
|
|
690
713
|
* Fragment - Container component for managing arrays of child elements
|
|
691
714
|
*
|
|
@@ -707,17 +730,44 @@ const kredraw = function () {
|
|
|
707
730
|
*/
|
|
708
731
|
function Fragment$1(props) {
|
|
709
732
|
// key parameter reserved for future enhancement, currently unused
|
|
710
|
-
const { key: _key } = props;
|
|
711
|
-
const
|
|
733
|
+
// const { key: _key } = props;
|
|
734
|
+
const redraw = () => {
|
|
735
|
+
const newElements = childrenRef.value;
|
|
736
|
+
const parent = anchor.parentNode;
|
|
737
|
+
if (!parent) {
|
|
738
|
+
// If anchor is not in DOM, only update internal state
|
|
739
|
+
elements.length = 0;
|
|
740
|
+
for (let i = 0; i < newElements.length; i++) {
|
|
741
|
+
elements.push(newElements[i]);
|
|
742
|
+
}
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
// Simple replacement algorithm: remove all old elements, insert all new elements
|
|
746
|
+
// todo Future enhancement: key-based optimization
|
|
747
|
+
// 1. Remove all old elements
|
|
748
|
+
for (let i = 0; i < elements.length; i++) {
|
|
749
|
+
elements[i].remove();
|
|
750
|
+
}
|
|
751
|
+
// 2. Insert all new elements
|
|
752
|
+
const fragment = document.createDocumentFragment();
|
|
753
|
+
elements.length = 0;
|
|
754
|
+
for (let i = 0; i < newElements.length; i++) {
|
|
755
|
+
const element = newElements[i];
|
|
756
|
+
elements.push(element);
|
|
757
|
+
fragment.appendChild(element);
|
|
758
|
+
}
|
|
759
|
+
// Insert after anchor
|
|
760
|
+
parent.insertBefore(fragment, anchor.nextSibling);
|
|
761
|
+
};
|
|
762
|
+
let initialized = false;
|
|
763
|
+
const childrenRef = toReactive(props.children, redraw);
|
|
764
|
+
const elements = [];
|
|
712
765
|
const anchor = document.createComment('kt-fragment');
|
|
713
|
-
anchor.kredraw = kredraw;
|
|
714
|
-
anchor.kchildrenRef = childrenRef;
|
|
715
|
-
anchor.kFragmentList = [];
|
|
716
|
-
anchor.kisFragmentAnchor = true;
|
|
717
766
|
// Observe DOM insertion
|
|
718
767
|
const observer = new MutationObserver(() => {
|
|
719
|
-
if (anchor.isConnected) {
|
|
720
|
-
|
|
768
|
+
if (anchor.isConnected && !initialized) {
|
|
769
|
+
initialized = true;
|
|
770
|
+
redraw();
|
|
721
771
|
observer.disconnect();
|
|
722
772
|
}
|
|
723
773
|
});
|
|
@@ -732,7 +782,7 @@ function Fragment$1(props) {
|
|
|
732
782
|
function convertChildrenToElements(children) {
|
|
733
783
|
const elements = [];
|
|
734
784
|
const processChild = (child) => {
|
|
735
|
-
if (child
|
|
785
|
+
if (child === undefined || child === null || child === false || child === true) {
|
|
736
786
|
// Ignore null, undefined, false, true
|
|
737
787
|
return;
|
|
738
788
|
}
|
|
@@ -1062,4 +1112,4 @@ function getSequence(arr) {
|
|
|
1062
1112
|
return result;
|
|
1063
1113
|
}
|
|
1064
1114
|
|
|
1065
|
-
export { $modelOrRef, $setRef, Fragment, KTAsync, KTComputed, KTFor, KTRef, computed, h as createElement, createRedrawable,
|
|
1115
|
+
export { $modelOrRef, $setRef, Fragment, KTAsync, KTComputed, KTFor, KTRef, computed, h as createElement, createRedrawable, dereactive, effect, h, isComputed, isKT, isRef, jsx, jsxDEV, jsxs, ref, surfaceRef, toReactive, toRef };
|