auto-gen-crud 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/API.md +130 -0
- package/README.md +234 -0
- package/dist/adapters/ant-design-vue.d.ts +8 -0
- package/dist/adapters/element-plus.d.ts +8 -0
- package/dist/adapters/index.d.ts +4 -0
- package/dist/adapters/index.js +7 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/naive.d.ts +8 -0
- package/dist/adapters/types.d.ts +162 -0
- package/dist/components/AutoCrud.vue.d.ts +398 -0
- package/dist/components/CopyText.vue.d.ts +6 -0
- package/dist/components/CrudModal.vue.d.ts +47 -0
- package/dist/components/DecodeText.vue.d.ts +8 -0
- package/dist/components/DynamicForm.vue.d.ts +63 -0
- package/dist/components/DynamicFormItem.vue.d.ts +20 -0
- package/dist/components/DynamicTable.vue.d.ts +30 -0
- package/dist/context.d.ts +36 -0
- package/dist/core/date-format-utils.d.ts +8 -0
- package/dist/core/form-runtime-cache.d.ts +27 -0
- package/dist/core/form-utils.d.ts +51 -0
- package/dist/core/object-utils.d.ts +43 -0
- package/dist/core/table-utils.d.ts +19 -0
- package/dist/core/types.d.ts +443 -0
- package/dist/generator/fields.d.ts +27 -0
- package/dist/generator/index.d.ts +14 -0
- package/dist/generator/index.js +740 -0
- package/dist/generator/index.js.map +1 -0
- package/dist/generator/openapi.d.ts +10 -0
- package/dist/generator/operations.d.ts +8 -0
- package/dist/generator/schema.d.ts +32 -0
- package/dist/generator/string-utils.d.ts +11 -0
- package/dist/generator/templates.d.ts +6 -0
- package/dist/generator/types.d.ts +185 -0
- package/dist/generator/writer.d.ts +8 -0
- package/dist/hooks/index.d.ts +3 -0
- package/dist/hooks/index.js +7 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useModalForm.d.ts +11 -0
- package/dist/hooks/useSearchForm.d.ts +10 -0
- package/dist/hooks/useTable.d.ts +11 -0
- package/dist/index-Dc-solHn.js +299 -0
- package/dist/index-Dc-solHn.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +985 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +17 -0
- package/dist/style.css +1 -0
- package/dist/useTable-BwDSqD1q.js +214 -0
- package/dist/useTable-BwDSqD1q.js.map +1 -0
- package/dist/utils/clipboard.d.ts +16 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +532 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/message.d.ts +9 -0
- package/dist/utils/request.d.ts +8 -0
- package/dist/utils/sm4-core.d.ts +12 -0
- package/dist/utils/sm4.d.ts +29 -0
- package/docs/AI_AGENT_GUIDE.md +210 -0
- package/mcp/auto-crud-mcp.mjs +502 -0
- package/mcp/auto-gen-crud-mcp.mjs +586 -0
- package/package.json +93 -0
- package/skills/auto-gen-crud-agent/SKILL.md +114 -0
package/API.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# auto-gen-crud API
|
|
2
|
+
|
|
3
|
+
## createAutoCrud
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
createAutoCrud(options?: AutoCrudInstallOptions)
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
用于初始化全局配置。UI 只在这里配置一次。
|
|
10
|
+
|
|
11
|
+
### AutoCrudInstallOptions
|
|
12
|
+
|
|
13
|
+
| 字段 | 类型 | 默认值 | 说明 |
|
|
14
|
+
| --- | --- | --- | --- |
|
|
15
|
+
| `ui` | `"element-plus" \| "naive" \| "ant-design-vue"` | `"element-plus"` | 选择当前项目使用的 UI。 |
|
|
16
|
+
| `adapter` | `CrudUIAdapter` | - | 高级自定义 Adapter,优先级高于 `ui`。 |
|
|
17
|
+
| `request` | `CrudRequestHandler` | - | 用户注入请求函数。 |
|
|
18
|
+
| `decode` | `CrudDecodeHandler` | - | 敏感信息解密函数。 |
|
|
19
|
+
| `decodeLog` | `CrudDecodeLogHandler` | - | 敏感信息查看日志函数。 |
|
|
20
|
+
| `sm4Key` | `string` | - | 默认 SM4 密钥。 |
|
|
21
|
+
|
|
22
|
+
## useSearchForm
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const searchForm = useSearchForm(options)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
搜索表单状态 hook。它只管理查询条件,不直接请求列表。
|
|
29
|
+
|
|
30
|
+
### 常用字段
|
|
31
|
+
|
|
32
|
+
| 字段 | 说明 |
|
|
33
|
+
| --- | --- |
|
|
34
|
+
| `columns` | 搜索字段配置。 |
|
|
35
|
+
| `data` | 初始搜索数据。 |
|
|
36
|
+
| `onSubmit` | 搜索提交回调。 |
|
|
37
|
+
| `onReset` | 搜索重置回调。 |
|
|
38
|
+
| `addButton` | 是否显示默认新增按钮。 |
|
|
39
|
+
| `addButtonText` | 默认新增按钮文案。 |
|
|
40
|
+
|
|
41
|
+
## useTable
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
const table = useTable(options)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
表格状态 hook。它负责数据、分页、loading、tabs 和业务查询函数。
|
|
48
|
+
|
|
49
|
+
### getDataList
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
async function getDataList(context) {
|
|
53
|
+
const { pagination, searchData, tabsValue } = context;
|
|
54
|
+
return {
|
|
55
|
+
data: [],
|
|
56
|
+
total: 0,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
设计原因:每个项目接口分页字段和返回结构都不同,所以新包不写死请求逻辑。
|
|
62
|
+
兼容旧写法:你仍然可以在 `getDataList` 内部直接修改 `table.data` 和 `table.pagination.total`。
|
|
63
|
+
|
|
64
|
+
## useModalForm
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const modalForm = useModalForm(options)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
弹窗表单状态 hook。它管理新增、编辑、查看三种模式。
|
|
71
|
+
|
|
72
|
+
### 暴露方法
|
|
73
|
+
|
|
74
|
+
| 方法 | 说明 |
|
|
75
|
+
| --- | --- |
|
|
76
|
+
| `openAdd(data?)` | 打开新增弹窗,并根据 columns 重建默认数据。 |
|
|
77
|
+
| `openEdit(row)` | 打开编辑弹窗,深拷贝行数据。 |
|
|
78
|
+
| `openView(row)` | 打开查看弹窗,深拷贝行数据。 |
|
|
79
|
+
| `close()` | 关闭弹窗,并调用用户传入的关闭回调。 |
|
|
80
|
+
| `setFormData(prop, value)` | 按字段路径设置表单值。 |
|
|
81
|
+
|
|
82
|
+
## CrudFormItem
|
|
83
|
+
|
|
84
|
+
表单字段 schema。
|
|
85
|
+
|
|
86
|
+
| 字段 | 说明 |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| `label` | 字段标签。 |
|
|
89
|
+
| `prop` | 字段路径,支持 `user.name`。 |
|
|
90
|
+
| `component` | 推荐字段,表示中立组件类型。 |
|
|
91
|
+
| `valueKey` | 兼容旧字段。 |
|
|
92
|
+
| `value` | 初始值。 |
|
|
93
|
+
| `options` | 选项。 |
|
|
94
|
+
| `required` | 是否必填。 |
|
|
95
|
+
| `rules` | 自定义规则。 |
|
|
96
|
+
| `show` | 动态显隐。 |
|
|
97
|
+
| `disabled` | 动态禁用。 |
|
|
98
|
+
| `props` | 当前 UI 控件透传属性。 |
|
|
99
|
+
| `render` | 自定义渲染。 |
|
|
100
|
+
|
|
101
|
+
## CrudTableColumn
|
|
102
|
+
|
|
103
|
+
表格列 schema。
|
|
104
|
+
|
|
105
|
+
| 字段 | 说明 |
|
|
106
|
+
| --- | --- |
|
|
107
|
+
| `title` | 表头。 |
|
|
108
|
+
| `field` | 推荐字段,字段路径。 |
|
|
109
|
+
| `key` | 兼容旧字段。 |
|
|
110
|
+
| `render` | 自定义单元格。 |
|
|
111
|
+
| `actions` | 操作按钮。 |
|
|
112
|
+
| `buttons` | 兼容旧字段。 |
|
|
113
|
+
| `copy` | 复制展示。 |
|
|
114
|
+
| `decode` | 敏感信息展示。 |
|
|
115
|
+
| `image` | 图片展示。 |
|
|
116
|
+
| `dateTime` | 日期格式化。 |
|
|
117
|
+
|
|
118
|
+
## Adapter 学习重点
|
|
119
|
+
|
|
120
|
+
Adapter 的职责不是写业务,而是映射 UI 差异:
|
|
121
|
+
|
|
122
|
+
- 表单组件名称。
|
|
123
|
+
- 表单 v-model 字段。
|
|
124
|
+
- 表单校验方法。
|
|
125
|
+
- 表格 columns 结构。
|
|
126
|
+
- 分页字段。
|
|
127
|
+
- 弹窗显隐字段。
|
|
128
|
+
- 按钮、图片、消息提示。
|
|
129
|
+
|
|
130
|
+
因此 hooks、core types、业务 schema 都不需要依赖具体 UI 库。
|
package/README.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# auto-gen-crud
|
|
2
|
+
|
|
3
|
+
`auto-gen-crud` 是一套面向 Vue 3 的配置驱动 CRUD 组件、OpenAPI 页面生成器和 AI agent 辅助工具。
|
|
4
|
+
|
|
5
|
+
它的核心目标是:页面只写 schema 和业务函数,组件负责渲染搜索表单、表格和弹窗表单。
|
|
6
|
+
|
|
7
|
+
## 设计特点
|
|
8
|
+
|
|
9
|
+
- 默认使用 Element Plus。
|
|
10
|
+
- 初始化时可切换为 Naive UI 或 Ant Design Vue。
|
|
11
|
+
- UI 只配置一次,业务字段里不写 `uiProps.naive`、`uiProps.elementPlus`、`uiProps.antdv`。
|
|
12
|
+
- hooks 不依赖任何 UI 库。
|
|
13
|
+
- core 类型不出现具体 UI 类型。
|
|
14
|
+
- 内置复制、解密展示、请求适配、SM4 工具,不依赖旧 `@libs/request`、`@libs/shared-components`、`@libs/utils`。
|
|
15
|
+
|
|
16
|
+
## 安装和初始化
|
|
17
|
+
|
|
18
|
+
默认 Element Plus:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createAutoCrud } from "auto-gen-crud";
|
|
22
|
+
import "auto-gen-crud/style.css";
|
|
23
|
+
|
|
24
|
+
app.use(createAutoCrud());
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
切换 Naive UI:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
app.use(createAutoCrud({ ui: "naive" }));
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
切换 Ant Design Vue:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
app.use(createAutoCrud({ ui: "ant-design-vue" }));
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
如果需要敏感信息解密:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
app.use(
|
|
43
|
+
createAutoCrud({
|
|
44
|
+
decode: async value => {
|
|
45
|
+
return String(value);
|
|
46
|
+
},
|
|
47
|
+
decodeLog: async (value, row) => {
|
|
48
|
+
console.log("记录查看日志", value, row);
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 最小示例
|
|
55
|
+
|
|
56
|
+
```vue
|
|
57
|
+
<script setup lang="ts">
|
|
58
|
+
import { AutoCrud, useModalForm, useSearchForm, useTable } from "auto-gen-crud";
|
|
59
|
+
|
|
60
|
+
const searchForm = useSearchForm({
|
|
61
|
+
columns: [
|
|
62
|
+
{ label: "名称", prop: "name", component: "input" },
|
|
63
|
+
{
|
|
64
|
+
label: "状态",
|
|
65
|
+
prop: "status",
|
|
66
|
+
component: "select",
|
|
67
|
+
options: [
|
|
68
|
+
{ label: "启用", value: 1 },
|
|
69
|
+
{ label: "禁用", value: 0 },
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const modalForm = useModalForm({
|
|
76
|
+
columns: [
|
|
77
|
+
{ label: "名称", prop: "name", component: "input", required: true },
|
|
78
|
+
{ label: "状态", prop: "status", component: "switch" },
|
|
79
|
+
],
|
|
80
|
+
async onSubmit(data, mode) {
|
|
81
|
+
console.log("提交", mode, data);
|
|
82
|
+
modalForm.close();
|
|
83
|
+
await table.load(searchForm.data);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const table = useTable({
|
|
88
|
+
columns: [
|
|
89
|
+
{ title: "名称", field: "name", copy: true },
|
|
90
|
+
{ title: "创建时间", field: "createdAt", dateTime: true },
|
|
91
|
+
{
|
|
92
|
+
title: "操作",
|
|
93
|
+
actions: [
|
|
94
|
+
{ key: "edit", label: "编辑", type: "primary", onClick: row => modalForm.openEdit(row) },
|
|
95
|
+
{ key: "delete", label: "删除", type: "danger", confirm: true, onClick: row => console.log(row) },
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
async getDataList({ pagination, searchData }) {
|
|
100
|
+
console.log("查询", pagination, searchData);
|
|
101
|
+
table.data = [];
|
|
102
|
+
if (table.pagination) table.pagination.total = 0;
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
</script>
|
|
106
|
+
|
|
107
|
+
<template>
|
|
108
|
+
<AutoCrud
|
|
109
|
+
title="用户"
|
|
110
|
+
:search-form-config="searchForm"
|
|
111
|
+
:table-config="table"
|
|
112
|
+
:modal-form-config="modalForm"
|
|
113
|
+
/>
|
|
114
|
+
</template>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## 本地 Demo
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
npm run dev
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
打开 `http://127.0.0.1:5173/` 可查看本地 demo。它使用原生轻量 adapter 测试组件逻辑,不额外依赖具体 UI 库。
|
|
124
|
+
Demo 内置轻量 hash 路由:
|
|
125
|
+
|
|
126
|
+
- `http://127.0.0.1:5173/#/crud`:CRUD 页面。
|
|
127
|
+
- `http://127.0.0.1:5173/#/guide`:说明页面。
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
npm run demo:smoke
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
可运行 demo 数据流冒烟测试,覆盖搜索条件、`useTable` 返回值接收和弹窗编辑数据隔离。
|
|
134
|
+
|
|
135
|
+
## 独立 Vue3 消费示例
|
|
136
|
+
|
|
137
|
+
`examples/vue3-consumer` 是一个独立 Vue3 项目,用本地 tarball 安装 `auto-gen-crud`,可切换 UI adapter 验证 npm 包入口、类型声明、样式导出和路由页面渲染。
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
npm run build
|
|
141
|
+
npm pack --pack-destination /private/tmp
|
|
142
|
+
cd examples/vue3-consumer
|
|
143
|
+
npm install --legacy-peer-deps --offline --cache /private/tmp/auto-gen-crud-consumer-npm-cache
|
|
144
|
+
npm run dev
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
打开实际终端输出的 Vite 地址,访问:
|
|
148
|
+
|
|
149
|
+
- `#/crud`:CRUD 页面。
|
|
150
|
+
- `#/guide`:说明页面。
|
|
151
|
+
|
|
152
|
+
## AI MCP
|
|
153
|
+
|
|
154
|
+
项目内置一个本地 stdio MCP server,供 AI 查询 `auto-gen-crud` 文档、API、schema 字段,并生成 CRUD 页面代码。
|
|
155
|
+
|
|
156
|
+
启动命令:
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
npm run mcp
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
发布到 npm 后,也可以直接使用:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
npx auto-gen-crud-mcp
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
MCP 客户端配置示例:
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"mcpServers": {
|
|
173
|
+
"auto-gen-crud": {
|
|
174
|
+
"command": "node",
|
|
175
|
+
"args": ["node_modules/auto-gen-crud/mcp/auto-gen-crud-mcp.mjs"]
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
工具列表:
|
|
182
|
+
|
|
183
|
+
- `vac_get_overview`:查询安装、示例、adapter、迁移说明。
|
|
184
|
+
- `vac_get_api_reference`:按 API 名称查询文档片段。
|
|
185
|
+
- `vac_get_agent_guide`:查询 VSCode AI agent 使用规范、工程边界和 prompt 建议。
|
|
186
|
+
- `vac_get_generator_reference`:查询 OpenAPI 生成器参数、输出目录和 requestAdapter 用法。
|
|
187
|
+
- `vac_list_schema_fields`:列出表单、表格、安装配置和组件类型字段。
|
|
188
|
+
- `vac_generate_crud_page`:根据字段生成 Vue3 CRUD 页面示例。
|
|
189
|
+
- `vac_generate_install_snippet`:生成指定 UI 的安装初始化代码。
|
|
190
|
+
- `vac_search_docs`:搜索 README、API 和核心类型文件。
|
|
191
|
+
|
|
192
|
+
更完整的 agent 接入说明见 `docs/AI_AGENT_GUIDE.md`。包内也提供 `skills/auto-gen-crud-agent/SKILL.md`,可复制到 Codex / 自研 VSCode AI agent 的 skills 目录中使用。
|
|
193
|
+
|
|
194
|
+
## 为什么要用 Adapter
|
|
195
|
+
|
|
196
|
+
不同 UI 的同一个能力写法不同:
|
|
197
|
+
|
|
198
|
+
- Element Plus 表单 v-model 是 `modelValue/onUpdate:modelValue`。
|
|
199
|
+
- Naive UI 表单 v-model 常用 `value/onUpdate:value`。
|
|
200
|
+
- Ant Design Vue 弹窗显示是 `open/onUpdate:open`。
|
|
201
|
+
|
|
202
|
+
Adapter 把这些差异集中起来,业务 schema 不需要关心。
|
|
203
|
+
|
|
204
|
+
## 从旧 auto-gen-crud 迁移
|
|
205
|
+
|
|
206
|
+
旧字段:
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
{ label: "名称", prop: "name", valueKey: "input" }
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
新字段推荐:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
{ label: "名称", prop: "name", component: "input" }
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
旧表格列:
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
{ title: "名称", key: "name", buttons: [] }
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
新表格列推荐:
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
{ title: "名称", field: "name", actions: [] }
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
新包仍兼容 `valueKey`、`key`、`buttons`,但文档和新代码推荐使用 `component`、`field`、`actions`。
|
|
231
|
+
|
|
232
|
+
npm_config_cache=/private/tmp/auto-gen-crud-npm-cache npm pack --pack-destination /private/tmp
|
|
233
|
+
|
|
234
|
+
npm install ../../auto-gen-crud-0.1.0.tgz --no-audit --no-fund
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CrudUIAdapter } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Ant Design Vue Adapter。
|
|
4
|
+
*
|
|
5
|
+
* 与 Element Plus 最大差异:Drawer/Modal 使用 `open/onUpdate:open`,
|
|
6
|
+
* Table 更偏对象 columns + `customRender`,按钮危险态使用 `danger` 布尔值。
|
|
7
|
+
*/
|
|
8
|
+
export declare const antDesignVueAdapter: CrudUIAdapter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Component, VNodeChild } from 'vue';
|
|
2
|
+
import { CrudButtonSize, CrudButtonType, CrudFormComponent, CrudFormItem, CrudFormLayout, CrudPagination, CrudTableAction, CrudTableColumn, CrudUIName } from '../core/types';
|
|
3
|
+
/** 组件解析结果:可以是全局注册的组件名,也可以是自定义 adapter 返回的组件对象。 */
|
|
4
|
+
export type CrudAdapterComponent = string | Component;
|
|
5
|
+
/**
|
|
6
|
+
* Adapter 中维护的组件名称映射。
|
|
7
|
+
*
|
|
8
|
+
* 本包不静态 import Element Plus、Naive UI、Ant Design Vue,
|
|
9
|
+
* 因此这里默认返回全局组件名,例如 `ElInput`、`NInput`、`AInput`。
|
|
10
|
+
*/
|
|
11
|
+
export interface CrudAdapterComponents {
|
|
12
|
+
card: CrudAdapterComponent;
|
|
13
|
+
form: CrudAdapterComponent;
|
|
14
|
+
formItem: CrudAdapterComponent;
|
|
15
|
+
button: CrudAdapterComponent;
|
|
16
|
+
table: CrudAdapterComponent;
|
|
17
|
+
tableColumn?: CrudAdapterComponent;
|
|
18
|
+
pagination: CrudAdapterComponent;
|
|
19
|
+
drawer: CrudAdapterComponent;
|
|
20
|
+
/** 部分 UI 的抽屉需要内部内容组件承载 title/footer,例如 Naive UI 的 NDrawerContent。 */
|
|
21
|
+
drawerContent?: CrudAdapterComponent;
|
|
22
|
+
modal: CrudAdapterComponent;
|
|
23
|
+
popconfirm: CrudAdapterComponent;
|
|
24
|
+
image: CrudAdapterComponent;
|
|
25
|
+
tooltip?: CrudAdapterComponent;
|
|
26
|
+
fields: Record<CrudFormComponent, CrudAdapterComponent>;
|
|
27
|
+
option?: CrudAdapterComponent;
|
|
28
|
+
radio?: CrudAdapterComponent;
|
|
29
|
+
checkbox?: CrudAdapterComponent;
|
|
30
|
+
}
|
|
31
|
+
/** 表单字段 v-model 映射结果。 */
|
|
32
|
+
export interface CrudModelBinding {
|
|
33
|
+
/** 当前 UI 接收值的 prop 名。 */
|
|
34
|
+
prop: string;
|
|
35
|
+
/** 当前 UI 更新值的事件名。 */
|
|
36
|
+
event: string;
|
|
37
|
+
}
|
|
38
|
+
/** 表单 Adapter,集中处理三套 UI 的表单差异。 */
|
|
39
|
+
export interface CrudFormAdapter {
|
|
40
|
+
/** 获取表单根组件 props。 */
|
|
41
|
+
getFormProps: (input: {
|
|
42
|
+
model: Record<string, unknown>;
|
|
43
|
+
rules: Record<string, unknown>;
|
|
44
|
+
disabled?: boolean;
|
|
45
|
+
}) => Record<string, unknown>;
|
|
46
|
+
/** 获取表单项 props。 */
|
|
47
|
+
getFormItemProps: (item: CrudFormItem, input: {
|
|
48
|
+
prop: string;
|
|
49
|
+
label: string;
|
|
50
|
+
rules?: unknown[];
|
|
51
|
+
}) => Record<string, unknown>;
|
|
52
|
+
/** 获取字段组件 v-model 映射。 */
|
|
53
|
+
getModelBinding: (component: CrudFormComponent, item: CrudFormItem) => CrudModelBinding;
|
|
54
|
+
/** 获取字段组件 props。 */
|
|
55
|
+
getFieldProps: (item: CrudFormItem, input: {
|
|
56
|
+
value: unknown;
|
|
57
|
+
placeholder: string;
|
|
58
|
+
disabled: boolean;
|
|
59
|
+
options?: unknown[];
|
|
60
|
+
}) => Record<string, unknown>;
|
|
61
|
+
/** 获取表单布局样式变量。 */
|
|
62
|
+
getLayoutStyle: (layout?: CrudFormLayout, formType?: "search" | "modal") => Record<string, string | number>;
|
|
63
|
+
/** 触发表单校验。 */
|
|
64
|
+
validate: (formRef: unknown) => Promise<boolean>;
|
|
65
|
+
/** 清空表单校验。 */
|
|
66
|
+
clearValidate: (formRef: unknown) => void;
|
|
67
|
+
}
|
|
68
|
+
/** 表格 Adapter,集中处理 columns、分页和事件差异。 */
|
|
69
|
+
export interface CrudTableAdapter {
|
|
70
|
+
/** 当前 UI 是否优先使用对象 columns 渲染,Naive/Ant Design Vue 为 true,Element Plus 为 false。 */
|
|
71
|
+
useColumnConfig: boolean;
|
|
72
|
+
/** 获取表格根组件 props。 */
|
|
73
|
+
getTableProps: <T extends Record<string, unknown>>(input: {
|
|
74
|
+
columns: unknown[];
|
|
75
|
+
data: T[];
|
|
76
|
+
loading: boolean;
|
|
77
|
+
rowKey?: string | ((row: T) => string | number);
|
|
78
|
+
/** 纵向滚动高度,由 DynamicTable 计算后交给各 UI Adapter 映射成真实组件属性。 */
|
|
79
|
+
scrollY?: number | string;
|
|
80
|
+
scrollX?: number | string;
|
|
81
|
+
}) => Record<string, unknown>;
|
|
82
|
+
/** 获取表格列 props,用于 Element Plus 这类 TableColumn 组件。 */
|
|
83
|
+
getTableColumnProps: <T extends Record<string, unknown>>(column: CrudTableColumn<T>) => Record<string, unknown>;
|
|
84
|
+
/** 获取分页组件 props。 */
|
|
85
|
+
getPaginationProps: (pagination: CrudPagination) => Record<string, unknown>;
|
|
86
|
+
/** 获取分页事件。 */
|
|
87
|
+
getPaginationEvents: (handlers: {
|
|
88
|
+
onPageChange: (page: number) => void;
|
|
89
|
+
onPageSizeChange: (pageSize: number) => void;
|
|
90
|
+
}) => Record<string, unknown>;
|
|
91
|
+
}
|
|
92
|
+
/** 弹窗 Adapter,集中处理 drawer/modal 显隐字段差异。 */
|
|
93
|
+
export interface CrudModalAdapter {
|
|
94
|
+
/** 获取 drawer 或 modal 的组件 props。 */
|
|
95
|
+
getModalProps: (input: {
|
|
96
|
+
visible: boolean;
|
|
97
|
+
title: string;
|
|
98
|
+
width?: string | number;
|
|
99
|
+
onUpdateVisible: (visible: boolean) => void;
|
|
100
|
+
}) => Record<string, unknown>;
|
|
101
|
+
}
|
|
102
|
+
/** 基础渲染 Adapter,用于按钮、确认框、图片等小片段。 */
|
|
103
|
+
export interface CrudRenderAdapter {
|
|
104
|
+
/** 映射按钮 props。 */
|
|
105
|
+
getButtonProps: (input: {
|
|
106
|
+
type?: CrudButtonType;
|
|
107
|
+
size?: CrudButtonSize;
|
|
108
|
+
disabled?: boolean;
|
|
109
|
+
}) => Record<string, unknown>;
|
|
110
|
+
/** 映射确认框 props。 */
|
|
111
|
+
getPopconfirmProps: (input: {
|
|
112
|
+
title: string;
|
|
113
|
+
confirmText?: string;
|
|
114
|
+
cancelText?: string;
|
|
115
|
+
onConfirm: () => void;
|
|
116
|
+
}) => Record<string, unknown>;
|
|
117
|
+
/** 映射图片 props。 */
|
|
118
|
+
getImageProps: (input: {
|
|
119
|
+
src: string;
|
|
120
|
+
width: number;
|
|
121
|
+
height: number;
|
|
122
|
+
objectFit: string;
|
|
123
|
+
preview: boolean;
|
|
124
|
+
}) => Record<string, unknown>;
|
|
125
|
+
/** 渲染表格对象列时的 custom render 字段名。 */
|
|
126
|
+
customRenderKey: "render" | "customRender";
|
|
127
|
+
/** 将中立按钮动作转为当前 UI 友好的类型。 */
|
|
128
|
+
normalizeActionType: <T>(action: CrudTableAction<T>) => CrudButtonType;
|
|
129
|
+
}
|
|
130
|
+
/** 消息提示 Adapter。 */
|
|
131
|
+
export interface CrudFeedbackAdapter {
|
|
132
|
+
success: (message: string) => void;
|
|
133
|
+
warning: (message: string) => void;
|
|
134
|
+
error: (message: string) => void;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* UI Adapter 总接口。
|
|
138
|
+
*
|
|
139
|
+
* 学习重点:
|
|
140
|
+
* - hooks 不碰 UI。
|
|
141
|
+
* - 组件只问 adapter 当前 UI 组件叫什么、props 怎么映射、事件叫什么。
|
|
142
|
+
* - 这样同一份业务 schema 可以在初始化时切换到不同 UI。
|
|
143
|
+
*/
|
|
144
|
+
export interface CrudUIAdapter {
|
|
145
|
+
name: CrudUIName;
|
|
146
|
+
components: CrudAdapterComponents;
|
|
147
|
+
form: CrudFormAdapter;
|
|
148
|
+
table: CrudTableAdapter;
|
|
149
|
+
modal: CrudModalAdapter;
|
|
150
|
+
render: CrudRenderAdapter;
|
|
151
|
+
feedback: CrudFeedbackAdapter;
|
|
152
|
+
}
|
|
153
|
+
/** Adapter 工厂参数。 */
|
|
154
|
+
export type CrudAdapterFactory = () => CrudUIAdapter;
|
|
155
|
+
/** 表格对象列渲染上下文。 */
|
|
156
|
+
export interface CrudColumnRenderContext<T extends Record<string, unknown>> {
|
|
157
|
+
row: T;
|
|
158
|
+
column: CrudTableColumn<T>;
|
|
159
|
+
index: number;
|
|
160
|
+
value: unknown;
|
|
161
|
+
}
|
|
162
|
+
export type CrudCellRenderer<T extends Record<string, unknown>> = (context: CrudColumnRenderContext<T>) => VNodeChild;
|