feat: 列表组件的image渲染支持url模式

This commit is contained in:
tnt group 2023-05-17 20:09:03 +08:00
parent 6bbe489162
commit 9f41ad1811

View File

@ -1,87 +1,89 @@
import { ChartList } from '@/packages/components/Charts/index'
import { DecorateList } from '@/packages/components/Decorates/index'
import { InformationList } from '@/packages/components/Informations/index'
import { TableList } from '@/packages/components/Tables/index'
import { PackagesCategoryEnum, PackagesType, ConfigType, FetchComFlagType } from '@/packages/index.d'
const configModules: Record<string, { default: string }> = import.meta.glob('./components/**/config.vue', {
eager: true
})
const indexModules: Record<string, { default: string }> = import.meta.glob('./components/**/index.vue', {
eager: true
})
const imagesModules: Record<string, { default: string }> = import.meta.glob('../assets/images/chart/**', {
eager: true
})
// * 所有图表
export let packagesList: PackagesType = {
[PackagesCategoryEnum.CHARTS]: ChartList,
[PackagesCategoryEnum.INFORMATIONS]: InformationList,
[PackagesCategoryEnum.TABLES]: TableList,
[PackagesCategoryEnum.DECORATES]: DecorateList
}
/**
* *
* @param targetData
*/
export const createComponent = async (targetData: ConfigType) => {
const { category, key } = targetData
const chart = await import(`./components/${targetData.package}/${category}/${key}/config.ts`)
return new chart.default()
}
/**
* *
* @param {string} chartName
* @param {FetchComFlagType} flag 0, 1
*/
const fetchComponent = (chartName: string, flag: FetchComFlagType) => {
const module = flag === FetchComFlagType.VIEW ? indexModules : configModules
for (const key in module) {
const urlSplit = key.split('/')
if (urlSplit[urlSplit.length - 2] === chartName) {
return module[key]
}
}
}
/**
* *
* @param {ConfigType} dropData
*/
export const fetchChartComponent = (dropData: ConfigType) => {
const { key } = dropData
return fetchComponent(key, FetchComFlagType.VIEW)?.default
}
/**
* *
* @param {ConfigType} dropData
*/
export const fetchConfigComponent = (dropData: ConfigType) => {
const { key } = dropData
return fetchComponent(key, FetchComFlagType.CONFIG)?.default
}
/**
* *
* @param {ConfigType} targetData
*/
export const fetchImages = async (targetData?: ConfigType) => {
if (!targetData) return ''
// 新数据动态处理
const { image, package: targetDataPackage } = targetData
// 兼容旧数据
if (image.includes('@') || image.includes('base64')) return image
const imageName = image.substring(image.lastIndexOf('/') + 1)
for (const key in imagesModules) {
const urlSplit = key.split('/')
if (urlSplit[urlSplit.length - 1] === imageName) {
return imagesModules[key]?.default
}
}
return ''
}
import { ChartList } from '@/packages/components/Charts/index'
import { DecorateList } from '@/packages/components/Decorates/index'
import { InformationList } from '@/packages/components/Informations/index'
import { TableList } from '@/packages/components/Tables/index'
import { PackagesCategoryEnum, PackagesType, ConfigType, FetchComFlagType } from '@/packages/index.d'
const configModules: Record<string, { default: string }> = import.meta.glob('./components/**/config.vue', {
eager: true
})
const indexModules: Record<string, { default: string }> = import.meta.glob('./components/**/index.vue', {
eager: true
})
const imagesModules: Record<string, { default: string }> = import.meta.glob('../assets/images/chart/**', {
eager: true
})
// * 所有图表
export let packagesList: PackagesType = {
[PackagesCategoryEnum.CHARTS]: ChartList,
[PackagesCategoryEnum.INFORMATIONS]: InformationList,
[PackagesCategoryEnum.TABLES]: TableList,
[PackagesCategoryEnum.DECORATES]: DecorateList
}
/**
* *
* @param targetData
*/
export const createComponent = async (targetData: ConfigType) => {
const { category, key } = targetData
const chart = await import(`./components/${targetData.package}/${category}/${key}/config.ts`)
return new chart.default()
}
/**
* *
* @param {string} chartName
* @param {FetchComFlagType} flag 0, 1
*/
const fetchComponent = (chartName: string, flag: FetchComFlagType) => {
const module = flag === FetchComFlagType.VIEW ? indexModules : configModules
for (const key in module) {
const urlSplit = key.split('/')
if (urlSplit[urlSplit.length - 2] === chartName) {
return module[key]
}
}
}
/**
* *
* @param {ConfigType} dropData
*/
export const fetchChartComponent = (dropData: ConfigType) => {
const { key } = dropData
return fetchComponent(key, FetchComFlagType.VIEW)?.default
}
/**
* *
* @param {ConfigType} dropData
*/
export const fetchConfigComponent = (dropData: ConfigType) => {
const { key } = dropData
return fetchComponent(key, FetchComFlagType.CONFIG)?.default
}
/**
* *
* @param {ConfigType} targetData
*/
export const fetchImages = async (targetData?: ConfigType) => {
if (!targetData) return ''
// 判断图片是否为 url是则直接返回该 url
if (/^(?:https?):\/\/[^\s/.?#].[^\s]*/.test(targetData.image)) return targetData.image
// 新数据动态处理
const { image, package: targetDataPackage } = targetData
// 兼容旧数据
if (image.includes('@') || image.includes('base64')) return image
const imageName = image.substring(image.lastIndexOf('/') + 1)
for (const key in imagesModules) {
const urlSplit = key.split('/')
if (urlSplit[urlSplit.length - 1] === imageName) {
return imagesModules[key]?.default
}
}
return ''
}