@ktjs/core 0.18.8 → 0.19.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,7 +1,53 @@
1
+ // Cached native methods for performance optimization
2
+ const $isArray = Array.isArray;
3
+ const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
4
+
5
+ // Error handling utilities
1
6
  const $throw = (message) => {
2
- throw new Error('kt.js: ' + message);
7
+ throw new Error('@ktjs/shared: ' + message);
3
8
  };
4
9
 
10
+ // DOM manipulation utilities
11
+ /**
12
+ * & Remove `bind` because it is shockingly slower than wrapper
13
+ * & `window.document` is safe because it is not configurable and its setter is undefined
14
+ */
15
+ const $appendChild = HTMLElement.prototype.appendChild;
16
+ const originAppend = HTMLElement.prototype.append;
17
+ const $append = // for ie 9/10/11
18
+ typeof originAppend === 'function'
19
+ ? originAppend
20
+ : function (...nodes) {
21
+ if (nodes.length < 50) {
22
+ for (let i = 0; i < nodes.length; i++) {
23
+ const node = nodes[i];
24
+ if (typeof node === 'string') {
25
+ $appendChild.call(this, document.createTextNode(node));
26
+ }
27
+ else {
28
+ $appendChild.call(this, node);
29
+ }
30
+ }
31
+ }
32
+ else {
33
+ const fragment = document.createDocumentFragment();
34
+ for (let i = 0; i < nodes.length; i++) {
35
+ const node = nodes[i];
36
+ if (typeof node === 'string') {
37
+ $appendChild.call(fragment, document.createTextNode(node));
38
+ }
39
+ else {
40
+ $appendChild.call(fragment, node);
41
+ }
42
+ }
43
+ $appendChild.call(this, fragment);
44
+ }
45
+ };
46
+
47
+ // Shared utilities and cached native methods for kt.js framework
48
+ // Re-export all utilities
49
+ Object.defineProperty(window, '@ktjs/shared', { value: '0.19.0' });
50
+
5
51
  const booleanHandler = (element, key, value) => {
6
52
  if (key in element) {
7
53
  element[key] = !!value;
@@ -72,7 +118,7 @@ function attrIsObject(element, attr) {
72
118
  const o = attr[key];
73
119
  // normal event handler
74
120
  if (key.startsWith('on:')) {
75
- element.addEventListener(key.slice(3), o); // chop off the `@`
121
+ element.addEventListener(key.slice(3), o); // chop off the `on:`
76
122
  }
77
123
  // normal attributes
78
124
  else {
@@ -92,51 +138,6 @@ function applyAttr(element, attr) {
92
138
  }
93
139
  }
94
140
 
95
- /**
96
- * & Remove `bind` because it is shockingly slower than wrapper
97
- * & `window.document` is safe because it is not configurable and its setter is undefined
98
- */
99
- const $appendChild = HTMLElement.prototype.appendChild;
100
- const originAppend = HTMLElement.prototype.append;
101
- const $append = // for ie 9/10/11
102
- typeof originAppend === 'function'
103
- ? function (...args) {
104
- return originAppend.apply(this, args);
105
- }
106
- : function (...nodes) {
107
- if (nodes.length < 50) {
108
- for (let i = 0; i < nodes.length; i++) {
109
- const node = nodes[i];
110
- if (typeof node === 'string') {
111
- $appendChild.call(this, document.createTextNode(node));
112
- }
113
- else {
114
- $appendChild.call(this, node);
115
- }
116
- }
117
- }
118
- else {
119
- const fragment = document.createDocumentFragment();
120
- for (let i = 0; i < nodes.length; i++) {
121
- const node = nodes[i];
122
- if (typeof node === 'string') {
123
- $appendChild.call(fragment, document.createTextNode(node));
124
- }
125
- else {
126
- $appendChild.call(fragment, node);
127
- }
128
- }
129
- $appendChild.call(this, fragment);
130
- }
131
- };
132
-
133
- const $isArray = Array.isArray;
134
- const emptyPromiseHandler = () => ({});
135
- if (typeof Promise === 'undefined') {
136
- window.Promise = { resolve: emptyPromiseHandler, reject: emptyPromiseHandler };
137
- }
138
- const $isThenable = (o) => typeof o === 'object' && o !== null && typeof o.then === 'function';
139
-
140
141
  function apdSingle(element, c) {
141
142
  // & JSX should ignore false, undefined, and null
142
143
  if (c === false || c === undefined || c === null) {
@@ -199,7 +200,7 @@ const svgTempWrapper = document.createElement('div');
199
200
  * ## About
200
201
  * @package @ktjs/core
201
202
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
202
- * @version 0.18.8 (Last Update: 2026.01.31 14:28:10.930)
203
+ * @version 0.19.0 (Last Update: 2026.01.31 22:50:55.987)
203
204
  * @license MIT
204
205
  * @link https://github.com/baendlorel/kt.js
205
206
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -222,47 +223,60 @@ const h = (tag, attr, content) => {
222
223
  return element;
223
224
  };
224
225
 
226
+ class KTRef {
227
+ /**
228
+ * Indicates that this is a KTRef instance
229
+ */
230
+ isKT = true;
231
+ _value;
232
+ _onChanges;
233
+ constructor(_value, _onChanges) {
234
+ this._value = _value;
235
+ this._onChanges = _onChanges;
236
+ }
237
+ /**
238
+ * If new value and old value are both nodes, the old one will be replaced in the DOM
239
+ */
240
+ get value() {
241
+ return this._value;
242
+ }
243
+ set value(newValue) {
244
+ if (newValue === this._value) {
245
+ return;
246
+ }
247
+ // replace the old node with the new one in the DOM if both are nodes
248
+ if (this._value instanceof Node && newValue instanceof Node) {
249
+ if (newValue.contains(this._value)) {
250
+ this._value.remove();
251
+ }
252
+ this._value.replaceWith(newValue);
253
+ }
254
+ const oldValue = this._value;
255
+ this._value = newValue;
256
+ for (let i = 0; i < this._onChanges.length; i++) {
257
+ this._onChanges[i](newValue, oldValue);
258
+ }
259
+ }
260
+ addOnChange(callback) {
261
+ this._onChanges.push(callback);
262
+ }
263
+ removeOnChange(callback) {
264
+ for (let i = this._onChanges.length - 1; i >= 0; i--) {
265
+ if (this._onChanges[i] === callback) {
266
+ this._onChanges.splice(i, 1);
267
+ return true;
268
+ }
269
+ }
270
+ return false;
271
+ }
272
+ }
225
273
  /**
226
274
  * Reference to the created HTML element.
227
275
  * - can alse be used to store normal values, but it is not reactive.
228
276
  * @param value mostly an HTMLElement
229
277
  */
230
278
  function ref(value, onChange) {
231
- let _value = value;
232
- let _onChanges = [];
233
- return {
234
- isKT: true,
235
- get value() {
236
- return _value;
237
- },
238
- set value(newValue) {
239
- if (newValue === _value) {
240
- return;
241
- }
242
- // replace the old node with the new one in the DOM if both are nodes
243
- if (_value instanceof Node && newValue instanceof Node) {
244
- if (newValue.contains(_value)) {
245
- _value.remove();
246
- }
247
- _value.replaceWith(newValue);
248
- }
249
- const oldValue = _value;
250
- _value = newValue;
251
- for (let i = 0; i < _onChanges.length; i++) {
252
- _onChanges[i](newValue, oldValue);
253
- }
254
- },
255
- addOnChange: (callback) => _onChanges.push(callback),
256
- removeOnChange: (callback) => {
257
- for (let i = _onChanges.length - 1; i >= 0; i--) {
258
- if (_onChanges[i] === callback) {
259
- _onChanges.splice(i, 1);
260
- return true;
261
- }
262
- }
263
- return false;
264
- },
265
- };
279
+ return new KTRef(value, []);
266
280
  }
267
281
 
268
282
  const dummyRef = { value: null };
@@ -277,13 +291,13 @@ function jsx(tag, props = {}) {
277
291
  props = newProps ? { ...props, ...newProps } : props;
278
292
  el = jsx(tag, props);
279
293
  if (ref !== dummyRef) {
280
- ref.value = el; // ref setter automatically replaces old element in DOM
294
+ ref.value = el; // & ref setter automatically replaces old element in DOM
281
295
  }
282
296
  return el;
283
297
  };
284
298
  if ('k-if' in props && !props['k-if']) {
285
299
  el = document.createComment('k-if');
286
- ref.value = el;
300
+ ref.value = el; // & ref setter automatically replaces old element in DOM
287
301
  el.redraw = redraw;
288
302
  return el;
289
303
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktjs/core",
3
- "version": "0.18.8",
3
+ "version": "0.19.0",
4
4
  "description": "Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support",
5
5
  "type": "module",
6
6
  "module": "./dist/index.mjs",
@@ -43,6 +43,9 @@
43
43
  "url": "https://github.com/baendlorel/kt.js",
44
44
  "directory": "packages/core"
45
45
  },
46
+ "dependencies": {
47
+ "@ktjs/shared": "0.19.0"
48
+ },
46
49
  "scripts": {
47
50
  "build": "rollup -c rollup.config.mjs",
48
51
  "dev": "rollup -c rollup.config.mjs -w"