Skip to content

Commit f97d2fe

Browse files
committed
fix: review suggestion
1 parent 1902b41 commit f97d2fe

2 files changed

Lines changed: 94 additions & 68 deletions

File tree

packages/toolbars/generate-code/src/FileSelector.vue

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
@close="$emit('cancel')"
1010
@open="initdialogBox"
1111
>
12-
<div class="mcp-switch-container">
13-
<span class="mcp-switch-label">启用 MCP 集成:</span>
14-
<tiny-switch v-model="mcpEnabled" @change="handleMcpChange"></tiny-switch>
15-
<span class="mcp-switch-tip">(开启后将生成 MCP 相关配置和工具代码)</span>
16-
</div>
1712
<div class="dialog-grid">
1813
<div class="tree-wrap">
1914
<tiny-tree
@@ -40,8 +35,17 @@
4035
</div>
4136
</div>
4237
<template #footer>
43-
<tiny-button type="primary" @click="confirm">确定</tiny-button>
44-
<tiny-button @click="$emit('cancel')">取消</tiny-button>
38+
<div class="dialog-footer">
39+
<div class="mcp-switch-container">
40+
<span class="mcp-switch-label">启用 MCP 集成:</span>
41+
<tiny-switch v-model="mcpEnabled" @change="handleMcpChange"></tiny-switch>
42+
<span class="mcp-switch-tip">(开启后将生成 MCP 相关配置和工具代码)</span>
43+
</div>
44+
<div class="dialog-footer-actions">
45+
<tiny-button @click="$emit('cancel')">取消</tiny-button>
46+
<tiny-button type="primary" @click="confirm">确定</tiny-button>
47+
</div>
48+
</div>
4549
</template>
4650
</tiny-dialog-box>
4751
</template>
@@ -117,6 +121,18 @@ export default {
117121
}
118122
const mcpEnabled = ref(props.enableMcp)
119123
124+
// 更新树的展开和选中状态
125+
const updateTreeState = (checkedKeys: string[]) => {
126+
if (!fileTreeRef.value) return
127+
128+
nextTick(() => {
129+
// 设置选中的节点
130+
fileTreeRef.value.setCheckedKeys(checkedKeys)
131+
// 展开所有节点
132+
fileTreeRef.value.expandAllNodes(true)
133+
})
134+
}
135+
120136
// 监听 props.enableMcp 的变化,同步到本地状态
121137
watch(
122138
() => props.enableMcp,
@@ -129,12 +145,22 @@ export default {
129145
watch(
130146
() => props.data,
131147
() => {
132-
nextTick(() => {
133-
if (gridRef.value) {
134-
gridRef.value.setAllTreeExpansion(true)
135-
gridRef.value.setAllSelection(true)
136-
}
137-
})
148+
if (fileTreeRef.value && props.treeData?.checkedTreeData) {
149+
const allKeys = ['all', ...props.treeData.checkedTreeData]
150+
updateTreeState(allKeys)
151+
}
152+
},
153+
{ deep: true }
154+
)
155+
156+
// 监听 props.treeData 的变化,更新树的选中状态
157+
watch(
158+
() => props.treeData,
159+
(newTreeData) => {
160+
if (newTreeData?.checkedTreeData && fileTreeRef.value) {
161+
const allKeys = ['all', ...newTreeData.checkedTreeData]
162+
updateTreeState(allKeys)
163+
}
138164
},
139165
{ deep: true }
140166
)
@@ -166,14 +192,14 @@ export default {
166192
if (currentPage) {
167193
const initCurrentNode: any = props.data.find((item: any) => item.fileName === `${currentPage.name}.vue`)
168194
// 同步 props 到本地状态
169-
mcpEnabled.value = props.enableMcp
195+
mcpEnabled.value = props.enableMcp
170196
171-
nextTick(() => {
172-
if (gridRef.value) {
197+
nextTick(() => {
198+
if (fileTreeRef.value && initCurrentNode) {
173199
fileContent.value = initCurrentNode.fileContent
174200
fileTreeRef.value.setCurrentKey(initCurrentNode.fileName)
175201
}
176-
})
202+
})
177203
}
178204
}
179205
@@ -197,23 +223,46 @@ export default {
197223
</script>
198224

199225
<style lang="less" scoped>
226+
.dialog-footer {
227+
width: 100%;
228+
display: flex;
229+
align-items: center;
230+
justify-content: space-between;
231+
gap: 16px;
232+
}
233+
234+
.dialog-footer-actions {
235+
display: flex;
236+
align-items: center;
237+
gap: 8px;
238+
flex-shrink: 0;
239+
240+
@media (max-width: 768px) {
241+
width: 100%;
242+
justify-content: flex-end;
243+
}
244+
}
245+
200246
.mcp-switch-container {
201247
display: flex;
202248
align-items: center;
203-
padding: 12px 0;
204-
margin-bottom: 12px;
205-
border-bottom: 1px solid var(--te-toolbars-generate-code-border-color, #ddd);
249+
gap: 8px;
250+
flex-wrap: wrap;
206251
207252
.mcp-switch-label {
208253
font-size: 14px;
209254
color: var(--te-toolbars-generate-code-text-color);
210-
margin-right: 8px;
255+
white-space: nowrap;
211256
}
212257
213258
.mcp-switch-tip {
214259
font-size: 12px;
215260
color: var(--te-toolbars-generate-code-text-color-secondary, #999);
216-
margin-left: 8px;
261+
line-height: 1.5;
262+
263+
@media (max-width: 768px) {
264+
flex-basis: 100%;
265+
}
217266
}
218267
}
219268
@@ -241,6 +290,9 @@ export default {
241290
}
242291
243292
.tiny-dialog-box__footer {
293+
display: flex;
294+
justify-content: space-around;
295+
244296
.tiny-button--primary {
245297
background-color: var(--te-toolbars-generate-code-bg-color-primary);
246298
border: none;

packages/toolbars/generate-code/src/Main.vue

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export default {
5555
showDialogbox: false,
5656
saveFilesInfo: [],
5757
saveFilesTree: [],
58-
enableMcp: false, // MCP 开关状态
59-
appSchemaCache: null // 缓存应用 schema,用于重新生成代码
58+
enableMcp: false,
59+
appSchemaCache: null
6060
})
6161
6262
const getParams = () => {
@@ -95,6 +95,15 @@ export default {
9595
9696
const { getAllNestedBlocksSchema, generateAppCode } = getMetaApi('engine.service.generateCode')
9797
98+
// 创建代码生成配置
99+
const createCodeGenOptions = (enableMcp: boolean) => ({
100+
pluginConfig: {
101+
mcp: {
102+
enabled: enableMcp
103+
}
104+
}
105+
})
106+
98107
const getAllPageDetails = async (pageList) => {
99108
const detailPromise = pageList.map(({ id }) => getMetaApi(META_APP.AppManage).getPageById(id))
100109
const detailList = await Promise.allSettled(detailPromise)
@@ -253,18 +262,10 @@ export default {
253262
// 缓存 appSchema,用于后续根据 MCP 开关重新生成
254263
state.appSchemaCache = appSchema
255264
256-
// 根据 MCP 开关状态配置出码选项
257-
const codeGenOptions = {
258-
pluginConfig: {
259-
mcp: {
260-
enabled: state.enableMcp
261-
}
262-
}
263-
}
264-
265-
const res = await generateAppCode(appSchema, codeGenOptions)
265+
const res = await generateAppCode(appSchema, createCodeGenOptions(state.enableMcp))
266266
267267
const { genResult = [] } = res || {}
268+
268269
// 将文件目录处理成树状结构
269270
const fileTreeInfo = fileListToTreeObject(genResult)
270271
@@ -358,48 +359,21 @@ export default {
358359
try {
359360
useNotify({ type: 'info', title: '正在重新生成代码...' })
360361
361-
// 深拷贝 schema,避免生成过程修改原始缓存
362+
// 深拷贝 schema,避免 generateAppCode 修改原始缓存
362363
const schemaForGeneration = JSON.parse(JSON.stringify(state.appSchemaCache))
363364
364-
// 根据新的 MCP 开关状态重新生成代码
365-
const codeGenOptions = {
366-
pluginConfig: {
367-
mcp: {
368-
enabled: state.enableMcp
369-
}
370-
}
371-
}
372-
373-
const res = await generateAppCode(schemaForGeneration, codeGenOptions)
365+
const res = await generateAppCode(schemaForGeneration, createCodeGenOptions(state.enableMcp))
374366
const { genResult = [] } = res || {}
375367
376-
const fileRes = genResult.map(({ fileContent, fileName, path, fileType }) => {
377-
const slash = path.endsWith('/') || path === '.' ? '' : '/'
378-
let filePath = `${path}${slash}`
379-
if (filePath.startsWith('./')) {
380-
filePath = filePath.slice(2)
381-
}
382-
if (filePath.startsWith('.')) {
383-
filePath = filePath.slice(1)
384-
}
385-
if (filePath.startsWith('/')) {
386-
filePath = filePath.slice(1)
387-
}
388-
389-
return {
390-
fileContent,
391-
filePath: `${filePath}${fileName}`,
392-
fileType
393-
}
394-
})
395-
396-
// 更新文件列表
397-
state.saveFilesInfo = fileRes
368+
// 更新文件列表和树结构
369+
state.saveFilesInfo = genResult
370+
const fileTreeInfo = fileListToTreeObject(genResult)
371+
state.saveFilesTree = fileTreeInfo as any
398372
399373
useNotify({
400374
type: 'success',
401375
title: '代码重新生成完成',
402-
message: `已${enabled ? '启用' : '禁用'} MCP 集成,共 ${fileRes.length} 个文件`
376+
message: `已${enabled ? '启用' : '禁用'} MCP 集成,共 ${genResult.length} 个文件`
403377
})
404378
} catch (error) {
405379
useNotify({

0 commit comments

Comments
 (0)