Skip to content

Commit 450d08e

Browse files
committedJul 1, 2024·
fix: renderer bugs fix
1 parent a855c05 commit 450d08e

26 files changed

+469
-373
lines changed
 

‎packages/react-renderer/src/api/app.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createRenderer } from '@alilc/lowcode-renderer-core';
22
import { type Root, createRoot } from 'react-dom/client';
3-
import { type ReactAppOptions, RendererContext } from './context';
3+
import { RendererContext } from './context';
44
import { ApplicationView, boosts } from '../app';
5+
import { type ReactAppOptions } from './types';
56

67
export const createApp = async (options: ReactAppOptions) => {
78
return createRenderer(async (context) => {

‎packages/react-renderer/src/api/component.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import { createRenderer, type AppOptions } from '@alilc/lowcode-renderer-core';
1+
import { createRenderer } from '@alilc/lowcode-renderer-core';
22
import { FunctionComponent } from 'react';
3-
import { type LowCodeComponentProps, createComponentBySchema } from '../runtime/schema';
4-
import { RendererContext } from '../api/context';
3+
import {
4+
type LowCodeComponentProps,
5+
createComponent as createSchemaComponent,
6+
} from '../runtime/createComponent';
7+
import { RendererContext } from './context';
8+
import { type ReactAppOptions } from './types';
59

610
interface Render {
711
toComponent(): FunctionComponent<LowCodeComponentProps>;
812
}
913

10-
export async function createComponent(options: AppOptions) {
14+
export async function createComponent(options: ReactAppOptions) {
1115
const creator = createRenderer<Render>((context) => {
1216
const { schema } = context;
17+
const componentsTree = schema.get('componentsTree')[0];
18+
19+
const LowCodeComponent = createSchemaComponent(componentsTree, {
20+
displayName: componentsTree.componentName,
21+
...options.component,
22+
});
1323

14-
const LowCodeComponent = createComponentBySchema(schema.get('componentsTree')[0]);
1524
const contextValue = { ...context, options };
1625

1726
function Component(props: LowCodeComponentProps) {

‎packages/react-renderer/src/api/context.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { type ComponentType, createContext, useContext } from 'react';
2-
import { type AppOptions, type RenderContext } from '@alilc/lowcode-renderer-core';
3-
4-
export interface ReactAppOptions extends AppOptions {
5-
faultComponent?: ComponentType<any>;
6-
}
1+
import { createContext, useContext } from 'react';
2+
import { type RenderContext } from '@alilc/lowcode-renderer-core';
3+
import { type ReactAppOptions } from './types';
74

85
export const RendererContext = createContext<RenderContext & { options: ReactAppOptions }>(
96
undefined!,
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { type AppOptions } from '@alilc/lowcode-renderer-core';
2+
import { type ComponentType } from 'react';
3+
import { type ComponentOptions } from '../runtime/createComponent';
4+
5+
export interface ReactAppOptions extends AppOptions {
6+
component?: Pick<
7+
ComponentOptions,
8+
'beforeElementCreate' | 'elementCreated' | 'componentRefAttached'
9+
>;
10+
faultComponent?: ComponentType<any>;
11+
}

‎packages/react-renderer/src/app/view.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useRendererContext } from '../api/context';
2-
import { getComponentByName } from '../runtime/schema';
2+
import { getComponentByName } from '../runtime/createComponent';
33
import { boosts } from './boosts';
44

55
export function ApplicationView() {
66
const rendererContext = useRendererContext();
7-
const { schema } = rendererContext;
7+
const { schema, options } = rendererContext;
88
const appWrappers = boosts.getAppWrappers();
99
const Outlet = boosts.getOutlet();
1010

@@ -16,7 +16,7 @@ export function ApplicationView() {
1616

1717
if (layoutConfig) {
1818
const componentName = layoutConfig.componentName;
19-
const Layout = getComponentByName(componentName, rendererContext);
19+
const Layout = getComponentByName(componentName, rendererContext, options.component);
2020

2121
if (Layout) {
2222
const layoutProps: any = layoutConfig.props ?? {};

‎packages/react-renderer/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ export * from './router';
66
export { LifecyclePhase } from '@alilc/lowcode-renderer-core';
77

88
export type { Spec, ProCodeComponent, LowCodeComponent } from '@alilc/lowcode-shared';
9-
export type { PackageLoader, CodeScope, Plugin } from '@alilc/lowcode-renderer-core';
9+
export type {
10+
PackageLoader,
11+
CodeScope,
12+
Plugin,
13+
ModelDataSourceCreator,
14+
ModelStateCreator,
15+
} from '@alilc/lowcode-renderer-core';
1016
export type { ReactRendererBoostsApi } from './app/boosts';

‎packages/react-renderer/src/router/route.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { useMemo } from 'react';
22
import { useRendererContext } from '../api/context';
33
import { OutletProps } from '../app/boosts';
44
import { useRouteLocation } from './context';
5-
import { createComponentBySchema } from '../runtime/schema';
5+
import { createComponent } from '../runtime/createComponent';
66

77
export function RouteOutlet(props: OutletProps) {
88
const context = useRendererContext();
99
const location = useRouteLocation();
10-
const { schema, packageManager } = context;
10+
const { schema, packageManager, options } = context;
1111

1212
const pageConfig = useMemo(() => {
1313
const pages = schema.get('pages') ?? [];
@@ -27,11 +27,12 @@ export function RouteOutlet(props: OutletProps) {
2727
const componentsMap = schema.get('componentsMap');
2828
packageManager.resolveComponentMaps(componentsMap);
2929

30-
const LowCodeComponent = createComponentBySchema(pageConfig.mappingId, {
30+
const LowCodeComponent = createComponent(pageConfig.mappingId, {
3131
displayName: pageConfig.id,
3232
modelOptions: {
3333
metadata: pageConfig,
3434
},
35+
...options.component,
3536
});
3637

3738
return <LowCodeComponent {...props} />;

‎packages/react-renderer/src/runtime/context.ts

-11
This file was deleted.

‎packages/react-renderer/src/runtime/schema.tsx renamed to ‎packages/react-renderer/src/runtime/createComponent.tsx

+35-26
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ import { forwardRef, useRef, useEffect } from 'react';
33
import { isValidElementType } from 'react-is';
44
import { useRendererContext } from '../api/context';
55
import { reactiveStateFactory } from './reactiveState';
6-
import { type ReactComponent, type ReactWidget, createElementByWidget } from './components';
7-
import { ModelContextProvider } from './context';
6+
import { type ReactComponent, type ReactWidget, createElementByWidget } from './elements';
87
import { appendExternalStyle } from '../utils/element';
98

109
import type {
1110
RenderContext,
1211
IComponentTreeModel,
13-
ComponentTreeModelOptions,
12+
CreateComponentTreeModelOptions,
1413
} from '@alilc/lowcode-renderer-core';
15-
import type { ReactInstance, CSSProperties, ForwardedRef } from 'react';
14+
import type { ReactInstance, CSSProperties, ForwardedRef, ReactNode } from 'react';
1615

1716
export interface ComponentOptions {
1817
displayName?: string;
19-
modelOptions?: ComponentTreeModelOptions;
18+
modelOptions?: Pick<CreateComponentTreeModelOptions, 'id' | 'metadata'>;
2019

21-
widgetCreated?(widget: ReactWidget): void;
22-
componentRefAttached?(widget: ReactWidget, instance: ReactInstance): void;
20+
beforeElementCreate?(widget: ReactWidget): ReactWidget;
21+
elementCreated?(widget: ReactWidget, element: ReactNode): ReactNode;
22+
componentRefAttached?(widget: ReactWidget, instance: ReactInstance | null): void;
2323
}
2424

2525
export interface LowCodeComponentProps {
@@ -37,6 +37,7 @@ const lowCodeComponentsCache = new Map<string, ReactComponent>();
3737
export function getComponentByName(
3838
name: string,
3939
{ packageManager, boostsManager }: RenderContext,
40+
componentOptions: ComponentOptions = {},
4041
): ReactComponent {
4142
const result = lowCodeComponentsCache.get(name) || packageManager.getComponent(name);
4243

@@ -58,7 +59,8 @@ export function getComponentByName(
5859
});
5960
}
6061

61-
const lowCodeComponent = createComponentBySchema(componentsTree[0], {
62+
const lowCodeComponent = createComponent(componentsTree[0], {
63+
...componentOptions,
6264
displayName: name,
6365
modelOptions: {
6466
id: metadata.id,
@@ -76,40 +78,49 @@ export function getComponentByName(
7678
return result;
7779
}
7880

79-
export function createComponentBySchema(
81+
export function createComponent(
8082
schema: string | Spec.ComponentTreeRoot,
81-
{ displayName = '__LowCodeComponent__', modelOptions }: ComponentOptions = {},
83+
componentOptions: ComponentOptions = {},
8284
) {
85+
const { displayName = '__LowCodeComponent__', modelOptions } = componentOptions;
86+
8387
const LowCodeComponent = forwardRef(function (
8488
props: LowCodeComponentProps,
8589
ref: ForwardedRef<any>,
8690
) {
87-
const renderContext = useRendererContext();
88-
const { options, componentTreeModel } = renderContext;
91+
const context = useRendererContext();
92+
const { options: globalOptions, componentTreeModel } = context;
8993

9094
const modelRef = useRef<IComponentTreeModel<ReactComponent, ReactInstance>>();
9195

9296
if (!modelRef.current) {
97+
const finalOptions: CreateComponentTreeModelOptions = {
98+
...modelOptions,
99+
codeScopeValue: {
100+
props,
101+
},
102+
stateCreator: reactiveStateFactory,
103+
dataSourceCreator: globalOptions.dataSourceCreator,
104+
};
105+
93106
if (typeof schema === 'string') {
94-
modelRef.current = componentTreeModel.createById(schema, modelOptions);
107+
modelRef.current = componentTreeModel.createById(schema, finalOptions);
95108
} else {
96-
modelRef.current = componentTreeModel.create(schema, modelOptions);
109+
modelRef.current = componentTreeModel.create(schema, finalOptions);
97110
}
111+
console.log(
112+
'%c [ model ]-103',
113+
'font-size:13px; background:pink; color:#bf2c9f;',
114+
modelRef.current,
115+
);
98116
}
99117

100118
const model = modelRef.current!;
101-
console.log('%c [ model ]-103', 'font-size:13px; background:pink; color:#bf2c9f;', model);
102119

103120
const isConstructed = useRef(false);
104121
const isMounted = useRef(false);
105122

106123
if (!isConstructed.current) {
107-
model.initialize({
108-
defaultProps: props,
109-
stateCreator: reactiveStateFactory,
110-
dataSourceCreator: options.dataSourceCreator,
111-
});
112-
113124
model.triggerLifeCycle('constructor');
114125
isConstructed.current = true;
115126

@@ -142,11 +153,9 @@ export function createComponentBySchema(
142153
}, []);
143154

144155
return (
145-
<ModelContextProvider value={model}>
146-
<div id={props.id} className={props.className} style={props.style} ref={ref}>
147-
{model.widgets.map((w) => createElementByWidget(w, model.codeScope))}
148-
</div>
149-
</ModelContextProvider>
156+
<div id={props.id} className={props.className} style={props.style} ref={ref}>
157+
{model.widgets.map((w) => createElementByWidget(w, w.model.codeRuntime, componentOptions))}
158+
</div>
150159
);
151160
});
152161

0 commit comments

Comments
 (0)
Please sign in to comment.