Skip to content

Commit

Permalink
fix(vue-generator): adapt for more scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
chilingling committed Mar 30, 2024
1 parent f794a0e commit f1259d3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
28 changes: 24 additions & 4 deletions packages/toolbars/generate-vue/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ export default {
})
.map((name) => fetchBlockSchema(name))
const schemaList = await Promise.allSettled(promiseList)
const extraList = []
schemaList.forEach((item) => {
if (item.status === 'fulfilled' && item.value?.[0]?.content) {
res.push(item.value[0].content)
res.push(...getBlocksSchema(item.value[0].content, blockSet))
extraList.push(getBlocksSchema(item.value[0].content, blockSet))
}
})
;(await Promise.allSettled(extraList)).forEach((item) => {
if (item.status === 'fulfilled' && item.value) {
res.push(...item.value)
}
})
Expand Down Expand Up @@ -161,6 +167,19 @@ export default {
}
})
const getAllPageDetails = async (pageList) => {
const detailPromise = pageList.map(({ id }) => useLayout().getPluginApi('AppManage').getPageById(id))
const detailList = await Promise.allSettled(detailPromise)
return detailList
.map((item) => {
if (item.status === 'fulfilled' && item.value) {
return item.value
}
})
.filter((item) => Boolean(item))
}
const getPreGenerateInfo = async () => {
const params = getParams()
const { id } = useEditorInfo().useInfo()
Expand All @@ -175,23 +194,24 @@ export default {
}
const [appData, metaData, pageList, dirHandle] = await Promise.all(promises)
const pageDetailList = await getAllPageDetails(pageList)
const blockSet = new Set()
const list = pageList.map((page) => getBlocksSchema(page.page_content, blockSet))
const list = pageDetailList.map((page) => getBlocksSchema(page.page_content, blockSet))
const blocks = await Promise.allSettled(list)
const blockSchema = []
blocks.forEach((item) => {
if (item.status === 'fulfilled' && Array.isArray(item.value)) {
blockSchema.push(...blockSchema)
blockSchema.push(...item.value)
}
})
const appSchema = {
// dataSource、utils、i18n、globalState
...metaData,
// 页面 schema
pageSchema: pageList.map((item) => {
pageSchema: pageDetailList.map((item) => {
const { page_content, ...meta } = item
return {
Expand Down
3 changes: 3 additions & 0 deletions packages/vue-generator/src/generator/vue/sfc/genSetupSFC.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ const generateSFCFile = (schema, componentsMap, config = {}) => {
const parsedConfig = parseConfig(config)
const { blockRelativePath, blockSuffix, scriptConfig: initScriptConfig, styleConfig: initStyleConfig } = parsedConfig
// 前置动作,对 Schema 进行解析初始化相关配置与变量
if (!schema.state) {
schema.state = {}
}

// 解析 import
const { pkgMap, blockPkgMap } = getImportMap(schema, componentsMap, { blockRelativePath, blockSuffix })
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-generator/src/generator/vue/sfc/generateTag.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hyphenate } from '@vue/shared'

const HTML_DEFAULT_VOID_ELEMENTS = [
export const HTML_DEFAULT_VOID_ELEMENTS = [
'img',
'input',
'br',
Expand Down Expand Up @@ -38,7 +38,7 @@ export const generateTag = (tagName, config = {}) => {
}

if (isVoidEle) {
return `<${renderTagName} />`
return `<${renderTagName} ${attribute || ''}/>`
}

if (isStartTag) {
Expand Down
14 changes: 9 additions & 5 deletions packages/vue-generator/src/generator/vue/sfc/generateTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import {
JS_EXPRESSION,
JS_I18N
} from '@/constant'
import { generateTag } from './generateTag'
import { generateTag, HTML_DEFAULT_VOID_ELEMENTS } from './generateTag'
import { thisPropsBindRe, thisRegexp } from '@/utils'

export const handleComponentNameHook = (nameObj) => {
const { componentName, schema } = nameObj
export const handleComponentNameHook = (optionData) => {
const { componentName, schema } = optionData

// 内置 component
if (!BUILTIN_COMPONENT_NAME_MAP[componentName]) {
return
}

if (componentName === BUILTIN_COMPONENT_NAME.TEXT && schema.props.text) {
schema.children = [schema.props.text]
schema.children = schema.props.text
delete schema.props.text
}

nameObj.componentName = BUILTIN_COMPONENT_NAME_MAP[componentName]
optionData.componentName = BUILTIN_COMPONENT_NAME_MAP[componentName]

if (HTML_DEFAULT_VOID_ELEMENTS.includes(componentName)) {
optionData.voidElement = true
}
}

export const handleTinyIcon = (nameObj, globalHooks) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/vue-generator/src/generator/vue/sfc/parseImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export const getImportMap = (schema, componentsMap, config) => {
pkgMap[key].push(item)
})

const { blockRelativePath = '../components/', blockSuffix = '.vue' } = config
const { blockRelativePath = '../components', blockSuffix = '.vue' } = config
const blockPkgMap = {}
const relativePath = blockRelativePath.endsWith('/') ? blockRelativePath.slice(0, -1) : blockRelativePath

blocks.map((name) => {
const source = `${blockRelativePath}/${name}${blockSuffix}`
const source = `${relativePath}/${name}${blockSuffix}`

blockPkgMap[source] = blockPkgMap[source] || []
blockPkgMap[source].push({
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-generator/src/plugins/genBlockPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function genBlockPlugin(options = {}) {

resBlocks.push({
fileType: 'vue',
fileName: `${block.componentName}.vue`,
fileName: `${block.fileName}.vue`,
path: blockBasePath,
fileContent: res
})
Expand Down
8 changes: 1 addition & 7 deletions scripts/setup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')

fs.access(path.resolve(__dirname, 'packages/vue-generator/dist'), (err) => {
if (err) {
exec('pnpm -F @opentiny/tiny-engine-dsl-vue build')
}
})
exec('pnpm -F @opentiny/tiny-engine-dsl-vue build')

0 comments on commit f1259d3

Please sign in to comment.