@@ -11,7 +11,9 @@ import {
1111 ILowCodePluginConfigMeta ,
1212 PluginPreference ,
1313 ILowCodePluginPreferenceDeclaration ,
14+ isLowCodeRegisterOptions ,
1415} from './plugin-types' ;
16+ import { filterValidOptions } from './plugin-utils' ;
1517import { LowCodePlugin } from './plugin' ;
1618import LowCodePluginContext from './plugin-context' ;
1719import { invariant } from '../utils' ;
@@ -41,15 +43,30 @@ export class LowCodePluginManager implements ILowCodePluginManager {
4143 return semverSatisfies ( engineVersion , versionExp ) ;
4244 }
4345
46+ /**
47+ * register a plugin
48+ * @param pluginConfigCreator - a creator function which returns the plugin config
49+ * @param options - the plugin options
50+ * @param registerOptions - the plugin register options
51+ */
4452 async register (
45- pluginConfigCreator : ( ctx : ILowCodePluginContext ) => ILowCodePluginConfig ,
46- options ?: ILowCodeRegisterOptions ,
53+ pluginConfigCreator : ( ctx : ILowCodePluginContext , options : any ) => ILowCodePluginConfig ,
54+ options ?: any ,
55+ registerOptions ?: ILowCodeRegisterOptions ,
4756 ) : Promise < void > {
48- const { pluginName, meta = { } } = pluginConfigCreator as any ;
57+ // registerOptions maybe in the second place
58+ if ( isLowCodeRegisterOptions ( options ) ) {
59+ registerOptions = options ;
60+ options = { } ;
61+ }
62+ let { pluginName, meta = { } } = pluginConfigCreator as any ;
4963 const { preferenceDeclaration, engines } = meta as ILowCodePluginConfigMeta ;
5064 const ctx = this . _getLowCodePluginContext ( { pluginName } ) ;
51- const config = pluginConfigCreator ( ctx ) ;
52-
65+ const customFilterValidOptions = engineConfig . get ( 'customPluginFilterOptions' , filterValidOptions ) ;
66+ const config = pluginConfigCreator ( ctx , customFilterValidOptions ( options , preferenceDeclaration ! ) ) ;
67+ // compat the legacy way to declare pluginName
68+ // @ts -ignore
69+ pluginName = pluginName || config . name ;
5370 invariant (
5471 pluginName ,
5572 'pluginConfigCreator.pluginName required' ,
@@ -58,7 +75,7 @@ export class LowCodePluginManager implements ILowCodePluginManager {
5875
5976 ctx . setPreference ( pluginName , ( preferenceDeclaration as ILowCodePluginPreferenceDeclaration ) ) ;
6077
61- const allowOverride = options ?. override === true ;
78+ const allowOverride = registerOptions ?. override === true ;
6279
6380 if ( this . pluginsMap . has ( pluginName ) ) {
6481 if ( ! allowOverride ) {
@@ -83,7 +100,8 @@ export class LowCodePluginManager implements ILowCodePluginManager {
83100 }
84101
85102 const plugin = new LowCodePlugin ( pluginName , this , config , meta ) ;
86- if ( options ?. autoInit ) {
103+ // support initialization of those plugins which registered after normal initialization by plugin-manager
104+ if ( registerOptions ?. autoInit ) {
87105 await plugin . init ( ) ;
88106 }
89107 this . plugins . push ( plugin ) ;
0 commit comments