fix: 修复弹窗展示无法撤销问题

This commit is contained in:
liujuping.liujupin 2021-10-09 14:30:41 +08:00
parent 66b23ea757
commit e3d265e0d5
11 changed files with 95 additions and 37 deletions

View File

@ -198,7 +198,7 @@ export class DocumentModel {
* id * id
*/ */
getNodeCount(): number { getNodeCount(): number {
return this._nodesMap.size; return this._nodesMap?.size;
} }
/** /**

View File

@ -187,7 +187,7 @@ class Renderer extends Component<{
render() { render() {
const { documentInstance } = this.props; const { documentInstance } = this.props;
const { container } = documentInstance; const { container, document } = documentInstance;
const { designMode, device } = container; const { designMode, device } = container;
const { rendererContainer: renderer } = this.props; const { rendererContainer: renderer } = this.props;
this.startTime = Date.now(); this.startTime = Date.now();
@ -209,6 +209,7 @@ class Renderer extends Component<{
onCompGetRef={(schema: any, ref: any) => { onCompGetRef={(schema: any, ref: any) => {
documentInstance.mountInstance(schema.id, ref); documentInstance.mountInstance(schema.id, ref);
}} }}
documentId={document.id}
getNode={(id: string) => documentInstance.getNode(id) as any} getNode={(id: string) => documentInstance.getNode(id) as any}
customCreateElement={(Component: any, props: any, children: any) => { customCreateElement={(Component: any, props: any, children: any) => {
const { __id, ...viewProps } = props; const { __id, ...viewProps } = props;

View File

@ -154,7 +154,7 @@ class Renderer extends Component<{
render() { render() {
const { documentInstance, rendererContainer: renderer } = this.props; const { documentInstance, rendererContainer: renderer } = this.props;
const { container } = documentInstance; const { container, document } = documentInstance;
const { designMode, device, locale } = container; const { designMode, device, locale } = container;
const messages = container.context?.utils?.i18n?.messages || {}; const messages = container.context?.utils?.i18n?.messages || {};
this.startTime = Date.now(); this.startTime = Date.now();
@ -172,6 +172,7 @@ class Renderer extends Component<{
appHelper={container.context} appHelper={container.context}
designMode={designMode} designMode={designMode}
device={device} device={device}
documentId={document.id}
suspended={renderer.suspended} suspended={renderer.suspended}
self={renderer.scope} self={renderer.scope}
getNode={(id: string) => documentInstance.getNode(id) as Node} getNode={(id: string) => documentInstance.getNode(id) as Node}

View File

@ -50,16 +50,30 @@ enum RerenderType {
} }
// 缓存 Leaf 层组件,防止重新渲染问题 // 缓存 Leaf 层组件,防止重新渲染问题
const leafComponentCache: { let leafComponentCaches: {
[componentName: string]: any; [componentName: string]: any;
} = {}; } = {};
let cacheDocumentId: any;
function clearCaches(curDocumentId: any, {
__debug,
}: any) {
if (cacheDocumentId === curDocumentId) {
return;
}
__debug(`DocumentId changed to ${curDocumentId}, clear caches!`);
cacheDocumentId = curDocumentId;
leafComponentCaches = {};
}
// 缓存导致 rerender 的订阅事件 // 缓存导致 rerender 的订阅事件
const rerenderEventCache: { const rerenderEventCache: {
[componentId: string]: any; [componentId: string]: any;
} = {}; } = {};
/** 部分没有渲染的 node 节点进行兜底处理 or 渲染方式没有渲染 LeafWrapper */ /** 部分没有渲染的 node 节点进行兜底处理 or 渲染方式没有渲染 LeafWrapper */
function makeRerenderEvent({ function initRerenderEvent({
schema, schema,
__debug, __debug,
container, container,
@ -98,6 +112,7 @@ function clearRerenderEvent(id: string): void {
if (!rerenderEventCache[id]) { if (!rerenderEventCache[id]) {
rerenderEventCache[id] = { rerenderEventCache[id] = {
clear: true, clear: true,
dispose: [],
}; };
return; return;
} }
@ -119,27 +134,34 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
} = baseRenderer; } = baseRenderer;
const engine = baseRenderer.context.engine; const engine = baseRenderer.context.engine;
const host: BuiltinSimulatorHost = baseRenderer.props.__host; const host: BuiltinSimulatorHost = baseRenderer.props.__host;
const curDocumentId = baseRenderer.props?.documentId;
const getNode = baseRenderer.props?.getNode; const getNode = baseRenderer.props?.getNode;
const container: BuiltinSimulatorHost = baseRenderer.props.__container; const container: BuiltinSimulatorHost = baseRenderer.props.__container;
const editor = host?.designer?.editor; const editor = host?.designer?.editor;
const { Component } = adapter.getRuntime(); const { Component, forwardRef } = adapter.getRuntime();
if (curDocumentId !== cacheDocumentId) {
clearCaches(curDocumentId, {
__debug,
});
}
if (!isReactComponent(Comp)) { if (!isReactComponent(Comp)) {
console.error(`${schema.componentName} component may be has errors: `, Comp); console.error(`${schema.componentName} component may be has errors: `, Comp);
} }
makeRerenderEvent({ initRerenderEvent({
schema, schema,
__debug, __debug,
container, container,
getNode, getNode,
}); });
if (leafComponentCache[schema.componentName]) { if (leafComponentCaches[schema.componentName]) {
return leafComponentCache[schema.componentName]; return leafComponentCaches[schema.componentName];
} }
class LeafWrapper extends Component { class LeafHoc extends Component {
recordInfo: { recordInfo: {
startTime?: number | null; startTime?: number | null;
type?: string; type?: string;
@ -156,7 +178,7 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
return; return;
} }
const endTime = Date.now(); const endTime = Date.now();
const nodeCount = host.designer.currentDocument?.getNodeCount?.(); const nodeCount = host?.designer?.currentDocument?.getNodeCount?.();
const componentName = this.recordInfo.node?.componentName || this.leaf?.componentName || 'UnknownComponent'; const componentName = this.recordInfo.node?.componentName || this.leaf?.componentName || 'UnknownComponent';
editor?.emit(GlobalEvent.Node.Rerender, { editor?.emit(GlobalEvent.Node.Rerender, {
componentName, componentName,
@ -171,18 +193,39 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
this.recordTime(); this.recordTime();
} }
componentDidMount() {
this.recordTime();
}
get childrenMap(): any {
const map = new Map();
if (!this.hasChildren) {
return map;
}
this.children.forEach((d: any) => {
if (Array.isArray(d)) {
map.set(d[0].props.componentId, d);
return;
}
map.set(d.props.componentId, d);
});
return map;
}
constructor(props: IProps, context: any) { constructor(props: IProps, context: any) {
super(props, context); super(props, context);
// 监听以下事件,当变化时更新自己 // 监听以下事件,当变化时更新自己
clearRerenderEvent(schema.id); __debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`);
clearRerenderEvent(this.props.componentId);
this.initOnPropsChangeEvent(); this.initOnPropsChangeEvent();
this.initOnChildrenChangeEvent(); this.initOnChildrenChangeEvent();
this.initOnVisibleChangeEvent(); this.initOnVisibleChangeEvent();
__debug(`${schema.componentName}[${schema.id}] leaf render in SimulatorRendererView`);
this.state = { this.state = {
nodeChildren: null, nodeChildren: null,
childrenInState: false, childrenInState: false,
__tag: props.__tag,
}; };
} }
@ -198,17 +241,11 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
// } // }
componentWillReceiveProps(nextProps: any) { componentWillReceiveProps(nextProps: any) {
const { _leaf, __tag, children, ...rest } = nextProps; const { _leaf } = nextProps;
if (nextProps.__tag === this.state.__tag) { if (nextProps.__tag === this.props.__tag) {
const nextProps = getProps(this.leaf?.export?.(TransformStage.Render) as types.ISchema, Comp, componentInfo);
this.setState({
nodeProps: {
...rest,
...nextProps,
},
});
return null; return null;
} }
if (_leaf && this.leaf && _leaf !== this.leaf) { if (_leaf && this.leaf && _leaf !== this.leaf) {
this.disposeFunctions.forEach(fn => fn()); this.disposeFunctions.forEach(fn => fn());
this.disposeFunctions = []; this.disposeFunctions = [];
@ -218,10 +255,9 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
} }
this.setState({ this.setState({
nodeChildren: children, nodeChildren: null,
nodeProps: rest, nodeProps: {},
childrenInState: true, childrenInState: false,
__tag,
}); });
} }
@ -300,7 +336,7 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
// } // }
this.beforeRender(`${RerenderType.ChildChanged}-${type}`, node); this.beforeRender(`${RerenderType.ChildChanged}-${type}`, node);
__debug(`${leaf} component trigger onChildrenChange event`); __debug(`${leaf} component trigger onChildrenChange event`);
const nextChild = getChildren(leaf?.export?.(TransformStage.Render) as types.ISchema, Comp); const nextChild = getChildren(leaf?.export?.(TransformStage.Render) as types.ISchema, Comp, this.childrenMap);
this.setState({ this.setState({
nodeChildren: nextChild, nodeChildren: nextChild,
childrenInState: true, childrenInState: true,
@ -359,17 +395,28 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
return null; return null;
} }
const {
ref,
...rest
} = this.props;
const compProps = { const compProps = {
...this.props, ...rest,
...(this.state.nodeProps || {}), ...(this.state.nodeProps || {}),
children: [], children: [],
__id: this.props.componentId, __id: this.props.componentId,
ref: this.props.forwardedRef,
}; };
return engine.createElement(Comp, compProps, this.hasChildren ? this.children : null); return engine.createElement(Comp, compProps, this.hasChildren ? this.children : null);
} }
} }
const LeafWrapper = forwardRef((props: any, ref: any) => (
// @ts-ignore
<LeafHoc {...props} forwardedRef={ref} />
));
if (typeof Comp === 'object') { if (typeof Comp === 'object') {
const compExtraPropertyNames = Object.getOwnPropertyNames(Comp).filter(d => !compDefaultPropertyNames.includes(d)); const compExtraPropertyNames = Object.getOwnPropertyNames(Comp).filter(d => !compDefaultPropertyNames.includes(d));
@ -382,7 +429,7 @@ export function leafWrapper(Comp: types.IBaseRenderer, {
LeafWrapper.displayName = (Comp as any).displayName; LeafWrapper.displayName = (Comp as any).displayName;
leafComponentCache[schema.componentName] = LeafWrapper; leafComponentCaches[schema.componentName] = LeafWrapper;
return LeafWrapper; return LeafWrapper;
} }

View File

@ -77,7 +77,7 @@ export default function addonRendererFactory() {
return '插件 schema 结构异常!'; return '插件 schema 结构异常!';
} }
this.__debug(`render - ${__schema.fileName}`); this.__debug(`${AddonRenderer.dislayName} render - ${__schema.fileName}`);
this.__generateCtx({ this.__generateCtx({
component: this, component: this,
}); });

View File

@ -521,7 +521,7 @@ export default function baseRenererFactory() {
props.key = props.__id; props.key = props.__id;
} }
let child: any = this.__getSchemaChildrenVirtualDom(schema, Comp); let child: any = parentInfo.componentChildren || this.__getSchemaChildrenVirtualDom(schema, Comp);
const renderComp = (props: any) => engine.createElement(Comp, props, child); const renderComp = (props: any) => engine.createElement(Comp, props, child);
// 设计模式下的特殊处理 // 设计模式下的特殊处理
if (engine && [DESIGN_MODE.EXTEND, DESIGN_MODE.BORDER].includes(engine.props.designMode)) { if (engine && [DESIGN_MODE.EXTEND, DESIGN_MODE.BORDER].includes(engine.props.designMode)) {
@ -580,7 +580,7 @@ export default function baseRenererFactory() {
.map((d: IComponentHoc) => d.hoc); .map((d: IComponentHoc) => d.hoc);
} }
__getSchemaChildrenVirtualDom = (schema: ISchema, Comp: any) => { __getSchemaChildrenVirtualDom = (schema: ISchema, Comp: any, childrenMap?: Map<any, any>) => {
let _children = this.getSchemaChildren(schema); let _children = this.getSchemaChildren(schema);
let children: any = []; let children: any = [];
@ -596,6 +596,8 @@ export default function baseRenererFactory() {
{ {
schema, schema,
Comp, Comp,
// 有 childrenMap 情况下children 只计算第一层,不需要遍历多层。
componentChildren: childrenMap?.get(_child.id)?.props.children || null,
}, },
); );
@ -834,11 +836,16 @@ export default function baseRenererFactory() {
componentInfo: {}, componentInfo: {},
}); });
const { className } = data; const { className } = data;
const otherProps: any = {};
const { engine } = this.context || {}; const { engine } = this.context || {};
if (!engine) { if (!engine) {
return null; return null;
} }
if (this._designModeIsDesign) {
otherProps.__tag = Math.random();
}
const child = engine.createElement( const child = engine.createElement(
Comp, Comp,
{ {
@ -847,6 +854,7 @@ export default function baseRenererFactory() {
ref: this.__getRef, ref: this.__getRef,
className: classnames(getFileCssName(__schema?.fileName), className, this.props.className), className: classnames(getFileCssName(__schema?.fileName), className, this.props.className),
__id: __schema?.id, __id: __schema?.id,
...otherProps,
}, },
this.__createDom(), this.__createDom(),
); );

View File

@ -23,7 +23,7 @@ export default function blockRendererFactory() {
return '区块 schema 结构异常!'; return '区块 schema 结构异常!';
} }
this.__debug(`render - ${__schema.fileName}`); this.__debug(`${BlockRenderer.dislayName} render - ${__schema.fileName}`);
this.__generateCtx({}); this.__generateCtx({});
this.__render(); this.__render();

View File

@ -23,7 +23,7 @@ export default function componentRendererFactory() {
if (this.__checkSchema(__schema)) { if (this.__checkSchema(__schema)) {
return '自定义组件 schema 结构异常!'; return '自定义组件 schema 结构异常!';
} }
this.__debug(`render - ${__schema.fileName}`); this.__debug(`${CompRenderer.dislayName} render - ${__schema.fileName}`);
this.__generateCtx({ this.__generateCtx({
component: this, component: this,

View File

@ -36,7 +36,7 @@ export default function pageRendererFactory() {
if (this.__checkSchema(__schema)) { if (this.__checkSchema(__schema)) {
return '页面schema结构异常'; return '页面schema结构异常';
} }
this.__debug(`render - ${__schema.fileName}`); this.__debug(`${PageRenderer.dislayName} render - ${__schema.fileName}`);
this.__bindCustomMethods(this.props); this.__bindCustomMethods(this.props);
this.__initDataSource(this.props); this.__initDataSource(this.props);

View File

@ -48,7 +48,7 @@ export default function tempRendererFactory() {
return '下钻编辑 schema 结构异常!'; return '下钻编辑 schema 结构异常!';
} }
this.__debug(`render - ${__schema.fileName}`); this.__debug(`${TempRenderer.dislayName} render - ${__schema.fileName}`);
return this.__renderContent(this.__renderContextProvider({ __ctx })); return this.__renderContent(this.__renderContextProvider({ __ctx }));
} }

View File

@ -63,6 +63,7 @@ export interface IInfo {
schema: ISchema; schema: ISchema;
Comp: any; Comp: any;
componentInfo?: any; componentInfo?: any;
componentChildren?: any
} }
export interface JSExpression { export interface JSExpression {