kt.js 0.1.0 → 0.1.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/README.md +45 -2
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/README.zh.md +0 -109
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://github.com/baendlorel/kt.js/blob/main/LICENSE)
|
|
6
6
|
|
|
7
|
-
[
|
|
7
|
+
[CHANGLOG✨](CHANGELOG.md)
|
|
8
8
|
|
|
9
9
|
> Note: This framework is still under development. APIs, type declarations, and other parts **may change frequently**. If you use it, please watch for updates in the near future. Feel free to mail me if you have any questions!
|
|
10
10
|
|
|
@@ -45,7 +45,16 @@ This will create the following DOM structure:
|
|
|
45
45
|
</section>
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
If you give a function in the attributes, it will be treated as an event listener, and the key will be considered as the event name:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const button = btn({ click: () => alert('Clicked!') }, 'Click me');
|
|
52
|
+
// this equals
|
|
53
|
+
const button = btn(undefined, 'Click me');
|
|
54
|
+
button.addEventListener('click', () => alert('Clicked!'));
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Work with `@emotion/css`
|
|
49
58
|
|
|
50
59
|
```ts
|
|
51
60
|
import { css } from '@emotion/css';
|
|
@@ -58,6 +67,40 @@ const className = css`
|
|
|
58
67
|
h('div', { class: className }, 'Styled text');
|
|
59
68
|
```
|
|
60
69
|
|
|
70
|
+
## Router
|
|
71
|
+
|
|
72
|
+
KT.js includes a lightweight client-side router:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { createRouter, div, h1 } from 'kt.js';
|
|
76
|
+
|
|
77
|
+
const router = createRouter({
|
|
78
|
+
mode: 'hash', // or 'history'
|
|
79
|
+
container: document.getElementById('app'),
|
|
80
|
+
routes: [
|
|
81
|
+
{
|
|
82
|
+
path: '/',
|
|
83
|
+
handler: () => div({}, [h1({}, 'Home Page')]),
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
path: '/user/:id',
|
|
87
|
+
handler: (ctx) => div({}, [h1({}, `User ${ctx.params.id}`)]),
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
router.start();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**Features:**
|
|
96
|
+
|
|
97
|
+
- Hash and History API routing
|
|
98
|
+
- Dynamic route parameters
|
|
99
|
+
- Query string parsing
|
|
100
|
+
- Navigation guards
|
|
101
|
+
- Lazy loading support
|
|
102
|
+
- No dependencies
|
|
103
|
+
|
|
61
104
|
## Notes
|
|
62
105
|
|
|
63
106
|
- `call`, `apply` on `Function.prototype` is trusted.
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=document.createElement,e=HTMLElement.prototype.append,n=HTMLElement.prototype.addEventListener,o=HTMLElement.prototype.setAttribute,l=Array.isArray,a=Object.keys,c=Object.assign,d=t=>{throw new Error("Kt.js:"+t)},i=(t,e,n)=>{e in t?t[e]=!!n:o.call(t,e,n)},u=(t,e,n)=>{e in t?t[e]=n:o.call(t,e,n)},r={checked:i,selected:i,value:u,valueAsDate:u,valueAsNumber:u,defaultValue:u,defaultChecked:i,defaultSelected:i,disabled:i,readOnly:i,multiple:i,required:i,autofocus:i,open:i,controls:i,autoplay:i,loop:i,muted:i,defer:i,async:i,hidden:(t,e,n)=>t.hidden=!!n},s=(t,e,n)=>o.call(t,e,n);function f(t,e){"string"==typeof e?t.className=e:"object"==typeof e&&null!==e?function(t,e){const l=e.class,d=e.style;void 0!==l&&(t.className=l,delete e.class),void 0!==d&&("string"==typeof d?o.call(t,"style",d):c(t.style,d),delete e.style);const i=a(e);for(let o=i.length-1;o>=0;o--){const l=i[o],a=e[l];"function"!=typeof a?(r[l]||s)(t,l,a):n.call(t,l,a)}void 0!==l&&(e.style=l),void 0!==d&&(e.style=d)}(t,e):d("applyAttr attr must be an object.")}function p(n,o="",a=""){"string"!=typeof n&&d("h tagName must be a string.");const c=(i=n,t.call(document,i));var i;return f(c,o),function(t,n){"string"==typeof n?t.
|
|
1
|
+
const t=document.createElement,e=HTMLElement.prototype.append,n=HTMLElement.prototype.addEventListener,o=HTMLElement.prototype.setAttribute,l=Array.isArray,a=Object.keys,c=Object.assign,d=t=>{throw new Error("Kt.js:"+t)},i=(t,e,n)=>{e in t?t[e]=!!n:o.call(t,e,n)},u=(t,e,n)=>{e in t?t[e]=n:o.call(t,e,n)},r={checked:i,selected:i,value:u,valueAsDate:u,valueAsNumber:u,defaultValue:u,defaultChecked:i,defaultSelected:i,disabled:i,readOnly:i,multiple:i,required:i,autofocus:i,open:i,controls:i,autoplay:i,loop:i,muted:i,defer:i,async:i,hidden:(t,e,n)=>t.hidden=!!n},s=(t,e,n)=>o.call(t,e,n);function f(t,e){"string"==typeof e?t.className=e:"object"==typeof e&&null!==e?function(t,e){const l=e.class,d=e.style;void 0!==l&&(t.className=l,delete e.class),void 0!==d&&("string"==typeof d?o.call(t,"style",d):c(t.style,d),delete e.style);const i=a(e);for(let o=i.length-1;o>=0;o--){const l=i[o],a=e[l];"function"!=typeof a?(r[l]||s)(t,l,a):n.call(t,l,a)}void 0!==l&&(e.style=l),void 0!==d&&(e.style=d)}(t,e):d("applyAttr attr must be an object.")}function p(n,o="",a=""){"string"!=typeof n&&d("h tagName must be a string.");const c=(i=n,t.call(document,i));var i;return f(c,o),function(t,n){"string"==typeof n?t.textContent=n:l(n)?e.apply(t,n):n instanceof HTMLElement?e.call(t,n):d("applyContent invalid content.")}(c,a),c}const m=t=>(e,n)=>p(t,e,n),y=m("div"),h=m("span"),b=m("label"),v=m("p"),g=m("h1"),j=m("h2"),E=m("h3"),A=m("h4"),H=m("h5"),L=m("h6"),M=m("ul"),T=m("ol"),O=m("li"),k=m("button"),w=m("form"),C=m("input"),N=m("select"),q=m("option"),x=m("table"),D=m("thead"),K=m("tbody"),S=m("tr"),V=m("th"),z=m("td"),B=m("a"),F=m("img");export{B as a,k as btn,y as div,w as form,p as h,g as h1,j as h2,E as h3,A as h4,H as h5,L as h6,F as img,C as input,b as label,O as li,T as ol,q as option,v as p,N as select,h as span,x as table,K as tbody,z as td,V as th,D as thead,S as tr,M as ul};
|
package/package.json
CHANGED
package/README.zh.md
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
# KT.js
|
|
2
|
-
|
|
3
|
-
[](https://github.com/baendlorel/kt.js/blob/main/LICENSE)
|
|
4
|
-
|
|
5
|
-
[中文](README.zh.md) | [English](README.md) | [CHANGLOG](CHANGELOG.md)
|
|
6
|
-
|
|
7
|
-
> 注意:本框架仍在开发中。API、类型声明及其它部分可能会经常变更。若在生产中使用,请关注后续更新;有问题也欢迎发邮件联系我。
|
|
8
|
-
|
|
9
|
-
KT.js 是一个极简、高性能的 DOM 工具库,专注于最小化重渲染和直接的 DOM 操作。它避免不必要的强制重渲染,力求将 DOM 更新控制在最少范围,从而获得最佳性能。
|
|
10
|
-
|
|
11
|
-
更多优秀的项目请访问作者主页:[我的主页💛](https://baendlorel.github.io/?repoType=npm)
|
|
12
|
-
|
|
13
|
-
## 设计理念
|
|
14
|
-
|
|
15
|
-
作为一个前端工具库,频繁创建大量变量与对象会对浏览器及 JS 引擎(如 V8、SpiderMonkey 或 JSC)造成负担。因此我设计了 KT.js。
|
|
16
|
-
|
|
17
|
-
KT.js 遵循一条简单规则:避免不必要的重绘(repaint)。它直接并且只在必要的位置更新 DOM —— 无虚拟 DOM、无自动 diff。这样可以让 UI 更可预测且更快,同时把控制权交还给开发者。
|
|
18
|
-
|
|
19
|
-
- 直接操作 DOM:仅更新发生变化的部分。
|
|
20
|
-
- 最小化更新:避免不必要的重排(reflow)与重绘(repaint)。
|
|
21
|
-
- 可预测且高性能:界面稳定、响应迅速。
|
|
22
|
-
- 开发者掌控:你决定何时更新。
|
|
23
|
-
|
|
24
|
-
## 快速开始
|
|
25
|
-
|
|
26
|
-
推荐使用 pnpm 安装,但也支持 npm:
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
npm install kt.js
|
|
30
|
-
# or
|
|
31
|
-
pnpm add kt.js
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
在项目中引入并使用:
|
|
35
|
-
|
|
36
|
-
```ts
|
|
37
|
-
import { h, createApp } from 'kt.js';
|
|
38
|
-
|
|
39
|
-
const app = h('div', { id: 'app' }, 'Hello, KT.js!');
|
|
40
|
-
|
|
41
|
-
createApp(app);
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
## API 概览
|
|
45
|
-
|
|
46
|
-
- `h` — 创建一个加强版的HTML元素。
|
|
47
|
-
- 常见用法:`h('div', { id: 'root' }, 'Hello')`。
|
|
48
|
-
- 返回一个 `HTMLKElement`(即带有额外 enhance 属性的 HTMLElement,详见下文)。
|
|
49
|
-
- 提供一组别名,例如 `div`、`span`、`ul` 等。
|
|
50
|
-
- `div(attr, content)` 等价于 `h('div', attr, content)`。
|
|
51
|
-
|
|
52
|
-
- `createApp` — 将根元素挂载到文档中。
|
|
53
|
-
- 默认查找 `#app`,找不到时回退到 `document.body`,也可以通过`mountTo`入参指定挂载位置。
|
|
54
|
-
- 函数签名:`createApp(rootElement: HTMLKElement, mountTo?: HTMLElement)`
|
|
55
|
-
|
|
56
|
-
** 与 `@emotion/css` 一起使用 **
|
|
57
|
-
|
|
58
|
-
> 早期我实现过自己的 CSS 处理器,但后来发现没必要。
|
|
59
|
-
|
|
60
|
-
示例:
|
|
61
|
-
|
|
62
|
-
```ts
|
|
63
|
-
import { css } from '@emotion/css';
|
|
64
|
-
|
|
65
|
-
const className = css`
|
|
66
|
-
color: red;
|
|
67
|
-
font-size: 20px;
|
|
68
|
-
`;
|
|
69
|
-
|
|
70
|
-
h('div', { class: className }, 'Styled text');
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Enhance 增强后新增的属性与方法
|
|
74
|
-
|
|
75
|
-
当对一个元素调用 `enhance(element)`(`h` 在需要时会在内部完成该步骤)后,HTMLElement 会变为 `HTMLKElement`,并添加如下扩展:
|
|
76
|
-
|
|
77
|
-
- 属性
|
|
78
|
-
- `ktext`(字符串):getter/setter,代理到存放在元素内部的 Text 节点(用于读写元素文本内容)。
|
|
79
|
-
- 该 Text 节点由 WeakMap 存储,因此通常无法移除它。将 `ktext` 设为空字符串 `''`可以让它看不见。
|
|
80
|
-
- `kchildren`(数组):getter/setter,将元素的子节点以字符串或增强元素数组的形式暴露。设置 `kchildren` 会替换元素的内容,接受字符串、Text 节点或其它 KT.js 的增强元素。
|
|
81
|
-
|
|
82
|
-
- 方法
|
|
83
|
-
- `kon(type, listener, options?)` — 增强版的 addEventListener
|
|
84
|
-
- 规范化事件选项。支持 `triggerLimit` 选项:当事件触发次数达到限制时会自动移除监听器(若 `triggerLimit === 1` 则等同于 `once`)。
|
|
85
|
-
- 返回实际注册的监听器(可能是包裹器以支持触发次数限制)。
|
|
86
|
-
- **重要提示**:当使用 `triggerLimit` 时,`kon` 会返回一个新的包装函数而不是原始监听器。在调用 `koff` 时必须使用这个返回值才能正确移除监听器。示例:
|
|
87
|
-
```ts
|
|
88
|
-
const wrappedHandler = element.kon('click', handler, { triggerLimit: 3 });
|
|
89
|
-
// 稍后要移除时:
|
|
90
|
-
element.koff('click', wrappedHandler); // 使用 wrappedHandler,而不是原始的 handler
|
|
91
|
-
```
|
|
92
|
-
- `koff(type, listener, options?)` — 增强版的 removeEventListener
|
|
93
|
-
- 在移除监听器时会考虑传入的选项。
|
|
94
|
-
- `kmount(element)` — 挂载/追加助手
|
|
95
|
-
- 等价于 `element.appendChild(this)`,并返回 `this` 以便链式调用。
|
|
96
|
-
|
|
97
|
-
## 说明与注意事项
|
|
98
|
-
|
|
99
|
-
- 本库直接操作 DOM,并有意将重渲染控制到最小。
|
|
100
|
-
- API 设计小而精,偏底层 —— 目标是作为构建高性能 UI 的基础工具,而不是完整的组件框架。
|
|
101
|
-
- 本库假设`Function.prototype` 上的 `call`, `apply` 未被改动过。
|
|
102
|
-
|
|
103
|
-
## 参与贡献
|
|
104
|
-
|
|
105
|
-
欢迎提交 PR 与 issue。如果你的改动会影响公共 API,请同时更新 README 与相应的测试。
|
|
106
|
-
|
|
107
|
-
## 许可证
|
|
108
|
-
|
|
109
|
-
MIT
|