Skip to content

Commit 0cf3f70

Browse files
committed
feat(model-manager): fix bugs
1 parent 5609a2e commit 0cf3f70

10 files changed

Lines changed: 350 additions & 3 deletions

File tree

packages/builtinComponent/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ export { default as CanvasRow } from './src/components/CanvasRow.vue'
33
export { default as CanvasRowColContainer } from './src/components/CanvasRowColContainer.vue'
44
export { default as CanvasFlexBox } from './src/components/CanvasFlexBox.vue'
55
export { default as CanvasSection } from './src/components/CanvasSection.vue'
6+
export { default as FormModel } from './src/components/BaseForm.vue'
7+
export { default as TableModel } from './src/components/BaseTable.vue'
8+
export { default as PageModel } from './src/components/BasePage.vue'
69
export { default as meta } from './src/meta'

packages/builtinComponent/src/meta/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import CanvasRow from './CanvasRow.json'
33
import CanvasRowColContainer from './CanvasRowColContainer.json'
44
import CanvasFlexBox from './CanvasFlexBox.json'
55
import CanvasSection from './CanvasSection.json'
6+
import BaseForm from './BaseForm.json'
7+
import BaseTable from './BaseTable.json'
8+
import BasePage from './BasePage.json'
69

710
export default {
811
components: [
912
CanvasCol.component,
1013
CanvasRow.component,
1114
CanvasRowColContainer.component,
1215
CanvasFlexBox.component,
13-
CanvasSection.component
16+
CanvasSection.component,
17+
BaseForm.component,
18+
BaseTable.component,
19+
BasePage.component
1420
],
1521
snippets: [
1622
{
@@ -19,6 +25,13 @@ export default {
1925
zh_CN: '布局与容器'
2026
},
2127
children: [CanvasRowColContainer.snippet, CanvasFlexBox.snippet, CanvasSection.snippet]
28+
},
29+
{
30+
group: 'model',
31+
label: {
32+
zh_CN: '模型组件'
33+
},
34+
children: [BaseForm.snippet, BaseTable.snippet, BasePage.snippet]
2235
}
2336
]
2437
}

packages/canvas/render/src/material-function/material-getter.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
CanvasCol,
88
CanvasRowColContainer,
99
CanvasFlexBox,
10-
CanvasSection
10+
CanvasSection,
11+
FormModel,
12+
TableModel,
13+
PageModel
1114
} from '@opentiny/tiny-engine-builtin-component'
1215
import {
1316
CanvasBox,
@@ -38,6 +41,9 @@ export const Mapper = {
3841
CanvasCol,
3942
CanvasRowColContainer,
4043
CanvasPlaceholder,
44+
FormModel,
45+
TableModel,
46+
PageModel,
4147
RouterView: CanvasRouterView,
4248
RouterLink: CanvasRouterLink
4349
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { z } from 'zod'
2+
import useTranslate from '../useTranslate'
3+
import { createOutputSchema, createSuccessResponse, createErrorResponse } from './commonSchema'
4+
5+
// 定义为普通对象,用于传递给 inputSchema 字段
6+
const inputSchema = z.object({
7+
key: z.string().describe('The unique key for the i18n entry, e.g. lowcode.36223242'),
8+
zh_CN: z.string().describe('The Chinese translation text'),
9+
en_US: z.string().describe('The English translation text')
10+
})
11+
12+
// 定义 data 部分的 Schema(用于新增的 i18n 条目数据)
13+
const addI18nDataSchema = z.object({
14+
key: z.string().describe('The unique key of the created entry'),
15+
zh_CN: z.string().describe('The Chinese translation text'),
16+
en_US: z.string().describe('The English translation text'),
17+
type: z.string().describe('The type of the entry')
18+
})
19+
20+
// 输出schema定义 - 使用 Zod 版本的统一输出结构
21+
const outputSchema = createOutputSchema(addI18nDataSchema)
22+
23+
export const addI18n = {
24+
name: 'add_i18n',
25+
title: '新增 I18n 词条',
26+
description:
27+
'Add a new i18n entry to the current TinyEngine low-code application. Use this when you need to add new internationalization translations to your application.',
28+
inputSchema: inputSchema.shape,
29+
outputSchema: outputSchema.shape, // 使用 Zod 版本的统一输出结构
30+
annotations: {
31+
title: 'Add I18n Entry',
32+
readOnlyHint: false,
33+
destructiveHint: false,
34+
idempotentHint: true,
35+
openWorldHint: false
36+
},
37+
callback: async (args: z.infer<typeof inputSchema>) => {
38+
const { key, zh_CN, en_US } = args
39+
const { getLangs, ensureI18n } = useTranslate()
40+
const langs = getLangs() as Record<string, any>
41+
42+
if (langs[key]) {
43+
// 错误情况:key已存在 - 使用通用错误响应
44+
return createErrorResponse('I18n key already exists', `Key "${key}" is already defined in the i18n dictionary`)
45+
}
46+
47+
try {
48+
await ensureI18n(
49+
{
50+
en_US,
51+
key,
52+
type: 'i18n',
53+
zh_CN
54+
},
55+
true
56+
)
57+
58+
// 成功情况 - 使用通用成功响应
59+
return createSuccessResponse('I18n entry created successfully', {
60+
key,
61+
zh_CN,
62+
en_US,
63+
type: 'i18n'
64+
})
65+
} catch (error) {
66+
// 处理执行过程中的异常 - 使用通用错误响应
67+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
68+
return createErrorResponse('Failed to create i18n entry', errorMessage)
69+
}
70+
}
71+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { z } from 'zod'
2+
import useTranslate from '../useTranslate'
3+
import { createOutputSchema, createSuccessResponse, createErrorResponse } from './commonSchema'
4+
5+
// 定义为普通对象,用于传递给 inputSchema 字段
6+
const inputSchema = z.object({
7+
key: z.string().describe('The unique key for the i18n entry to delete')
8+
})
9+
10+
// 定义 data 部分的 Schema(用于删除的 i18n 条目数据)
11+
const delI18nDataSchema = z.object({
12+
key: z.string().describe('The unique key of the deleted entry'),
13+
deletedEntry: z.record(z.any()).describe('The deleted i18n entry data')
14+
})
15+
16+
// 输出schema定义 - 使用 Zod 版本的统一输出结构
17+
const outputSchema = createOutputSchema(delI18nDataSchema)
18+
19+
export const delI18n = {
20+
name: 'delete_i18n',
21+
title: '删除 I18n 词条',
22+
description:
23+
'Delete an existing i18n entry from the current TinyEngine low-code application. Use this when you need to remove internationalization translations.',
24+
inputSchema: inputSchema.shape,
25+
outputSchema: outputSchema.shape,
26+
annotations: {
27+
title: 'Delete I18n Entry',
28+
readOnlyHint: false,
29+
destructiveHint: true,
30+
idempotentHint: true,
31+
openWorldHint: false
32+
},
33+
callback: async (args: z.infer<typeof inputSchema>) => {
34+
const { key } = args
35+
const { getLangs, removeI18n } = useTranslate()
36+
const langs = getLangs() as Record<string, any>
37+
38+
if (!langs[key]) {
39+
// 错误情况:key不存在 - 使用通用错误响应
40+
return createErrorResponse('I18n key not found', `Key "${key}" does not exist in the i18n dictionary`)
41+
}
42+
43+
try {
44+
const deletedEntry = langs[key]
45+
46+
// removeI18n expects an array of keys
47+
;(removeI18n as (keys: string[]) => void)([key])
48+
49+
// 成功情况 - 使用通用成功响应
50+
return createSuccessResponse('I18n entry deleted successfully', {
51+
key,
52+
deletedEntry
53+
})
54+
} catch (error) {
55+
// 处理执行过程中的异常 - 使用通用错误响应
56+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
57+
return createErrorResponse('Failed to delete i18n entry', errorMessage)
58+
}
59+
}
60+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { z } from 'zod'
2+
import useTranslate from '../useTranslate'
3+
import { createOutputSchema, createSuccessResponse, createErrorResponse } from './commonSchema'
4+
5+
// 定义为普通对象,用于传递给 inputSchema 字段,key是可选的
6+
const inputSchema = z.object({
7+
key: z
8+
.string()
9+
.optional()
10+
.describe(
11+
'The unique key for the i18n entry to retrieve (optional). If provided, returns specific entry; if omitted, returns all entries.'
12+
)
13+
})
14+
15+
// 定义 data 部分的 Schema(用于 i18n 条目数据)
16+
const i18nDataSchema = z.object({
17+
entries: z.record(z.any()).describe('I18n entries object containing key-value pairs'),
18+
count: z.number().describe('Total number of entries returned')
19+
})
20+
21+
// 输出schema定义 - 使用 Zod 版本的统一输出结构
22+
const outputSchema = createOutputSchema(i18nDataSchema)
23+
24+
export const getI18n = {
25+
name: 'get_i18n',
26+
title: '获取 I18n 词条',
27+
description:
28+
'Retrieve i18n entries from the current TinyEngine low-code application. Can get a specific entry by key or all entries if no key is provided.',
29+
inputSchema: inputSchema.shape,
30+
outputSchema: outputSchema.shape, // 使用 Zod 版本的统一输出结构
31+
annotations: {
32+
title: 'Get I18n Entries',
33+
readOnlyHint: true,
34+
openWorldHint: false
35+
},
36+
callback: async (args: z.infer<typeof inputSchema>) => {
37+
const { key } = args
38+
39+
try {
40+
const { getLangs } = useTranslate()
41+
const langs = getLangs() as Record<string, any>
42+
43+
// 如果提供了key,返回特定的i18n条目(仍使用统一格式)
44+
if (key) {
45+
if (!langs[key]) {
46+
// 错误情况:指定的key不存在
47+
return createErrorResponse('I18n key not found', `Key "${key}" does not exist in the i18n dictionary`)
48+
}
49+
50+
// 成功情况:返回指定的条目,使用统一的entries格式
51+
const singleEntryData = {
52+
entries: { [key]: langs[key] },
53+
count: 1
54+
}
55+
56+
return createSuccessResponse(`I18n entry for key "${key}" retrieved successfully`, singleEntryData)
57+
}
58+
59+
// 如果没有提供key,返回所有i18n条目
60+
const entryCount = Object.keys(langs).length
61+
62+
if (!entryCount) {
63+
// 成功情况:但没有找到任何条目
64+
return createSuccessResponse('No i18n entries found', {
65+
entries: {},
66+
count: 0
67+
})
68+
}
69+
70+
// 成功情况:返回所有条目
71+
const res = createSuccessResponse(`Retrieved ${entryCount} i18n entries`, {
72+
entries: langs,
73+
count: entryCount
74+
})
75+
76+
return res
77+
} catch (error) {
78+
// 处理执行过程中的异常
79+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
80+
return createErrorResponse('Failed to retrieve i18n entries', errorMessage)
81+
}
82+
}
83+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { z } from 'zod'
2+
import useTranslate from '../useTranslate'
3+
import { createOutputSchema, createSuccessResponse, createErrorResponse } from './commonSchema'
4+
5+
// 定义为普通对象,用于传递给 inputSchema 字段
6+
const inputSchema = z.object({
7+
key: z.string().describe('The unique key for the i18n entry to update'),
8+
zh_CN: z.string().optional().describe('The Chinese translation text (optional, only update if provided)'),
9+
en_US: z.string().optional().describe('The English translation text (optional, only update if provided)')
10+
})
11+
12+
// 定义 data 部分的 Schema(用于更新的 i18n 条目数据)
13+
const updateI18nDataSchema = z.object({
14+
key: z.string().describe('The unique key of the updated entry'),
15+
zh_CN: z.string().describe('The updated Chinese translation text'),
16+
en_US: z.string().describe('The updated English translation text'),
17+
type: z.string().describe('The type of the entry'),
18+
originalEntry: z.record(z.any()).describe('The original i18n entry before update')
19+
})
20+
21+
// 输出schema定义 - 使用 Zod 版本的统一输出结构
22+
const outputSchema = createOutputSchema(updateI18nDataSchema)
23+
24+
export const updateI18n = {
25+
name: 'update_i18n',
26+
title: '更新 I18n 词条',
27+
description:
28+
'Update an existing i18n entry in the current TinyEngine low-code application. Use this when you need to modify internationalization translations.',
29+
inputSchema: inputSchema.shape,
30+
outputSchema: outputSchema.shape,
31+
annotations: {
32+
title: 'Update I18n Entry',
33+
readOnlyHint: false,
34+
destructiveHint: false,
35+
idempotentHint: true,
36+
openWorldHint: false
37+
},
38+
callback: async (args: z.infer<typeof inputSchema>) => {
39+
const { key, zh_CN, en_US } = args
40+
// 验证至少有一个翻译字段
41+
const translationValidation = z
42+
.object({
43+
zh_CN: z.string().optional(),
44+
en_US: z.string().optional()
45+
})
46+
.safeParse(args)
47+
48+
if (!translationValidation.success) {
49+
// 直接返回验证错误,已经符合新的结构化格式
50+
return createErrorResponse(
51+
'Invalid translation fields',
52+
'At least one translation (zh_CN or en_US) must be provided'
53+
)
54+
}
55+
56+
const { getLangs, ensureI18n } = useTranslate()
57+
const langs = getLangs() as Record<string, any>
58+
59+
if (!langs[key]) {
60+
// 错误情况:key不存在 - 使用通用错误响应
61+
return createErrorResponse('I18n key not found', `Key "${key}" does not exist in the i18n dictionary`)
62+
}
63+
64+
try {
65+
// Get existing translations
66+
const existingEntry = langs[key]
67+
68+
// Update with new translations, keeping existing values for ones not provided
69+
const updatedEntry = {
70+
key,
71+
zh_CN: zh_CN || existingEntry.zh_CN,
72+
en_US: en_US || existingEntry.en_US,
73+
type: existingEntry.type
74+
}
75+
76+
await ensureI18n(updatedEntry, true)
77+
78+
// 成功情况 - 使用通用成功响应
79+
return createSuccessResponse('I18n entry updated successfully', {
80+
...updatedEntry,
81+
originalEntry: existingEntry
82+
})
83+
} catch (error) {
84+
// 处理执行过程中的异常 - 使用通用错误响应
85+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
86+
return createErrorResponse('Failed to update i18n entry', errorMessage)
87+
}
88+
}
89+
}

packages/plugins/model-manager/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"license": "MIT",
3030
"homepage": "https://opentiny.design/tiny-engine",
3131
"devDependencies": {
32+
"@opentiny/tiny-engine-vite-plugin-meta-comments": "workspace:*",
3233
"@vitejs/plugin-vue": "^5.1.2",
3334
"@vitejs/plugin-vue-jsx": "^4.0.1",
3435
"vite": "^5.4.2"

packages/plugins/model-manager/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default defineConfig({
2525
sourcemap: true,
2626
lib: {
2727
entry: path.resolve(__dirname, './index.ts'),
28-
name: 'plugin-page',
28+
name: 'plugin-model-manager',
2929
fileName: (_format, entryName) => `${entryName}.js`,
3030
formats: ['es']
3131
},

0 commit comments

Comments
 (0)