Skip to content

Commit

Permalink
fix: fix renderer some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
1ncounter committed Jun 28, 2024
1 parent ac8aa2c commit a855c05
Show file tree
Hide file tree
Showing 33 changed files with 255 additions and 388 deletions.
10 changes: 1 addition & 9 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@
},
"dependencies": {
"@alilc/lowcode-shared": "workspace:*",
"@alilc/lowcode-types": "workspace:*",
"@alilc/lowcode-utils": "workspace:*",
"@formatjs/intl": "^2.10.1",
"lodash-es": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"events": "^3.3.0"
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
Expand All @@ -49,8 +43,6 @@
},
"peerDependencies": {
"@alilc/lowcode-shared": "workspace:*",
"@alilc/lowcode-types": "workspace:*",
"@alilc/lowcode-utils": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from './preference';
export * from './hotkey';
export * from './intl';
export * from './instantiation';
154 changes: 0 additions & 154 deletions packages/core/src/intl.ts

This file was deleted.

19 changes: 5 additions & 14 deletions packages/react-renderer/src/api/app.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { createRenderer, type AppOptions } from '@alilc/lowcode-renderer-core';
import { type ComponentType } from 'react';
import { createRenderer } from '@alilc/lowcode-renderer-core';
import { type Root, createRoot } from 'react-dom/client';
import { ApplicationView, RendererContext, boosts } from '../app';

export interface ReactAppOptions extends AppOptions {
faultComponent?: ComponentType<any>;
}
import { type ReactAppOptions, RendererContext } from './context';
import { ApplicationView, boosts } from '../app';

export const createApp = async (options: ReactAppOptions) => {
return createRenderer(async (context) => {
const { schema, boostsManager } = context;

// set config
// if (options.faultComponent) {
// context.config.set('faultComponent', options.faultComponent);
// }

// extends boosts
boostsManager.extend(boosts.toExpose());

let root: Root | undefined;
Expand All @@ -27,10 +17,11 @@ export const createApp = async (options: ReactAppOptions) => {

const defaultId = schema.get('config')?.targetRootID ?? 'app';
const rootElement = normalizeContainer(containerOrId, defaultId);
const contextValue = { ...context, options };

root = createRoot(rootElement);
root.render(
<RendererContext.Provider value={context}>
<RendererContext.Provider value={contextValue}>
<ApplicationView />
</RendererContext.Provider>,
);
Expand Down
5 changes: 3 additions & 2 deletions packages/react-renderer/src/api/component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRenderer, type AppOptions } from '@alilc/lowcode-renderer-core';
import { FunctionComponent } from 'react';
import { type LowCodeComponentProps, createComponentBySchema } from '../runtime/schema';
import { RendererContext } from '../app/context';
import { RendererContext } from '../api/context';

interface Render {
toComponent(): FunctionComponent<LowCodeComponentProps>;
Expand All @@ -12,10 +12,11 @@ export async function createComponent(options: AppOptions) {
const { schema } = context;

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

function Component(props: LowCodeComponentProps) {
return (
<RendererContext.Provider value={context}>
<RendererContext.Provider value={contextValue}>
<LowCodeComponent {...props} />
</RendererContext.Provider>
);
Expand Down
14 changes: 14 additions & 0 deletions packages/react-renderer/src/api/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type ComponentType, createContext, useContext } from 'react';
import { type AppOptions, type RenderContext } from '@alilc/lowcode-renderer-core';

export interface ReactAppOptions extends AppOptions {
faultComponent?: ComponentType<any>;
}

export const RendererContext = createContext<RenderContext & { options: ReactAppOptions }>(
undefined!,
);

RendererContext.displayName = 'RendererContext';

export const useRendererContext = () => useContext(RendererContext);
8 changes: 0 additions & 8 deletions packages/react-renderer/src/app/context.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/react-renderer/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './context';
export * from './boosts';
export * from './view';
8 changes: 4 additions & 4 deletions packages/react-renderer/src/app/view.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useRenderContext } from './context';
import { useRendererContext } from '../api/context';
import { getComponentByName } from '../runtime/schema';
import { boosts } from './boosts';

export function ApplicationView() {
const renderContext = useRenderContext();
const { schema } = renderContext;
const rendererContext = useRendererContext();
const { schema } = rendererContext;
const appWrappers = boosts.getAppWrappers();
const Outlet = boosts.getOutlet();

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

if (layoutConfig) {
const componentName = layoutConfig.componentName;
const Layout = getComponentByName(componentName, renderContext);
const Layout = getComponentByName(componentName, rendererContext);

if (Layout) {
const layoutProps: any = layoutConfig.props ?? {};
Expand Down
4 changes: 3 additions & 1 deletion packages/react-renderer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export * from './api/app';
export * from './api/component';
export { useRenderContext, defineRendererPlugin } from './app';
export * from './api/context';
export { defineRendererPlugin } from './app';
export * from './router';
export { LifecyclePhase } from '@alilc/lowcode-renderer-core';

export type { Spec, ProCodeComponent, LowCodeComponent } from '@alilc/lowcode-shared';
export type { PackageLoader, CodeScope, Plugin } from '@alilc/lowcode-renderer-core';
Expand Down
2 changes: 1 addition & 1 deletion packages/react-renderer/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './context';
export * from './plugin';
export type { Router, RouterHistory } from '@alilc/lowcode-renderer-router';
export type * from '@alilc/lowcode-renderer-router';
14 changes: 5 additions & 9 deletions packages/react-renderer/src/router/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineRendererPlugin } from '../app/boosts';
import { LifecyclePhase } from '@alilc/lowcode-renderer-core';
import { createRouter, type RouterOptions } from '@alilc/lowcode-renderer-router';
import { createRouterView } from './routerView';
import { RouteOutlet } from './route';
Expand All @@ -13,7 +12,7 @@ const defaultRouterOptions: RouterOptions = {
export const routerPlugin = defineRendererPlugin({
name: 'rendererRouter',
async setup(context) {
const { whenLifeCylePhaseChange, schema, boosts } = context;
const { schema, boosts } = context;

let routerConfig = defaultRouterOptions;

Expand All @@ -27,17 +26,14 @@ export const routerPlugin = defineRendererPlugin({
}

const router = createRouter(routerConfig);

boosts.codeRuntime.getScope().set('router', router);
boosts.temporaryUse('router', router);

const RouterView = createRouterView(router);

boosts.addAppWrapper(RouterView);
boosts.setOutlet(RouteOutlet);

whenLifeCylePhaseChange(LifecyclePhase.AfterInitPackageLoad).then(() => {
return router.isReady();
});
boosts.codeRuntime.getScope().set('router', router);
boosts.temporaryUse('router', router);

await router.isReady();
},
});
4 changes: 2 additions & 2 deletions packages/react-renderer/src/router/route.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useMemo } from 'react';
import { useRenderContext } from '../app/context';
import { useRendererContext } from '../api/context';
import { OutletProps } from '../app/boosts';
import { useRouteLocation } from './context';
import { createComponentBySchema } from '../runtime/schema';

export function RouteOutlet(props: OutletProps) {
const context = useRenderContext();
const context = useRendererContext();
const location = useRouteLocation();
const { schema, packageManager } = context;

Expand Down
Loading

0 comments on commit a855c05

Please sign in to comment.