@ai-slot/adapter-dom 0.1.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/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +19 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iannil
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @ai-slot/adapter-dom
|
|
2
|
+
|
|
3
|
+
DOM renderer adapter for [ai-slot-component](https://github.com/iannil/ai-slot-component#readme): map validated component trees to plain HTML elements — no framework required.
|
|
4
|
+
|
|
5
|
+
Text is always written via `textContent`, never `innerHTML`; unregistered components are skipped with a warning.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @ai-slot/adapter-dom
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { registerRenderer } from "@ai-slot/runtime";
|
|
17
|
+
import { createDomRenderer } from "@ai-slot/adapter-dom";
|
|
18
|
+
|
|
19
|
+
registerRenderer(
|
|
20
|
+
"dom",
|
|
21
|
+
createDomRenderer({
|
|
22
|
+
Banner: {
|
|
23
|
+
tag: "section",
|
|
24
|
+
class: "hero",
|
|
25
|
+
applyProps: (el, props) => {
|
|
26
|
+
/* write props onto the element */
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Docs
|
|
34
|
+
|
|
35
|
+
- [Root README](https://github.com/iannil/ai-slot-component#readme)
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Renderer } from '@ai-slot/runtime';
|
|
2
|
+
|
|
3
|
+
interface DomComponentDef {
|
|
4
|
+
/** 组件映射到的 HTML 标签 */
|
|
5
|
+
tag: string;
|
|
6
|
+
class?: string;
|
|
7
|
+
/** 写入 props。实现方必须用 textContent 写文本,禁止把 AI 文本塞进 innerHTML。 */
|
|
8
|
+
applyProps?: (el: HTMLElement, props: Record<string, unknown>) => void;
|
|
9
|
+
/** 子元素挂载点,默认组件元素本身 */
|
|
10
|
+
childrenTarget?: (el: HTMLElement) => HTMLElement;
|
|
11
|
+
}
|
|
12
|
+
/** 内置 DOM 渲染器:按注册表 tag 映射直接建 DOM。未注册组件跳过并警告。只消费默认槽位 children,命名槽位(ctx.slots)会被忽略。 */
|
|
13
|
+
declare function createDomRenderer(components: Record<string, DomComponentDef>): Renderer;
|
|
14
|
+
|
|
15
|
+
export { type DomComponentDef, createDomRenderer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/dom-renderer.ts
|
|
2
|
+
function createDomRenderer(components) {
|
|
3
|
+
return (node, ctx) => {
|
|
4
|
+
const def = Object.hasOwn(components, node.component) ? components[node.component] : void 0;
|
|
5
|
+
if (!def) {
|
|
6
|
+
console.warn(`[ai-slot] \u7EC4\u4EF6\u672A\u5728\u5BA2\u6237\u7AEF\u6CE8\u518C\uFF0C\u5DF2\u8DF3\u8FC7: ${node.component}`);
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const el = document.createElement(def.tag);
|
|
10
|
+
if (def.class) el.className = def.class;
|
|
11
|
+
def.applyProps?.(el, node.props ?? {});
|
|
12
|
+
const target = def.childrenTarget?.(el) ?? el;
|
|
13
|
+
for (const child of ctx.children) target.appendChild(child);
|
|
14
|
+
return el;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
createDomRenderer
|
|
19
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ai-slot/adapter-dom",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "DOM renderer adapter for ai-slot: map validated component trees to plain HTML elements.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"dom",
|
|
8
|
+
"web-components",
|
|
9
|
+
"ai",
|
|
10
|
+
"renderer"
|
|
11
|
+
],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/iannil/ai-slot-component.git",
|
|
25
|
+
"directory": "packages/adapter-dom"
|
|
26
|
+
},
|
|
27
|
+
"bugs": "https://github.com/iannil/ai-slot-component/issues",
|
|
28
|
+
"homepage": "https://github.com/iannil/ai-slot-component/tree/master/packages/adapter-dom#readme",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public",
|
|
31
|
+
"registry": "https://registry.npmjs.org/"
|
|
32
|
+
},
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@ai-slot/registry": "0.1.0",
|
|
36
|
+
"@ai-slot/runtime": "0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"jsdom": "^25.0.0",
|
|
40
|
+
"tsup": "^8.3.0",
|
|
41
|
+
"typescript": "^5.6.0",
|
|
42
|
+
"vitest": "^2.1.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|