@ktjs/core 0.21.1 → 0.22.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 +9 -4
- package/dist/index.iife.js +136 -100
- package/dist/index.legacy.js +133 -97
- package/dist/index.mjs +136 -100
- package/dist/jsx/index.d.ts +6 -3
- package/dist/jsx/index.mjs +136 -100
- package/dist/jsx/jsx-runtime.d.ts +6 -3
- package/dist/jsx/jsx-runtime.mjs +112 -76
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ type HTMLTag = keyof HTMLElementTagNameMap;
|
|
|
7
7
|
type SVGTag = keyof SVGElementTagNameMap;
|
|
8
8
|
type MathMLTag = keyof MathMLElementTagNameMap;
|
|
9
9
|
|
|
10
|
+
type InputElementTag = 'input' | 'select' | 'textarea';
|
|
11
|
+
|
|
10
12
|
type RefChangeHandler<T> = (newValue: T, oldValue: T) => void;
|
|
11
13
|
declare class KTRef<T> {
|
|
12
14
|
/**
|
|
@@ -72,12 +74,15 @@ interface KTBaseAttribute {
|
|
|
72
74
|
// # kt-specific attributes
|
|
73
75
|
ref?: KTRef<JSX.Element>;
|
|
74
76
|
|
|
75
|
-
// todo 是否要让k-if是KTRef的时候具备响应能力?
|
|
76
77
|
/**
|
|
77
78
|
* If a `KTRef` is bound, it will be reactive; otherwise, it will be static.
|
|
78
79
|
*/
|
|
79
80
|
'k-if'?: any;
|
|
80
|
-
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Register two-way data binding between an input element and a KTRef.
|
|
84
|
+
* - Default to regist `input` event and `value` property(`checked` for checkboxes and radios).
|
|
85
|
+
*/
|
|
81
86
|
'k-model'?: KTRef<any>;
|
|
82
87
|
|
|
83
88
|
// # normal HTML attributes
|
|
@@ -157,7 +162,7 @@ type KTComponent = (
|
|
|
157
162
|
* ## About
|
|
158
163
|
* @package @ktjs/core
|
|
159
164
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
160
|
-
* @version 0.
|
|
165
|
+
* @version 0.22.2 (Last Update: 2026.02.02 20:28:37.901)
|
|
161
166
|
* @license MIT
|
|
162
167
|
* @link https://github.com/baendlorel/kt.js
|
|
163
168
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -1463,4 +1468,4 @@ interface KTForProps<T> {
|
|
|
1463
1468
|
declare function KTFor<T>(props: KTForProps<T>): KTForElement;
|
|
1464
1469
|
|
|
1465
1470
|
export { Fragment, KTAsync, KTFor, KTRef, h as createElement, createRedrawable, h, isKTRef, jsx, jsxDEV, jsxs, ref, surfaceRef };
|
|
1466
|
-
export type { EventHandler, HTMLTag, KTAttribute, KTForElement, KTForProps, KTRawAttr, KTRawContent, KTRawContents, KTSurfaceRef };
|
|
1471
|
+
export type { EventHandler, HTMLTag, InputElementTag, KTAttribute, KTForElement, KTForProps, KTRawAttr, KTRawContent, KTRawContents, KTSurfaceRef, MathMLTag, SVGTag };
|
package/dist/index.iife.js
CHANGED
|
@@ -6,14 +6,8 @@ var __ktjs_core__ = (function (exports) {
|
|
|
6
6
|
const $entries = Object.entries;
|
|
7
7
|
const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
|
|
8
8
|
|
|
9
|
-
// Error handling utilities
|
|
10
|
-
const $throw = (message) => {
|
|
11
|
-
throw new Error('@ktjs/shared: ' + message);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
9
|
// DOM manipulation utilities
|
|
15
10
|
// # dom natives
|
|
16
|
-
const $replaceWith = Element.prototype.replaceWith;
|
|
17
11
|
/**
|
|
18
12
|
* & Remove `bind` because it is shockingly slower than wrapper
|
|
19
13
|
* & `window.document` is safe because it is not configurable and its setter is undefined
|
|
@@ -50,10 +44,108 @@ var __ktjs_core__ = (function (exports) {
|
|
|
50
44
|
}
|
|
51
45
|
};
|
|
52
46
|
const { get: $buttonDisabledGetter, set: $buttonDisabledSetter } = Object.getOwnPropertyDescriptor(HTMLButtonElement.prototype, 'disabled');
|
|
47
|
+
/**
|
|
48
|
+
* Used for `k-model`
|
|
49
|
+
*/
|
|
50
|
+
const applyModel = (element, valueRef, propName, eventName) => {
|
|
51
|
+
element[propName] = valueRef.value; // initialize
|
|
52
|
+
valueRef.addOnChange((newValue) => (element[propName] = newValue));
|
|
53
|
+
element.addEventListener(eventName, () => (valueRef.value = element[propName]));
|
|
54
|
+
};
|
|
53
55
|
|
|
54
56
|
// Shared utilities and cached native methods for kt.js framework
|
|
55
57
|
// Re-export all utilities
|
|
56
|
-
Object.defineProperty(window, '
|
|
58
|
+
Object.defineProperty(window, '__ktjs__', { value: '0.22.2' });
|
|
59
|
+
|
|
60
|
+
class KTRef {
|
|
61
|
+
/**
|
|
62
|
+
* Indicates that this is a KTRef instance
|
|
63
|
+
*/
|
|
64
|
+
isKT = true;
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
_value;
|
|
69
|
+
/**
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
_onChanges;
|
|
73
|
+
constructor(_value, _onChanges) {
|
|
74
|
+
this._value = _value;
|
|
75
|
+
this._onChanges = _onChanges;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* If new value and old value are both nodes, the old one will be replaced in the DOM
|
|
79
|
+
*/
|
|
80
|
+
get value() {
|
|
81
|
+
return this._value;
|
|
82
|
+
}
|
|
83
|
+
set value(newValue) {
|
|
84
|
+
if (newValue === this._value) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// replace the old node with the new one in the DOM if both are nodes
|
|
88
|
+
if (this._value instanceof Node && newValue instanceof Node) {
|
|
89
|
+
if (newValue.contains(this._value)) {
|
|
90
|
+
this._value.remove();
|
|
91
|
+
}
|
|
92
|
+
this._value.replaceWith(newValue);
|
|
93
|
+
}
|
|
94
|
+
const oldValue = this._value;
|
|
95
|
+
this._value = newValue;
|
|
96
|
+
for (let i = 0; i < this._onChanges.length; i++) {
|
|
97
|
+
this._onChanges[i](newValue, oldValue);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
addOnChange(callback) {
|
|
101
|
+
this._onChanges.push(callback);
|
|
102
|
+
}
|
|
103
|
+
removeOnChange(callback) {
|
|
104
|
+
for (let i = this._onChanges.length - 1; i >= 0; i--) {
|
|
105
|
+
if (this._onChanges[i] === callback) {
|
|
106
|
+
this._onChanges.splice(i, 1);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const isKTRef = (obj) => {
|
|
114
|
+
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Reference to the created HTML element.
|
|
118
|
+
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
119
|
+
* - can alse be used to store normal values, but it is not reactive.
|
|
120
|
+
* @param value mostly an HTMLElement
|
|
121
|
+
*/
|
|
122
|
+
function ref(value, onChange) {
|
|
123
|
+
return new KTRef(value, onChange ? [onChange] : []);
|
|
124
|
+
}
|
|
125
|
+
function kcollect() {
|
|
126
|
+
const newObj = {};
|
|
127
|
+
const entries = $entries(this);
|
|
128
|
+
for (let i = 0; i < entries.length; i++) {
|
|
129
|
+
const key = entries[i][0];
|
|
130
|
+
if (key === 'kcollect') {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
newObj[key] = entries[i][1].value;
|
|
134
|
+
}
|
|
135
|
+
return newObj;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Make all first-level properties of the object a `KTRef`.
|
|
139
|
+
* - `obj.a.b` is not reactive
|
|
140
|
+
*/
|
|
141
|
+
const surfaceRef = (obj) => {
|
|
142
|
+
const entries = $entries(obj);
|
|
143
|
+
const newObj = { kcollect };
|
|
144
|
+
for (let i = 0; i < entries.length; i++) {
|
|
145
|
+
newObj[entries[i][0]] = ref(entries[i][1]);
|
|
146
|
+
}
|
|
147
|
+
return newObj;
|
|
148
|
+
};
|
|
57
149
|
|
|
58
150
|
const booleanHandler = (element, key, value) => {
|
|
59
151
|
if (key in element) {
|
|
@@ -119,6 +211,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
119
211
|
key === 'style' ||
|
|
120
212
|
key === 'children' ||
|
|
121
213
|
key === 'k-if' ||
|
|
214
|
+
key.startsWith('k-model') ||
|
|
122
215
|
key === 'ref') {
|
|
123
216
|
continue;
|
|
124
217
|
}
|
|
@@ -141,7 +234,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
141
234
|
attrIsObject(element, attr);
|
|
142
235
|
}
|
|
143
236
|
else {
|
|
144
|
-
throw new Error('kt.js
|
|
237
|
+
throw new Error('[kt.js error] attr must be an object.');
|
|
145
238
|
}
|
|
146
239
|
}
|
|
147
240
|
|
|
@@ -196,6 +289,30 @@ var __ktjs_core__ = (function (exports) {
|
|
|
196
289
|
}
|
|
197
290
|
}
|
|
198
291
|
|
|
292
|
+
function applyKModel(element, valueRef) {
|
|
293
|
+
if (!isKTRef(valueRef)) {
|
|
294
|
+
console.warn('[kt.js warn] k-model value must be a KTRef.');
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (element instanceof HTMLInputElement) {
|
|
298
|
+
if (element.type === 'radio' || element.type === 'checkbox') {
|
|
299
|
+
applyModel(element, valueRef, 'checked', 'change');
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
applyModel(element, valueRef, 'value', 'input');
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else if (element instanceof HTMLSelectElement) {
|
|
306
|
+
applyModel(element, valueRef, 'value', 'change');
|
|
307
|
+
}
|
|
308
|
+
else if (element instanceof HTMLTextAreaElement) {
|
|
309
|
+
applyModel(element, valueRef, 'value', 'input');
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
console.warn('[kt.js warn] not supported element for k-model:');
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
199
316
|
const htmlCreator = (tag) => document.createElement(tag);
|
|
200
317
|
const svgCreator = (tag) => document.createElementNS('http://www.w3.org/2000/svg', tag);
|
|
201
318
|
const mathMLCreator = (tag) => document.createElementNS('http://www.w3.org/1998/Math/MathML', tag);
|
|
@@ -213,7 +330,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
213
330
|
* ## About
|
|
214
331
|
* @package @ktjs/core
|
|
215
332
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
216
|
-
* @version 0.
|
|
333
|
+
* @version 0.22.2 (Last Update: 2026.02.02 20:28:37.901)
|
|
217
334
|
* @license MIT
|
|
218
335
|
* @link https://github.com/baendlorel/kt.js
|
|
219
336
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -222,7 +339,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
222
339
|
*/
|
|
223
340
|
const h = (tag, attr, content) => {
|
|
224
341
|
if (typeof tag !== 'string') {
|
|
225
|
-
|
|
342
|
+
throw new Error('[kt.js error] tagName must be a string.');
|
|
226
343
|
}
|
|
227
344
|
if (attr) {
|
|
228
345
|
if (SVG_ATTR_FLAG in attr) {
|
|
@@ -242,97 +359,16 @@ var __ktjs_core__ = (function (exports) {
|
|
|
242
359
|
// * Handle content
|
|
243
360
|
applyAttr(element, attr);
|
|
244
361
|
applyContent(element, content);
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Indicates that this is a KTRef instance
|
|
251
|
-
*/
|
|
252
|
-
isKT = true;
|
|
253
|
-
/**
|
|
254
|
-
* @internal
|
|
255
|
-
*/
|
|
256
|
-
_value;
|
|
257
|
-
/**
|
|
258
|
-
* @internal
|
|
259
|
-
*/
|
|
260
|
-
_onChanges;
|
|
261
|
-
constructor(_value, _onChanges) {
|
|
262
|
-
this._value = _value;
|
|
263
|
-
this._onChanges = _onChanges;
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* If new value and old value are both nodes, the old one will be replaced in the DOM
|
|
267
|
-
*/
|
|
268
|
-
get value() {
|
|
269
|
-
return this._value;
|
|
270
|
-
}
|
|
271
|
-
set value(newValue) {
|
|
272
|
-
if (newValue === this._value) {
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
// replace the old node with the new one in the DOM if both are nodes
|
|
276
|
-
if (this._value instanceof Node && newValue instanceof Node) {
|
|
277
|
-
if (newValue.contains(this._value)) {
|
|
278
|
-
this._value.remove();
|
|
279
|
-
}
|
|
280
|
-
this._value.replaceWith(newValue);
|
|
281
|
-
}
|
|
282
|
-
const oldValue = this._value;
|
|
283
|
-
this._value = newValue;
|
|
284
|
-
for (let i = 0; i < this._onChanges.length; i++) {
|
|
285
|
-
this._onChanges[i](newValue, oldValue);
|
|
362
|
+
if (typeof attr === 'object' && attr !== null && 'k-model' in attr) {
|
|
363
|
+
const kmodel = attr['k-model'];
|
|
364
|
+
if (isKTRef(kmodel)) {
|
|
365
|
+
applyKModel(element, kmodel);
|
|
286
366
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
this._onChanges.push(callback);
|
|
290
|
-
}
|
|
291
|
-
removeOnChange(callback) {
|
|
292
|
-
for (let i = this._onChanges.length - 1; i >= 0; i--) {
|
|
293
|
-
if (this._onChanges[i] === callback) {
|
|
294
|
-
this._onChanges.splice(i, 1);
|
|
295
|
-
return true;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return false;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
const isKTRef = (obj) => {
|
|
302
|
-
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
303
|
-
};
|
|
304
|
-
/**
|
|
305
|
-
* Reference to the created HTML element.
|
|
306
|
-
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
307
|
-
* - can alse be used to store normal values, but it is not reactive.
|
|
308
|
-
* @param value mostly an HTMLElement
|
|
309
|
-
*/
|
|
310
|
-
function ref(value, onChange) {
|
|
311
|
-
return new KTRef(value, onChange ? [onChange] : []);
|
|
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;
|
|
367
|
+
else {
|
|
368
|
+
throw new Error('[kt.js error] k-model value must be a KTRef.');
|
|
320
369
|
}
|
|
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
370
|
}
|
|
335
|
-
return
|
|
371
|
+
return element;
|
|
336
372
|
};
|
|
337
373
|
|
|
338
374
|
const dummyRef = { value: null };
|
|
@@ -363,7 +399,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
363
399
|
}
|
|
364
400
|
const oldEl = el;
|
|
365
401
|
el = newValue ? create(tag, props) : placeholder();
|
|
366
|
-
|
|
402
|
+
oldEl.replaceWith(el);
|
|
367
403
|
maybeDummyRef.value = el;
|
|
368
404
|
});
|
|
369
405
|
condition = kif.value;
|
|
@@ -384,7 +420,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
384
420
|
* Note: kt.js doesn't have a real Fragment concept,
|
|
385
421
|
*/
|
|
386
422
|
function Fragment(_props) {
|
|
387
|
-
throw new Error("kt.js doesn't have a Fragment concept");
|
|
423
|
+
throw new Error("[kt.js error] doesn't have a Fragment concept");
|
|
388
424
|
// const { children } = props ?? {};
|
|
389
425
|
// if (!children) {
|
|
390
426
|
// return ;
|
package/dist/index.legacy.js
CHANGED
|
@@ -6,14 +6,8 @@ var __ktjs_core__ = (function (exports) {
|
|
|
6
6
|
const $entries = Object.entries;
|
|
7
7
|
const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
|
|
8
8
|
|
|
9
|
-
// Error handling utilities
|
|
10
|
-
const $throw = (message) => {
|
|
11
|
-
throw new Error('@ktjs/shared: ' + message);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
9
|
// DOM manipulation utilities
|
|
15
10
|
// # dom natives
|
|
16
|
-
const $replaceWith = Element.prototype.replaceWith;
|
|
17
11
|
/**
|
|
18
12
|
* & Remove `bind` because it is shockingly slower than wrapper
|
|
19
13
|
* & `window.document` is safe because it is not configurable and its setter is undefined
|
|
@@ -50,10 +44,105 @@ var __ktjs_core__ = (function (exports) {
|
|
|
50
44
|
}
|
|
51
45
|
};
|
|
52
46
|
const { get: $buttonDisabledGetter, set: $buttonDisabledSetter } = Object.getOwnPropertyDescriptor(HTMLButtonElement.prototype, 'disabled');
|
|
47
|
+
/**
|
|
48
|
+
* Used for `k-model`
|
|
49
|
+
*/
|
|
50
|
+
const applyModel = (element, valueRef, propName, eventName) => {
|
|
51
|
+
element[propName] = valueRef.value; // initialize
|
|
52
|
+
valueRef.addOnChange((newValue) => (element[propName] = newValue));
|
|
53
|
+
element.addEventListener(eventName, () => (valueRef.value = element[propName]));
|
|
54
|
+
};
|
|
53
55
|
|
|
54
56
|
// Shared utilities and cached native methods for kt.js framework
|
|
55
57
|
// Re-export all utilities
|
|
56
|
-
Object.defineProperty(window, '
|
|
58
|
+
Object.defineProperty(window, '__ktjs__', { value: '0.22.2' });
|
|
59
|
+
|
|
60
|
+
var KTRef = /** @class */ (function () {
|
|
61
|
+
function KTRef(_value, _onChanges) {
|
|
62
|
+
/**
|
|
63
|
+
* Indicates that this is a KTRef instance
|
|
64
|
+
*/
|
|
65
|
+
this.isKT = true;
|
|
66
|
+
this._value = _value;
|
|
67
|
+
this._onChanges = _onChanges;
|
|
68
|
+
}
|
|
69
|
+
Object.defineProperty(KTRef.prototype, "value", {
|
|
70
|
+
/**
|
|
71
|
+
* If new value and old value are both nodes, the old one will be replaced in the DOM
|
|
72
|
+
*/
|
|
73
|
+
get: function () {
|
|
74
|
+
return this._value;
|
|
75
|
+
},
|
|
76
|
+
set: function (newValue) {
|
|
77
|
+
if (newValue === this._value) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
// replace the old node with the new one in the DOM if both are nodes
|
|
81
|
+
if (this._value instanceof Node && newValue instanceof Node) {
|
|
82
|
+
if (newValue.contains(this._value)) {
|
|
83
|
+
this._value.remove();
|
|
84
|
+
}
|
|
85
|
+
this._value.replaceWith(newValue);
|
|
86
|
+
}
|
|
87
|
+
var oldValue = this._value;
|
|
88
|
+
this._value = newValue;
|
|
89
|
+
for (var i = 0; i < this._onChanges.length; i++) {
|
|
90
|
+
this._onChanges[i](newValue, oldValue);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
enumerable: false,
|
|
94
|
+
configurable: true
|
|
95
|
+
});
|
|
96
|
+
KTRef.prototype.addOnChange = function (callback) {
|
|
97
|
+
this._onChanges.push(callback);
|
|
98
|
+
};
|
|
99
|
+
KTRef.prototype.removeOnChange = function (callback) {
|
|
100
|
+
for (var i = this._onChanges.length - 1; i >= 0; i--) {
|
|
101
|
+
if (this._onChanges[i] === callback) {
|
|
102
|
+
this._onChanges.splice(i, 1);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
};
|
|
108
|
+
return KTRef;
|
|
109
|
+
}());
|
|
110
|
+
var isKTRef = function (obj) {
|
|
111
|
+
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Reference to the created HTML element.
|
|
115
|
+
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
116
|
+
* - can alse be used to store normal values, but it is not reactive.
|
|
117
|
+
* @param value mostly an HTMLElement
|
|
118
|
+
*/
|
|
119
|
+
function ref(value, onChange) {
|
|
120
|
+
return new KTRef(value, onChange ? [onChange] : []);
|
|
121
|
+
}
|
|
122
|
+
function kcollect() {
|
|
123
|
+
var newObj = {};
|
|
124
|
+
var entries = $entries(this);
|
|
125
|
+
for (var i = 0; i < entries.length; i++) {
|
|
126
|
+
var key = entries[i][0];
|
|
127
|
+
if (key === 'kcollect') {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
newObj[key] = entries[i][1].value;
|
|
131
|
+
}
|
|
132
|
+
return newObj;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Make all first-level properties of the object a `KTRef`.
|
|
136
|
+
* - `obj.a.b` is not reactive
|
|
137
|
+
*/
|
|
138
|
+
var surfaceRef = function (obj) {
|
|
139
|
+
var entries = $entries(obj);
|
|
140
|
+
var newObj = { kcollect: kcollect };
|
|
141
|
+
for (var i = 0; i < entries.length; i++) {
|
|
142
|
+
newObj[entries[i][0]] = ref(entries[i][1]);
|
|
143
|
+
}
|
|
144
|
+
return newObj;
|
|
145
|
+
};
|
|
57
146
|
|
|
58
147
|
var booleanHandler = function (element, key, value) {
|
|
59
148
|
if (key in element) {
|
|
@@ -121,6 +210,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
121
210
|
key === 'style' ||
|
|
122
211
|
key === 'children' ||
|
|
123
212
|
key === 'k-if' ||
|
|
213
|
+
key.startsWith('k-model') ||
|
|
124
214
|
key === 'ref') {
|
|
125
215
|
continue;
|
|
126
216
|
}
|
|
@@ -143,7 +233,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
143
233
|
attrIsObject(element, attr);
|
|
144
234
|
}
|
|
145
235
|
else {
|
|
146
|
-
throw new Error('kt.js
|
|
236
|
+
throw new Error('[kt.js error] attr must be an object.');
|
|
147
237
|
}
|
|
148
238
|
}
|
|
149
239
|
|
|
@@ -201,6 +291,30 @@ var __ktjs_core__ = (function (exports) {
|
|
|
201
291
|
}
|
|
202
292
|
}
|
|
203
293
|
|
|
294
|
+
function applyKModel(element, valueRef) {
|
|
295
|
+
if (!isKTRef(valueRef)) {
|
|
296
|
+
console.warn('[kt.js warn] k-model value must be a KTRef.');
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (element instanceof HTMLInputElement) {
|
|
300
|
+
if (element.type === 'radio' || element.type === 'checkbox') {
|
|
301
|
+
applyModel(element, valueRef, 'checked', 'change');
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
applyModel(element, valueRef, 'value', 'input');
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else if (element instanceof HTMLSelectElement) {
|
|
308
|
+
applyModel(element, valueRef, 'value', 'change');
|
|
309
|
+
}
|
|
310
|
+
else if (element instanceof HTMLTextAreaElement) {
|
|
311
|
+
applyModel(element, valueRef, 'value', 'input');
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
console.warn('[kt.js warn] not supported element for k-model:');
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
204
318
|
var htmlCreator = function (tag) { return document.createElement(tag); };
|
|
205
319
|
var svgCreator = function (tag) { return document.createElementNS('http://www.w3.org/2000/svg', tag); };
|
|
206
320
|
var mathMLCreator = function (tag) { return document.createElementNS('http://www.w3.org/1998/Math/MathML', tag); };
|
|
@@ -218,7 +332,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
218
332
|
* ## About
|
|
219
333
|
* @package @ktjs/core
|
|
220
334
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
221
|
-
* @version 0.
|
|
335
|
+
* @version 0.22.2 (Last Update: 2026.02.02 20:28:37.901)
|
|
222
336
|
* @license MIT
|
|
223
337
|
* @link https://github.com/baendlorel/kt.js
|
|
224
338
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
@@ -227,7 +341,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
227
341
|
*/
|
|
228
342
|
var h = function (tag, attr, content) {
|
|
229
343
|
if (typeof tag !== 'string') {
|
|
230
|
-
|
|
344
|
+
throw new Error('[kt.js error] tagName must be a string.');
|
|
231
345
|
}
|
|
232
346
|
if (attr) {
|
|
233
347
|
if (SVG_ATTR_FLAG in attr) {
|
|
@@ -247,94 +361,16 @@ var __ktjs_core__ = (function (exports) {
|
|
|
247
361
|
// * Handle content
|
|
248
362
|
applyAttr(element, attr);
|
|
249
363
|
applyContent(element, content);
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
function KTRef(_value, _onChanges) {
|
|
255
|
-
/**
|
|
256
|
-
* Indicates that this is a KTRef instance
|
|
257
|
-
*/
|
|
258
|
-
this.isKT = true;
|
|
259
|
-
this._value = _value;
|
|
260
|
-
this._onChanges = _onChanges;
|
|
261
|
-
}
|
|
262
|
-
Object.defineProperty(KTRef.prototype, "value", {
|
|
263
|
-
/**
|
|
264
|
-
* If new value and old value are both nodes, the old one will be replaced in the DOM
|
|
265
|
-
*/
|
|
266
|
-
get: function () {
|
|
267
|
-
return this._value;
|
|
268
|
-
},
|
|
269
|
-
set: function (newValue) {
|
|
270
|
-
if (newValue === this._value) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
// replace the old node with the new one in the DOM if both are nodes
|
|
274
|
-
if (this._value instanceof Node && newValue instanceof Node) {
|
|
275
|
-
if (newValue.contains(this._value)) {
|
|
276
|
-
this._value.remove();
|
|
277
|
-
}
|
|
278
|
-
this._value.replaceWith(newValue);
|
|
279
|
-
}
|
|
280
|
-
var oldValue = this._value;
|
|
281
|
-
this._value = newValue;
|
|
282
|
-
for (var i = 0; i < this._onChanges.length; i++) {
|
|
283
|
-
this._onChanges[i](newValue, oldValue);
|
|
284
|
-
}
|
|
285
|
-
},
|
|
286
|
-
enumerable: false,
|
|
287
|
-
configurable: true
|
|
288
|
-
});
|
|
289
|
-
KTRef.prototype.addOnChange = function (callback) {
|
|
290
|
-
this._onChanges.push(callback);
|
|
291
|
-
};
|
|
292
|
-
KTRef.prototype.removeOnChange = function (callback) {
|
|
293
|
-
for (var i = this._onChanges.length - 1; i >= 0; i--) {
|
|
294
|
-
if (this._onChanges[i] === callback) {
|
|
295
|
-
this._onChanges.splice(i, 1);
|
|
296
|
-
return true;
|
|
297
|
-
}
|
|
364
|
+
if (typeof attr === 'object' && attr !== null && 'k-model' in attr) {
|
|
365
|
+
var kmodel = attr['k-model'];
|
|
366
|
+
if (isKTRef(kmodel)) {
|
|
367
|
+
applyKModel(element, kmodel);
|
|
298
368
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return KTRef;
|
|
302
|
-
}());
|
|
303
|
-
var isKTRef = function (obj) {
|
|
304
|
-
return typeof obj === 'object' && obj !== null && obj.isKT === true;
|
|
305
|
-
};
|
|
306
|
-
/**
|
|
307
|
-
* Reference to the created HTML element.
|
|
308
|
-
* - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
|
|
309
|
-
* - can alse be used to store normal values, but it is not reactive.
|
|
310
|
-
* @param value mostly an HTMLElement
|
|
311
|
-
*/
|
|
312
|
-
function ref(value, onChange) {
|
|
313
|
-
return new KTRef(value, onChange ? [onChange] : []);
|
|
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;
|
|
369
|
+
else {
|
|
370
|
+
throw new Error('[kt.js error] k-model value must be a KTRef.');
|
|
322
371
|
}
|
|
323
|
-
newObj[key] = entries[i][1].value;
|
|
324
372
|
}
|
|
325
|
-
return
|
|
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;
|
|
373
|
+
return element;
|
|
338
374
|
};
|
|
339
375
|
|
|
340
376
|
var dummyRef = { value: null };
|
|
@@ -365,7 +401,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
365
401
|
}
|
|
366
402
|
var oldEl = el;
|
|
367
403
|
el = newValue ? create(tag, props) : placeholder();
|
|
368
|
-
|
|
404
|
+
oldEl.replaceWith(el);
|
|
369
405
|
maybeDummyRef.value = el;
|
|
370
406
|
});
|
|
371
407
|
condition = kif.value;
|
|
@@ -386,7 +422,7 @@ var __ktjs_core__ = (function (exports) {
|
|
|
386
422
|
* Note: kt.js doesn't have a real Fragment concept,
|
|
387
423
|
*/
|
|
388
424
|
function Fragment(_props) {
|
|
389
|
-
throw new Error("kt.js doesn't have a Fragment concept");
|
|
425
|
+
throw new Error("[kt.js error] doesn't have a Fragment concept");
|
|
390
426
|
// const { children } = props ?? {};
|
|
391
427
|
// if (!children) {
|
|
392
428
|
// return ;
|