2021-11-03 10:41:59 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import { IComponentOptions, IController } from 'angular';
|
|
|
|
|
2021-11-16 16:33:51 +00:00
|
|
|
import { RootProvider } from './RootProvider';
|
|
|
|
|
2021-11-03 10:41:59 +00:00
|
|
|
function toProps(
|
|
|
|
propNames: string[],
|
|
|
|
controller: IController,
|
|
|
|
$q: ng.IQService
|
|
|
|
) {
|
|
|
|
return Object.fromEntries(
|
|
|
|
propNames.map((key) => {
|
|
|
|
const prop = controller[key];
|
|
|
|
if (typeof prop !== 'function') {
|
|
|
|
return [key, prop];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
key,
|
|
|
|
(...args: unknown[]) =>
|
|
|
|
$q((resolve) => resolve(controller[key](...args))),
|
|
|
|
];
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function react2angular<T>(
|
|
|
|
Component: React.ComponentType<T>,
|
|
|
|
propNames: string[]
|
|
|
|
): IComponentOptions {
|
|
|
|
const bindings = Object.fromEntries(propNames.map((key) => [key, '<']));
|
|
|
|
|
|
|
|
return {
|
|
|
|
bindings,
|
|
|
|
controller: Controller,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* @ngInject */
|
|
|
|
function Controller(
|
|
|
|
this: IController,
|
|
|
|
$element: HTMLElement[],
|
|
|
|
$q: ng.IQService
|
|
|
|
) {
|
|
|
|
const el = $element[0];
|
|
|
|
this.$onChanges = () => {
|
|
|
|
const props = toProps(propNames, this, $q);
|
|
|
|
ReactDOM.render(
|
2021-11-16 16:33:51 +00:00
|
|
|
<RootProvider>
|
2021-11-03 10:41:59 +00:00
|
|
|
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
|
|
|
|
<Component {...(props as T)} />
|
2021-11-16 16:33:51 +00:00
|
|
|
</RootProvider>,
|
2021-11-03 10:41:59 +00:00
|
|
|
el
|
|
|
|
);
|
|
|
|
};
|
|
|
|
this.$onDestroy = () => ReactDOM.unmountComponentAtNode(el);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const r2a = react2angular;
|