Skip to content

Commit

Permalink
feat: add common.utils.executeTransaction API to change multi nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Nov 8, 2022
1 parent 5536519 commit 7bf7ca4
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 34 deletions.
19 changes: 17 additions & 2 deletions packages/designer/src/builtin-simulator/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
hasOwnProperty,
UtilsMetadata,
getClosestNode,
transactionManager,
} from '@alilc/lowcode-utils';
import {
DragObjectType,
Expand All @@ -59,9 +60,8 @@ import { getClosestClickableNode } from './utils/clickable';
import {
ComponentMetadata,
ComponentSchema,
TransformStage,
ActivityData,
Package,
TransitionType,
} from '@alilc/lowcode-types';
import { BuiltinSimulatorRenderer } from './renderer';
import clipboard from '../designer/clipboard';
Expand Down Expand Up @@ -181,6 +181,14 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
*/
autoRender = true;

stopAutoRepaintNode() {
this.renderer?.stopAutoRepaintNode();
}

enableAutoRepaintNode() {
this.renderer?.enableAutoRepaintNode();
}

constructor(project: Project) {
makeObservable(this);
this.project = project;
Expand All @@ -194,6 +202,13 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
i18n: this.project.i18n,
};
});
transactionManager.onStartTransaction(() => {
this.stopAutoRepaintNode();
}, TransitionType.REPAINT);
transactionManager.onEndTransaction(() => {
this.rerender();
this.enableAutoRepaintNode();
}, TransitionType.REPAINT);
}

get currentDocument() {
Expand Down
2 changes: 2 additions & 0 deletions packages/designer/src/builtin-simulator/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface BuiltinSimulatorRenderer {
setCopyState(state: boolean): void;
loadAsyncLibrary(asyncMap: { [index: string]: any }): void;
clearState(): void;
stopAutoRepaintNode(): void;
enableAutoRepaintNode(): void;
run(): void;
}

Expand Down
33 changes: 16 additions & 17 deletions packages/designer/src/document/history.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events';
import { autorun, reaction, mobx, untracked, globalContext, Editor } from '@alilc/lowcode-editor-core';
import { reaction, untracked, globalContext, Editor } from '@alilc/lowcode-editor-core';
import { NodeSchema } from '@alilc/lowcode-types';
import { History as ShellHistory } from '@alilc/lowcode-shell';

Expand Down Expand Up @@ -32,31 +32,30 @@ export class History<T = NodeSchema> {
this.currentSerialization = serialization;
}

constructor(dataFn: () => T, private redoer: (data: T) => void, private timeGap: number = 1000) {
constructor(dataFn: () => T | null, private redoer: (data: T) => void, private timeGap: number = 1000) {
this.session = new Session(0, null, this.timeGap);
this.records = [this.session];

reaction(() => {
reaction((): any => {
return dataFn();
}, (data: T) => {
if (this.asleep) return;
untracked(() => {
const log = this.currentSerialization.serialize(data);
if (this.session.isActive()) {
this.session.log(log);
} else {
this.session.end();
const lastState = this.getState();
const cursor = this.session.cursor + 1;
const session = new Session(cursor, log, this.timeGap);
this.session = session;
this.records.splice(cursor, this.records.length - cursor, session);
const currentState = this.getState();
if (currentState !== lastState) {
this.emitter.emit('statechange', currentState);
}
if (this.session.isActive()) {
this.session.log(log);
} else {
this.session.end();
const lastState = this.getState();
const cursor = this.session.cursor + 1;
const session = new Session(cursor, log, this.timeGap);
this.session = session;
this.records.splice(cursor, this.records.length - cursor, session);
const currentState = this.getState();
if (currentState !== lastState) {
this.emitter.emit('statechange', currentState);
}
// }
}
});
}, { fireImmediately: true });
}
Expand Down
5 changes: 3 additions & 2 deletions packages/engine/src/modules/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isFormEvent, compatibleLegaoSchema, getNodeSchemaById } from '@alilc/lowcode-utils';
import { isFormEvent, compatibleLegaoSchema, getNodeSchemaById, transactionManager } from '@alilc/lowcode-utils';
import { isNodeSchema } from '@alilc/lowcode-types';
import { getConvertedExtraKey, getOriginalExtraKey, isNode, isSettingField } from '@alilc/lowcode-designer';
import { getConvertedExtraKey, getOriginalExtraKey } from '@alilc/lowcode-designer';

const utils = {
isNodeSchema,
Expand All @@ -9,6 +9,7 @@ const utils = {
getNodeSchemaById,
getConvertedExtraKey,
getOriginalExtraKey,
executeTransaction: transactionManager.executeTransaction.bind(transactionManager),
};

export default utils;
13 changes: 13 additions & 0 deletions packages/react-simulator-renderer/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
* 是否为画布自动渲染
*/
autoRender = true;

/**
* 画布是否自动监听事件来重绘节点
*/
autoRepaintNode = true;
/**
* 加载资源
*/
Expand Down Expand Up @@ -491,6 +496,14 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
this._appContext = { ...this._appContext };
}

stopAutoRepaintNode() {
this.autoRepaintNode = false;
}

enableAutoRepaintNode() {
this.autoRepaintNode = true;
}

dispose() {
this.disposeFunctions.forEach(fn => fn());
this.documentInstances.forEach(docInst => docInst.dispose());
Expand Down
46 changes: 35 additions & 11 deletions packages/renderer-core/src/hoc/leaf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,23 @@ function initRerenderEvent({
leaf,
dispose: [
leaf?.onPropChange?.(() => {
if (!container.autoRepaintNode) {
return;
}
__debug(`${schema.componentName}[${schema.id}] leaf not render in SimulatorRendererView, leaf onPropsChange make rerender`);
container.rerender();
}),
leaf?.onChildrenChange?.(() => {
if (!container.autoRepaintNode) {
return;
}
__debug(`${schema.componentName}[${schema.id}] leaf not render in SimulatorRendererView, leaf onChildrenChange make rerender`);
container.rerender();
}) as Function,
leaf?.onVisibleChange?.(() => {
if (!container.autoRepaintNode) {
return;
}
__debug(`${schema.componentName}[${schema.id}] leaf not render in SimulatorRendererView, leaf onVisibleChange make rerender`);
container.rerender();
}),
Expand Down Expand Up @@ -213,14 +222,18 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
}

componentDidMount() {
const _leaf = this.leaf;
this.initOnPropsChangeEvent(_leaf);
this.initOnChildrenChangeEvent(_leaf);
this.initOnVisibleChangeEvent(_leaf);
this.recordTime();
}

get defaultState() {
getDefaultState(nextProps: any) {
const {
hidden = false,
condition = true,
} = this.leaf?.export?.(TransformStage.Render) || {};
} = nextProps.__inner__ || this.leaf?.export?.(TransformStage.Render) || {};
return {
nodeChildren: null,
childrenInState: false,
Expand All @@ -236,19 +249,15 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
// 监听以下事件,当变化时更新自己
__debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`);
clearRerenderEvent(componentCacheId);
const _leaf = this.leaf;
this.initOnPropsChangeEvent(_leaf);
this.initOnChildrenChangeEvent(_leaf);
this.initOnVisibleChangeEvent(_leaf);
this.curEventLeaf = _leaf;
this.curEventLeaf = this.leaf;

cache.ref.set(componentCacheId, {
makeUnitRender: this.makeUnitRender,
});

let cacheState = cache.state.get(componentCacheId);
if (!cacheState || cacheState.__tag !== props.__tag) {
cacheState = this.defaultState;
cacheState = this.getDefaultState(props);
}

this.state = cacheState;
Expand Down Expand Up @@ -279,6 +288,10 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
singleRender?: boolean;
};

get autoRepaintNode() {
return container.autoRepaintNode;
}

judgeMiniUnitRender() {
if (!this.renderUnitInfo) {
this.getRenderUnitInfo();
Expand Down Expand Up @@ -380,13 +393,16 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
const {
visible,
...resetState
} = this.defaultState;
} = this.getDefaultState(nextProps);
this.setState(resetState);
}

/** 监听参数变化 */
initOnPropsChangeEvent(leaf = this.leaf): void {
const dispose = leaf?.onPropChange?.(debounce((propChangeInfo: PropChangeOptions) => {
const dispose = leaf?.onPropChange?.((propChangeInfo: PropChangeOptions) => {
if (!this.autoRepaintNode) {
return;
}
const {
key,
newValue = null,
Expand Down Expand Up @@ -433,7 +449,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
});

this.judgeMiniUnitRender();
}, 30));
});

dispose && this.disposeFunctions.push(dispose);
}
Expand All @@ -443,6 +459,9 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
*/
initOnVisibleChangeEvent(leaf = this.leaf) {
const dispose = leaf?.onVisibleChange?.((flag: boolean) => {
if (!this.autoRepaintNode) {
return;
}
if (this.state.visible === flag) {
return;
}
Expand All @@ -463,6 +482,9 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
*/
initOnChildrenChangeEvent(leaf = this.leaf) {
const dispose = leaf?.onChildrenChange?.((param): void => {
if (!this.autoRepaintNode) {
return;
}
const {
type,
node,
Expand Down Expand Up @@ -540,6 +562,8 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
ref: forwardedRef,
};

delete compProps.__inner__;

return engine.createElement(Comp, compProps, this.hasChildren ? this.children : null);
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/renderer-core/src/renderer/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,14 @@ export default function baseRendererFactory(): IBaseRenderComponent {
}
}
}
return renderComp({ ...props, ...otherProps });
return renderComp({
...props,
...otherProps,
__inner__: {
hidden: schema.hidden,
condition,
},
});
} catch (e) {
return engine.createElement(engine.getFaultComponent(), {
error: e,
Expand Down
3 changes: 2 additions & 1 deletion packages/renderer-core/tests/hoc/leaf.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const baseRenderer: any = {
__container: {
rerender: () => {
rerenderCount = 1 + rerenderCount;
}
},
autoRepaintNode: true,
},
documentId: '01'
},
Expand Down
Loading

0 comments on commit 7bf7ca4

Please sign in to comment.