Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into build-toggle-cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
rhlin committed May 13, 2024
2 parents 85e42b2 + 3a66996 commit e11c76c
Show file tree
Hide file tree
Showing 128 changed files with 10,767 additions and 139 deletions.
2 changes: 1 addition & 1 deletion mockServer/src/mock/get/app-center/v1/apps/schema/918.json
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@
},
{
"componentName": "TinyPlusFrozenPage",
"package": "@opentiny/vuee",
"package": "@opentiny/vue",
"exportName": "FrozenPage",
"destructuring": true,
"version": "3.4.1"
Expand Down
1 change: 1 addition & 0 deletions packages/design-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@opentiny/tiny-engine-canvas": "workspace:*",
"@opentiny/tiny-engine-common": "workspace:*",
"@opentiny/tiny-engine-controller": "workspace:*",
"@opentiny/tiny-engine-dsl-vue": "workspace:*",
"@opentiny/tiny-engine-http": "workspace:*",
"@opentiny/tiny-engine-i18n-host": "workspace:*",
"@opentiny/tiny-engine-plugin-block": "workspace:*",
Expand Down
79 changes: 56 additions & 23 deletions packages/design-core/src/preview/src/preview/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { defineComponent, computed, defineAsyncComponent } from 'vue'
import { Repl, ReplStore } from '@vue/repl'
import vueJsx from '@vue/babel-plugin-jsx'
import { transformSync } from '@babel/core'
import { Notify } from '@opentiny/vue'
import { genSFCWithDefaultPlugin, parseRequiredBlocks } from '@opentiny/tiny-engine-dsl-vue'
import importMap from './importMap'
import srcFiles from './srcFiles'
import generateMetaFiles, { processAppJsCode } from './generate'
import { getSearchParams, fetchCode, fetchMetaData, fetchImportMap } from './http'
import { getSearchParams, fetchMetaData, fetchImportMap, fetchAppSchema, fetchBlockSchema } from './http'
import { PanelType, PreviewTips } from '../constant'
import { injectDebugSwitch } from './debugSwitch'
import '@vue/repl/style.css'
Expand Down Expand Up @@ -69,6 +69,33 @@ export default {
const newImportMap = { imports: { ...importMap.imports, ...utilsImportMaps } }
store.setImportMap(newImportMap)
}
const getBlocksSchema = async (pageSchema, blockSet = new Set()) => {
let res = []
const blockNames = parseRequiredBlocks(pageSchema)
const promiseList = blockNames
.filter((name) => {
if (blockSet.has(name)) {
return false
}
blockSet.add(name)
return true
})
.map((name) => fetchBlockSchema(name))
const schemaList = await Promise.allSettled(promiseList)
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))
}
})
return res
}
const queryParams = getSearchParams()
const getImportMap = async () => {
Expand All @@ -85,32 +112,38 @@ export default {
}
const promiseList = [
fetchCode(queryParams),
fetchAppSchema(queryParams?.app),
fetchMetaData(queryParams),
setFiles(srcFiles, 'src/Main.vue'),
getImportMap()
]
Promise.all(promiseList).then(([codeList, metaData, _void, importMapData]) => {
Promise.all(promiseList).then(async ([appData, metaData, _void, importMapData]) => {
addUtilsImportMap(importMapData, metaData.utils || [])
const codeErrorMsgs = codeList
.filter(({ errors }) => errors?.length)
.map(({ errors }) => errors)
.flat()
.map(({ message }) => message)
if (codeErrorMsgs.length) {
const title = PreviewTips.ERROR_WHEN_COMPILE
Notify({
type: 'error',
title,
message: codeErrorMsgs.join('\n'),
// 不自动关闭
duration: 0,
position: 'top-right'
})
return title
}
const blocks = await getBlocksSchema(queryParams.pageInfo?.schema)
// TODO: 需要验证级联生成 block schema
// TODO: 物料内置 block 需要如何处理?
const pageCode = [
{
panelName: 'Main.vue',
panelValue:
genSFCWithDefaultPlugin(queryParams.pageInfo?.schema, appData?.componentsMap || [], {
blockRelativePath: './'
}) || '',
panelType: 'vue',
index: true
},
...(blocks || []).map((blockSchema) => {
return {
panelName: blockSchema.fileName,
panelValue:
genSFCWithDefaultPlugin(blockSchema, appData?.componentsMap || [], { blockRelativePath: './' }) || '',
panelType: 'vue',
index: true
}
})
]
// [@vue/repl] `Only lang="ts" is supported for <script> blocks.`
const langReg = /lang="jsx"/
Expand Down Expand Up @@ -158,7 +191,7 @@ export default {
newFiles['app.js'] = appJsCode
codeList.map(fixScriptLang).forEach(assignFiles)
pageCode.map(fixScriptLang).forEach(assignFiles)
const metaFiles = generateMetaFiles(metaData)
Object.assign(newFiles, metaFiles)
Expand Down
3 changes: 3 additions & 0 deletions packages/design-core/src/preview/src/preview/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ export const fetchImportMap = async () => {
const baseUrl = new URL(import.meta.env.BASE_URL, location.href)
return fetch(new URL('./preview-import-map-static/preview-importmap.json', baseUrl).href).then((res) => res.json())
}

export const fetchAppSchema = async (id) => http.get(`/app-center/v1/api/apps/schema/${id}`)
export const fetchBlockSchema = async (blockName) => http.get(`/material-center/api/block?label=${blockName}`)
65 changes: 65 additions & 0 deletions packages/design-core/src/preview/src/preview/srcFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,69 @@ srcFiles['locales.js'] = localesJS
srcFiles['stores.js'] = storesJS
srcFiles['storesHelper.js'] = storesHelperJS

export const genPreviewTemplate = () => {
return [
{
fileName: 'App.vue',
path: '',
fileContent: appVue
},
{
fileName: 'constant.js',
path: '',
fileContent: constantJS
},
{
fileName: 'app.js',
path: '',
fileContent: appJS.replace(/VITE_CDN_DOMAIN/g, import.meta.env.VITE_CDN_DOMAIN)
},
{
fileName: 'injectGlobal.js',
path: '',
fileContent: injectGlobalJS
},
{
fileName: 'lowcode.js',
path: '',
fileContent: lowcodeJS
},
{
fileName: 'dataSourceMap.js',
path: '',
fileContent: dataSourceMapJS
},
{
fileName: 'dataSource.js',
path: '',
fileContent: dataSourceJS
},
{
fileName: 'utils.js',
path: '',
fileContent: utilsJS
},
{
fileName: 'bridge.js',
path: '',
fileContent: bridgeJS
},
{
fileName: 'locales.js',
path: '',
fileContent: localesJS
},
{
fileName: 'stores.js',
path: '',
fileContent: storesJS
},
{
fileName: 'storesHelper.js',
path: '',
fileContent: storesHelperJS
}
]
}

export default srcFiles
1 change: 1 addition & 0 deletions packages/toolbars/generate-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"@opentiny/tiny-engine-canvas": "workspace:*",
"@opentiny/tiny-engine-controller": "workspace:*",
"@opentiny/tiny-engine-dsl-vue": "workspace:*",
"@opentiny/tiny-engine-http": "workspace:*",
"@opentiny/tiny-engine-utils": "workspace:*",
"prettier": "2.7.1"
Expand Down
25 changes: 20 additions & 5 deletions packages/toolbars/generate-vue/src/FileSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,30 @@ export default {
emits: ['cancel', 'confirm'],
setup(props, { emit }) {
const getTableTreeData = (data) => {
const dataMap = {}
const res = []
data.forEach((item) => {
if (!dataMap[item.fileType]) {
dataMap[item.fileType] = { fileType: item.fileType, children: [] }
const folder = item.filePath.split('/').slice(0, -1)
if (!folder.length) {
res.push(item)
return
}
dataMap[item.fileType].children.push(item)
const parentFolder = folder.reduce((parent, curPath) => {
let curItem = parent.find((parItem) => parItem.path === curPath)
if (!curItem) {
curItem = { path: curPath, filePath: curPath, children: [] }
parent.push(curItem)
}
return curItem.children
}, res)
parentFolder.push(item)
})
return Object.values(dataMap)
return res
}
const tableData = computed(() => getTableTreeData(props.data))
Expand Down
Loading

0 comments on commit e11c76c

Please sign in to comment.