feat: add common.utils.startTransaction API to change multi nodes

This commit is contained in:
liujuping 2022-11-02 14:39:29 +08:00
parent 319b13b559
commit e8fde6189c
12 changed files with 134 additions and 35 deletions

View File

@ -36,6 +36,7 @@ import {
hasOwnProperty,
UtilsMetadata,
getClosestNode,
transactionManager,
} from '@alilc/lowcode-utils';
import {
DragObjectType,
@ -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';
@ -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;
@ -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() {

View File

@ -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;
}

View File

@ -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';
@ -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 => {
if (this.asleep) return null;
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 });
}

View File

@ -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,
@ -9,6 +9,7 @@ const utils = {
getNodeSchemaById,
getConvertedExtraKey,
getOriginalExtraKey,
startTransaction: transactionManager.startTransaction,
};
export default utils;

View File

@ -349,6 +349,11 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
*
*/
autoRender = true;
/**
*
*/
autoRepaintNode = true;
/**
*
*/
@ -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());

View File

@ -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();
}),
@ -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__ || {};
return {
nodeChildren: null,
childrenInState: false,
@ -236,11 +249,7 @@ 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,
@ -248,7 +257,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
let cacheState = cache.state.get(componentCacheId);
if (!cacheState || cacheState.__tag !== props.__tag) {
cacheState = this.defaultState;
cacheState = this.getDefaultState(props);
}
this.state = cacheState;
@ -279,6 +288,10 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
singleRender?: boolean;
};
get autoRepaintNode() {
return container.autoRepaintNode;
}
judgeMiniUnitRender() {
if (!this.renderUnitInfo) {
this.getRenderUnitInfo();
@ -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,
@ -433,7 +449,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
});
this.judgeMiniUnitRender();
}, 30));
});
dispose && this.disposeFunctions.push(dispose);
}
@ -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;
}
@ -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,
@ -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);
}
}

View File

@ -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,

View File

@ -35,7 +35,8 @@ const baseRenderer: any = {
__container: {
rerender: () => {
rerenderCount = 1 + rerenderCount;
}
},
autoRepaintNode: true,
},
documentId: '01'
},

View File

@ -21,3 +21,4 @@ export * from './code-result';
export * from './assets';
export * as GlobalEvent from './event';
export * from './disposable';
export * from './start-transaction';

View File

@ -0,0 +1,4 @@
export enum TransitionType {
/** 节点更新后重绘处理 */
REPAINT
}

View File

@ -26,3 +26,4 @@ export * from './node-helper';
export * from './clone-enumerable-property';
export * from './logger';
export * as css from './css-helper';
export { transactionManager } from './start-transaction';

View File

@ -0,0 +1,31 @@
import { TransitionType } from '@alilc/lowcode-types';
import { transaction } from 'mobx';
import EventEmitter from 'events';
class TransactionManager {
emitter = new EventEmitter();
startTransaction = (fn: () => void, type: TransitionType = TransitionType.REPAINT): void => {
this.emitter.emit(`[${type}]startTransaction`);
transaction(fn);
this.emitter.emit(`[${type}]endTransaction`);
};
onStartTransaction = (fn: () => void, type: TransitionType = TransitionType.REPAINT): () => void => {
this.emitter.on(`[${type}]startTransaction`, fn);
return () => {
this.emitter.off(`[${type}]startTransaction`, fn);
};
};
onEndTransaction = (fn: () => void, type: TransitionType = TransitionType.REPAINT): () => void => {
this.emitter.on(`[${type}]endTransaction`, fn);
return () => {
this.emitter.off(`[${type}]endTransaction`, fn);
};
};
}
export const transactionManager = new TransactionManager();
export default transactionManager;