Skip to content

Commit

Permalink
fix: fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
1ncounter committed Jun 1, 2024
1 parent f1711e0 commit 0157bbd
Show file tree
Hide file tree
Showing 28 changed files with 270 additions and 290 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"private": true,
"type": "module",
"scripts": {
"playground": "pnpm --filter playground dev",
"build": "node ./scripts/build.js",
"test": "vitest",
"clean": "rimraf ./packages/*/dist",
Expand Down Expand Up @@ -35,6 +34,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"globals": "^15.0.0",
"husky": "^9.0.11",
"jsdom": "^24.1.0",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"rimraf": "^5.0.2",
Expand Down
5 changes: 5 additions & 0 deletions packages/react-renderer/src/components/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function RouteOutlet({ pageConfig }: OutletProps) {
const context = useRenderContext();
const { schema, packageManager } = context;
const { type = 'lowCode', mappingId } = pageConfig;
console.log(
'%c [ pageConfig ]-29',
'font-size:13px; background:pink; color:#bf2c9f;',
pageConfig,
);

if (type === 'lowCode') {
// 在页面渲染时重新获取 componentsMap
Expand Down
6 changes: 3 additions & 3 deletions packages/react-renderer/src/components/routerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export const createRouterProvider = (router: Router) => {
return () => remove();
}, []);

const pageSchema = useMemo(() => {
const pageConfig = useMemo(() => {
const pages = schema.get('pages') ?? [];
const matched = location.matched[location.matched.length - 1];

if (matched) {
const page = pages.find((item) => matched.page === item.id);
const page = pages.find((item) => matched.page === item.mappingId);
return page;
}

Expand All @@ -28,7 +28,7 @@ export const createRouterProvider = (router: Router) => {
return (
<RouterContext.Provider value={router}>
<RouteLocationContext.Provider value={location}>
<PageConfigContext.Provider value={pageSchema}>{children}</PageConfigContext.Provider>
<PageConfigContext.Provider value={pageConfig}>{children}</PageConfigContext.Provider>
</RouteLocationContext.Provider>
</RouterContext.Provider>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/react-renderer/src/runtime/dataSource.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export const dataSourceCreator = () => ({}) as any;
export const dataSourceCreator = () =>
({
dataSourceMap: {},
reloadDataSource: () => {},
}) as any;
35 changes: 23 additions & 12 deletions packages/react-renderer/src/runtime/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { processValue, someValue } from '@alilc/lowcode-renderer-core';
import {
watch,
isJSExpression,
isJSFunction,
isJSSlot,
invariant,
isLowCodeComponentSchema,
isLowCodeComponentPackage,
isJSI18nNode,
} from '@alilc/lowcode-shared';
import { forwardRef, useRef, useEffect, createElement, memo } from 'react';
import { appendExternalStyle } from '../utils/element';
import { appendExternalStyle } from '../../../../playground/renderer/src/plugin/remote/element';
import { reactive } from '../utils/reactive';
import { useRenderContext } from '../context/render';
import { reactiveStateCreator } from './reactiveState';
Expand Down Expand Up @@ -63,7 +62,7 @@ export function getComponentByName(

invariant(result, `${name} component not found in componentsRecord`);

if (isLowCodeComponentSchema(result)) {
if (isLowCodeComponentPackage(result)) {
const { componentsMap, componentsTree, utils, i18n } = result.schema;

if (componentsMap.length > 0) {
Expand Down Expand Up @@ -120,6 +119,7 @@ export function createComponentBySchema(
}

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

const isConstructed = useRef(false);
const isMounted = useRef(false);
Expand All @@ -133,7 +133,7 @@ export function createComponentBySchema(
const scopeValue = model.codeScope.value;

// init dataSource
scopeValue.reloadDataSource();
scopeValue.reloadDataSource?.();

let styleEl: HTMLElement | undefined;
const cssText = model.getCssText();
Expand All @@ -148,19 +148,19 @@ export function createComponentBySchema(
model.triggerLifeCycle('componentDidMount');

// 当 state 改变之后调用
const unwatch = watch(scopeValue.state, (_, oldVal) => {
if (isMounted.current) {
model.triggerLifeCycle('componentDidUpdate', props, oldVal);
}
});
// const unwatch = watch(scopeValue.state, (_, oldVal) => {
// if (isMounted.current) {
// model.triggerLifeCycle('componentDidUpdate', props, oldVal);
// }
// });

isMounted.current = true;

return () => {
// componentWillUnmount?.();
model.triggerLifeCycle('componentWillUnmount');
styleEl?.parentNode?.removeChild(styleEl);
unwatch();
// unwatch();
isMounted.current = false;
};
}, []);
Expand Down Expand Up @@ -247,7 +247,7 @@ function createElementByWidget(
};

// 先将 jsslot, jsFunction 对象转换
const finalProps = processValue(
let finalProps = processValue(
componentProps,
(node) => isJSFunction(node) || isJSSlot(node),
(node: Spec.JSSlot | Spec.JSFunction) => {
Expand Down Expand Up @@ -288,6 +288,17 @@ function createElementByWidget(
},
);

finalProps = processValue(
finalProps,
(value) => {
return value.type === 'JSSlot' && !value.value;
},
(node) => {
console.log('%c [ node ]-303', 'font-size:13px; background:pink; color:#bf2c9f;', node);
return null;
},
);

const childElements = children?.map((child) =>
createElementByWidget(child, codeScope, renderContext, componentRefAttached),
);
Expand Down
158 changes: 0 additions & 158 deletions packages/react-renderer/src/utils/element.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/react-renderer/src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export function normalizeComponentNode(node: Spec.ComponentNode): NormalizedComp
...node,
loopArgs: node.loopArgs ?? ['item', 'index'],
props: node.props ?? {},
condition: node.condition || node.condition === false ? node.condition : true,
};
}
2 changes: 1 addition & 1 deletion packages/react-renderer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"]
"include": ["src", "../../playground/renderer/src/plugin/remote/element.ts"]
}
17 changes: 0 additions & 17 deletions packages/renderer-core/__tests__/boosts.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/renderer-core/__tests__/code-runtime.spec.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/renderer-core/__tests__/package.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, beforeAll } from 'vitest';
import { ICodeScope, CodeScope } from '../../../src/parts/code-runtime';

describe('codeScope', () => {
let scope: ICodeScope;

beforeAll(() => {
scope = new CodeScope({});
});

it('should inject a new value', () => {
scope.inject('username', 'Alice');
expect(scope.value).toEqual({ username: 'Alice' });
});

it('should not overwrite an existing value without force', () => {
scope.inject('username', 'Bob');
expect(scope.value).toEqual({ username: 'Alice' });
});

it('should overwrite an existing value with force', () => {
scope.inject('username', 'Bob', true);
expect(scope.value).toEqual({ username: 'Bob' });
});

it('should set new value without replacing existing values', () => {
scope.setValue({ age: 25 });
expect(scope.value).toEqual({ username: 'Bob', age: 25 });
});

it('should set new value and replace all existing values', () => {
scope.setValue({ loggedIn: true }, true);
expect(scope.value).toEqual({ loggedIn: true });
});

it('should create a child scope with initial values', () => {
const childScope = scope.createChild({ sessionId: 'abc123' });
expect(childScope.value).toEqual({ loggedIn: true, sessionId: 'abc123' });
});

it('should set new values in the child scope without affecting the parent scope', () => {
const childScope = scope.createChild({ theme: 'dark' });
expect(childScope.value).toEqual({ loggedIn: true, sessionId: 'abc123', theme: 'dark' });
expect(scope.value).toEqual({ loggedIn: true });
});
});
Loading

0 comments on commit 0157bbd

Please sign in to comment.