GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — lowcode-engine/packages/utils/src/build-components.ts at main · annlay/lowcode-engine Expand file tree/ Copy pathbuild-components.tsMore file actions 161 lines (149 loc) · 4.81 KB / Copy pathbuild-components.tsFile metadata and controls161 lines (149 loc) · 4.81 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 import { ComponentType, forwardRef, createElement, FunctionComponent } from 'react'; import { NpmInfo, ComponentSchema } from '@alilc/lowcode-types'; import { Component } from '@alilc/lowcode-designer'; import { isESModule } from './is-es-module'; import { isReactComponent, acceptsRef, wrapReactClass } from './is-react'; import { isObject } from './is-object'; interface LibraryMap { [key: string]: string; } export function accessLibrary(library: string | Record<string, unknown>) { if (typeof library !== 'string') { return library; } return (window as any)[library] || generateHtmlComp(library); } export function generateHtmlComp(library: string) { if (['a', 'img', 'div', 'span', 'svg'].includes(library)) { return forwardRef((props, ref) => { return createElement(library, { ref, ...props }, props.children); }); } } export function getSubComponent(library: any, paths: string[]) { const l = paths.length; if (l < 1 || !library) { return library; } let i = 0; let component: any; while (i < l) { const key = paths[i]!; let ex: any; try { component = library[key]; } catch (e) { ex = e; component = null; } if (i === 0 && component == null && key === 'default') { if (ex) { return l === 1 ? library : null; } component = library; } else if (component == null) { return null; } library = component; i++; } return component; } function findComponent(libraryMap: LibraryMap, componentName: string, npm?: NpmInfo) { if (!npm) { return accessLibrary(componentName); } // libraryName the key access to global // export { exportName } from xxx exportName === global.libraryName.exportName // export exportName from xxx exportName === global.libraryName.default || global.libraryName // export { exportName as componentName } from package // if exportName == null exportName === componentName; // const componentName = exportName.subName, if exportName empty subName donot use const exportName = npm.exportName || npm.componentName || componentName; const libraryName = libraryMap[npm.package] || exportName; const library = accessLibrary(libraryName); const paths = npm.exportName && npm.subName ? npm.subName.split('.') : []; if (npm.destructuring) { paths.unshift(exportName); } else if (isESModule(library)) { paths.unshift('default'); } return getSubComponent(library, paths); } /** * 判断是否是一个混合组件,即 components 是一个对象,对象值是 React 组件 * 示例: * { * Button: ReactNode, * Text: ReactNode, * } */ function isMixinComponent(components: any) { if (!isObject(components)) { return false; } return Object.keys(components).some(componentName => isReactComponent(components[componentName])); } export function buildComponents(libraryMap: LibraryMap, componentsMap: { [componentName: string]: NpmInfo | ComponentType<any> | ComponentSchema }, createComponent: (schema: ComponentSchema) => Component | null) { const components: any = {}; Object.keys(componentsMap).forEach((componentName) => { let component = componentsMap[componentName]; if (component && (component as ComponentSchema).componentName === 'Component') { components[componentName] = createComponent(component as ComponentSchema); } else if (isReactComponent(component)) { if (!acceptsRef(component)) { component = wrapReactClass(component as FunctionComponent); } components[componentName] = component; } else if (isMixinComponent(component)) { components[componentName] = component; } else { component = findComponent(libraryMap, componentName, component); if (component) { if (!acceptsRef(component)) { component = wrapReactClass(component as FunctionComponent); } components[componentName] = component; } } }); return components; } export interface UtilsMetadata { name: string; npm: { package: string; version?: string; exportName: string; subName?: string; destructuring?: boolean; main?: string; }; } interface LibrayMap { [key: string]: string; } interface ProjectUtils { [packageName: string]: any; } export function getProjectUtils(librayMap: LibrayMap, utilsMetadata: UtilsMetadata[]): ProjectUtils { const projectUtils: ProjectUtils = {}; if (utilsMetadata) { utilsMetadata.forEach(meta => { if (librayMap[meta?.npm?.package]) { const lib = accessLibrary(librayMap[meta?.npm.package]); if (lib?.destructuring) { Object.keys(lib).forEach(name => { if (name === 'destructuring') return; projectUtils[name] = lib[name]; }); } else if (meta.name) { projectUtils[meta.name] = lib; } } }); } return projectUtils; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |