diff --git a/packages/vision-polyfill/src/bundle/bundle.ts b/packages/vision-polyfill/src/bundle/bundle.ts index 7f994ebc5..b969512d9 100644 --- a/packages/vision-polyfill/src/bundle/bundle.ts +++ b/packages/vision-polyfill/src/bundle/bundle.ts @@ -33,6 +33,37 @@ export interface ComponentViewBundle { module: ComponentType; } +const prototypeViewWrapperList: any[] = []; + +function registerPrototypeViewWrapper(name: string, prototypeViewWrapper: Function) { + if (!prototypeViewWrapperList.find(p => p.name === name) && typeof prototypeViewWrapper === 'function') { + prototypeViewWrapperList.push({ + name, + prototypeViewWrapper, + }); + } +} + +function wrapperPrototypeView(view: ComponentClass) { + const newView = prototypeViewWrapperList.reduce((acc, { prototypeViewWrapper }) => { + return prototypeViewWrapper(acc.displayName, acc) || acc; + }, view); + if (!newView.displayName && view.displayName) { + newView.displayName = view.displayName; + } + if (!newView.propTypes && view.propTypes) { + newView.propTypes = view.propTypes; + } + if (!newView.defaultProps && view.defaultProps) { + newView.defaultProps = view.defaultProps; + } + return newView; +} + +function getPrototypeViewWrapperList() { + return prototypeViewWrapperList; +} + export default class Bundle { static createPrototype = Prototype.create; @@ -46,6 +77,14 @@ export default class Bundle { static overridePropsConfigure = Prototype.overridePropsConfigure; + static registerPrototypeConfigPreprocessor = Prototype.registerPrototypeConfigPreprocessor; + + static getPrototypeConfigPreprocessorList = Prototype.getPrototypeConfigPreprocessorList; + + static registerPrototypeViewWrapper = registerPrototypeViewWrapper; + + static getPrototypeViewWrapperList = getPrototypeViewWrapperList; + static create(protos: ComponentProtoBundle[], views?: ComponentViewBundle[]) { return new Bundle(protos, views); } @@ -161,7 +200,7 @@ export default class Bundle { viewDetail.displayName = getCamelName(viewName || item.name); } (viewDetail as any)._packageName_ = viewName || item.name; - this.viewsMap[viewDetail.displayName] = viewDetail; + this.viewsMap[viewDetail.displayName] = wrapperPrototypeView(viewDetail); }); } diff --git a/packages/vision-polyfill/src/bundle/prototype.ts b/packages/vision-polyfill/src/bundle/prototype.ts index 6c6937040..e16db1c77 100644 --- a/packages/vision-polyfill/src/bundle/prototype.ts +++ b/packages/vision-polyfill/src/bundle/prototype.ts @@ -205,6 +205,27 @@ function isNewSpec(options: any): options is ComponentMetadata { ); } +const prototypeConfigPreprocessorList: any[] = []; + +function registerPrototypeConfigPreprocessor(name: String, preprocessor: Function) { + if (!prototypeConfigPreprocessorList.find(p => p.name === name) && typeof preprocessor === 'function') { + prototypeConfigPreprocessorList.push({ + name, + preprocessor, + }); + } +} + +function modifyPrototypeConfig(config: any) { + const { componentName } = config; + return prototypeConfigPreprocessorList.reduce((acc, { preprocessor }) => { + return preprocessor(componentName, acc) || acc; + }, config); +} + +function getPrototypeConfigPreprocessorList() { + return prototypeConfigPreprocessorList; +} class Prototype { static addGlobalPropsReducer = addGlobalPropsReducer; @@ -216,8 +237,13 @@ class Prototype { static overridePropsConfigure = overridePropsConfigure; + static registerPrototypeConfigPreprocessor = registerPrototypeConfigPreprocessor; + + static getPrototypeConfigPreprocessorList = getPrototypeConfigPreprocessorList; + static create(config: OldPrototypeConfig | ComponentMetadata | ComponentMeta, extraConfigs: any = null, lookup = false) { - return new Prototype(config, extraConfigs, lookup); + const modifiedConfig = modifyPrototypeConfig(config); + return new Prototype(modifiedConfig, extraConfigs, lookup); } readonly isPrototype = true; diff --git a/packages/vision-polyfill/tests/prototypeWrapper/index.js b/packages/vision-polyfill/tests/prototypeWrapper/index.js new file mode 100644 index 000000000..4b64e8ce5 --- /dev/null +++ b/packages/vision-polyfill/tests/prototypeWrapper/index.js @@ -0,0 +1,24 @@ +// const { Bundle } = window.VisualEngine; +// Bundle.registerPrototypeConfigPreprocessor('aaa', (componentName, config) => { +// if (componentName === 'Text') { +// config.configure[0].initialValue.zh_CN = 'XXXXXXXXXX'; +// return { +// ...config, +// }; +// } +// }); + +// Bundle.registerPrototypeViewWrapper('aaa', function (componentName, View) { +// return componentName === 'Text' +// ? class Wrapper extends window.React.Component { +// render() { +// return ( +//
+// +//
AAAAAA
+//
+// ); +// } +// } +// : null; +// });