override.ts 2.63 KB

import { ConfigType } from "../index.d"
import { compatibleConfig } from "./compatibleConfig"
import { matchExternalPrefixReg } from "./useWidgetKey"

const getAllConfigFile = async () => {
  return import.meta.glob('../components/**/config.ts', )
}

const getChartConfigFile = async (path: string) => {
  const fileList = await getAllConfigFile()
  if (path.includes('group')) return // 分组则终止
  return fileList[path]() as any
} 

export const createComponent = async (configType: ConfigType) => {
  // eslint-disable-next-line prefer-const
  // eslint-disable-next-line prefer-const
  let { key, chartKey, category, package: packageName, redirectComponent, categoryName , conKey, isThreeModel} = configType
  // redirectComponent 是给图片组件库和图标组件库使用的
  if (!isThreeModel && redirectComponent) {
    const [packageName, categoryName, keyName] = redirectComponent.split('/')
    const filePath = `../components/${packageName}/${categoryName}/${keyName}/config.ts`
    const redirectChart = await getChartConfigFile(filePath)
    return new redirectChart.default()
  }
  // isThreeModel是给3D模型使用的
  if(isThreeModel && redirectComponent) {
    const [packageName, categoryName, keyName] = (redirectComponent as string).split('/')
    const hasExternalPrefix = matchExternalPrefixReg.test(chartKey)
    const filePath = `../components${hasExternalPrefix ? '/external' : ''}/${packageName}/${categoryName}/${keyName}/config.ts`
    const redirectChart = await getChartConfigFile(filePath)
    return new redirectChart.default()
   }


  /**
   * 兼容之前导入错误的补丁
   */
  const includeOldTitleKeys=['LeftCenterRightHead','LeftCenterRightHeadAnimat','Title1','Title2','Title3']
  compatibleConfig?.forEach(item => {
    if (item?.key === key) {
      key = includeOldTitleKeys.includes(item?.key)?item?.newKey:item?.key as any
      packageName = item?.newPackage as string
      category = item?.newCategory as string
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      categoryName = item?.newCategoryName as string
      chartKey =  includeOldTitleKeys.includes(item?.key)?item?.newChartKey:chartKey as any
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      conKey = includeOldTitleKeys.includes(item?.key)?item?.newConKey:conKey as any
    }
  })
  //
  const hasExternalPrefix = matchExternalPrefixReg.test(chartKey)
  const filePath = `../components${hasExternalPrefix ? '/external' : ''}/${packageName}/${category}/${key}/config.ts`
  if (filePath.includes('group')) return // 分组则终止 
  const chart = await getChartConfigFile(filePath)
  return new chart.default()
}