feat: 支持设计器里的 utils 注入机制 (vu-xxx & 简单类型的 umd)

This commit is contained in:
力皓 2021-05-10 17:10:37 +08:00
parent 678e13c159
commit b23231e8d0
5 changed files with 70 additions and 5 deletions

View File

@ -14,6 +14,7 @@ import {
isElement, isElement,
isFormEvent, isFormEvent,
hasOwnProperty, hasOwnProperty,
UtilsMetadata,
} from '@ali/lowcode-utils'; } from '@ali/lowcode-utils';
import { import {
DragObjectType, DragObjectType,
@ -65,6 +66,7 @@ export interface BuiltinSimulatorProps {
requestHandlersMap?: any; requestHandlersMap?: any;
extraEnvironment?: Asset; extraEnvironment?: Asset;
library?: LibraryItem[]; library?: LibraryItem[];
utilsMetadata?: UtilsMetadata;
simulatorUrl?: Asset; simulatorUrl?: Asset;
theme?: Asset; theme?: Asset;
componentsAsset?: Asset; componentsAsset?: Asset;

View File

@ -60,10 +60,11 @@ export default class DesignerPlugin extends PureComponent<PluginProps, DesignerP
if (!this._mounted) { if (!this._mounted) {
return; return;
} }
const { components, packages, extraEnvironment } = assets; const { components, packages, extraEnvironment, utils } = assets;
const state = { const state = {
componentMetadatas: components || [], componentMetadatas: components || [],
library: packages || [], library: packages || [],
utilsMetadata: utils || [],
extraEnvironment, extraEnvironment,
renderEnv, renderEnv,
device, device,
@ -96,6 +97,7 @@ export default class DesignerPlugin extends PureComponent<PluginProps, DesignerP
const { editor } = this.props; const { editor } = this.props;
const { const {
componentMetadatas, componentMetadatas,
utilsMetadata,
library, library,
extraEnvironment, extraEnvironment,
renderEnv, renderEnv,
@ -121,6 +123,7 @@ export default class DesignerPlugin extends PureComponent<PluginProps, DesignerP
componentMetadatas={componentMetadatas} componentMetadatas={componentMetadatas}
simulatorProps={{ simulatorProps={{
library, library,
utilsMetadata,
extraEnvironment, extraEnvironment,
renderEnv, renderEnv,
device, device,

View File

@ -15,7 +15,7 @@ import {
compatibleLegaoSchema, compatibleLegaoSchema,
isPlainObject, isPlainObject,
AssetLoader, AssetLoader,
getProjectUtils,
} from '@ali/lowcode-utils'; } from '@ali/lowcode-utils';
import { RootSchema, ComponentSchema, TransformStage, NodeSchema } from '@ali/lowcode-types'; import { RootSchema, ComponentSchema, TransformStage, NodeSchema } from '@ali/lowcode-types';
@ -201,9 +201,6 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
if (this._libraryMap !== host.libraryMap || this._componentsMap !== host.designer.componentsMap) { if (this._libraryMap !== host.libraryMap || this._componentsMap !== host.designer.componentsMap) {
this._libraryMap = host.libraryMap || {}; this._libraryMap = host.libraryMap || {};
this._componentsMap = host.designer.componentsMap; this._componentsMap = host.designer.componentsMap;
// 需要注意的是autorun 依赖收集的是同步执行的代码,所以 await / promise / callback 里的变量不会被收集依赖
// 此例中host.designer.componentsMap 是需要被收集依赖的,否则无法响应式
// await host.waitForCurrentDocument();
this.buildComponents(); this.buildComponents();
} }
@ -276,6 +273,7 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
currentLocale: this.locale, currentLocale: this.locale,
messages: {}, messages: {},
}, },
...getProjectUtils(this._libraryMap, host.get('utilsMetadata')),
}, },
constants: {}, constants: {},
requestHandlersMap: this._requestHandlersMap, requestHandlersMap: this._requestHandlersMap,

View File

@ -0,0 +1,26 @@
interface UtilsMetadata {
name: string;
npm: {
package: string;
version?: string;
exportName: string;
subName?: string;
destructuring?: boolean;
main?: string;
}
}
interface LibrayMap {
[key: string]: string;
}
export function getProjectUtils(librayMap: LibrayMap, utilsMetadata: UtilsMetadata[]) {
const projectUtils: { [packageName: string]: any } = {};
if (utilsMetadata) {
utilsMetadata.forEach(meta => {
if (librayMap[meta?.npm.package]) {
const lib = window[librayMap[meta?.npm.package]];
}
});
}
}

View File

@ -101,3 +101,39 @@ export function buildComponents(libraryMap: LibraryMap,
}); });
return components; 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;
}
export function getProjectUtils(librayMap: LibrayMap, utilsMetadata: UtilsMetadata[]) {
const projectUtils: { [packageName: string]: any } = {};
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 {
projectUtils[meta?.npm?.exportName] = lib;
}
}
});
}
return projectUtils;
}