diff --git a/packages/react-generator/src/generator/page.js b/packages/react-generator/src/generator/page.js index f2a467f69..54cd1704c 100644 --- a/packages/react-generator/src/generator/page.js +++ b/packages/react-generator/src/generator/page.js @@ -24,6 +24,7 @@ import { } from '../utils' // import { validateByParse, validateByCompile } from '../utils/vue-sfc-validator' import { traverse as traverseState, unwrapExpression, translateHookState } from '../parser/state' +import { capitalizeFirstLetter } from '../utils/uaperCase' import { preProcess } from '../pre-processor' import { DEFAULT_COMPONENTS_MAP, @@ -235,7 +236,7 @@ const generateReactImports = (description, moduleName, type, componentsMap) => { const importsFromReact = [ 'import * as React from "react"', - 'import * as utils from "../lowcode/utils.js"', + // 'import * as utils from "../lowcode/utils.js"', `import './${moduleName}.css'` ] @@ -411,20 +412,20 @@ const generateReactCode = ({ schema, name, type, componentsMap }) => { const shouldComponentUpdate = lifecycleMap['shouldComponentUpdate'] const stringUseEffect = ` - useEffect(() => { + React.useEffect(() => { ${componentDidMount && componentDidMount['body'] ? componentDidMount['body'] : ''} ${componentWillUnmount && componentWillUnmount['body'] ? `return () => {${componentWillUnmount['body']}}` : ''} }, [${componentDidUpdate && componentDidUpdate['params'] ? componentDidUpdate['params'] : ''}]) ` const stringUseLayoutEffect = ` - useLayoutEffect(() => { + React.useLayoutEffect(() => { ${componentWillMount && componentWillMount['body'] ? componentWillMount['body'] : ''} }, [${componentDidUpdate && componentDidUpdate['params'] ? componentDidUpdate['params'] : ''}]) ` const stringUseMemo = ` - useMemo(() => { + React.useMemo(() => { ${shouldComponentUpdate && shouldComponentUpdate['body'] ? shouldComponentUpdate['body'] : ''} }, [${shouldComponentUpdate && shouldComponentUpdate['params'] ? shouldComponentUpdate['params'] : ''}]) ` @@ -434,10 +435,11 @@ const generateReactCode = ({ schema, name, type, componentsMap }) => { console.log(arrowMethods.join('\n'), 'arrowMethod>>>>>>') + const componentName = capitalizeFirstLetter(name) // 生成模板 const result = `${imports.join('\n')} - export default ${name} = () => { + const ${componentName} = () => { ${statement} ${getters.join('\n')} @@ -456,6 +458,8 @@ const generateReactCode = ({ schema, name, type, componentsMap }) => { ) } + + export default ${componentName} ` return result diff --git a/packages/react-generator/src/plugins/genRouterPlugin.js b/packages/react-generator/src/plugins/genRouterPlugin.js index 03edd78de..fc458c3bc 100644 --- a/packages/react-generator/src/plugins/genRouterPlugin.js +++ b/packages/react-generator/src/plugins/genRouterPlugin.js @@ -1,4 +1,5 @@ import { mergeOptions } from '../utils/mergeOptions' +import { capitalizeFirstLetter } from '../utils/uaperCase' const defaultOption = { fileName: 'index.jsx', @@ -46,28 +47,45 @@ function genRouterPlugin(options = {}) { const importSnippet = 'import { Routes, Route } from "react-router-dom"\n import { useLazy } from "../hooks/uselazy"' - const lazyPage = routesList.map(({ fileName, filePath }) => { - console.log(filePath) + const lazyPage = routesList.map(({ fileName }) => { if (!fileName) { return '' } + const componentName = capitalizeFirstLetter(fileName) + return ` - const ${fileName} = useLazy(import("@views/${fileName}/${fileName}.jsx")) + const ${componentName} = useLazy(import("@/views/${fileName}/${fileName}.jsx")) ` }) + let homePage = '' + let temp = '' + for (const { fileName, path, redirect } of routesList) { + if (redirect) { + temp = redirect + continue + } + + if (fileName) { + if (temp === path) { + homePage = capitalizeFirstLetter(fileName) + } + } + } + const routes = routesList.map(({ fileName, path, redirect }) => { let pathAttr = `path='${path}'` let redirectAttr = '' let componentAttr = '' if (redirect) { - redirectAttr = `redirect='${redirect}'` + redirectAttr = `redirect='${redirect}' element={<${homePage} />}` } if (fileName) { - componentAttr = `element={${fileName}}` + const fileUpperName = capitalizeFirstLetter(fileName) + componentAttr = `element={<${fileUpperName} />}` } const ans = `` diff --git a/packages/react-generator/src/utils/uaperCase.js b/packages/react-generator/src/utils/uaperCase.js new file mode 100644 index 000000000..50918acf4 --- /dev/null +++ b/packages/react-generator/src/utils/uaperCase.js @@ -0,0 +1,4 @@ +export const capitalizeFirstLetter = (string) => { + if (!string) return string // 检查空字符串 + return string.charAt(0).toUpperCase() + string.slice(1) +} diff --git a/packages/toolbars/generate-vue/src/Main.vue b/packages/toolbars/generate-vue/src/Main.vue index 5cebba97d..9d553bd9f 100644 --- a/packages/toolbars/generate-vue/src/Main.vue +++ b/packages/toolbars/generate-vue/src/Main.vue @@ -5,8 +5,6 @@ - import { reactive } from 'vue' -import { Popover, Select } from '@opentiny/vue' +import { Popover } from '@opentiny/vue' import { getGlobalConfig, useBlock, @@ -34,12 +32,10 @@ import { generateApp as generateReactApp } from '../../../react-generator/src/in import { fetchMetaData, fetchPageList, fetchCode as fetchBlockSchema } from './http' import FileSelector from './FileSelector.vue' -// import { generateVuePage } from './generateCode' export default { components: { TinyPopover: Popover, - TinySelect: Select, GenerateFileSelector: FileSelector }, props: { @@ -71,7 +67,9 @@ export default { ] }) + const curFramework = getGlobalConfig()?.dslMode const getParams = () => { + // console.log(useCanvas().canvasApi.value, 'value>>>>>') const { getSchema } = useCanvas().canvasApi.value const params = { framework: getGlobalConfig()?.dslMode, @@ -155,8 +153,7 @@ export default { const params = getParams() const { id } = useEditorInfo().useInfo() const promises = [ - params.framework === 'Vue' ? useHttp().get(`/app-center/v1/api/apps/schema/${id}`) : fetchBlockSchema(params), - // state.value === 'React' ? fetchBlockSchema(params) : useHttp().get(`/app-center/v1/api/apps/schema/${id}`), + curFramework === 'Vue' ? useHttp().get(`/app-center/v1/api/apps/schema/${id}`) : fetchBlockSchema(params), fetchMetaData(params), fetchPageList(params.app) ] @@ -166,14 +163,7 @@ export default { } const [appData, metaData, pageList, dirHandle] = await Promise.all(promises) - // 如果是React,其appData数据的格式与Vue中的appData不一致,React是(一般有两个文件,第一个文件是Jsx文件,第二个文件是Css文件): - // error: Array { - // const { page_content, ...meta } = item - - // return { - // ...page_content, - // meta: { - // ...meta, - // router: meta.route - // } - // } - // }), - // blockSchema, - // // 物料数据 - // // componentsMap: [...(appData.componentsMap || [])],由于React在调用fetchcode的时候就已经调用了react-dsl里面的函数, - // // 所以在这直接调用./generateCode里面的函数直接格式化返回的React的代码就可以了 - // componentsMap: [...(appData.componentsMap || [])], - // meta: { - // ...(appData.meta || {}) - // }, - // reactData: state.value === 'React' ? [...(appData || [])] : [] - // } - - state.instance = state.value === 'React' ? generateReactApp() : generateVueApp() - - const res = await state.instance.generate(await allSchema[state.value]) + state.instance = curFramework === 'React' ? generateReactApp() : generateVueApp() + + const res = await state.instance.generate(await allSchema[curFramework]) const { genResult = [] } = res || {} const fileRes = genResult.map(({ fileContent, fileName, path, fileType }) => {