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

This commit is contained in:
liujuping 2022-11-04 10:27:46 +08:00
parent 3653f6a970
commit d2046cd225
7 changed files with 34 additions and 28 deletions

View File

@ -36,7 +36,7 @@ import {
hasOwnProperty, hasOwnProperty,
UtilsMetadata, UtilsMetadata,
getClosestNode, getClosestNode,
transactionManage, transactionManager,
} from '@alilc/lowcode-utils'; } from '@alilc/lowcode-utils';
import { import {
DragObjectType, DragObjectType,
@ -202,11 +202,16 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
i18n: this.project.i18n, i18n: this.project.i18n,
}; };
}); });
transactionManage.onStartTransaction(this.stopAutoRepaintNode, TransitionType.repaint); transactionManager.onStartTransaction(() => {
transactionManage.onEndTransaction(() => { this.project.currentDocument?.history?.sleep();
this.stopAutoRepaintNode();
}, TransitionType.REPAINT);
transactionManager.onEndTransaction(() => {
this.project.currentDocument?.history?.wakeup();
this.project.currentDocument?.history?.savePoint();
this.rerender(); this.rerender();
this.enableAutoRepaintNode(); this.enableAutoRepaintNode();
}, TransitionType.repaint); }, TransitionType.REPAINT);
} }
get currentDocument() { get currentDocument() {

View File

@ -32,11 +32,12 @@ export class History<T = NodeSchema> {
this.currentSerialization = serialization; 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.session = new Session(0, null, this.timeGap);
this.records = [this.session]; this.records = [this.session];
reaction(() => { reaction((): any => {
if (this.asleep) return null;
return dataFn(); return dataFn();
}, (data: T) => { }, (data: T) => {
if (this.asleep) return; if (this.asleep) return;
@ -69,11 +70,11 @@ export class History<T = NodeSchema> {
return this.point !== this.session.cursor; return this.point !== this.session.cursor;
} }
private sleep() { sleep() {
this.asleep = true; this.asleep = true;
} }
private wakeup() { wakeup() {
this.asleep = false; this.asleep = false;
} }

View File

@ -1,4 +1,4 @@
import { isFormEvent, compatibleLegaoSchema, getNodeSchemaById, transactionManage } from '@alilc/lowcode-utils'; import { isFormEvent, compatibleLegaoSchema, getNodeSchemaById, transactionManager } from '@alilc/lowcode-utils';
import { isNodeSchema } from '@alilc/lowcode-types'; import { isNodeSchema } from '@alilc/lowcode-types';
import { getConvertedExtraKey, getOriginalExtraKey } from '@alilc/lowcode-designer'; import { getConvertedExtraKey, getOriginalExtraKey } from '@alilc/lowcode-designer';
@ -9,7 +9,7 @@ const utils = {
getNodeSchemaById, getNodeSchemaById,
getConvertedExtraKey, getConvertedExtraKey,
getOriginalExtraKey, getOriginalExtraKey,
startTransaction: transactionManage.startTransaction, startTransaction: transactionManager.startTransaction,
}; };
export default utils; export default utils;

View File

@ -222,6 +222,10 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
} }
componentDidMount() { componentDidMount() {
const _leaf = this.leaf;
this.initOnPropsChangeEvent(_leaf);
this.initOnChildrenChangeEvent(_leaf);
this.initOnVisibleChangeEvent(_leaf);
this.recordTime(); this.recordTime();
} }
@ -245,11 +249,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
// 监听以下事件,当变化时更新自己 // 监听以下事件,当变化时更新自己
__debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`); __debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`);
clearRerenderEvent(componentCacheId); clearRerenderEvent(componentCacheId);
const _leaf = this.leaf; this.curEventLeaf = this.leaf;
this.initOnPropsChangeEvent(_leaf);
this.initOnChildrenChangeEvent(_leaf);
this.initOnVisibleChangeEvent(_leaf);
this.curEventLeaf = _leaf;
cache.ref.set(componentCacheId, { cache.ref.set(componentCacheId, {
makeUnitRender: this.makeUnitRender, makeUnitRender: this.makeUnitRender,
@ -399,7 +399,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
/** 监听参数变化 */ /** 监听参数变化 */
initOnPropsChangeEvent(leaf = this.leaf): void { initOnPropsChangeEvent(leaf = this.leaf): void {
const dispose = leaf?.onPropChange?.(debounce((propChangeInfo: PropChangeOptions) => { const dispose = leaf?.onPropChange?.((propChangeInfo: PropChangeOptions) => {
if (!this.autoRepaintNode) { if (!this.autoRepaintNode) {
return; return;
} }
@ -449,7 +449,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
}); });
this.judgeMiniUnitRender(); this.judgeMiniUnitRender();
}, 30)); });
dispose && this.disposeFunctions.push(dispose); dispose && this.disposeFunctions.push(dispose);
} }

View File

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

View File

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

View File

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