GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: the renderer-core leaf component removes the react.createElemen… · DBvc/lowcode-engine@c3ce042

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • designer/src
      • builtin-simulator
        • renderer.ts
      • document
        • document-model.ts
        • node
          • node.ts
    • renderer-core
      • src
        • hoc
          • leaf.tsx
        • renderer
          • base.tsx
          • renderer.tsx
        • types
          • index.ts
      • tests
        • hoc
          • leaf.test.tsx
        • renderer
          • base.test.tsx
        • utils
          • node.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/designer/src/builtin-simulator/renderer.ts‎

Copy file name to clipboardExpand all lines: packages/designer/src/builtin-simulator/renderer.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { IPublicTypeNodeSchema, IPublicTypeComponentInstance, IPublicTypeNodeIns
33

44
export interface BuiltinSimulatorRenderer {
55
readonly isSimulatorRenderer: true;
6+
autoRepaintNode?: boolean;
7+
components: Record<string, Component>;
8+
rerender: () => void;
69
createComponent(schema: IPublicTypeNodeSchema): Component | null;
710
getComponent(componentName: string): Component;
811
getClosestNodeInstance(
Collapse file

‎packages/designer/src/document/document-model.ts‎

Copy file name to clipboardExpand all lines: packages/designer/src/document/document-model.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface IDocumentModel extends Omit< IPublicModelDocumentModel, 'select
6464
dragObject: IPublicTypeDragNodeObject | IPublicTypeNodeSchema | INode | IPublicTypeDragNodeDataObject,
6565
): boolean;
6666

67+
getNodeCount(): number;
6768
}
6869

6970
export class DocumentModel implements IDocumentModel {
Collapse file

‎packages/designer/src/document/node/node.ts‎

Copy file name to clipboardExpand all lines: packages/designer/src/document/node/node.ts
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
IPublicModelNode,
1616
IPublicModelExclusiveGroup,
1717
IPublicEnumTransformStage,
18+
IPublicTypeDisposable,
1819
} from '@alilc/lowcode-types';
1920
import { compatStage, isDOMText, isJSExpression, isNode } from '@alilc/lowcode-utils';
2021
import { SettingTopEntry } from '@alilc/lowcode-designer';
@@ -112,6 +113,10 @@ export interface INode extends IPublicModelNode {
112113
replaceChild(node: INode, data: any): INode;
113114

114115
getSuitablePlace(node: INode, ref: any): any;
116+
117+
onChildrenChange(fn: (param?: { type: string; node: INode }) => void): IPublicTypeDisposable;
118+
119+
onPropChange(func: (info: IPublicTypePropChangeOptions) => void): IPublicTypeDisposable;
115120
}
116121

117122
/**
@@ -1080,7 +1085,7 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
10801085
return this.props;
10811086
}
10821087

1083-
onChildrenChange(fn: (param?: { type: string; node: Node }) => void): (() => void) | undefined {
1088+
onChildrenChange(fn: (param?: { type: string; node: Node }) => void): IPublicTypeDisposable {
10841089
const wrappedFunc = wrapWithEventSwitch(fn);
10851090
return this.children?.onChange(wrappedFunc);
10861091
}
@@ -1273,7 +1278,7 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
12731278
this.emitter?.emit('propChange', val);
12741279
}
12751280

1276-
onPropChange(func: (info: IPublicTypePropChangeOptions) => void): Function {
1281+
onPropChange(func: (info: IPublicTypePropChangeOptions) => void): IPublicTypeDisposable {
12771282
const wrappedFunc = wrapWithEventSwitch(func);
12781283
this.emitter.on('propChange', wrappedFunc);
12791284
return () => {
Collapse file

‎packages/renderer-core/src/hoc/leaf.tsx‎

Copy file name to clipboardExpand all lines: packages/renderer-core/src/hoc/leaf.tsx
+86-80Lines changed: 86 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BuiltinSimulatorHost, Node, IPublicTypePropChangeOptions } from '@alilc/lowcode-designer';
1+
import { INode, IPublicTypePropChangeOptions } from '@alilc/lowcode-designer';
22
import { GlobalEvent, IPublicEnumTransformStage, IPublicTypeNodeSchema, IPublicTypeEngineOptions } from '@alilc/lowcode-types';
33
import { isReactComponent, cloneEnumerableProperty } from '@alilc/lowcode-utils';
44
import { debounce } from '../utils/common';
@@ -16,15 +16,17 @@ export interface IComponentHocProps {
1616
__tag: any;
1717
componentId: any;
1818
_leaf: any;
19-
forwardedRef: any;
19+
forwardedRef?: any;
2020
}
2121

2222
export interface IComponentHocState {
2323
childrenInState: boolean;
2424
nodeChildren: any;
2525
nodeCacheProps: any;
26+
2627
/** 控制是否显示隐藏 */
2728
visible: boolean;
29+
2830
/** 控制是否渲染 */
2931
condition: boolean;
3032
nodeProps: any;
@@ -40,15 +42,17 @@ export interface IComponentHoc {
4042
export type IComponentConstruct = (Comp: types.IBaseRenderComponent, info: IComponentHocInfo) => types.IGeneralConstructor;
4143

4244
interface IProps {
43-
_leaf: Node | undefined;
45+
_leaf: INode | undefined;
4446

4547
visible: boolean;
4648

47-
componentId?: number;
49+
componentId: number;
4850

49-
children?: Node[];
51+
children?: INode[];
5052

51-
__tag?: number;
53+
__tag: number;
54+
55+
forwardedRef?: any;
5256
}
5357

5458
enum RerenderType {
@@ -61,8 +65,7 @@ enum RerenderType {
6165

6266
// 缓存 Leaf 层组件,防止重新渲染问题
6367
class LeafCache {
64-
constructor(public documentId: string, public device: string) {
65-
}
68+
6669
/** 组件缓存 */
6770
component = new Map();
6871

@@ -77,6 +80,9 @@ class LeafCache {
7780
event = new Map();
7881

7982
ref = new Map();
83+
84+
constructor(public documentId: string, public device: string) {
85+
}
8086
}
8187

8288
let cache: LeafCache;
@@ -155,11 +161,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
155161
const curDocumentId = baseRenderer.props?.documentId ?? '';
156162
const curDevice = baseRenderer.props?.device ?? '';
157163
const getNode = baseRenderer.props?.getNode;
158-
const container: BuiltinSimulatorHost = baseRenderer.props?.__container;
164+
const container = baseRenderer.props?.__container;
159165
const setSchemaChangedSymbol = baseRenderer.props?.setSchemaChangedSymbol;
160166
const editor = host?.designer?.editor;
161167
const runtime = adapter.getRuntime();
162-
const { forwardRef } = runtime;
168+
const { forwardRef, createElement } = runtime;
163169
const Component = runtime.Component as types.IGeneralConstructor<
164170
IComponentHocProps, IComponentHocState
165171
>;
@@ -192,14 +198,64 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
192198
recordInfo: {
193199
startTime?: number | null;
194200
type?: string;
195-
node?: Node;
201+
node?: INode;
196202
} = {};
203+
204+
private curEventLeaf: INode | undefined;
205+
197206
static displayName = schema.componentName;
198207

199208
disposeFunctions: Array<((() => void) | Function)> = [];
200209

201210
__component_tag = 'leafWrapper';
202211

212+
renderUnitInfo: {
213+
minimalUnitId?: string;
214+
minimalUnitName?: string;
215+
singleRender?: boolean;
216+
};
217+
218+
// 最小渲染单元做防抖处理
219+
makeUnitRenderDebounced = debounce(() => {
220+
this.beforeRender(RerenderType.MinimalRenderUnit);
221+
const schema = this.leaf?.export?.(IPublicEnumTransformStage.Render);
222+
if (!schema) {
223+
return;
224+
}
225+
const nextProps = getProps(schema, scope, Comp, componentInfo);
226+
const children = getChildren(schema, scope, Comp);
227+
const nextState = {
228+
nodeProps: nextProps,
229+
nodeChildren: children,
230+
childrenInState: true,
231+
};
232+
if ('children' in nextProps) {
233+
nextState.nodeChildren = nextProps.children;
234+
}
235+
236+
__debug(`${this.leaf?.componentName}(${this.props.componentId}) MinimalRenderUnit Render!`);
237+
this.setState(nextState);
238+
}, 20);
239+
240+
constructor(props: IProps, context: any) {
241+
super(props, context);
242+
// 监听以下事件,当变化时更新自己
243+
__debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`);
244+
clearRerenderEvent(componentCacheId);
245+
this.curEventLeaf = this.leaf;
246+
247+
cache.ref.set(componentCacheId, {
248+
makeUnitRender: this.makeUnitRender,
249+
});
250+
251+
let cacheState = cache.state.get(componentCacheId);
252+
if (!cacheState || cacheState.__tag !== props.__tag) {
253+
cacheState = this.getDefaultState(props);
254+
}
255+
256+
this.state = cacheState;
257+
}
258+
203259
recordTime = () => {
204260
if (!this.recordInfo.startTime) {
205261
return;
@@ -216,6 +272,14 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
216272
this.recordInfo.startTime = null;
217273
};
218274

275+
makeUnitRender = () => {
276+
this.makeUnitRenderDebounced();
277+
};
278+
279+
get autoRepaintNode() {
280+
return container?.autoRepaintNode;
281+
}
282+
219283
componentDidUpdate() {
220284
this.recordTime();
221285
}
@@ -243,27 +307,6 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
243307
};
244308
}
245309

246-
constructor(props: IProps, context: any) {
247-
super(props, context);
248-
// 监听以下事件,当变化时更新自己
249-
__debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`);
250-
clearRerenderEvent(componentCacheId);
251-
this.curEventLeaf = this.leaf;
252-
253-
cache.ref.set(componentCacheId, {
254-
makeUnitRender: this.makeUnitRender,
255-
});
256-
257-
let cacheState = cache.state.get(componentCacheId);
258-
if (!cacheState || cacheState.__tag !== props.__tag) {
259-
cacheState = this.getDefaultState(props);
260-
}
261-
262-
this.state = cacheState;
263-
}
264-
265-
private curEventLeaf: Node | undefined;
266-
267310
setState(state: any) {
268311
cache.state.set(componentCacheId, {
269312
...this.state,
@@ -274,23 +317,13 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
274317
}
275318

276319
/** 由于内部属性变化,在触发渲染前,会执行该函数 */
277-
beforeRender(type: string, node?: Node): void {
320+
beforeRender(type: string, node?: INode): void {
278321
this.recordInfo.startTime = Date.now();
279322
this.recordInfo.type = type;
280323
this.recordInfo.node = node;
281324
setSchemaChangedSymbol?.(true);
282325
}
283326

284-
renderUnitInfo: {
285-
minimalUnitId?: string;
286-
minimalUnitName?: string;
287-
singleRender?: boolean;
288-
};
289-
290-
get autoRepaintNode() {
291-
return container.autoRepaintNode;
292-
}
293-
294327
judgeMiniUnitRender() {
295328
if (!this.renderUnitInfo) {
296329
this.getRenderUnitInfo();
@@ -308,7 +341,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
308341

309342
if (!ref) {
310343
__debug('Cant find minimalRenderUnit ref! This make rerender!');
311-
container.rerender();
344+
container?.rerender();
312345
return;
313346
}
314347
__debug(`${this.leaf?.componentName}(${this.props.componentId}) need render, make its minimalRenderUnit ${renderUnitInfo.minimalUnitName}(${renderUnitInfo.minimalUnitId})`);
@@ -321,7 +354,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
321354
return;
322355
}
323356

324-
if (leaf.isRoot()) {
357+
if (leaf.isRootNode) {
325358
this.renderUnitInfo = {
326359
singleRender: true,
327360
...(this.renderUnitInfo || {}),
@@ -347,32 +380,6 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
347380
}
348381
}
349382

350-
// 最小渲染单元做防抖处理
351-
makeUnitRenderDebounced = debounce(() => {
352-
this.beforeRender(RerenderType.MinimalRenderUnit);
353-
const schema = this.leaf?.export?.(IPublicEnumTransformStage.Render);
354-
if (!schema) {
355-
return;
356-
}
357-
const nextProps = getProps(schema, scope, Comp, componentInfo);
358-
const children = getChildren(schema, scope, Comp);
359-
const nextState = {
360-
nodeProps: nextProps,
361-
nodeChildren: children,
362-
childrenInState: true,
363-
};
364-
if ('children' in nextProps) {
365-
nextState.nodeChildren = nextProps.children;
366-
}
367-
368-
__debug(`${this.leaf?.componentName}(${this.props.componentId}) MinimalRenderUnit Render!`);
369-
this.setState(nextState);
370-
}, 20);
371-
372-
makeUnitRender = () => {
373-
this.makeUnitRenderDebounced();
374-
};
375-
376383
componentWillReceiveProps(nextProps: any) {
377384
let { componentId } = nextProps;
378385
if (nextProps.__tag === this.props.__tag) {
@@ -420,7 +427,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
420427
// 目前多层循坏无法判断需要从哪一层开始渲染,故先粗暴解决
421428
if (key === '___loop___') {
422429
__debug('key is ___loop___, render a page!');
423-
container.rerender();
430+
container?.rerender();
424431
// 由于 scope 变化,需要清空缓存,使用新的 scope
425432
cache.component.delete(componentCacheId);
426433
return;
@@ -536,7 +543,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
536543
return [];
537544
}
538545

539-
get leaf(): Node | undefined {
546+
get leaf(): INode | undefined {
540547
if (this.props._leaf?.isMock) {
541548
// 低代码组件作为一个整体更新,其内部的组件不需要监听相关事件
542549
return undefined;
@@ -570,13 +577,12 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
570577
}
571578
}
572579

573-
let LeafWrapper = forwardRef((props: any, ref: any) => (
574-
// @ts-ignore
575-
<LeafHoc
576-
{...props}
577-
forwardedRef={ref}
578-
/>
579-
));
580+
let LeafWrapper = forwardRef((props: any, ref: any) => {
581+
return createElement(LeafHoc, {
582+
...props,
583+
forwardedRef: ref,
584+
});
585+
});
580586

581587
LeafWrapper = cloneEnumerableProperty(LeafWrapper, Comp);
582588

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.