@ktjs/core 0.22.5 → 0.24.0

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.
@@ -1,13 +1,38 @@
1
1
  var __ktjs_core__ = (function (exports) {
2
2
  'use strict';
3
3
 
4
+ // Shared constants
5
+ // Empty for now - can be extended with framework-wide constants
6
+ /**
7
+ * Mark the attribute as SVG to handle special cases during rendering.
8
+ */
9
+ const SVG_ATTR_FLAG = '__kt_svg__';
10
+ /**
11
+ * Mark the attribute as MathML to handle special cases during rendering.
12
+ */
13
+ const MATHML_ATTR_FLAG = '__kt_mathml__';
14
+
4
15
  // Cached native methods for performance optimization
5
16
  const $isArray = Array.isArray;
17
+ const $is = Object.is;
6
18
  const $entries = Object.entries;
7
- const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
19
+ const $random = Math.random;
20
+ const $isThenable = (o) => typeof o?.then === 'function';
8
21
 
9
22
  // DOM manipulation utilities
10
23
  // # dom natives
24
+ const $isNode = (x) => x?.nodeType > 0;
25
+ /**
26
+ * Safe replace `oldNode` With `newNode`
27
+ */
28
+ const $replaceNode = (oldNode, newNode) => {
29
+ if ($isNode(oldNode) && $isNode(newNode)) {
30
+ if (newNode.contains(oldNode)) {
31
+ newNode.remove();
32
+ }
33
+ oldNode.replaceWith(newNode);
34
+ }
35
+ };
11
36
  /**
12
37
  * & Remove `bind` because it is shockingly slower than wrapper
13
38
  * & `window.document` is safe because it is not configurable and its setter is undefined
@@ -53,129 +78,21 @@ var __ktjs_core__ = (function (exports) {
53
78
  element.addEventListener(eventName, () => (valueRef.value = element[propName]));
54
79
  };
55
80
 
81
+ if (typeof Symbol === 'undefined') {
82
+ window.Symbol = function Symbol(description) {
83
+ return `@@SYMBOL_${description || ''}_${$random().toString(36).slice(2)}`;
84
+ };
85
+ }
86
+
56
87
  // Shared utilities and cached native methods for kt.js framework
57
- // Re-export all utilities
58
- Object.defineProperty(window, '__ktjs__', { value: '0.22.2' });
88
+ Object.defineProperty(window, '__ktjs__', { value: '0.22.6' });
59
89
 
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
- /**
101
- * Register a callback when the value changes
102
- * @param callback (newValue, oldValue) => xxx
103
- */
104
- addOnChange(callback) {
105
- if (typeof callback !== 'function') {
106
- throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
107
- }
108
- this._onChanges.push(callback);
109
- }
110
- removeOnChange(callback) {
111
- for (let i = this._onChanges.length - 1; i >= 0; i--) {
112
- if (this._onChanges[i] === callback) {
113
- this._onChanges.splice(i, 1);
114
- return true;
115
- }
116
- }
117
- return false;
118
- }
119
- }
120
- const isKTRef = (obj) => obj?.isKT === true;
121
- /**
122
- * Reference to the created HTML element.
123
- * - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
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**.
126
- * @param value mostly an HTMLElement
127
- */
128
- function ref(value, onChange) {
129
- if (isKTRef(value)) {
130
- if (onChange) {
131
- value.addOnChange(onChange);
132
- }
133
- return value;
134
- }
135
- return new KTRef(value, onChange ? [onChange] : []);
136
- }
137
- function deref(value) {
138
- return isKTRef(value) ? value.value : value;
139
- }
140
- function kcollect() {
141
- const newObj = {};
142
- const entries = $entries(this);
143
- for (let i = 0; i < entries.length; i++) {
144
- const key = entries[i][0];
145
- if (key === 'kcollect') {
146
- continue;
147
- }
148
- newObj[key] = entries[i][1].value;
149
- }
150
- return newObj;
151
- }
152
- /**
153
- * Make all first-level properties of the object a `KTRef`.
154
- * - `obj.a.b` is not reactive
155
- */
156
- const surfaceRef = (obj) => {
157
- const entries = $entries(obj);
158
- const newObj = { kcollect };
159
- for (let i = 0; i < entries.length; i++) {
160
- newObj[entries[i][0]] = ref(entries[i][1]);
161
- }
162
- return newObj;
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
- };
90
+ // export const KT_TYPE_REF = 1 as const;
91
+ // export const KT_TYPE_COMPUTED = 2 as const;
92
+ // export type KTReactiveTypeEnum = typeof KT_TYPE_REF | typeof KT_TYPE_COMPUTED;
93
+ const isKT = (obj) => obj?.isKT;
94
+ const isRef = (obj) => obj?.ktType === 1 /* KTReactiveType.REF */;
95
+ const isComputed = (obj) => obj?.ktType === 2 /* KTReactiveType.COMPUTED */;
179
96
 
180
97
  const booleanHandler = (element, key, value) => {
181
98
  if (key in element) {
@@ -235,14 +152,25 @@ var __ktjs_core__ = (function (exports) {
235
152
  }
236
153
  }
237
154
  }
155
+ if ('k-html' in attr) {
156
+ const html = attr['k-html'];
157
+ if (isKT(html)) {
158
+ element.innerHTML = html.value;
159
+ html.addOnChange((v) => (element.innerHTML = v));
160
+ }
161
+ else {
162
+ element.innerHTML = html;
163
+ }
164
+ }
238
165
  for (const key in attr) {
239
- if (key === 'class' ||
166
+ if (key === 'k-if' ||
167
+ key === 'k-model' ||
168
+ key === 'ref' ||
169
+ key === 'class' ||
240
170
  key === 'className' ||
241
171
  key === 'style' ||
242
172
  key === 'children' ||
243
- key === 'k-if' ||
244
- key.startsWith('k-model') ||
245
- key === 'ref') {
173
+ key === 'k-html') {
246
174
  continue;
247
175
  }
248
176
  const o = attr[key];
@@ -252,6 +180,7 @@ var __ktjs_core__ = (function (exports) {
252
180
  }
253
181
  // normal attributes
254
182
  else {
183
+ // todo 这里也可以绑定ref的
255
184
  (handlers[key] || defaultHandler)(element, key, o);
256
185
  }
257
186
  }
@@ -268,13 +197,25 @@ var __ktjs_core__ = (function (exports) {
268
197
  }
269
198
  }
270
199
 
200
+ const assureNode = (o) => ($isNode(o) ? o : document.createTextNode(o));
271
201
  function apdSingle(element, c) {
272
202
  // & JSX should ignore false, undefined, and null
273
203
  if (c === false || c === undefined || c === null) {
274
204
  return;
275
205
  }
276
- if (typeof c === 'object' && c !== null && 'isKT' in c) {
277
- $append.call(element, c.value);
206
+ if (isKT(c)) {
207
+ let node = assureNode(c.value);
208
+ $append.call(element, node);
209
+ c.addOnChange((newValue, oldValue) => {
210
+ if ($isNode(newValue) && $isNode(oldValue)) {
211
+ // & this case is handled automically in `class KTRef`
212
+ // todo 2 cases might be able to merge into 1
213
+ return;
214
+ }
215
+ const oldNode = node;
216
+ node = assureNode(newValue);
217
+ oldNode.replaceWith(node);
218
+ });
278
219
  }
279
220
  else {
280
221
  $append.call(element, c);
@@ -319,8 +260,218 @@ var __ktjs_core__ = (function (exports) {
319
260
  }
320
261
  }
321
262
 
263
+ class KTRef {
264
+ /**
265
+ * Indicates that this is a KTRef instance
266
+ */
267
+ isKT = true;
268
+ ktType = 1 /* KTReactiveType.REF */;
269
+ /**
270
+ * @internal
271
+ */
272
+ _value;
273
+ /**
274
+ * @internal
275
+ */
276
+ _onChanges;
277
+ constructor(_value, _onChanges) {
278
+ this._value = _value;
279
+ this._onChanges = _onChanges;
280
+ }
281
+ /**
282
+ * If new value and old value are both nodes, the old one will be replaced in the DOM
283
+ */
284
+ get value() {
285
+ return this._value;
286
+ }
287
+ set value(newValue) {
288
+ if ($is(newValue, this._value)) {
289
+ return;
290
+ }
291
+ const oldValue = this._value;
292
+ $replaceNode(oldValue, newValue);
293
+ this._value = newValue;
294
+ for (let i = 0; i < this._onChanges.length; i++) {
295
+ this._onChanges[i](newValue, oldValue);
296
+ }
297
+ }
298
+ /**
299
+ * Register a callback when the value changes
300
+ * @param callback (newValue, oldValue) => xxx
301
+ */
302
+ addOnChange(callback) {
303
+ if (typeof callback !== 'function') {
304
+ throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
305
+ }
306
+ this._onChanges.push(callback);
307
+ }
308
+ removeOnChange(callback) {
309
+ for (let i = this._onChanges.length - 1; i >= 0; i--) {
310
+ if (this._onChanges[i] === callback) {
311
+ this._onChanges.splice(i, 1);
312
+ return true;
313
+ }
314
+ }
315
+ return false;
316
+ }
317
+ }
318
+ /**
319
+ * Reference to the created HTML element.
320
+ * - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
321
+ * - can alse be used to store normal values, but it is not reactive.
322
+ * - if the value is already a `KTRef`, it will be returned **directly**.
323
+ * @param value mostly an HTMLElement
324
+ */
325
+ function ref(value, onChange) {
326
+ return new KTRef(value, onChange ? [onChange] : []);
327
+ }
328
+ /**
329
+ * Convert a value to `KTRef`.
330
+ * - Returns the original value if it is already a `KTRef`.
331
+ * - Throws error if the value is a `KTComputed`.
332
+ * - Otherwise wraps the value with `ref()`.
333
+ * @param o value to convert
334
+ */
335
+ const toRef = (o) => {
336
+ if (isRef(o)) {
337
+ return o;
338
+ }
339
+ else if (isComputed(o)) {
340
+ throw new Error('[kt.js error] Computed values cannot be used as KTRef.');
341
+ }
342
+ else {
343
+ return ref(o);
344
+ }
345
+ };
346
+ function deref(value) {
347
+ return isKT(value) ? value.value : value;
348
+ }
349
+ function kcollect() {
350
+ const newObj = {};
351
+ const entries = $entries(this);
352
+ for (let i = 0; i < entries.length; i++) {
353
+ const key = entries[i][0];
354
+ if (key === 'kcollect') {
355
+ continue;
356
+ }
357
+ newObj[key] = entries[i][1].value;
358
+ }
359
+ return newObj;
360
+ }
361
+ /**
362
+ * Make all first-level properties of the object a `KTRef`.
363
+ * - `obj.a.b` is not reactive
364
+ */
365
+ const surfaceRef = (obj) => {
366
+ const entries = $entries(obj);
367
+ const newObj = { kcollect };
368
+ for (let i = 0; i < entries.length; i++) {
369
+ newObj[entries[i][0]] = ref(entries[i][1]);
370
+ }
371
+ return newObj;
372
+ };
373
+ // # asserts
374
+ /**
375
+ * Assert k-model to be a ref object
376
+ */
377
+ const $modelOrRef = (props, defaultValue) => {
378
+ // & props is an object. Won't use it in any other place
379
+ if ('k-model' in props) {
380
+ const kmodel = props['k-model'];
381
+ if (!kmodel?.isKT) {
382
+ throw new Error(`[kt.js error] k-model data must be a KTRef object, please use 'ref(...)' to wrap it.`);
383
+ }
384
+ return kmodel;
385
+ }
386
+ return ref(defaultValue);
387
+ };
388
+
389
+ class KTComputed {
390
+ /**
391
+ * Indicates that this is a KTRef instance
392
+ */
393
+ isKT = true;
394
+ ktType = 2 /* KTReactiveType.COMPUTED */;
395
+ /**
396
+ * @internal
397
+ */
398
+ _calculator;
399
+ /**
400
+ * @internal
401
+ */
402
+ _value;
403
+ /**
404
+ * @internal
405
+ */
406
+ _onChanges = [];
407
+ _subscribe(reactives) {
408
+ for (let i = 0; i < reactives.length; i++) {
409
+ const reactive = reactives[i];
410
+ reactive.addOnChange(() => {
411
+ const oldValue = this._value;
412
+ this._value = this._calculator();
413
+ if (oldValue === this._value) {
414
+ return;
415
+ }
416
+ $replaceNode(oldValue, this._value);
417
+ for (let i = 0; i < this._onChanges.length; i++) {
418
+ this._onChanges[i](this._value, oldValue);
419
+ }
420
+ });
421
+ }
422
+ }
423
+ constructor(_calculator, reactives) {
424
+ this._calculator = _calculator;
425
+ this._value = _calculator();
426
+ this._subscribe(reactives);
427
+ }
428
+ /**
429
+ * If new value and old value are both nodes, the old one will be replaced in the DOM
430
+ */
431
+ get value() {
432
+ return this._value;
433
+ }
434
+ set value(_newValue) {
435
+ throw new Error('[kt.js error] KTComputed: cannot set value of a computed value');
436
+ }
437
+ /**
438
+ * Register a callback when the value changes
439
+ * @param callback (newValue, oldValue) => xxx
440
+ */
441
+ addOnChange(callback) {
442
+ if (typeof callback !== 'function') {
443
+ throw new Error('[kt.js error] KTRef.addOnChange: callback must be a function');
444
+ }
445
+ this._onChanges.push(callback);
446
+ }
447
+ /**
448
+ * Unregister a callback
449
+ * @param callback (newValue, oldValue) => xxx
450
+ */
451
+ removeOnChange(callback) {
452
+ for (let i = this._onChanges.length - 1; i >= 0; i--) {
453
+ if (this._onChanges[i] === callback) {
454
+ this._onChanges.splice(i, 1);
455
+ return true;
456
+ }
457
+ }
458
+ return false;
459
+ }
460
+ }
461
+ /**
462
+ * Create a reactive computed value
463
+ * @param computeFn
464
+ * @param reactives refs and computeds that this computed depends on
465
+ */
466
+ function computed(computeFn, reactives) {
467
+ if (reactives.some((v) => !isKT(v))) {
468
+ throw new Error('[kt.js error] computed: all reactives must be KTRef or KTComputed instances');
469
+ }
470
+ return new KTComputed(computeFn, reactives);
471
+ }
472
+
322
473
  function applyKModel(element, valueRef) {
323
- if (!isKTRef(valueRef)) {
474
+ if (!isKT(valueRef)) {
324
475
  console.warn('[kt.js warn] k-model value must be a KTRef.');
325
476
  return;
326
477
  }
@@ -347,9 +498,6 @@ var __ktjs_core__ = (function (exports) {
347
498
  const svgCreator = (tag) => document.createElementNS('http://www.w3.org/2000/svg', tag);
348
499
  const mathMLCreator = (tag) => document.createElementNS('http://www.w3.org/1998/Math/MathML', tag);
349
500
  let creator = htmlCreator;
350
- // # consts
351
- const SVG_ATTR_FLAG = '__kt_svg__';
352
- const MATHML_ATTR_FLAG = '__kt_mathml__';
353
501
  /**
354
502
  * Create an enhanced HTMLElement.
355
503
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -360,7 +508,7 @@ var __ktjs_core__ = (function (exports) {
360
508
  * ## About
361
509
  * @package @ktjs/core
362
510
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
363
- * @version 0.22.5 (Last Update: 2026.02.03 09:07:21.411)
511
+ * @version 0.24.0 (Last Update: 2026.02.05 12:08:54.164)
364
512
  * @license MIT
365
513
  * @link https://github.com/baendlorel/kt.js
366
514
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -390,13 +538,7 @@ var __ktjs_core__ = (function (exports) {
390
538
  applyAttr(element, attr);
391
539
  applyContent(element, content);
392
540
  if (typeof attr === 'object' && attr !== null && 'k-model' in attr) {
393
- const kmodel = attr['k-model'];
394
- if (isKTRef(kmodel)) {
395
- applyKModel(element, kmodel);
396
- }
397
- else {
398
- throw new Error('[kt.js error] k-model value must be a KTRef.');
399
- }
541
+ applyKModel(element, attr['k-model']);
400
542
  }
401
543
  return element;
402
544
  };
@@ -416,20 +558,23 @@ var __ktjs_core__ = (function (exports) {
416
558
  * @param props properties/attributes
417
559
  */
418
560
  function jsx(tag, props) {
419
- const maybeDummyRef = isKTRef(props.ref) ? props.ref : dummyRef;
561
+ if (isComputed(props.ref)) {
562
+ throw new Error('[kt.js error] Cannot assign a computed value to an element.');
563
+ }
564
+ const maybeDummyRef = isRef(props.ref) ? props.ref : dummyRef;
420
565
  let el;
421
566
  if ('k-if' in props) {
422
567
  const kif = props['k-if'];
423
568
  let condition = kif; // assume boolean by default
424
569
  // Handle reactive k-if
425
- if (isKTRef(kif)) {
570
+ if (isKT(kif)) {
426
571
  kif.addOnChange((newValue, oldValue) => {
427
572
  if (newValue === oldValue) {
428
573
  return;
429
574
  }
430
575
  const oldEl = el;
431
576
  el = newValue ? create(tag, props) : placeholder();
432
- oldEl.replaceWith(el);
577
+ $replaceNode(oldEl, el);
433
578
  maybeDummyRef.value = el;
434
579
  });
435
580
  condition = kif.value;
@@ -686,7 +831,7 @@ var __ktjs_core__ = (function (exports) {
686
831
  return anchor;
687
832
  };
688
833
  // Set ref if provided
689
- if (props.ref?.isKT) {
834
+ if (isRef(props.ref)) {
690
835
  props.ref.value = anchor;
691
836
  }
692
837
  return anchor;
@@ -737,18 +882,23 @@ var __ktjs_core__ = (function (exports) {
737
882
  exports.$modelOrRef = $modelOrRef;
738
883
  exports.Fragment = Fragment;
739
884
  exports.KTAsync = KTAsync;
885
+ exports.KTComputed = KTComputed;
740
886
  exports.KTFor = KTFor;
741
887
  exports.KTRef = KTRef;
888
+ exports.computed = computed;
742
889
  exports.createElement = h;
743
890
  exports.createRedrawable = createRedrawable;
744
891
  exports.deref = deref;
745
892
  exports.h = h;
746
- exports.isKTRef = isKTRef;
893
+ exports.isComputed = isComputed;
894
+ exports.isKT = isKT;
895
+ exports.isRef = isRef;
747
896
  exports.jsx = jsx;
748
897
  exports.jsxDEV = jsxDEV;
749
898
  exports.jsxs = jsxs;
750
899
  exports.ref = ref;
751
900
  exports.surfaceRef = surfaceRef;
901
+ exports.toRef = toRef;
752
902
 
753
903
  return exports;
754
904