@ktjs/core 0.22.2 → 0.22.5
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 +13 -3
- package/dist/index.iife.js +36 -4
- package/dist/index.legacy.js +36 -4
- package/dist/index.mjs +35 -5
- package/dist/jsx/index.d.ts +13 -3
- package/dist/jsx/index.mjs +35 -5
- package/dist/jsx/jsx-runtime.d.ts +5 -1
- package/dist/jsx/jsx-runtime.mjs +13 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,10 @@ declare class KTRef<T> {
|
|
|
21
21
|
*/
|
|
22
22
|
get value(): T;
|
|
23
23
|
set value(newValue: T);
|
|
24
|
+
/**
|
|
25
|
+
* Register a callback when the value changes
|
|
26
|
+
* @param callback (newValue, oldValue) => xxx
|
|
27
|
+
*/
|
|
24
28
|
addOnChange(callback: RefChangeHandler<T>): void;
|
|
25
29
|
removeOnChange(callback: RefChangeHandler<T>): boolean;
|
|
26
30
|
}
|
|
@@ -29,9 +33,11 @@ declare const isKTRef: <T = any>(obj: any) => obj is KTRef<T>;
|
|
|
29
33
|
* Reference to the created HTML element.
|
|
30
34
|
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
31
35
|
* - can alse be used to store normal values, but it is not reactive.
|
|
36
|
+
* - if the value is already a `KTRef`, it will be returned **directly**.
|
|
32
37
|
* @param value mostly an HTMLElement
|
|
33
38
|
*/
|
|
34
|
-
declare function ref<T = JSX.Element>(value?: T
|
|
39
|
+
declare function ref<T = JSX.Element>(value?: T | KTRef<T>, onChange?: RefChangeHandler<T>): KTRef<T>;
|
|
40
|
+
declare function deref<T = JSX.Element>(value: T | KTRef<T>): T;
|
|
35
41
|
type KTSurfaceRef<T extends Object> = {
|
|
36
42
|
[K in keyof T]: KTRef<T[K]>;
|
|
37
43
|
} & {
|
|
@@ -45,6 +51,10 @@ type KTSurfaceRef<T extends Object> = {
|
|
|
45
51
|
* - `obj.a.b` is not reactive
|
|
46
52
|
*/
|
|
47
53
|
declare const surfaceRef: <T extends Object>(obj: T) => KTSurfaceRef<T>;
|
|
54
|
+
/**
|
|
55
|
+
* Assert k-model to be a ref object
|
|
56
|
+
*/
|
|
57
|
+
declare const $modelOrRef: <T = any>(props: any, defaultValue?: T) => KTRef<T>;
|
|
48
58
|
|
|
49
59
|
type HTML<T extends (HTMLTag | SVGTag | MathMLTag) & otherstring> = T extends SVGTag
|
|
50
60
|
? SVGElementTagNameMap[T]
|
|
@@ -162,7 +172,7 @@ type KTComponent = (
|
|
|
162
172
|
* ## About
|
|
163
173
|
* @package @ktjs/core
|
|
164
174
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
165
|
-
* @version 0.22.
|
|
175
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
166
176
|
* @license MIT
|
|
167
177
|
* @link https://github.com/baendlorel/kt.js
|
|
168
178
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -1467,5 +1477,5 @@ interface KTForProps<T> {
|
|
|
1467
1477
|
*/
|
|
1468
1478
|
declare function KTFor<T>(props: KTForProps<T>): KTForElement;
|
|
1469
1479
|
|
|
1470
|
-
export { Fragment, KTAsync, KTFor, KTRef, h as createElement, createRedrawable, h, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
1480
|
+
export { $modelOrRef, Fragment, KTAsync, KTFor, KTRef, h as createElement, createRedrawable, deref, h, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
1471
1481
|
export type { EventHandler, HTMLTag, InputElementTag, KTAttribute, KTForElement, KTForProps, KTRawAttr, KTRawContent, KTRawContents, KTSurfaceRef, MathMLTag, SVGTag };
|
package/dist/index.iife.js
CHANGED
|
@@ -97,7 +97,14 @@ var __ktjs_core__ = (function (exports) {
|
|
|
97
97
|
this._onChanges[i](newValue, oldValue);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Register a callback when the value changes
|
|
102
|
+
* @param callback (newValue, oldValue) => xxx
|
|
103
|
+
*/
|
|
100
104
|
addOnChange(callback) {
|
|
105
|
+
if (typeof callback !== 'function') {
|
|
106
|
+
throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
|
|
107
|
+
}
|
|
101
108
|
this._onChanges.push(callback);
|
|
102
109
|
}
|
|
103
110
|
removeOnChange(callback) {
|
|
@@ -110,18 +117,26 @@ var __ktjs_core__ = (function (exports) {
|
|
|
110
117
|
return false;
|
|
111
118
|
}
|
|
112
119
|
}
|
|
113
|
-
const isKTRef = (obj) =>
|
|
114
|
-
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
115
|
-
};
|
|
120
|
+
const isKTRef = (obj) => obj?.isKT === true;
|
|
116
121
|
/**
|
|
117
122
|
* Reference to the created HTML element.
|
|
118
123
|
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
119
124
|
* - can alse be used to store normal values, but it is not reactive.
|
|
125
|
+
* - if the value is already a `KTRef`, it will be returned **directly**.
|
|
120
126
|
* @param value mostly an HTMLElement
|
|
121
127
|
*/
|
|
122
128
|
function ref(value, onChange) {
|
|
129
|
+
if (isKTRef(value)) {
|
|
130
|
+
if (onChange) {
|
|
131
|
+
value.addOnChange(onChange);
|
|
132
|
+
}
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
123
135
|
return new KTRef(value, onChange ? [onChange] : []);
|
|
124
136
|
}
|
|
137
|
+
function deref(value) {
|
|
138
|
+
return isKTRef(value) ? value.value : value;
|
|
139
|
+
}
|
|
125
140
|
function kcollect() {
|
|
126
141
|
const newObj = {};
|
|
127
142
|
const entries = $entries(this);
|
|
@@ -146,6 +161,21 @@ var __ktjs_core__ = (function (exports) {
|
|
|
146
161
|
}
|
|
147
162
|
return newObj;
|
|
148
163
|
};
|
|
164
|
+
// # asserts
|
|
165
|
+
/**
|
|
166
|
+
* Assert k-model to be a ref object
|
|
167
|
+
*/
|
|
168
|
+
const $modelOrRef = (props, defaultValue) => {
|
|
169
|
+
// & props is an object. Won't use it in any other place
|
|
170
|
+
if ('k-model' in props) {
|
|
171
|
+
const kmodel = props['k-model'];
|
|
172
|
+
if (!kmodel?.isKT) {
|
|
173
|
+
throw new Error(`[kt.js error] k-model data must be a KTRef object, please use 'ref(...)' to wrap it.`);
|
|
174
|
+
}
|
|
175
|
+
return kmodel;
|
|
176
|
+
}
|
|
177
|
+
return ref(defaultValue);
|
|
178
|
+
};
|
|
149
179
|
|
|
150
180
|
const booleanHandler = (element, key, value) => {
|
|
151
181
|
if (key in element) {
|
|
@@ -330,7 +360,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
330
360
|
* ## About
|
|
331
361
|
* @package @ktjs/core
|
|
332
362
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
333
|
-
* @version 0.22.
|
|
363
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
334
364
|
* @license MIT
|
|
335
365
|
* @link https://github.com/baendlorel/kt.js
|
|
336
366
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -704,12 +734,14 @@ var __ktjs_core__ = (function (exports) {
|
|
|
704
734
|
return result;
|
|
705
735
|
}
|
|
706
736
|
|
|
737
|
+
exports.$modelOrRef = $modelOrRef;
|
|
707
738
|
exports.Fragment = Fragment;
|
|
708
739
|
exports.KTAsync = KTAsync;
|
|
709
740
|
exports.KTFor = KTFor;
|
|
710
741
|
exports.KTRef = KTRef;
|
|
711
742
|
exports.createElement = h;
|
|
712
743
|
exports.createRedrawable = createRedrawable;
|
|
744
|
+
exports.deref = deref;
|
|
713
745
|
exports.h = h;
|
|
714
746
|
exports.isKTRef = isKTRef;
|
|
715
747
|
exports.jsx = jsx;
|
package/dist/index.legacy.js
CHANGED
|
@@ -93,7 +93,14 @@ var __ktjs_core__ = (function (exports) {
|
|
|
93
93
|
enumerable: false,
|
|
94
94
|
configurable: true
|
|
95
95
|
});
|
|
96
|
+
/**
|
|
97
|
+
* Register a callback when the value changes
|
|
98
|
+
* @param callback (newValue, oldValue) => xxx
|
|
99
|
+
*/
|
|
96
100
|
KTRef.prototype.addOnChange = function (callback) {
|
|
101
|
+
if (typeof callback !== 'function') {
|
|
102
|
+
throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
|
|
103
|
+
}
|
|
97
104
|
this._onChanges.push(callback);
|
|
98
105
|
};
|
|
99
106
|
KTRef.prototype.removeOnChange = function (callback) {
|
|
@@ -107,18 +114,26 @@ var __ktjs_core__ = (function (exports) {
|
|
|
107
114
|
};
|
|
108
115
|
return KTRef;
|
|
109
116
|
}());
|
|
110
|
-
var isKTRef = function (obj) {
|
|
111
|
-
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
112
|
-
};
|
|
117
|
+
var isKTRef = function (obj) { return (obj === null || obj === void 0 ? void 0 : obj.isKT) === true; };
|
|
113
118
|
/**
|
|
114
119
|
* Reference to the created HTML element.
|
|
115
120
|
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
116
121
|
* - can alse be used to store normal values, but it is not reactive.
|
|
122
|
+
* - if the value is already a `KTRef`, it will be returned **directly**.
|
|
117
123
|
* @param value mostly an HTMLElement
|
|
118
124
|
*/
|
|
119
125
|
function ref(value, onChange) {
|
|
126
|
+
if (isKTRef(value)) {
|
|
127
|
+
if (onChange) {
|
|
128
|
+
value.addOnChange(onChange);
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
120
132
|
return new KTRef(value, onChange ? [onChange] : []);
|
|
121
133
|
}
|
|
134
|
+
function deref(value) {
|
|
135
|
+
return isKTRef(value) ? value.value : value;
|
|
136
|
+
}
|
|
122
137
|
function kcollect() {
|
|
123
138
|
var newObj = {};
|
|
124
139
|
var entries = $entries(this);
|
|
@@ -143,6 +158,21 @@ var __ktjs_core__ = (function (exports) {
|
|
|
143
158
|
}
|
|
144
159
|
return newObj;
|
|
145
160
|
};
|
|
161
|
+
// # asserts
|
|
162
|
+
/**
|
|
163
|
+
* Assert k-model to be a ref object
|
|
164
|
+
*/
|
|
165
|
+
var $modelOrRef = function (props, defaultValue) {
|
|
166
|
+
// & props is an object. Won't use it in any other place
|
|
167
|
+
if ('k-model' in props) {
|
|
168
|
+
var kmodel = props['k-model'];
|
|
169
|
+
if (!(kmodel === null || kmodel === void 0 ? void 0 : kmodel.isKT)) {
|
|
170
|
+
throw new Error("[kt.js error] k-model data must be a KTRef object, please use 'ref(...)' to wrap it.");
|
|
171
|
+
}
|
|
172
|
+
return kmodel;
|
|
173
|
+
}
|
|
174
|
+
return ref(defaultValue);
|
|
175
|
+
};
|
|
146
176
|
|
|
147
177
|
var booleanHandler = function (element, key, value) {
|
|
148
178
|
if (key in element) {
|
|
@@ -332,7 +362,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
332
362
|
* ## About
|
|
333
363
|
* @package @ktjs/core
|
|
334
364
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
335
|
-
* @version 0.22.
|
|
365
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
336
366
|
* @license MIT
|
|
337
367
|
* @link https://github.com/baendlorel/kt.js
|
|
338
368
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -713,12 +743,14 @@ var __ktjs_core__ = (function (exports) {
|
|
|
713
743
|
return result;
|
|
714
744
|
}
|
|
715
745
|
|
|
746
|
+
exports.$modelOrRef = $modelOrRef;
|
|
716
747
|
exports.Fragment = Fragment;
|
|
717
748
|
exports.KTAsync = KTAsync;
|
|
718
749
|
exports.KTFor = KTFor;
|
|
719
750
|
exports.KTRef = KTRef;
|
|
720
751
|
exports.createElement = h;
|
|
721
752
|
exports.createRedrawable = createRedrawable;
|
|
753
|
+
exports.deref = deref;
|
|
722
754
|
exports.h = h;
|
|
723
755
|
exports.isKTRef = isKTRef;
|
|
724
756
|
exports.jsx = jsx;
|
package/dist/index.mjs
CHANGED
|
@@ -94,7 +94,14 @@ class KTRef {
|
|
|
94
94
|
this._onChanges[i](newValue, oldValue);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Register a callback when the value changes
|
|
99
|
+
* @param callback (newValue, oldValue) => xxx
|
|
100
|
+
*/
|
|
97
101
|
addOnChange(callback) {
|
|
102
|
+
if (typeof callback !== 'function') {
|
|
103
|
+
throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
|
|
104
|
+
}
|
|
98
105
|
this._onChanges.push(callback);
|
|
99
106
|
}
|
|
100
107
|
removeOnChange(callback) {
|
|
@@ -107,18 +114,26 @@ class KTRef {
|
|
|
107
114
|
return false;
|
|
108
115
|
}
|
|
109
116
|
}
|
|
110
|
-
const isKTRef = (obj) =>
|
|
111
|
-
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
112
|
-
};
|
|
117
|
+
const isKTRef = (obj) => obj?.isKT === true;
|
|
113
118
|
/**
|
|
114
119
|
* Reference to the created HTML element.
|
|
115
120
|
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
116
121
|
* - can alse be used to store normal values, but it is not reactive.
|
|
122
|
+
* - if the value is already a `KTRef`, it will be returned **directly**.
|
|
117
123
|
* @param value mostly an HTMLElement
|
|
118
124
|
*/
|
|
119
125
|
function ref(value, onChange) {
|
|
126
|
+
if (isKTRef(value)) {
|
|
127
|
+
if (onChange) {
|
|
128
|
+
value.addOnChange(onChange);
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
120
132
|
return new KTRef(value, onChange ? [onChange] : []);
|
|
121
133
|
}
|
|
134
|
+
function deref(value) {
|
|
135
|
+
return isKTRef(value) ? value.value : value;
|
|
136
|
+
}
|
|
122
137
|
function kcollect() {
|
|
123
138
|
const newObj = {};
|
|
124
139
|
const entries = $entries(this);
|
|
@@ -143,6 +158,21 @@ const surfaceRef = (obj) => {
|
|
|
143
158
|
}
|
|
144
159
|
return newObj;
|
|
145
160
|
};
|
|
161
|
+
// # asserts
|
|
162
|
+
/**
|
|
163
|
+
* Assert k-model to be a ref object
|
|
164
|
+
*/
|
|
165
|
+
const $modelOrRef = (props, defaultValue) => {
|
|
166
|
+
// & props is an object. Won't use it in any other place
|
|
167
|
+
if ('k-model' in props) {
|
|
168
|
+
const kmodel = props['k-model'];
|
|
169
|
+
if (!kmodel?.isKT) {
|
|
170
|
+
throw new Error(`[kt.js error] k-model data must be a KTRef object, please use 'ref(...)' to wrap it.`);
|
|
171
|
+
}
|
|
172
|
+
return kmodel;
|
|
173
|
+
}
|
|
174
|
+
return ref(defaultValue);
|
|
175
|
+
};
|
|
146
176
|
|
|
147
177
|
const booleanHandler = (element, key, value) => {
|
|
148
178
|
if (key in element) {
|
|
@@ -327,7 +357,7 @@ const MATHML_ATTR_FLAG = '__kt_mathml__';
|
|
|
327
357
|
* ## About
|
|
328
358
|
* @package @ktjs/core
|
|
329
359
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
330
|
-
* @version 0.22.
|
|
360
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
331
361
|
* @license MIT
|
|
332
362
|
* @link https://github.com/baendlorel/kt.js
|
|
333
363
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -701,4 +731,4 @@ function getSequence(arr) {
|
|
|
701
731
|
return result;
|
|
702
732
|
}
|
|
703
733
|
|
|
704
|
-
export { Fragment, KTAsync, KTFor, KTRef, h as createElement, createRedrawable, h, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
734
|
+
export { $modelOrRef, Fragment, KTAsync, KTFor, KTRef, h as createElement, createRedrawable, deref, h, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
package/dist/jsx/index.d.ts
CHANGED
|
@@ -19,6 +19,10 @@ declare class KTRef<T> {
|
|
|
19
19
|
*/
|
|
20
20
|
get value(): T;
|
|
21
21
|
set value(newValue: T);
|
|
22
|
+
/**
|
|
23
|
+
* Register a callback when the value changes
|
|
24
|
+
* @param callback (newValue, oldValue) => xxx
|
|
25
|
+
*/
|
|
22
26
|
addOnChange(callback: RefChangeHandler<T>): void;
|
|
23
27
|
removeOnChange(callback: RefChangeHandler<T>): boolean;
|
|
24
28
|
}
|
|
@@ -27,9 +31,11 @@ declare const isKTRef: <T = any>(obj: any) => obj is KTRef<T>;
|
|
|
27
31
|
* Reference to the created HTML element.
|
|
28
32
|
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
29
33
|
* - can alse be used to store normal values, but it is not reactive.
|
|
34
|
+
* - if the value is already a `KTRef`, it will be returned **directly**.
|
|
30
35
|
* @param value mostly an HTMLElement
|
|
31
36
|
*/
|
|
32
|
-
declare function ref<T = JSX.Element>(value?: T
|
|
37
|
+
declare function ref<T = JSX.Element>(value?: T | KTRef<T>, onChange?: RefChangeHandler<T>): KTRef<T>;
|
|
38
|
+
declare function deref<T = JSX.Element>(value: T | KTRef<T>): T;
|
|
33
39
|
type KTSurfaceRef<T extends Object> = {
|
|
34
40
|
[K in keyof T]: KTRef<T[K]>;
|
|
35
41
|
} & {
|
|
@@ -43,6 +49,10 @@ type KTSurfaceRef<T extends Object> = {
|
|
|
43
49
|
* - `obj.a.b` is not reactive
|
|
44
50
|
*/
|
|
45
51
|
declare const surfaceRef: <T extends Object>(obj: T) => KTSurfaceRef<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Assert k-model to be a ref object
|
|
54
|
+
*/
|
|
55
|
+
declare const $modelOrRef: <T = any>(props: any, defaultValue?: T) => KTRef<T>;
|
|
46
56
|
|
|
47
57
|
type HTML<T extends (HTMLTag | SVGTag | MathMLTag) & otherstring> = T extends SVGTag
|
|
48
58
|
? SVGElementTagNameMap[T]
|
|
@@ -146,7 +156,7 @@ type KTAttribute = KTBaseAttribute & KTPrefixedEventHandlers;
|
|
|
146
156
|
* ## About
|
|
147
157
|
* @package @ktjs/core
|
|
148
158
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
149
|
-
* @version 0.22.
|
|
159
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
150
160
|
* @license MIT
|
|
151
161
|
* @link https://github.com/baendlorel/kt.js
|
|
152
162
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -1425,5 +1435,5 @@ declare global {
|
|
|
1425
1435
|
}
|
|
1426
1436
|
}
|
|
1427
1437
|
|
|
1428
|
-
export { Fragment, KTRef, h as createElement, createRedrawable, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
1438
|
+
export { $modelOrRef, Fragment, KTRef, h as createElement, createRedrawable, deref, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
1429
1439
|
export type { KTSurfaceRef };
|
package/dist/jsx/index.mjs
CHANGED
|
@@ -94,7 +94,14 @@ class KTRef {
|
|
|
94
94
|
this._onChanges[i](newValue, oldValue);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Register a callback when the value changes
|
|
99
|
+
* @param callback (newValue, oldValue) => xxx
|
|
100
|
+
*/
|
|
97
101
|
addOnChange(callback) {
|
|
102
|
+
if (typeof callback !== 'function') {
|
|
103
|
+
throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
|
|
104
|
+
}
|
|
98
105
|
this._onChanges.push(callback);
|
|
99
106
|
}
|
|
100
107
|
removeOnChange(callback) {
|
|
@@ -107,18 +114,26 @@ class KTRef {
|
|
|
107
114
|
return false;
|
|
108
115
|
}
|
|
109
116
|
}
|
|
110
|
-
const isKTRef = (obj) =>
|
|
111
|
-
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
112
|
-
};
|
|
117
|
+
const isKTRef = (obj) => obj?.isKT === true;
|
|
113
118
|
/**
|
|
114
119
|
* Reference to the created HTML element.
|
|
115
120
|
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
116
121
|
* - can alse be used to store normal values, but it is not reactive.
|
|
122
|
+
* - if the value is already a `KTRef`, it will be returned **directly**.
|
|
117
123
|
* @param value mostly an HTMLElement
|
|
118
124
|
*/
|
|
119
125
|
function ref(value, onChange) {
|
|
126
|
+
if (isKTRef(value)) {
|
|
127
|
+
if (onChange) {
|
|
128
|
+
value.addOnChange(onChange);
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
120
132
|
return new KTRef(value, onChange ? [onChange] : []);
|
|
121
133
|
}
|
|
134
|
+
function deref(value) {
|
|
135
|
+
return isKTRef(value) ? value.value : value;
|
|
136
|
+
}
|
|
122
137
|
function kcollect() {
|
|
123
138
|
const newObj = {};
|
|
124
139
|
const entries = $entries(this);
|
|
@@ -143,6 +158,21 @@ const surfaceRef = (obj) => {
|
|
|
143
158
|
}
|
|
144
159
|
return newObj;
|
|
145
160
|
};
|
|
161
|
+
// # asserts
|
|
162
|
+
/**
|
|
163
|
+
* Assert k-model to be a ref object
|
|
164
|
+
*/
|
|
165
|
+
const $modelOrRef = (props, defaultValue) => {
|
|
166
|
+
// & props is an object. Won't use it in any other place
|
|
167
|
+
if ('k-model' in props) {
|
|
168
|
+
const kmodel = props['k-model'];
|
|
169
|
+
if (!kmodel?.isKT) {
|
|
170
|
+
throw new Error(`[kt.js error] k-model data must be a KTRef object, please use 'ref(...)' to wrap it.`);
|
|
171
|
+
}
|
|
172
|
+
return kmodel;
|
|
173
|
+
}
|
|
174
|
+
return ref(defaultValue);
|
|
175
|
+
};
|
|
146
176
|
|
|
147
177
|
const booleanHandler = (element, key, value) => {
|
|
148
178
|
if (key in element) {
|
|
@@ -327,7 +357,7 @@ const MATHML_ATTR_FLAG = '__kt_mathml__';
|
|
|
327
357
|
* ## About
|
|
328
358
|
* @package @ktjs/core
|
|
329
359
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
330
|
-
* @version 0.22.
|
|
360
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
331
361
|
* @license MIT
|
|
332
362
|
* @link https://github.com/baendlorel/kt.js
|
|
333
363
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -477,4 +507,4 @@ function createRedrawable(creator) {
|
|
|
477
507
|
return elRef;
|
|
478
508
|
}
|
|
479
509
|
|
|
480
|
-
export { Fragment, KTRef, h as createElement, createRedrawable, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
510
|
+
export { $modelOrRef, Fragment, KTRef, h as createElement, createRedrawable, deref, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
@@ -19,6 +19,10 @@ declare class KTRef<T> {
|
|
|
19
19
|
*/
|
|
20
20
|
get value(): T;
|
|
21
21
|
set value(newValue: T);
|
|
22
|
+
/**
|
|
23
|
+
* Register a callback when the value changes
|
|
24
|
+
* @param callback (newValue, oldValue) => xxx
|
|
25
|
+
*/
|
|
22
26
|
addOnChange(callback: RefChangeHandler<T>): void;
|
|
23
27
|
removeOnChange(callback: RefChangeHandler<T>): boolean;
|
|
24
28
|
}
|
|
@@ -125,7 +129,7 @@ type KTAttribute = KTBaseAttribute & KTPrefixedEventHandlers;
|
|
|
125
129
|
* ## About
|
|
126
130
|
* @package @ktjs/core
|
|
127
131
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
128
|
-
* @version 0.22.
|
|
132
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
129
133
|
* @license MIT
|
|
130
134
|
* @link https://github.com/baendlorel/kt.js
|
|
131
135
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
package/dist/jsx/jsx-runtime.mjs
CHANGED
|
@@ -93,7 +93,14 @@ class KTRef {
|
|
|
93
93
|
this._onChanges[i](newValue, oldValue);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Register a callback when the value changes
|
|
98
|
+
* @param callback (newValue, oldValue) => xxx
|
|
99
|
+
*/
|
|
96
100
|
addOnChange(callback) {
|
|
101
|
+
if (typeof callback !== 'function') {
|
|
102
|
+
throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
|
|
103
|
+
}
|
|
97
104
|
this._onChanges.push(callback);
|
|
98
105
|
}
|
|
99
106
|
removeOnChange(callback) {
|
|
@@ -106,16 +113,18 @@ class KTRef {
|
|
|
106
113
|
return false;
|
|
107
114
|
}
|
|
108
115
|
}
|
|
109
|
-
const isKTRef = (obj) =>
|
|
110
|
-
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
111
|
-
};
|
|
116
|
+
const isKTRef = (obj) => obj?.isKT === true;
|
|
112
117
|
/**
|
|
113
118
|
* Reference to the created HTML element.
|
|
114
119
|
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
115
120
|
* - can alse be used to store normal values, but it is not reactive.
|
|
121
|
+
* - if the value is already a `KTRef`, it will be returned **directly**.
|
|
116
122
|
* @param value mostly an HTMLElement
|
|
117
123
|
*/
|
|
118
124
|
function ref(value, onChange) {
|
|
125
|
+
if (isKTRef(value)) {
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
119
128
|
return new KTRef(value, []);
|
|
120
129
|
}
|
|
121
130
|
|
|
@@ -302,7 +311,7 @@ const MATHML_ATTR_FLAG = '__kt_mathml__';
|
|
|
302
311
|
* ## About
|
|
303
312
|
* @package @ktjs/core
|
|
304
313
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
305
|
-
* @version 0.22.
|
|
314
|
+
* @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
|
|
306
315
|
* @license MIT
|
|
307
316
|
* @link https://github.com/baendlorel/kt.js
|
|
308
317
|
* @link https://baendlorel.github.io/ Welcome to my site!
|