@lytjs/adapter-web 6.0.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.
- package/README.md +123 -0
- package/dist/index.cjs +1164 -0
- package/dist/index.d.cts +581 -0
- package/dist/index.d.ts +581 -0
- package/dist/index.mjs +1127 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# @lytjs/adapter-web
|
|
2
|
+
|
|
3
|
+
> Web 平台适配器,提供 DOM 操作、事件处理、样式管理等 Web 平台特定功能
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lytjs/adapter-web
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 核心功能
|
|
12
|
+
|
|
13
|
+
### WebRendererHost
|
|
14
|
+
|
|
15
|
+
Web 平台渲染器宿主实现,提供 DOM 操作接口:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { WebRendererHost } from '@lytjs/adapter-web';
|
|
19
|
+
|
|
20
|
+
const host = new WebRendererHost();
|
|
21
|
+
|
|
22
|
+
// 创建元素
|
|
23
|
+
const div = host.createElement('div');
|
|
24
|
+
|
|
25
|
+
// 设置文本
|
|
26
|
+
host.setText(div, 'Hello World');
|
|
27
|
+
|
|
28
|
+
// 插入子节点
|
|
29
|
+
host.insert(child, parent, null);
|
|
30
|
+
|
|
31
|
+
// 移除节点
|
|
32
|
+
host.remove(child);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### createDOMRenderer
|
|
36
|
+
|
|
37
|
+
创建 DOM 渲染器实例:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { createDOMRenderer } from '@lytjs/adapter-web';
|
|
41
|
+
|
|
42
|
+
const renderer = createDOMRenderer();
|
|
43
|
+
|
|
44
|
+
// 渲染 VNode 到容器
|
|
45
|
+
renderer.mount(vnode, document.getElementById('app'));
|
|
46
|
+
|
|
47
|
+
// 更新渲染
|
|
48
|
+
renderer.patch(oldVnode, newVnode);
|
|
49
|
+
|
|
50
|
+
// 卸载
|
|
51
|
+
renderer.unmount(vnode);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### DOM 操作工具
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import {
|
|
58
|
+
createElement,
|
|
59
|
+
createTextNode,
|
|
60
|
+
createComment,
|
|
61
|
+
setText,
|
|
62
|
+
setStyle,
|
|
63
|
+
addClass,
|
|
64
|
+
removeClass,
|
|
65
|
+
} from '@lytjs/adapter-web';
|
|
66
|
+
|
|
67
|
+
// 创建元素
|
|
68
|
+
const el = createElement('div');
|
|
69
|
+
|
|
70
|
+
// 设置样式
|
|
71
|
+
setStyle(el, { color: 'red', fontSize: '14px' });
|
|
72
|
+
|
|
73
|
+
// 操作 class
|
|
74
|
+
addClass(el, 'container');
|
|
75
|
+
removeClass(el, 'old-class');
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 事件处理
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { addEventListener, removeEventListener, removeAllEventListeners } from '@lytjs/adapter-web';
|
|
82
|
+
|
|
83
|
+
// 添加事件监听
|
|
84
|
+
const cleanup = addEventListener(el, 'click', (e) => {
|
|
85
|
+
console.log('clicked');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// 移除事件监听
|
|
89
|
+
cleanup();
|
|
90
|
+
|
|
91
|
+
// 移除元素上所有事件监听
|
|
92
|
+
removeAllEventListeners(el);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 属性操作
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { patchAttr, patchClass, patchStyle, patchProp } from '@lytjs/adapter-web';
|
|
99
|
+
|
|
100
|
+
// 设置属性
|
|
101
|
+
patchAttr(el, 'id', 'my-id');
|
|
102
|
+
|
|
103
|
+
// 设置 class
|
|
104
|
+
patchClass(el, 'class-a class-b');
|
|
105
|
+
|
|
106
|
+
// 设置样式
|
|
107
|
+
patchStyle(el, { color: 'blue' }, { color: 'red' });
|
|
108
|
+
|
|
109
|
+
// 设置 DOM 属性
|
|
110
|
+
patchProp(el, 'value', 'new value', 'old value');
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## 类型定义
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import type { WebRendererHost, DOMRenderer, RendererOptions } from '@lytjs/adapter-web';
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## 相关包
|
|
120
|
+
|
|
121
|
+
- [@lytjs/renderer](../renderer) - 渲染器实现
|
|
122
|
+
- [@lytjs/vdom](../vdom) - 虚拟 DOM
|
|
123
|
+
- [@lytjs/dom](../dom) - DOM 平台封装
|